frist section done

This commit is contained in:
Neythen
2021-05-08 12:10:05 +01:00
parent 079c95f559
commit 4374934237
5 changed files with 251 additions and 41 deletions

View File

@@ -5,6 +5,8 @@ import pytz
from datetime import datetime
import pyEX
import datetime as dt
import sys, os, base64, hashlib, hmac
import requests
def readCSV(max_stocks):
@@ -51,14 +53,16 @@ def updateStockPrices(symbols):
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 = [iexClient.quote(symbol=symbol) for symbol in symbols]
quotes = [finnhubClient.quote(symbol) for symbol in symbols]
current_prices = [quote['c'] for quote in quotes]
opening_prices = [quote['o'] for quote in quotes]
print(current_prices)
print(opening_prices)
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')
@@ -76,19 +80,81 @@ def updateStockPrices(symbols):
print(e)
apiCalledError = True
def updateStockPricesIEX(symbols):
symbols_str = ','.join(symbols)
method = 'GET'
host = 'https://cloud.iexapis.com/stable'
lastEndpoint = '/tops/last'
querystring = '?symbols=' + symbols_str +'&token='+iexAPIkey
last_request_url = host + lastEndpoint + querystring
print('\nBEGIN REQUEST++++++++++++++++++++++++++++++++++++')
print('Request URL = ' + last_request_url)
last_response = requests.get(last_request_url)
if last_response.status_code == 200:
print('last success')
current_prices = []
for stock in last_response.json():
current_prices.append(stock['price'])
for symbol in symbols:
symbol = 'MSFT'
method = 'GET'
host = 'https://cloud.iexapis.com/stable'
lastEndpoint = '/tops/last'
intradayEndpoint = '/stock/'+ symbol+ '/intraday-prices'
querystring = '?chartIEXOnly=true&token='+iexAPIkey
intraday_request_url = host + intradayEndpoint + querystring
print('\nBEGIN REQUEST++++++++++++++++++++++++++++++++++++')
print('Request URL = ' + intraday_request_url)
intraday_response = requests.get(intraday_request_url)
print('\nRESPONSE++++++++++++++++++++++++++++++++++++')
print('Response code: %d\n' % intraday_response.status_code)
print(intraday_response.text)
for time_point in intraday_response.json():
print(time_point['date'])
print(time_point['open'])
CSV = open('csv/tickers.csv', 'w+')
CSV.write('name,current,opening\n')
for i, symbol in enumerate(symbols):
CSV.write(symbol + ',' + str(current_prices[i]) + ',' + str(opening_prices[i]) + '\n')
CSV.close()
print(lasts)
if __name__ == '__main__':
#finnhubAPIkey = "c24qddqad3ickpckgg80" #Finnhub
#finnhubsandboxAPIkey = "sandbox_c24qddqad3ickpckgg8g" #Finnhub
finnhubAPIkey = "c24qddqad3ickpckgg80" #Finnhub
finnhubsandboxAPIkey = "sandbox_c24qddqad3ickpckgg8g" #Finnhub
sleeptime = 2 #minutes
finnhubClient = finnhub.Client(api_key=finnhubAPIkey)
max_stocks = 200
iexAPIkey = 'pk_68ef6a15902c41f887f0b544a0ca17cf' #IEX
iexSandboxAPIkey = 'Tpk_0078dff413ef4f979137f7111452dc4b'
#updateStockPricesIEX(symbols)
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)
finnhubClient = finnhub.Client(api_key=finnhubAPIkey)
NY_zone = pytz.timezone('America/New_York')
NY_time = datetime.now(NY_zone)
@@ -97,24 +163,31 @@ if __name__ == '__main__':
NY_time = datetime.now(NY_zone)
sleeptime = 2 #minutes
max_stocks = 200
print(NY_time)
while True:
NY_time = datetime.now(NY_zone)
symbols, stock_info = readCSV(max_stocks)
if opening < NY_time < closing and datetime.today().weekday() < 5: # we need to do real time updating
print('market open')
if opening < NY_time < closing: # we need to do real time updating
print('market open')
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]
@@ -128,6 +201,7 @@ if __name__ == '__main__':
print(last_update < yday_closing)
if last_update < yday_closing:
updateStockPrices(symbols)
updateUpdate(NY_time)