bugfixing pre-afterhours quote data
This commit is contained in:
parent
64f286360f
commit
9513ebd340
@ -1,4 +1,4 @@
|
|||||||
import ujson
|
import orjson
|
||||||
import asyncio
|
import asyncio
|
||||||
import aiohttp
|
import aiohttp
|
||||||
import sqlite3
|
import sqlite3
|
||||||
@ -55,16 +55,19 @@ async def get_bid_ask_quote_of_stocks(ticker_list):
|
|||||||
|
|
||||||
async def save_quote_as_json(symbol, data):
|
async def save_quote_as_json(symbol, data):
|
||||||
with open(f"json/quote/{symbol}.json", 'w') as file:
|
with open(f"json/quote/{symbol}.json", 'w') as file:
|
||||||
ujson.dump(data, file)
|
file.write(orjson.dumps(data).decode())
|
||||||
|
|
||||||
async def save_pre_post_quote_as_json(symbol, data):
|
async def save_pre_post_quote_as_json(symbol, data):
|
||||||
try:
|
try:
|
||||||
with open(f"json/quote/{symbol}.json", 'r') as file:
|
with open(f"json/quote/{symbol}.json", 'r') as file:
|
||||||
previous_close = (ujson.load(file))['price']
|
quote_data = orjson.loads(file.read())
|
||||||
|
exchange = quote_data.get('exchange',None)
|
||||||
|
previous_close = quote_data['price']
|
||||||
changes_percentage = round((data['price']/previous_close-1)*100,2)
|
changes_percentage = round((data['price']/previous_close-1)*100,2)
|
||||||
with open(f"json/pre-post-quote/{symbol}.json", 'w') as file:
|
if exchange in ['NASDAQ','AMEX','NYSE']:
|
||||||
res = {'symbol': symbol, 'price': round(data['price'],2), 'changesPercentage': changes_percentage, 'time': data['timestamp']}
|
with open(f"json/pre-post-quote/{symbol}.json", 'w') as file:
|
||||||
ujson.dump(res, file)
|
res = {'symbol': symbol, 'price': round(data['price'],2), 'changesPercentage': changes_percentage, 'time': data['timestamp']}
|
||||||
|
file.write(orjson.dumps(res).decode())
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@ -72,7 +75,7 @@ async def save_bid_ask_as_json(symbol, data):
|
|||||||
try:
|
try:
|
||||||
# Read previous close price and load existing quote data
|
# Read previous close price and load existing quote data
|
||||||
with open(f"json/quote/{symbol}.json", 'r') as file:
|
with open(f"json/quote/{symbol}.json", 'r') as file:
|
||||||
quote_data = ujson.load(file)
|
quote_data = orjson.loads(file.read())
|
||||||
|
|
||||||
# Update quote data with new price, ask, bid, changesPercentage, and timestamp
|
# Update quote data with new price, ask, bid, changesPercentage, and timestamp
|
||||||
quote_data.update({
|
quote_data.update({
|
||||||
@ -82,7 +85,7 @@ async def save_bid_ask_as_json(symbol, data):
|
|||||||
|
|
||||||
# Save the updated quote data back to the same JSON file
|
# Save the updated quote data back to the same JSON file
|
||||||
with open(f"json/quote/{symbol}.json", 'w') as file:
|
with open(f"json/quote/{symbol}.json", 'w') as file:
|
||||||
ujson.dump(quote_data, file)
|
file.write(orjson.dumps(quote_data).decode())
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"An error occurred: {e}") # Print the error for debugging
|
print(f"An error occurred: {e}") # Print the error for debugging
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user