-
Notifications
You must be signed in to change notification settings - Fork 0
/
stock_crypto_correlation.py
337 lines (285 loc) · 10.7 KB
/
stock_crypto_correlation.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import requests
import json
import pandas as pd
import numpy as np
import datetime
import plotly.express as px
import plotly.graph_objects as go
import plotly.io as pio
from plotly.subplots import make_subplots
import matplotlib.pyplot as plt
from datetime import datetime
from datetime import date
from scipy.stats import pearsonr
import statistics
import math
import streamlit as st
##################################################################
#create venv -- python3 -m venv env
#run venv -- .\env\Scripts\activate
# leave venv -- deactivate
##################################################################
# inputs #
### CRYPTO ##########################
#
exchange = "coinbase-pro" #
#pair = "btcusd" #
#
#####################################
############ Date Parameters ##############################
#
START_DATE = '1/1/2022' #
END_DATE = '4/5/2022' #
PERIOD = 86400 # Time period in seconds (e.g., 1 day = 86400) #
#
#################################################################
### Stocks ##############################################
#
#symbol = 'QQQ' #
#
#################################################################
# BELOW ARE ALL FUNCTIONS THAT ARE CALLED IN MAIN.PY
# Convert the dates to timestamps
def to_timestamp(dateString):
#element = datetime.strptime(dateString, '%m/%d/%Y')
element = date.strftime(dateString, '%Y/%m/%d')
element = datetime.strptime(element, '%Y/%m/%d')
return int(datetime.timestamp(element))
# Will be used later to convert back
def to_date(timestamp):
dt = datetime.fromtimestamp(timestamp)
return dt.strftime('%m/%d/%Y')
def to_backward_date(dateString):
element = date.strftime(dateString, '%Y/%m/%d')
element = datetime.strptime(dateString, '%m/%d/%Y')
return datetime.strftime(element, "%Y-%m-%d")
#start_ts = to_timestamp(START_DATE)
#end_ts = to_timestamp(END_DATE)
# cryptowat.ch API -- historical data for Crypto protocols on a variety of exchanges
# docs found here -- https://docs.cryptowat.ch/rest-api/
def cryptowatchAPIcall(option, START_DATE, END_DATE):
start_ts = to_timestamp(START_DATE)
end_ts = to_timestamp(END_DATE)
params = {
"after": start_ts,
"before": end_ts,
"periods": PERIOD,
}
pricefeed = requests.get(
f'https://api.cryptowat.ch/markets/{exchange}/{option}/ohlc',
params=params)
crypto_price = pricefeed.json()
return crypto_price
def json_to_df(cryptowat, option, START_DATE, END_DATE):
funcinput = cryptowat
day_count = len(funcinput["result"]["86400"])
print(f"Retrieving historical prices for {option} from {exchange}")
print(f"between {START_DATE} and {END_DATE}")
print(f'Analyzing {day_count} total days')
daily_resp = funcinput["result"]["86400"]
O =[]
H=[]
L=[]
C=[]
vol=[]
date=[]
weekday=[]
new_date = []
count = 0
#populate all lists from json response
while count < day_count:
date.append(daily_resp[count][0])
O.append(daily_resp[count][1])
H.append(daily_resp[count][2])
L.append(daily_resp[count][3])
C.append(daily_resp[count][4])
vol.append(daily_resp[count][5])
if count > day_count:
break
count = count + 1
#get day of week (1 = Mon, 2 = Tues, 3 = Wed,... 7 = Sun)
for x in date:
wkdy = datetime.fromtimestamp(x).strftime("%A")
weekday.append(wkdy)
#convert unix date to readable date
for x in date:
new = to_date(x)
new_date.append(new)
#create DataFrame
df = pd.DataFrame()
df["Date"] = new_date
df["Open"] = O
df["High"] = H
df["Low"] = L
df["Close"] = C
df["Volume"] = vol
df["Weekday"] = weekday
df["log_return"] = np.log(df.Close) - np.log(df.Close.shift(1))
# df.set_index(["Date"])
return df
def PolygonAPIcall(symbol, START_DATE, END_DATE):
new_start_date = START_DATE
new_end_date = END_DATE
apiKey = "ALb57Y72t3B2f3zVIAKUeaGMT4M2vzL9"
date_from = START_DATE #removed backward date function call
date_to = END_DATE #removed backward date function call
response2 = requests.get(f"https://api.polygon.io/v2/aggs/ticker/{symbol}/range/1/day/{date_from}/{date_to}?adjusted=true&sort=asc&limit=600&apiKey=ALb57Y72t3B2f3zVIAKUeaGMT4M2vzL9")
stock_price = response2.json()
return stock_price
def json_to_df2(poly, symbol, START_DATE, END_DATE):
call2= poly
print(call2)
O = []
H = []
L = []
C = []
weekday=[]
date = []
vol = []
newdate= []
day_count = len(call2["results"])
day_count
print(f"Retrieving historical prices for {symbol}")
print(f"between {START_DATE} and {END_DATE}")
print(f'Analyzing {day_count} total days')
count = 0
while count < day_count:
date.append(call2["results"][count]["t"])
O.append(call2["results"][count]["o"])
H.append(call2["results"][count]["h"])
L.append(call2["results"][count]["l"])
C.append(call2["results"][count]["c"])
vol.append(call2["results"][count]["v"])
if count > day_count:
break
count = count+1
#convert unix date to readable date
for x in date:
x = x/1000
new = to_date(x)
newdate.append(new)
#get day of week
for x in date:
wkdy = datetime.fromtimestamp(x/1000).strftime("%A")
weekday.append(wkdy)
#create DataFrame
df2 = pd.DataFrame()
df2["Date"] = newdate
df2["Open"] = O
df2["High"] = H
df2["Low"] = L
df2["Close"] = C
df2["Volume"] = vol
df2["Weekday"] = weekday
df2["log_return"] = np.log(df2.Close) - np.log(df2.Close.shift(1))
#df2.set_index(["Date"])
return df2
def combineLinechart(crypto, stock, option, symbol):
fig = make_subplots(specs=[[{"secondary_y": True}]])
# Add traces
fig.add_trace(
go.Scatter(x=crypto["Date"], y=crypto["Close"], name=option),
secondary_y=False,
)
fig.add_trace(
go.Scatter(x=stock["Date"], y=stock["Close"], name=symbol),
secondary_y=True,
)
fig.update_xaxes(
dtick=7,
tickformat="%b")
fig.update_layout(
title = "Equities vs Crypto Comparison",
template="plotly_dark")
return fig
def logLinechart(crypto, stock, option, symbol):
fig = make_subplots(specs=[[{"secondary_y": True}]])
# Add traces
fig.add_trace(
go.Scatter(x=crypto["Date"], y=crypto['log_return'], name=option),
secondary_y=False,
)
fig.add_trace(
go.Scatter(x=stock["Date"], y=stock['log_return'], name=symbol),
secondary_y=True,
)
fig.update_xaxes(
dtick=7,
tickformat="%b")
fig.update_layout(
title = "Logarithmic Returns",
template="plotly_dark")
return fig
def PearsonCorrValue(crypto,stock):
dfcorr = pd.DataFrame()
dfcorr["crypto_log"] = crypto['log_return']
dfcorr = dfcorr.set_index(crypto['Date'])
dfcorr2 = pd.DataFrame()
dfcorr2["stock_log"] = stock['log_return']
dfcorr2["date"] = stock['Date']
#dfcorr2 = dfcorr2.set_index(df2['Date'])
dfcorr3 = pd.merge(dfcorr, dfcorr2, left_index=True, right_on='date')
dfcorr3 = dfcorr3.set_index("date")
dfcorr3 = dfcorr3.corr()
return dfcorr3
#GET ROLLING CORRELATION (DEFAULT 20 days) OUTPUT LINE CHART
def ROLLINGcorrelation(crypto,stock):
dfcorr = pd.DataFrame()
dfcorr["crypto_log"] = crypto['log_return']
dfcorr = dfcorr.set_index(crypto['Date'])
dfcorr2 = pd.DataFrame()
dfcorr2["stock_log"] = stock['log_return']
dfcorr2["date"] = stock['Date']
#dfcorr2 = dfcorr2.set_index(df2['Date'])
dfcorr3 = pd.merge(dfcorr, dfcorr2, left_index=True, right_on='date')
dfcorr3 = dfcorr3.set_index("date")
#dfcorr3 = dfcorr3.corr()
fig1 = dfcorr3['crypto_log'].rolling(window=20).corr(dfcorr3['stock_log'])
#ax.axhline(df2["log_return"].corr().iloc[0,1], c='r')
return fig1.plot(figsize=(14,6))
#calculate rolling weekly vol Crypto (7 day rolling)
def RollingVolCrypto(crypto):
#calculate Vol
st_dev = crypto['log_return'].std()
print(f'The Average vol for any given day is {st_dev}')
volatility = crypto['log_return'].std()*365**.5
print(f'{volatility} vol asset in the given period')
crypto['weekly_vol'] = crypto['log_return'].rolling(window=7).std()*7**.5
fig = px.line(crypto, x=crypto["Date"], y=crypto["weekly_vol"], title='Weekly Rolling Volatility')
fig.update_xaxes(
dtick=7,
tickformat="%b")
fig.update_layout(
title = "Weekly Rolling Volatility",
template="plotly_dark")
#fig.show()
return fig
def messariCryptoAPI(proj):
token = proj
url = f"https://data.messari.io/api/v1/assets/{token}/metrics"
response = requests.get(url)
response = response.json()
name = response["data"]['name']
symbol = response["data"]['symbol']
mcap = response["data"]["marketcap"]['current_marketcap_usd']
price = response['data']["market_data"]['price_usd']
day = response['data']["market_data"]['percent_change_usd_last_24_hours']
month = response["data"]['roi_data']['percent_change_last_1_month']
month3 = response["data"]['roi_data']['percent_change_last_3_months']
year = response["data"]['roi_data']['percent_change_last_1_year']
dict = {
"Name": name,
"Symbol": symbol,
"marketcap": mcap,
"Price": price,
"24hr % Change": day,
"1mo % Change": month,
"3mo % Change": month3,
"1 Year % Change": year
}
StatisticsDF = pd.DataFrame.from_dict(dict, orient="index")
StatisticsDF = StatisticsDF.astype('str')
print(StatisticsDF.dtypes)
#StatisticsDF = StatisticsDF.rename(columns={0: "Statistics"})
print(StatisticsDF)
return StatisticsDF