switched to IEX api

This commit is contained in:
Neythen
2021-05-07 19:39:42 +01:00
parent 0cf368a527
commit 71d30146cf
3 changed files with 62 additions and 51 deletions

View File

@@ -3,26 +3,32 @@ import time
import csv
import pytz
from datetime import datetime
import pyEX
import datetime as dt
def readCSV():
def readCSV(max_stocks):
symbols = []
stock_info = {}
f = open('csv/tickers.csv', 'r')
CSV = csv.reader(f)
next(CSV)
i = 0
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] = []
if i < max_stocks:
i += 1
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] = []
else:
print('max stocks exceeded')
break
f.close()
@@ -43,26 +49,21 @@ def updateUpdate(NY_time):
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]
#quotes = [finnhubClient.quote(symbol) for symbol in symbols]
#current_prices = [quote['c'] for quote in quotes]
#opening_prices = [quote['o'] for quote in quotes]
quotes = [iexClient.quote(symbol=symbol) for symbol in symbols]
current_prices = [quote['iexRealtimePrice'] for quote in quotes]
opening_prices = [quote['iexOpen'] for quote in quotes]
print(current_prices, opening_prices)
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()
@@ -76,9 +77,17 @@ def updateStockPrices(symbols):
apiCalledError = True
if __name__ == '__main__':
APIkey = "c24qddqad3ickpckgg80"
sandboxAPIkey = "sandbox_c24qddqad3ickpckgg8g"
finnhubClient = finnhub.Client(api_key=APIkey)
#finnhubAPIkey = "c24qddqad3ickpckgg80" #Finnhub
#finnhubsandboxAPIkey = "sandbox_c24qddqad3ickpckgg8g" #Finnhub
iexAPIkey = 'pk_68ef6a15902c41f887f0b544a0ca17cf' #IEX
iexSandboxAPIkey = 'Tpk_0078dff413ef4f979137f7111452dc4b'
iexClient = pyEX.Client(api_token = iexSandboxAPIkey, version = 'stable')
print(iexClient.quote(symbol='MSFT')['iexRealtimePrice'] )
print(iexClient.quote(symbol='MSFT')['iexOpen'] )
#finnhubClient = finnhub.Client(api_key=APIkey)
NY_zone = pytz.timezone('America/New_York')
@@ -90,13 +99,14 @@ if __name__ == '__main__':
sleeptime = 2 #minutes
max_stocks = 200
while True:
NY_time = datetime.now(NY_zone)
symbols, stock_info = readCSV()
print(type(NY_time))
symbols, stock_info = readCSV(max_stocks)
if opening < NY_time < closing: # we need to do real time updating
print('market open')
updateStockPrices(symbols)
updateUpdate(NY_time)