-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalphavantage.py
74 lines (44 loc) · 2.35 KB
/
alphavantage.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
import urllib.request, json
from config import Config
import os
import pandas as pd
import datetime as dt
from utils import Utils
config = Config()
utils = Utils()
class AlphaVantage:
def __init__(self):
self.API_KEY = config.get_property("ALPHAVANTAGE_API_KEY")
self.BASE_URL_TEMPLATE = config.get_property("ALPHAVANTAGE_URL")
self.save_file_name_template = config.get_property("save_file_name_template")
def __hit_api_and_get_data(self, URL):
try:
with urllib.request.urlopen(URL) as url:
data = json.loads(url.read().decode())
except Exception as e:
print(e)
return data
def __save_data_frame(self, df, save_file_name):
df.to_csv(save_file_name)
def get_ticker_data(self, ticker):
URL_WITH_TICKER = self.BASE_URL_TEMPLATE.format(
time_series=config.get_property("time_series_type", "WEEKLY").upper(),
symbol=ticker,
apikey=self.API_KEY
)
save_file_name = self.save_file_name_template.format(ticker=ticker, date=utils.get_datetime(), time_series_type=config.get_property("time_series_type"))
if not utils.does_file_exist(save_file_name):
data = self.__hit_api_and_get_data(URL_WITH_TICKER)
key = 'Time Series (Daily)' if config.get_property("time_series_type") == "Daily" else f"{config.get_property("time_series_type")} Time Series"
data = data[key]
df = pd.DataFrame(columns=['Date', 'Low', 'High', 'Close', 'Open', 'Volume'])
for k, v in data.items():
date = dt.datetime.strptime(k, '%Y-%m-%d')
data_row = [date.date(), float(v['3. low']), float(v['2. high']),
float(v['4. close']), float(v['1. open']), float(v['5. volume'])]
df = pd.concat([df, pd.DataFrame([data_row], columns=df.columns)], ignore_index=True)
self.__save_data_frame(df, save_file_name=save_file_name)
else:
df = pd.read_csv(save_file_name)
return df
pass