Intro
This application pulls live stock data in json format then parses the data accordingly. It requires an API key which can be requested at this link https://www.alphavantage.co/support/#api-key.
Installation
In order to run this program, package alpha-vatange is required. Install the package on your localhost or virtual environment using:
pip install alpha_vantage
API Key
This portion of the code initializes the API key. The user is also required to specify the stocks in a list format.
apiKey = os.getenv('STOCK_API')
stock_sym = ['GOOGL','FB']
ts = TimeSeries(apiKey)
Weekday
This function is responsible for determining the day of the week. If the end user runs the program during the weekend, data from Friday will be displayed.
def weekday():
today_date = date.today()
weekday = today_date.weekday()
if weekday <= 4:
return today_date
elif weekday == 5 :
return datetime.strftime(datetime.now() - timedelta(1), '%Y-%m-%d')
else:
return datetime.strftime(datetime.now() - timedelta(2), '%Y-%m-%d')
Stock Data
An API call is made to alpha vantage in this function, and the result is stored in a list.
def vatage_stock_data(date):
vantage_data = []
local_stock_list = []
for symbol in stock_sym:
symbol, meta = ts.get_daily(symbol=f'{symbol}')
vantage_data.append(symbol[f'{date}'])
for line,name in zip(vantage_data,stock_sym):
new_dict={f'{name}':line}
local_stock_list.append(new_dict)
return local_stock_list
Parse Data
The stock list retrieved via the API call is parsed in this function. This function displays stock analytics that can be easily modified. I chose to display the daily open, close, high, and close.
def stock_open(list):
for i in range(len(list)):
for symbol,value in list[i].items():
print("OPEN",symbol,value['1. open'],sep="-")
print("\n")
def stock_high(list):
for i in range(len(list)):
for symbol,value in list[i].items():
print("HIGH",symbol,value['2. high'],sep="-")
print("\n")
def stock_low(list):
for i in range(len(list)):
for symbol,value in list[i].items():
print("LOW",symbol,value['3. low'],sep=":")
print("\n")
def stock_close(list):
for i in range(len(list)):
for symbol,value in list[i].items():
print("ClOSE",symbol,value['4. close'],sep="-")
print("\n")
Full Code
import urllib.request
from alpha_vantage.timeseries import TimeSeries
from datetime import date, datetime, timedelta
import os
apiKey = os.getenv('STOCK_API')
stock_sym = ['GOOGL','FB']
ts = TimeSeries(apiKey)
def weekday():
today_date = date.today()
weekday = today_date.weekday()
if weekday <= 4:
return today_date
elif weekday == 5 :
return datetime.strftime(datetime.now() - timedelta(1), '%Y-%m-%d')
else:
return datetime.strftime(datetime.now() - timedelta(2), '%Y-%m-%d')
def vatage_stock_data(date):
vantage_data = []
local_stock_list = []
for symbol in stock_sym:
symbol, meta = ts.get_daily(symbol=f'{symbol}')
vantage_data.append(symbol[f'{date}'])
for line,name in zip(vantage_data,stock_sym):
new_dict={f'{name}':line}
local_stock_list.append(new_dict)
return local_stock_list
def stock_open(list):
for i in range(len(list)):
for symbol,value in list[i].items():
print("OPEN",symbol,value['1. open'],sep="-")
print("\n")
def stock_high(list):
for i in range(len(list)):
for symbol,value in list[i].items():
print("HIGH",symbol,value['2. high'],sep="-")
print("\n")
def stock_low(list):
for i in range(len(list)):
for symbol,value in list[i].items():
print("LOW",symbol,value['3. low'],sep=":")
print("\n")
def stock_close(list):
for i in range(len(list)):
for symbol,value in list[i].items():
print("ClOSE",symbol,value['4. close'],sep="-")
print("\n")
weekday = weekday()
stock_data = vatage_stock_data(weekday)
stock_open(stock_data)
stock_high(stock_data)
stock_low(stock_data)
stock_close(stock_data)
Top comments (3)
Hi, can I do this without installing alpha-vatange ? It would be very nice if you could do this!
Yes, I need it for forex. To simplify the work for the sewer market.
I read a very useful article about forex spread calculator and realized that this code will help me with this!
However, I don't want to install alpha-vatange because I don't know anything about it! And that makes me uneasy before setting it up.
Although if someone can convince me or help me figure out that alpha-vatange can be trusted, then I would install it...
So if you understand this , I would be happy to get a constructive answer! Thank you in advance !
Do you have anything for Forex?
Yes, that would be interesting.