forked from OpenBB-finance/OpenBB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gamestonk_terminal.py
328 lines (267 loc) · 13.4 KB
/
gamestonk_terminal.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
""" Example or something
"""
import argparse
import pandas as pd
from helper_funcs import *
from fundamental_analysis import fa_menu as fam
from technical_analysis import ta_menu as tam
from due_diligence import dd_menu as ddm
from discovery import disc_menu as dm
from sentiment import sen_menu as sm
from prediction_techniques import pred_menu as pm
import res_menu as rm
import config_terminal as cfg
from alpha_vantage.timeseries import TimeSeries
#import warnings
#warnings.simplefilter("always")
# ----------------------------------------------------- LOAD -----------------------------------------------------
def load(l_args, s_ticker, s_start, s_interval, df_stock):
parser = argparse.ArgumentParser(prog='load', description=""" Load a stock in order to perform analysis""")
parser.add_argument('-t', "--ticker", action="store", dest="s_ticker", required=True, help="Stock ticker")
parser.add_argument('-s', "--start", type=valid_date, dest="s_start_date", help="The starting date (format YYYY-MM-DD) of the stock")
parser.add_argument('-i', "--interval", action="store", dest="n_interval", type=int, default=1440, choices=[1,5,15,30,60], help="Intraday stock minutes")
try:
(ns_parser, l_unknown_args) = parser.parse_known_args(l_args)
except SystemExit:
print("")
return [s_ticker, s_start, s_interval, df_stock]
if l_unknown_args:
print(f"The following args couldn't be interpreted: {l_unknown_args}")
# Update values:
s_ticker = ns_parser.s_ticker
s_start = ns_parser.s_start_date
s_interval = str(ns_parser.n_interval)+'min'
try:
ts = TimeSeries(key=cfg.API_KEY_ALPHAVANTAGE, output_format='pandas')
# Daily
if s_interval == "1440min":
df_stock, d_stock_metadata = ts.get_daily_adjusted(symbol=ns_parser.s_ticker, outputsize='full')
# Intraday
else:
df_stock, d_stock_metadata = ts.get_intraday(symbol=ns_parser.s_ticker, outputsize='full', interval=s_interval)
df_stock.sort_index(ascending=True, inplace=True)
except:
print("Either the ticker or the API_KEY are invalids. Try again!")
return [s_ticker, s_start, s_interval, df_stock]
s_intraday = (f'Intraday {s_interval}', 'Daily')[s_interval == "1440min"]
if s_start:
# Slice dataframe from the starting date YYYY-MM-DD selected
df_stock = df_stock[ns_parser.s_start_date:]
print(f"Loading {s_intraday} {s_ticker} stock with starting period {s_start.strftime('%Y-%m-%d')} for analysis.")
else:
print(f"Loading {s_intraday} {s_ticker} stock for analysis.")
print("")
return [s_ticker, s_start, s_interval, df_stock]
# ----------------------------------------------------- VIEW -----------------------------------------------------
def view(l_args, s_ticker, s_start, s_interval, df_stock):
parser = argparse.ArgumentParser(prog='view', description='Visualise historical data of a stock. An alpha_vantage key is necessary.')
if s_ticker:
parser.add_argument('-t', "--ticker", action="store", dest="s_ticker", default=s_ticker, help='Stock ticker')
else:
parser.add_argument('-t', "--ticker", action="store", dest="s_ticker", required=True, help='Stock ticker')
parser.add_argument('-s', "--start", type=valid_date, dest="s_start_date", default=s_start, help="The starting date (format YYYY-MM-DD) of the stock")
parser.add_argument('-i', "--interval", action="store", dest="n_interval", type=int, default=0, choices=[1,5,15,30,60], help="Intraday stock minutes")
parser.add_argument("--type", action="store", dest="n_type", type=check_positive, default=5, # in case it's daily
help='1234 corresponds to types: 1. open; 2. high; 3.low; 4. close; while 14 corresponds to types: 1.open; 4. close')
try:
(ns_parser, l_unknown_args) = parser.parse_known_args(l_args)
except SystemExit:
print("")
return
if l_unknown_args:
print(f"The following args couldn't be interpreted: {l_unknown_args}")
# Update values:
s_ticker = ns_parser.s_ticker
# A new interval intraday period was given
if ns_parser.n_interval != 0:
s_interval = str(ns_parser.n_interval)+'min'
try:
ts = TimeSeries(key=cfg.API_KEY_ALPHAVANTAGE, output_format='pandas')
# Daily
if s_interval == "1440min":
df_stock, d_stock_metadata = ts.get_daily_adjusted(symbol=s_ticker, outputsize='full')
# Intraday
else:
df_stock, d_stock_metadata = ts.get_intraday(symbol=s_ticker, outputsize='full', interval=s_interval)
except:
print("Either the ticker or the API_KEY are invalids. Try again!")
return
df_stock.sort_index(ascending=True, inplace=True)
# Slice dataframe from the starting date YYYY-MM-DD selected
df_stock = df_stock[ns_parser.s_start_date:]
# Daily
if s_interval == "1440min":
# The default doesn't exist for intradaily data
ln_col_idx = [int(x)-1 for x in list(str(ns_parser.n_type))]
if 4 not in ln_col_idx:
ln_col_idx.append(4)
# Check that the types given are not bigger than 4, as there are only 5 types (0-4)
if len([i for i in ln_col_idx if i > 4]) > 0:
print("An index bigger than 4 was given, which is wrong. Try again")
return
# Append last column of df to be filtered which corresponds to: 6. Volume
ln_col_idx.append(5)
# Intraday
else:
# The default doesn't exist for intradaily data
if ns_parser.n_type == 5:
ln_col_idx = [3]
else:
ln_col_idx = [int(x)-1 for x in list(str(ns_parser.n_type))]
# Check that the types given are not bigger than 3, as there are only 4 types (0-3)
if len([i for i in ln_col_idx if i > 3]) > 0:
print("An index bigger than 3 was given, which is wrong. Try again")
return
# Append last column of df to be filtered which corresponds to: 5. Volume
ln_col_idx.append(4)
# Plot view of the stock
plot_view_stock(df_stock.iloc[:, ln_col_idx], ns_parser.s_ticker)
# ----------------------------------------------------- HELP ------------------------------------------------------------------
def print_help(s_ticker, s_start, s_interval, b_is_market_open):
""" Print help
"""
print("What do you want to do?")
print(" help help to see this menu again")
print(" quit to abandon the program")
print("")
print(" clear clear a specific stock ticker from analysis")
print(" load load a specific stock ticker for analysis")
print(" view view and load a specific stock ticker for technical analysis")
s_intraday = (f'Intraday {s_interval}', 'Daily')[s_interval == "1440min"]
if s_ticker and s_start:
print(f"\n{s_intraday} Stock: {s_ticker} (from {s_start.strftime('%Y-%m-%d')})")
elif s_ticker:
print(f"\n{s_intraday} Stock: {s_ticker}")
else:
print("\nStock: ?")
print(f"Market {('CLOSED', 'OPEN')[b_is_market_open]}.")
print("\nMenus:")
print(" disc discover trending stocks, \t e.g. map, sectors, high short interest")
print(" sen sentiment of the market, \t from: reddit, stocktwits, twitter")
if s_ticker:
print(" res research web page, \t e.g.: macroaxis, yahoo finance, fool")
print(" fa fundamental analysis, \t e.g.: income, balance, cash, earnings")
print(" ta technical analysis, \t e.g.: ema, macd, rsi, adx, bbands, obv")
print(" dd in-depth due-diligence, \t e.g.: news, analyst, shorts, insider, sec")
print(" pred prediction techniques, \t e.g.: regression, arima, rnn, lstm, prophet")
print("")
# -----------------------------------------------------------------------------------------------------------------------
def main():
"""Gamestonk Terminal is an awesome stock market terminal that has been developed for fun,
while I saw my GME shares tanking. But hey, I like the stock."""
s_ticker = ""
s_start = ""
df_stock = pd.DataFrame()
b_intraday = False
s_interval = "1440min"
'''
# Set stock by default to speed up testing
s_ticker = "BB"
ts = TimeSeries(key=cfg.API_KEY_ALPHAVANTAGE, output_format='pandas')
df_stock, d_stock_metadata = ts.get_daily_adjusted(symbol=s_ticker, outputsize='full')
df_stock.sort_index(ascending=True, inplace=True)
s_start = datetime.strptime("2020-06-04", "%Y-%m-%d")
df_stock = df_stock[s_start:]
'''
# Add list of arguments that the main parser accepts
menu_parser = argparse.ArgumentParser(prog='gamestonk_terminal', add_help=False)
menu_parser.add_argument('opt', choices=['help', 'quit', 'q',
'clear', 'load', 'view',
'disc', 'sen', 'res', 'fa', 'ta', 'dd', 'pred'])
# Print first welcome message and help
print("\nWelcome to Didier's Gamestonk Terminal\n")
print_help(s_ticker, s_start, s_interval, b_is_stock_market_open())
# Loop forever and ever
while True:
# Get input command from user
as_input = input('> ')
# Is command empty
if not len(as_input):
print("")
continue
# Parse main command of the list of possible commands
try:
(ns_known_args, l_args) = menu_parser.parse_known_args(as_input.split())
except SystemExit:
print("The command selected doesn't exist\n")
continue
if ns_known_args.opt == 'help':
print_help(s_ticker, s_start, s_interval, b_is_stock_market_open())
elif (ns_known_args.opt == 'quit') or (ns_known_args.opt == 'q'):
print("Hope you made money today. Good bye my lover, good bye my friend.\n")
return
elif ns_known_args.opt == 'clear':
print("Clearing stock ticker to be used for analysis")
s_ticker = ""
s_start = ""
elif ns_known_args.opt == 'load':
[s_ticker, s_start, s_interval, df_stock] = load(l_args, s_ticker, s_start, s_interval, df_stock)
elif ns_known_args.opt == 'view':
view(l_args, s_ticker, s_start, s_interval, df_stock)
# DISCOVERY MENU
elif ns_known_args.opt == 'disc':
b_quit = dm.disc_menu()
if b_quit:
print("Hope you made money today. Good bye my lover, good bye my friend.\n")
return
else:
print_help(s_ticker, s_start, s_interval, b_is_stock_market_open())
# SENTIMENT MARKET
elif ns_known_args.opt == 'sen':
b_quit = sm.sen_menu(s_ticker, s_start)
if b_quit:
print("Hope you made money today. Good bye my lover, good bye my friend.\n")
return
else:
print_help(s_ticker, s_start, s_interval, b_is_stock_market_open())
# RESEARCH MENU
elif ns_known_args.opt == 'res':
b_quit = rm.res_menu(s_ticker)
if b_quit:
print("Hope you made money today. Good bye my lover, good bye my friend.\n")
return
else:
print_help(s_ticker, s_start, s_interval, b_is_stock_market_open())
# FUNDAMENTAL ANALYSIS MENU
elif ns_known_args.opt == 'fa':
b_quit = fam.fa_menu(s_ticker, s_start, s_interval)
if b_quit:
print("Hope you made money today. Good bye my lover, good bye my friend.\n")
return
else:
print_help(s_ticker, s_start, s_interval, b_is_stock_market_open())
# TECHNICAL ANALYSIS MENU
elif ns_known_args.opt == 'ta':
b_quit = tam.ta_menu(df_stock, s_ticker, s_start, s_interval)
if b_quit:
print("Hope you made money today. Good bye my lover, good bye my friend.\n")
return
else:
print_help(s_ticker, s_start, s_interval, b_is_stock_market_open())
# DUE DILIGENCE MENU
elif ns_known_args.opt == 'dd':
b_quit = ddm.dd_menu(df_stock, s_ticker, s_start, s_interval)
if b_quit:
print("Hope you made money today. Good bye my lover, good bye my friend.\n")
return
else:
print_help(s_ticker, s_start, s_interval, b_is_stock_market_open())
# PREDICTION TECHNIQUES
elif ns_known_args.opt == 'pred':
if s_interval == "1440min":
b_quit = pm.pred_menu(df_stock, s_ticker, s_start, s_interval)
# If stock data is intradaily, we need to get data again as prediction techniques work on daily adjusted data
else:
df_stock_pred, _ = ts.get_daily_adjusted(symbol=s_ticker, outputsize='full')
df_stock_pred = df_stock_pred.sort_index(ascending=True)
df_stock_pred = df_stock_pred[s_start:]
b_quit = pm.pred_menu(df_stock_pred, s_ticker, s_start, s_interval="1440min")
if b_quit:
print("Hope you enjoyed the terminal. Remember that stonks only go up. Diamond hands.\n")
return
else:
print_help(s_ticker, s_start, s_interval, b_is_stock_market_open())
else:
print('Shouldnt see this command!')
if __name__ == "__main__":
main()