crypto added

This commit is contained in:
Neythen
2021-05-14 13:02:22 +01:00
parent 5db70489b6
commit c4d0a61ec0
7 changed files with 83 additions and 54 deletions

View File

@@ -3,16 +3,17 @@ import time
import csv
import pytz
from datetime import datetime
import pyEX
import datetime as dt
import sys, os, base64, hashlib, hmac
import requests
from pycoingecko import CoinGeckoAPI
def readCSV(max_stocks):
def readCSV(file_path, max_stocks):
symbols = []
stock_info = {}
f = open('csv/tickers.csv', 'r')
f = open(file_path, 'r')
CSV = csv.reader(f)
next(CSV)
i = 0
@@ -51,18 +52,13 @@ def updateUpdate(NY_time):
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]
print(current_prices)
print(opening_prices)
print(current_prices, opening_prices)
CSV = open('csv/tickers.csv', 'w+')
CSV.write('name,current,opening\n')
@@ -71,10 +67,8 @@ def updateStockPrices(symbols):
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)
@@ -138,7 +132,19 @@ def updateStockPricesIEX(symbols):
CSV.close()
print(lasts)
def updateCrypto(coins):
response = coingecko_client.get_price(ids=','.join(coins), vs_currencies = 'usd', include_24hr_change=True)
CSV = open('csv/crypto.csv', 'w+')
CSV.write('name,current,24hr change\n')
for coin in coins:
CSV.write(coin+ ',' + str(response[coin]['usd']) + ',' + str(response[coin]['usd_24h_change']) + '\n')
CSV.close()
if __name__ == '__main__':
finnhubAPIkey = "c24qddqad3ickpckgg80" #Finnhub
finnhubsandboxAPIkey = "sandbox_c24qddqad3ickpckgg8g" #Finnhub
@@ -155,6 +161,9 @@ if __name__ == '__main__':
finnhubClient = finnhub.Client(api_key=finnhubAPIkey)
coingecko_client = CoinGeckoAPI()
NY_zone = pytz.timezone('America/New_York')
NY_time = datetime.now(NY_zone)
@@ -164,30 +173,29 @@ if __name__ == '__main__':
NY_time = datetime.now(NY_zone)
print(NY_time)
coins, coin_info = readCSV('csv/crypto.csv', max_stocks)
print(coins, coin_info)
updateCrypto(coins)
sys.exit()
while True:
NY_time = datetime.now(NY_zone)
symbols, stock_info = readCSV(max_stocks)
symbols, stock_info = readCSV('csv/tickers.csv', max_stocks)
if opening < NY_time < closing and datetime.today().weekday() < 5: # 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]