-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfetchdata.py
116 lines (93 loc) · 3.25 KB
/
fetchdata.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
from dotenv import load_dotenv
import json
import os
import requests
import pandas as pd
from datetime import datetime
import datetime
load_dotenv()
apikey = str(os.environ.get("ALPHAVANTAGE_API_KEY"))
#intro Messages
print("--------------------------------------------------- ")
print(" ")
print("This application helps fetch real stock data from Alpha Vantage")
print(" ")
print("You can check the stock ticker of a company online")
print(" ")
print("E.G. The ticker for GOOGLE is GOOGL. The ticker for Amazon is AMZN.")
print(" ")
print("--------------------------------------------------- ")
print(" ")
#requesting ticker for the stock
while True:
ticker = input("Please enter the ticker of your stock of choice: ")
if ticker.isdigit():
print("Invalid entry: a stock ticker only uses characters - integers or symbols are not permitted")
else:
pull = requests.get("https://www.alphavantage.co/query?function=TIME_SERIES_DAILY_ADJUSTED&symbol=" + ticker + "&outputsize=full&apikey=" + apikey)
if "Error" in pull.text:
print("Error: stock either cannot be found or is not listed on Alpha Vantage - please enter another stock ticker")
else:
break
j=pull.json()
#variables created for the date to be used later
time = datetime.datetime.now()
a = time.strftime("%Y")
b = time.strftime("%m")
c = time.strftime("%d")
d = time.strftime("%I")
e = time.strftime("%M")
f = time.strftime("%p")
t,opn,h,l,close,adj_close,vol,div = [],[],[],[],[],[],[],[]
#adds values pulled from Alpha Vantage
for lx, value in j["Time Series (Daily)"].items():
t.append(lx)
opn.append(float(value["1. open"]))
h.append(float(value["2. high"]))
l.append(float(value["3. low"]))
close.append(float(value["4. close"]))
adj_close.append(float(value["5. adjusted close"]))
vol.append(float(value["6. volume"]))
div.append(float(value["7. dividend amount"]))
print(" ")
print("--------------------------------------------------- ")
print(" ")
print("Stock Ticker: " + ticker)
print(" ")
print("Program Run On: " + a + "-" + b + "-" + c + " " + d + ":" + e + " " + f)
print(" ")
print("...")
print(" ")
print("Now saving the requested information")
print(" ")
print("...")
print(" ")
#data headers are formatted in order to be put into a CSV
output = pd.DataFrame(
{
"Date":t, "Open":opn, "High": h, "Low":l, "Close":close, "Adjusted_Close":adj_close, "Volume": vol, "Dividends": div
}
)
#rearrange the data from oldest to newest
idx = output.index.values
output = output.iloc[::-1]
output.index = idx
#deletes a file if it is named in the same way (the data would essentially be the same)
while True:
if os.path.isfile("data/" +ticker + "_" + a + b + c + ".csv"):
os.remove("data/" + ticker + "_" + a + b + c + ".csv")
else:
break
#data is pushed into a CSV file
output.to_csv("data/" + ticker + "_" + a + b + c + ".csv")
print("File saved as " + ticker + "_" + a + b + c + ".csv in the 'data' folder")
print(" ")
print("--------------------------------------------------- ")
print(" ")
print(" ")
print("CALCULATING BASIC RELEVANT DATA")
print(" ")
print("...")
print(" ")
print("LAST DAY OF AVAILABLE DATA: " + output.iloc[max(idx)]["Date"])
print("")