-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
322 lines (254 loc) · 9.12 KB
/
app.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
import os
import json
import requests
from colorama import Fore, Style
from candlestick_chart import Candle, Chart
import inquirer
from datetime import datetime, timedelta
import asciichartpy
API_KEY_FILE = "secret/api_key.json"
STOCK_LIST_FILE = "secret/stocks.json"
STOCK_DATA_DIR = "stock_data"
# Function to clear the screen
def clear_screen():
os.system('cls' if os.name == 'nt' else 'clear')
# Load API key from file
def load_api_key():
if os.path.exists(API_KEY_FILE):
with open(API_KEY_FILE, "r") as f:
return json.load(f)
else:
return None
# Save API key to file
def save_api_key(api_key):
with open(API_KEY_FILE, "w") as f:
json.dump(api_key, f)
# Load stock list from file
def load_stock_list():
if os.path.exists(STOCK_LIST_FILE):
with open(STOCK_LIST_FILE, "r") as f:
return json.load(f)
else:
return []
# Save stock list to file
def save_stock_list(stock_list):
with open(STOCK_LIST_FILE, "w") as f:
json.dump(stock_list, f)
# Load stock data from file
def load_stock_data(symbol):
file_path = os.path.join(STOCK_DATA_DIR, f"{symbol}.json")
if os.path.exists(file_path):
with open(file_path, "r") as f:
return json.load(f)
else:
return None
# Save stock data to file
def save_stock_data(symbol, data):
if not os.path.exists(STOCK_DATA_DIR):
os.makedirs(STOCK_DATA_DIR)
file_path = os.path.join(STOCK_DATA_DIR, f"{symbol}.json")
with open(file_path, "w") as f:
json.dump(data, f)
# Check if stock data is stale (older than 5 minutes)
def is_stock_data_stale(symbol):
file_path = os.path.join(STOCK_DATA_DIR, f"{symbol}.json")
if os.path.exists(file_path):
modification_time = datetime.fromtimestamp(os.path.getmtime(file_path))
return (datetime.now() - modification_time) > timedelta(minutes=5)
return True
# Set API key
def set_api_key():
api_key = input("Enter your API key: ")
save_api_key(api_key)
print(Fore.GREEN + "API key saved successfully.")
# Get stock data from Alpha Vantage API
def get_stock_data(symbol):
if not is_stock_data_stale(symbol):
# Load data from file if not stale
return load_stock_data(symbol)
api_key = load_api_key()
if api_key is None:
print(Fore.RED + "API key is not set. Please set the API key first.")
return None
url = f'https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol={symbol}&interval=5min&apikey={api_key}'
response = requests.get(url)
if response.status_code == 200:
data = response.json()
# Save data to file
save_stock_data(symbol, data)
return data
else:
print(
Fore.RED + f"Failed to fetch data for {symbol}. Error {response.status_code}.")
return None
# Add stock to the list
def add_stock(symbol):
stock_list = load_stock_list()
if symbol not in stock_list:
stock_list.append(symbol)
save_stock_list(stock_list)
print(Fore.GREEN + f"Stock {symbol} added successfully.")
else:
print(Fore.YELLOW + f"Stock {symbol} is already in the list.")
# Remove stock from the list
def remove_stock(symbol):
stock_list = load_stock_list()
if symbol in stock_list:
stock_list.remove(symbol)
save_stock_list(stock_list)
print(Fore.GREEN + f"Stock {symbol} removed successfully.")
else:
print(Fore.YELLOW + f"Stock {symbol} is not in the list.")
# Show stock information
def show_stock_info():
stock_list = load_stock_list()
if not stock_list:
print(Fore.YELLOW + "No stock symbols found. Please add stocks.")
return
questions = [
inquirer.List('symbol',
message='Select a stock symbol:',
choices=stock_list,
),
]
answer = inquirer.prompt(questions)
symbol = answer['symbol']
stock_data = get_stock_data(symbol)
if stock_data is not None and 'Meta Data' in stock_data:
meta_data = stock_data['Meta Data']
print(Fore.CYAN + f"Stock Symbol: {meta_data['2. Symbol']}")
print(f"Last Refreshed: {meta_data['3. Last Refreshed']}")
print(f"Interval: {meta_data['4. Interval']}")
print(f"Output Size: {meta_data['5. Output Size']}")
print(f"Time Zone: {meta_data['6. Time Zone']}")
else:
print(Fore.RED + "Failed to fetch stock information.")
# Function to calculate moving averages
def calculate_moving_average(data, window_size):
moving_averages = []
for i in range(len(data) - window_size + 1):
window = data[i:i+window_size]
average = sum(window) / window_size
moving_averages.append(average)
return moving_averages
# Add moving averages to chart
def add_moving_averages_chart(closes):
window_sizes = [20, 50]
for window_size in window_sizes:
moving_averages = calculate_moving_average(closes, window_size)
print(Fore.CYAN + f"Moving Average ({window_size}):")
print(asciichartpy.plot(moving_averages, {'height': 5}))
# Plot stock chart
def plot_stock_chart():
stock_list = load_stock_list()
if not stock_list:
print(Fore.YELLOW + "No stock symbols found. Please add stocks.")
return
questions = [
inquirer.List('symbol',
message='Select a stock symbol:',
choices=stock_list,
),
]
answer = inquirer.prompt(questions)
symbol = answer['symbol']
stock_data = get_stock_data(symbol)
if stock_data is not None and 'Time Series (5min)' in stock_data:
candles = []
close_prices = [] # List to store close prices for moving averages
for key, value in stock_data['Time Series (5min)'].items():
open_price = float(value['1. open'])
high_price = float(value['2. high'])
low_price = float(value['3. low'])
close_price = float(value['4. close'])
candle = Candle(open=open_price, close=close_price,
high=high_price, low=low_price)
candles.append(candle)
close_prices.append(close_price)
chart = Chart(candles, title=f"Candlestick Chart for {symbol}")
chart.set_name(symbol)
new_width = 150
new_height = 25
chart.update_size(new_width, new_height)
# Draw chart
chart.draw()
# Add moving averages
add_moving_averages_chart(close_prices)
else:
print(Fore.RED + "Failed to fetch stock data.")
# View news for a stock
def view_stock_news():
stock_list = load_stock_list()
if not stock_list:
print(Fore.YELLOW + "No stock symbols found. Please add stocks.")
return
questions = [
inquirer.List('symbol',
message='Select a stock symbol:',
choices=stock_list,
),
]
answer = inquirer.prompt(questions)
symbol = answer['symbol']
news_data = get_stock_news(symbol)
if news_data is not None and 'feed' in news_data:
articles = news_data['feed']
print(Fore.CYAN + "Stock News:")
for article in articles:
print(Fore.YELLOW + "Title:", article['title'])
print(Fore.GREEN + "Source:", article['source'])
print(Fore.BLUE + "Published At:", article['time_published'])
print(Fore.MAGENTA + "Summary:", article['summary'])
print()
else:
print(Fore.RED + "Failed to fetch stock news.")
# Main menu
def main_menu():
questions = [
inquirer.List('action',
message='What would you like to do?',
choices=[
'Set API Key',
'Add Stock',
'Remove Stock',
'Show Stock Information',
'Plot Stock Chart',
'View Stock News',
'Exit'
],
),
]
answer = inquirer.prompt(questions)
return answer['action']
# Main function
def main():
while True:
choice = main_menu()
if choice == 'Set API Key':
set_api_key()
clear_screen()
elif choice == 'Add Stock':
symbol = input("Enter stock symbol to add: ")
clear_screen()
add_stock(symbol)
elif choice == 'Remove Stock':
symbol = input("Enter stock symbol to remove: ")
clear_screen()
remove_stock(symbol)
elif choice == 'Show Stock Information':
clear_screen()
show_stock_info()
elif choice == 'Plot Stock Chart':
clear_screen()
plot_stock_chart()
elif choice == 'View Stock News':
clear_screen()
view_stock_news()
elif choice == 'Exit':
print(Fore.YELLOW + "Exiting the program.")
break
else:
print(Fore.RED + "Invalid choice. Please enter a valid option.")
# Entry point of the program
if __name__ == "__main__":
main()