-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProject3BaseCode.py
More file actions
153 lines (119 loc) · 4.93 KB
/
Copy pathProject3BaseCode.py
File metadata and controls
153 lines (119 loc) · 4.93 KB
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
import requests
import matplotlib.pyplot as plt
import pandas as pd
import datetime
import webbrowser
API_KEY = "OVOCU1LAVMVSV4ZY"
BASE_URL = "https://www.alphavantage.co/query"
def get_stock_data(symbol, function, interval=None):
params = {
"function": function,
"symbol": symbol,
"apikey": API_KEY,
"datatype": "json"
}
if interval:
params["interval"] = interval
try:
response = requests.get(BASE_URL, params=params)
response.raise_for_status()
data = response.json()
#checking for API limit or API error
if "Note" in data:
print("API call limit reached. Please wait and try again later.")
return None
if "Error Message" in data:
print("Error: Invalid stock symbol or function. Please check input")
return None
time_series_key = next((key for key in data.keys() if 'Time Series' in key), None)
if not time_series_key:
print("Unexpected API response or response format. No time series data found.")
return None
df = pd.DataFrame.from_dict(data[time_series_key], orient='index')
df.index = pd.to_datetime(df.index)
df = df.apply(pd.to_numeric)
return df
except requests.exceptions.RequestException as e:
print(f"Networking error: {e}")
return None
except ValueError:
print("Error occured while attempting to parse API response. Please try again.")
return None
def plot_stock_data(df, start_date, end_date, chart_type, symbol):
df = df.sort_index()
filtered_df = df.loc[start_date:end_date]
if filtered_df.empty:
print("No data available for the selected date range.")
return
plt.figure(figsize=(10, 5))
if chart_type == "line":
plt.plot(filtered_df.index, filtered_df['4. close'], label=f'{symbol} Closing Price', linestyle='-')
elif chart_type == "bar":
plt.bar(filtered_df.index, filtered_df['4. close'], label=f'{symbol} Closing Price')
plt.xlabel("Date")
plt.ylabel("Closing Price (USD)")
plt.title(f"Stock Data for {symbol} ({start_date} to {end_date})")
plt.legend()
plt.xticks(rotation=45)
plt.tight_layout()
chart_filename = "stock_chart.png"
plt.savefig(chart_filename)
plt.show()
webbrowser.open(chart_filename)
def main():
symbol = input("Enter stock symbol (e.g., AAPL, MSFT): ").upper()
# Loop until valid graph type is entered
while True:
chart_type = input("Enter chart type (line/bar): ").strip().lower()
if chart_type in ["line", "bar"]:
break # Exit loop if valid input
else:
print("Invalid input. Please enter either 'line' or 'bar'.")
functions = {
"1": "TIME_SERIES_INTRADAY",
"2": "TIME_SERIES_DAILY",
"3": "TIME_SERIES_DAILY_ADJUSTED",
"4": "TIME_SERIES_WEEKLY",
"5": "TIME_SERIES_WEEKLY_ADJUSTED",
"6": "TIME_SERIES_MONTHLY",
"7": "TIME_SERIES_MONTHLY_ADJUSTED"
}
print("Select time series function:")
for key, value in functions.items():
print(f"{key}. {value}")
function_choice = input("Enter your choice (1-7): ").strip()
function = functions.get(function_choice)
if not function:
print("Invalid choice. Please restart and enter a valid integer between 1 and 7.")
return
interval = None
#checking for valid interval selection
if function == "TIME_SERIES_INTRADAY":
valid_intervals = ["1min", "5min", "15min", "30min", "60min"]
while True:
interval = input("Enter interval (1min, 5min, 15min, 30min, 60min): ")
if interval in valid_intervals:
break
else:
print("Invalid interval. Please choose from 1min, 5min, 15min, 30min, or 60min.")
while True:
start_date = input("Enter start date (YYYY-MM-DD): ")
end_date = input("Enter end date (YYYY-MM-DD): ")
try:
start_date_obj = datetime.datetime.strptime(start_date, "%Y-%m-%d")
end_date_obj = datetime.datetime.strptime(end_date, "%Y-%m-%d")
if start_date_obj > end_date_obj:
print("Error: End date cannot be before start date. Please try again.")
else:
break
except ValueError:
print("Invalid date format. Please enter in YYYY-MM-DD format.")
df = get_stock_data(symbol, function, interval)
if df is not None:
plot_stock_data(df, start_date, end_date, chart_type, symbol)
if __name__ == "__main__":
#soft-crash error handling implementation
try:
main()
except Exception as e:
print("An error occured: {e}")