-
Notifications
You must be signed in to change notification settings - Fork 1
/
stock.py
44 lines (35 loc) · 1.79 KB
/
stock.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
import os, requests, math,json
class Stock:
def __init__(self, symbol, date):
# Set symbol, date
self.symbol = symbol
self.date = date
# Get alphavantage key from ENV
if os.environ.get("ALPHAVANTAGE_API_KEY"):
self.alphavantage_api_key = os.environ.get("ALPHAVANTAGE_API_KEY")
else:
raise EnvironmentError("Must set ALPHAVANTAGE_API_KEY")
# Get daily data
self.daily_data = self.get_daily_data()
def get_daily_data(self):
# Get daily prices of stock for past 200 days
request_url = "https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol={0}&apikey={1}".format(self.symbol, self.alphavantage_api_key)
r = requests.get(request_url)
if r.status_code != 200:
return False
# Get data from json of successful request
return r.json()["Time Series (Daily)"]
def current_price(self):
# Get current price of stock (realtime)
request_url = "https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol={0}&interval=1min&apikey={1}".format(self.symbol, self.alphavantage_api_key)
r = requests.get(request_url)
if r.status_code != 200:
return False
# Return most recent data
for key, value in r.json()["Time Series (1min)"].items():
return float(value["4. close"])
def get_moving_average(self, timeperiod):
# Get moving average of a stock over a given timeperiod
url = "https://www.alphavantage.co/query?function=SMA&symbol="+self.symbol+"&interval=daily&time_period="+str(timeperiod)+"&series_type=low&apikey="+self.alphavantage_api_key
data = requests.get(url)
return data.json()["Technical Analysis: SMA"][self.date]["SMA"]