market opening time check added
This commit is contained in:
@@ -1,41 +1,55 @@
|
||||
import finnhub
|
||||
import time
|
||||
import csv
|
||||
import pytz
|
||||
from datetime import datetime
|
||||
|
||||
APIkey = "c24qddqad3ickpckgg80"
|
||||
sandboxAPIkey = "sandbox_c24qddqad3ickpckgg8g"
|
||||
finnhubClient = finnhub.Client(api_key=APIkey)
|
||||
|
||||
NY_zone = pytz.timezone('America/New_York')
|
||||
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)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def getStockPrices():
|
||||
apiCalledError = False
|
||||
stock_info = []
|
||||
|
||||
symbols = []
|
||||
f = open('csv/tickers.csv', 'r')
|
||||
CSV = csv.reader(f)
|
||||
|
||||
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]
|
||||
if opening < NY_time < closing:
|
||||
apiCalledError = False
|
||||
stock_info = []
|
||||
|
||||
CSV = open('csv/tickers.csv', 'w+')
|
||||
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')
|
||||
symbols = []
|
||||
f = open('csv/tickers.csv', 'r')
|
||||
CSV = csv.reader(f)
|
||||
|
||||
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]
|
||||
|
||||
|
||||
except Exception as e:
|
||||
print("Could not fetch data - API CALLS REACHED? - Will display old image")
|
||||
print(e)
|
||||
apiCalledError = True
|
||||
return stock_info, apiCalledError
|
||||
CSV = open('csv/tickers.csv', 'w+')
|
||||
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__':
|
||||
|
Reference in New Issue
Block a user