-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
290 lines (245 loc) · 8.2 KB
/
main.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
from datetime import date
import pandas as pd
import hackathon
import retrieve_data
import processor
import store_data
from processor.lambda_function import sic_lookup_table, calculate_correlation
from store_data.lambda_function import store_sharpe_ratio
def retrieve_nasdaq_symbols(exchange_file):
try:
nasdaq_list = processor.get_json([exchange_file])
processed_response = processor.process_exchange_json(nasdaq_list)
df = pd.DataFrame(processed_response)
return df
nasdaq_symbol_list = []
for key in df.iterrows():
key = key[1][0]
symbol = key["Symbol"]
nasdaq_symbol_list.append(symbol)
print("exchange symbols collected")
return "success"
except Exception as e:
print(f'--> error is: {e}')
def read_txt_symbols():
filepath = "/home/drich/financedata/nasdaq_symbols.txt"
symbol_list = []
try:
with open(filepath) as f:
symbols = f.readlines()
symbol_list = symbols[0].strip('][').split(', ')
print(f'-->first is {symbol_list[0]}')
store_response = store_data.store_symbols(symbol_list, "nasdaq")
print(f'--> store_response is: {store_response}')
except Exception as e:
print(f'error: {e}')
def read_copied_txt_symbols():
filepath = "/home/drich/financedata/spy_symbols.txt"
try:
with open(filepath) as f:
raw_symbols = f.read().splitlines()
symbols = []
for sub in raw_symbols:
if sub == "[" or sub == "]":
continue
symbols.append(sub.replace(",", ""))
print(f'--> symbol is {symbols[1]}')
store_response = store_data.store_symbols(symbols, "spy")
print(f'--> store_response is: {store_response}')
except Exception as e:
print(f'error: {e}')
def run_retrieve_symbols_data(symbols: list):
if symbols:
event = {"symbols": symbols}
response = retrieve_data.handler(event, None)
print(f'--> response is: {response}')
return response
def run_store_symbols_data(symbols: list):
for symbol in symbols:
event = {"symbol": symbol}
response = store_data.handler(event, None)
print(f'--> response is: {response}')
return response
if __name__ == '__main__':
# response = retrieve_data.get_from_market_data("lulu")
# print(response)
command = "sharpe_ratio"
# command: str = "correlation"
if command == "alpha_vantage":
try:
symbol_list = [
'DIS',
'EA',
'FOX',
'FOXA',
'GOOG',
'GOOGL',
'IPG',
'LYV',
'META',
'MTCH',
'NFLX',
'NWS',
'NWSA',
'OMC',
'PARA',
'T',
'TMUS',
'TTWO',
'VZ',
'WBD'
]
for symbol in symbol_list:
print(f'--> getting data for: {symbol}')
symbol = symbol.lower()
response = retrieve_data.fetch_alpha_vantage_data(symbol)
dates = []
symbols = []
values = []
for row in response:
dates.append(row)
symbols.append(symbol.upper())
values.append(response[row]["4. close"])
frame_data = {"week_date": dates, "symbol": symbols, "close": values}
df = pd.DataFrame(frame_data)
storage_response = store_data.store_df(df, "stocks_week")
except Exception as e:
if e == KeyError('Weekly Time Series'):
print(f'--> api rate limit reached')
print(f'--> error is: {e}')
elif command == "transcript_correlation":
processor.run_transcript_correlation("./LULU-Q32023.txt", "./LULU-Q22023.txt")
elif command == "efficiency":
try:
market_data = retrieve_data.get_from_market_data("lulu", "2023-10-5", "2023-12-8")
response = processor.process_efficiency_ratio(market_data)
df = pd.DataFrame(response, columns=["efficiency_ratio"])
print(f'--> response is: {response}')
except Exception as e:
print(f'--> error is: {e}')
elif command == "hackathon":
response = hackathon.lambda_function.lambda_handler({"symbol": "LULU", "expression": "portfolio"}, None)
elif command == "correlation":
symbol_list = [
'ADBE',
'ADI',
'ADSK',
'AKAM',
'AMAT',
'AMD',
'ANET',
'ANSS',
'APH',
'AVGO',
'CDNS',
'CDW',
'CSCO',
'CTSH',
'FICO',
'FTNT',
'GLW',
'HPE',
'HPQ',
'IBM',
'INTC',
'INTU',
'IT',
'KEYS',
'KLAC',
'LRCX',
'MCHP',
'MPWR',
'MSFT',
'MSI',
'MU',
'NOW',
'NTAP',
'NVDA',
'NXPI',
'ON'
]
for base_symbol in symbol_list:
sector = processor.lookup_sector(base_symbol)
matching_sector = processor.get_matching_sector_symbols(sector)
sector_symbols = processor.get_sector_symbols(matching_sector)
corr_calcs = []
for symbol in sector_symbols:
print(f'--> running cor on: {base_symbol} and {symbol}')
corr_symbol = symbol
response = calculate_correlation(base_symbol, corr_symbol)
if response == "error":
print(f'--> no data for {corr_symbol}')
continue
corr_calcs.append((corr_symbol, response))
today = date.today()
frame = {"date": [today], "base_symbol": [base_symbol], "corr_symbol": [corr_symbol],
"correlation": [response]}
df = pd.DataFrame(frame)
if response == None or response == float('nan'):
continue
store_response = store_data.store_df(df, "spdr_calculations")
print(f'--> response is: {response}')
sorted_data = sorted(corr_calcs, key=lambda x: x[1])
print(f'--> least correlated is: {sorted_data[0]}')
# response: list[str] = get_from_market_data("msft")
# record_count = response.count()
# processor_response = process_market_data(response)
elif command == "sharpe_ratio":
symbol_list = [
'AAPL',
'ACN',
'ADBE',
'ADI',
'ADSK',
'AKAM',
'AMAT',
'AMD',
'ANET',
'ANSS',
'APH',
'AVGO',
'avy',
'CDNS',
'CDW',
'CRM',
'CSCO',
'CTSH',
'FICO',
'FTNT',
'GLW',
'HPE',
'HPQ',
'IBM',
'INTC',
'INTU',
'IT',
'KEYS',
'KLAC',
'LRCX',
'MCHP',
'MPWR',
'MSFT',
'MSI',
'MU',
'NOW',
'NTAP',
'NVDA',
'NXPI',
'ON',
'ORCL'
]
for symbol in symbol_list:
response: dict = processor.calc_sharpe_ratio_sql(symbol)
response = store_sharpe_ratio(response["sharpe_ratio"], symbol)
if response == True:
print(f'-->sharpe ratio calculated and stored')
else:
print(f'-->error storing sharpe ratio response is: {response}')
# response = sic_lookup("lulu")
elif command == "sic_lookup":
sic_lookup_table("indexSymbols")
# symbols = ["lulu"]
# run_retrieve_symbols_data(symbols)
# run_store_symbols_data(symbols)
# read_copied_txt_symbols()
# read_txt_symbols()