2021-05-05 15:22:01 +00:00
|
|
|
import finnhub
|
|
|
|
import time
|
|
|
|
import csv
|
2021-05-05 19:26:56 +00:00
|
|
|
import pytz
|
|
|
|
from datetime import datetime
|
2021-05-05 15:22:01 +00:00
|
|
|
|
2021-05-06 19:59:27 +00:00
|
|
|
import datetime as dt
|
2021-05-05 19:26:56 +00:00
|
|
|
|
2021-05-06 19:59:27 +00:00
|
|
|
def readCSV():
|
|
|
|
|
|
|
|
symbols = []
|
|
|
|
stock_info = {}
|
|
|
|
f = open('csv/tickers.csv', 'r')
|
|
|
|
CSV = csv.reader(f)
|
|
|
|
next(CSV)
|
|
|
|
for row in CSV:
|
|
|
|
print(row)
|
|
|
|
try:
|
|
|
|
symbol, current_price, opening_price = row
|
|
|
|
symbols.append(symbol)
|
|
|
|
stock_info[symbol] = [current_price, opening_price]
|
|
|
|
except:
|
|
|
|
symbol = row[0]
|
|
|
|
symbols.append(symbol)
|
|
|
|
stock_info[symbol] = []
|
|
|
|
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
return symbols, stock_info
|
|
|
|
|
|
|
|
def emptyInfo(symbols, stock_info):
|
|
|
|
update = False
|
|
|
|
for symbol in symbols:
|
|
|
|
if len(stock_info[symbol]) == 1: # stock with no info
|
|
|
|
update = True
|
|
|
|
return update
|
|
|
|
|
|
|
|
def updateUpdate(NY_time):
|
|
|
|
NY_str = NY_time.strftime("%d/%m/%Y %H:%M:%S")
|
|
|
|
f = open('csv/last_update.csv', 'w+')
|
|
|
|
f.write(NY_str + '\n')
|
|
|
|
f.close()
|
2021-05-05 19:26:56 +00:00
|
|
|
|
2021-05-06 19:59:27 +00:00
|
|
|
def updateStockPrices(symbols):
|
|
|
|
|
|
|
|
apiCalledError = False
|
|
|
|
stock_info = []
|
|
|
|
|
|
|
|
symbols = []
|
|
|
|
f = open('csv/tickers.csv', 'r')
|
|
|
|
CSV = csv.reader(f)
|
|
|
|
next(CSV) #read through headers
|
|
|
|
for row in CSV:
|
|
|
|
symbol = row[0]
|
|
|
|
symbols.append(symbol)
|
|
|
|
f.close()
|
|
|
|
try:
|
|
|
|
quotes = [finnhubClient.quote(symbol) for symbol in symbols]
|
|
|
|
current_prices = [quote['c'] for quote in quotes]
|
|
|
|
opening_prices = [quote['o'] for quote in quotes]
|
|
|
|
|
|
|
|
CSV = open('csv/tickers.csv', 'w+')
|
|
|
|
CSV.write('name,current,opening\n')
|
|
|
|
for i, symbol in enumerate(symbols):
|
|
|
|
symbol + ',' + str(current_prices[i]) + ',' + str(opening_prices[i]) + '\n'
|
|
|
|
CSV.write(symbol + ',' + str(current_prices[i]) + ',' + str(opening_prices[i]) + '\n')
|
|
|
|
CSV.close()
|
|
|
|
|
|
|
|
|
|
|
|
print('API called successfully')
|
|
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
print("Could not fetch data - API CALLS REACHED? - Will display old image")
|
|
|
|
print(e)
|
|
|
|
apiCalledError = True
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
APIkey = "c24qddqad3ickpckgg80"
|
|
|
|
sandboxAPIkey = "sandbox_c24qddqad3ickpckgg8g"
|
|
|
|
finnhubClient = finnhub.Client(api_key=APIkey)
|
2021-05-05 19:26:56 +00:00
|
|
|
|
2021-05-06 19:59:27 +00:00
|
|
|
NY_zone = pytz.timezone('America/New_York')
|
2021-05-05 19:26:56 +00:00
|
|
|
|
2021-05-06 19:59:27 +00:00
|
|
|
NY_time = datetime.now(NY_zone)
|
|
|
|
opening = NY_time.replace(hour=9, minute=30, second=0, microsecond=0)
|
|
|
|
closing = NY_time.replace(hour=16, minute=0, second=0, microsecond=0)
|
2021-05-05 19:26:56 +00:00
|
|
|
|
2021-05-06 18:23:17 +00:00
|
|
|
NY_time = datetime.now(NY_zone)
|
|
|
|
|
2021-05-06 19:59:27 +00:00
|
|
|
sleeptime = 2 #minutes
|
2021-05-05 15:22:01 +00:00
|
|
|
|
2021-05-06 19:59:27 +00:00
|
|
|
while True:
|
|
|
|
NY_time = datetime.now(NY_zone)
|
2021-05-05 15:22:01 +00:00
|
|
|
|
2021-05-06 19:59:27 +00:00
|
|
|
symbols, stock_info = readCSV()
|
|
|
|
print(type(NY_time))
|
2021-05-05 15:22:01 +00:00
|
|
|
|
2021-05-06 19:59:27 +00:00
|
|
|
if opening < NY_time < closing: # we need to do real time updating
|
|
|
|
updateStockPrices(symbols)
|
|
|
|
updateUpdate(NY_time)
|
|
|
|
|
|
|
|
elif emptyInfo(symbols, stock_info): # if theres any empty stocks
|
|
|
|
updateStockPrices(symbols)
|
|
|
|
updateUpdate(NY_time)
|
|
|
|
else:
|
|
|
|
# update if last update was before the previous days closing
|
|
|
|
f = open('csv/last_update.csv', 'r')
|
|
|
|
CSV = csv.reader(f)
|
|
|
|
last_update_str = next(CSV)[0]
|
|
|
|
print(last_update_str)
|
|
|
|
last_update = datetime.strptime(last_update_str, "%d/%m/%Y %H:%M:%S")
|
2021-05-05 19:26:56 +00:00
|
|
|
|
2021-05-06 19:59:27 +00:00
|
|
|
yday_closing = closing - dt.timedelta(days=1)
|
|
|
|
yday_str = yday_closing.strftime("%d/%m/%Y %H:%M:%S")
|
|
|
|
yday_closing = datetime.strptime(yday_str, "%d/%m/%Y %H:%M:%S")
|
|
|
|
|
|
|
|
|
|
|
|
print(last_update < yday_closing)
|
|
|
|
if last_update < yday_closing:
|
|
|
|
updateStockPrices(symbols)
|
2021-05-05 19:26:56 +00:00
|
|
|
|
2021-05-06 19:59:27 +00:00
|
|
|
updateUpdate(NY_time)
|
2021-05-05 19:26:56 +00:00
|
|
|
|
2021-05-05 15:22:01 +00:00
|
|
|
time.sleep(sleeptime*60)
|