-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathyahoo_finance_api.py
94 lines (80 loc) · 3.64 KB
/
yahoo_finance_api.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
name = "python_finance_api"
import pandas as pd
import time as _time
import requests
from datetime import timedelta
IST_OFFSET = timedelta(hours=5,minutes=30).total_seconds()
class YahooFinance:
def __init__(self, ticker, result_range='1mo', start=None, end=None, interval='15m', dropna=True):
"""
Return the stock data of the specified range and interval
Note - Either Use result_range parameter or use start and end
Note - Intraday data cannot extend last 60 days
:param ticker: Trading Symbol of the stock should correspond with yahoo website
:param result_range: Valid Ranges "1d","5d","1mo","3mo","6mo","1y","2y","5y","10y","ytd","max"
:param start: Starting Date
:param end: End Date
:param interval:Valid Intervals - Valid intervals: [1m, 2m, 5m, 15m, 30m, 60m, 90m, 1h, 1d, 5d, 1wk, 1mo, 3mo]
:return:
"""
if result_range is None:
start = int(_time.mktime(_time.strptime(start, '%d-%m-%Y')))
end = int(_time.mktime(_time.strptime(end, '%d-%m-%Y')))
# defining a params dict for the parameters to be sent to the API
params = {'period1': start, 'period2': end, 'interval': interval}
else:
params = {'range': result_range, 'interval': interval}
print('calling yahoo finance link ')
# sending get request and saving the response as response object
url = "https://query1.finance.yahoo.com/v8/finance/chart/{}".format(ticker)
r = requests.get(url=url, params=params)
data = r.json()
print('received data from yahoo link')
# Getting data from json
error = data['chart']['error']
if error:
raise ValueError(error['description'])
self._result = self._parsing_json(data)
if dropna:
self._result.dropna(inplace=True)
self._return = self.get_returns()
if dropna:
self._return.dropna(inplace=True)
@property
def result(self):
return self._result
@property
def returns(self):
return self._return
def _parsing_json(self, data):
timestamps = data['chart']['result'][0]['timestamp']
# Formatting date from epoch to local time
#timestamps = [_time.strftime('%a, %d %b %Y %H:%M:%S', _time.localtime(x)) for x in timestamps]
timestamps = [_time.strftime('%a, %d %b %Y %H:%M:%S', _time.gmtime(x + IST_OFFSET)) for x in timestamps]
volumes = data['chart']['result'][0]['indicators']['quote'][0]['volume']
opens = data['chart']['result'][0]['indicators']['quote'][0]['open']
opens = self._round_of_list(opens)
closes = data['chart']['result'][0]['indicators']['quote'][0]['close']
closes = self._round_of_list(closes)
lows = data['chart']['result'][0]['indicators']['quote'][0]['low']
lows = self._round_of_list(lows)
highs = data['chart']['result'][0]['indicators']['quote'][0]['high']
highs = self._round_of_list(highs)
df_dict = {'Open': opens, 'High': highs, 'Low': lows, 'Close': closes, 'Volume': volumes}
df = pd.DataFrame(df_dict, index=timestamps)
df.index = pd.to_datetime(df.index)
return df
def _round_of_list(self, xlist):
temp_list = []
for x in xlist:
if isinstance(x, float):
temp_list.append(round(x, 2))
else:
temp_list.append(pd.np.nan)
return temp_list
def to_csv(self, file_name):
self.result.to_csv(file_name)
#added by manish
def get_returns(self):
res = self._result['Close'].pct_change()
return res