-
Notifications
You must be signed in to change notification settings - Fork 0
/
BTC futures hedge.py
65 lines (39 loc) · 2.02 KB
/
BTC futures hedge.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""
Created on Sun Jan 24 09:38:53 2021
@author: jeron
"""
############# Beautiful Soup ###############
from bs4 import BeautifulSoup
from urllib.request import Request, urlopen
import re #Function for clean up
#BTC SPOT
btc_url = "https://finance.yahoo.com/quote/BTC-USD?p=BTC-USD&.tsrc=fin-srch"
request_btc = Request(btc_url, headers={'User-Agent': 'Mozilla/5.0'})
page_btc = urlopen(request_btc).read()
soup_btc = BeautifulSoup(page_btc, 'html.parser')
#print(soup_stock)
#Inspect the source code and see if its span, dic, table... and copy the class in there...
price_btc = soup_btc.find('span', {'class':'Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)'}).text
#Still in STR format
price_btc = float(re.sub(r'[\D]' , '' , price_btc)) /100
#\D removes all non digit signs in the str divide by 100 so decimals line up.
#BTC FUTURES monthly
btcf_url = "https://finance.yahoo.com/quote/BTM%3DF/futures?p=BTM%3DF"
request_btcf = Request(btcf_url, headers={'User-Agent': 'Mozilla/5.0'})
page_btcf = urlopen(request_btcf).read()
soup_btcf = BeautifulSoup(page_btcf, 'html.parser')
#print(soup_stock)
#Inspect the source code and see if its span, dic, table... and copy the class in there...
price_btcf = soup_btcf.find('span', {'class':'Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)'}).text
#Still in STR format
price_btcf = float(re.sub(r'[\D]' , '' , price_btcf)) /100
#\D removes all non digit signs in the str divide by 100 so decimals line up.
#Hedge Package
amount_btc = 10
def package_hedge(price_btcf, price_btc, amount_btc):
prcnt = (price_btcf - price_btc)/price_btcf
if price_btc < price_btcf:
print("You will have to earn at least {}%, in one month´s time, to make a profit. You´re in CONTANGO.".format(round(prcnt*100, 3)))
if price_btc > price_btcf:
print("You can loose up to {}%, in one month´s time, and still make a profit. You´re in BACKWARDATION.".format(round(-prcnt*100, 3)))
package_hedge(price_btcf, price_btc, amount_btc)