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,18 +3,21 @@ 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)
if i < max_stocks:
i += 1
try:
symbol, current_price, opening_price = row
symbols.append(symbol)
@ -23,6 +26,9 @@ def readCSV():
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)

View File

@ -1 +1 @@
06/05/2021 15:58:05
07/05/2021 14:25:26

1 06/05/2021 15:58:05 07/05/2021 14:25:26

View File

@ -1,21 +1,22 @@
name,current,opening
MSFT,249.35,246.45
NFLX,498.645,495.99
GOOG,2376.26,2350.64
TSLA,663.1871,680.76
AAPL,129.68,127.89
INTC,57.095,56.615
TXN,184.04,181.93
HPQ,34.975,34.42
HOG,48.895,49.36
LUV,60.65,60.97
WMT,140.925,140
BJ,46.68,45.74
ETSY,157.86,167.01
G,47.115,47.17
GDDY,81.28,80.11
GNRC,317.81,315.29
PEP,145.47,144.47
STMYELP,0,0
XRAY,67.36,66.6
ZTS,167.11,167.25
MSFT,252.64,252.64
NFLX,502.76,502.76
GOOG,2402.26,2402.26
TSLA,669.66,669.66
AAPL,130.175,130.175
INTC,57.51,57.51
TXN,187.09,187.09
HPQ,35.49,35.49
HOG,49.53,49.53
LUV,61.89,61.89
WMT,140.68,140.68
BJ,45.675,45.675
ETSY,164.27,164.27
G,47.14,47.14
GDDY,81.14,81.14
GNRC,324.9,324.9
PEP,145.53,145.53
STM,37.26,37.26
YELP,39.02,39.02
XRAY,68.69,68.69
ZTS,172.2,172.2

1 name current opening
2 MSFT 249.35 252.64 246.45 252.64
3 NFLX 498.645 502.76 495.99 502.76
4 GOOG 2376.26 2402.26 2350.64 2402.26
5 TSLA 663.1871 669.66 680.76 669.66
6 AAPL 129.68 130.175 127.89 130.175
7 INTC 57.095 57.51 56.615 57.51
8 TXN 184.04 187.09 181.93 187.09
9 HPQ 34.975 35.49 34.42 35.49
10 HOG 48.895 49.53 49.36 49.53
11 LUV 60.65 61.89 60.97 61.89
12 WMT 140.925 140.68 140 140.68
13 BJ 46.68 45.675 45.74 45.675
14 ETSY 157.86 164.27 167.01 164.27
15 G 47.115 47.14 47.17 47.14
16 GDDY 81.28 81.14 80.11 81.14
17 GNRC 317.81 324.9 315.29 324.9
18 PEP 145.47 145.53 144.47 145.53
19 STMYELP STM 0 37.26 0 37.26
20 XRAY YELP 67.36 39.02 66.6 39.02
21 ZTS XRAY 167.11 68.69 167.25 68.69
22 ZTS 172.2 172.2