bugfixing

This commit is contained in:
MuslemRahimi 2024-11-25 23:02:37 +01:00
parent 33de38905e
commit 441f154fbc
2 changed files with 145 additions and 116 deletions

View File

@ -352,102 +352,129 @@ async def get_analyst_report():
return {} return {}
async def run(): async def run():
async with aiohttp.ClientSession() as session: async with aiohttp.ClientSession() as session:
recent_earnings = await get_recent_earnings(session) recent_earnings = await get_recent_earnings(session)
upcoming_earnings = await get_upcoming_earnings(session, today, filter_today=False) upcoming_earnings = await get_upcoming_earnings(session, today, filter_today=False)
# If results are less than 5, try without the time filter.
if len(upcoming_earnings) < 5:
upcoming_earnings = await get_upcoming_earnings(session, today, filter_today=True)
# If still less than 5 results, try fetching for tomorrow. upcoming_earnings = [
if len(upcoming_earnings) < 5: item for item in upcoming_earnings
upcoming_earnings = await get_upcoming_earnings(session, tomorrow, filter_today=True) if item['symbol'] not in [earning['symbol'] for earning in recent_earnings]
]
if len(upcoming_earnings) < 5:
upcoming_earnings = await get_upcoming_earnings(session, today, filter_today=True)
if len(upcoming_earnings) < 5:
upcoming_earnings = await get_upcoming_earnings(session, tomorrow, filter_today=True)
recent_analyst_report = await get_analyst_report()
upcoming_earnings = [
item for item in upcoming_earnings
if item['symbol'] not in [earning['symbol'] for earning in recent_earnings]
]
#recent_dividends = await get_recent_dividends(session)
recent_analyst_report = await get_analyst_report()
#Avoid clashing of recent and upcoming earnings try:
upcoming_earnings = [item for item in upcoming_earnings if item['symbol'] not in [earning['symbol'] for earning in recent_earnings]] with open("json/options-flow/feed/data.json", 'r') as file:
options_flow = ujson.load(file)
try: # Filter the options_flow to include only items with ticker in total_symbol
with open(f"json/retail-volume/data.json", 'r') as file: options_flow = [item for item in options_flow if item['ticker'] in stock_symbols]
retail_tracker = ujson.load(file)[0:5]
except:
retail_tracker = []
try:
with open(f"json/options-flow/feed/data.json", 'r') as file:
options_flow = ujson.load(file)
# Filter the options_flow to include only items with ticker in total_symbol highest_volume = sorted(options_flow, key=lambda x: int(x['volume']), reverse=True)
options_flow = [item for item in options_flow if item['ticker'] in stock_symbols] highest_volume = [
{key: item[key] for key in ['cost_basis', 'ticker', 'underlying_type', 'date_expiration', 'put_call', 'volume', 'strike_price']}
for item in highest_volume[0:4]
]
highest_volume = sorted(options_flow, key=lambda x: int(x['volume']), reverse=True) highest_premium = sorted(options_flow, key=lambda x: int(x['cost_basis']), reverse=True)
highest_volume = [{key: item[key] for key in ['cost_basis', 'ticker','underlying_type', 'date_expiration', 'put_call', 'volume', 'strike_price']} for item in highest_volume[0:4]] highest_premium = [
{key: item[key] for key in ['cost_basis', 'ticker', 'underlying_type', 'date_expiration', 'put_call', 'volume', 'strike_price']}
for item in highest_premium[0:4]
]
highest_premium = sorted(options_flow, key=lambda x: int(x['cost_basis']), reverse=True) highest_open_interest = sorted(options_flow, key=lambda x: int(x['open_interest']), reverse=True)
highest_premium = [{key: item[key] for key in ['cost_basis', 'ticker','underlying_type', 'date_expiration', 'put_call', 'volume', 'strike_price']} for item in highest_premium[0:4]] highest_open_interest = [
{key: item[key] for key in ['cost_basis', 'ticker', 'underlying_type', 'date_expiration', 'put_call', 'open_interest', 'strike_price']}
for item in highest_open_interest[0:4]
]
highest_open_interest = sorted(options_flow, key=lambda x: int(x['open_interest']), reverse=True) options_flow = {
highest_open_interest = [{key: item[key] for key in ['cost_basis', 'ticker','underlying_type', 'date_expiration', 'put_call', 'open_interest', 'strike_price']} for item in highest_open_interest[0:4]] 'premium': highest_premium,
'volume': highest_volume,
'openInterest': highest_open_interest
}
except Exception as e:
print(e)
options_flow = {}
options_flow = {'premium': highest_premium, 'volume': highest_volume, 'openInterest':highest_open_interest} market_status = check_market_hours()
except Exception as e: if market_status == 0:
print(e) try:
options_flow = {} with open("json/market-movers/markethours/gainers.json", 'r') as file:
gainers = ujson.load(file)
with open("json/market-movers/markethours/losers.json", 'r') as file:
losers = ujson.load(file)
market_movers = {'gainers': gainers['1D'][:5], 'losers': losers['1D'][:5]}
except:
market_movers = {}
elif market_status == 1:
try:
with open("json/market-movers/premarket/gainers.json", 'r') as file:
data = ujson.load(file)
gainers = [
{'symbol': item['symbol'], 'name': item['name'], 'price': item['price'],
'changesPercentage': item['changesPercentage'], 'marketCap': item['marketCap']}
for item in data[:5]
]
with open("json/market-movers/premarket/losers.json", 'r') as file:
data = ujson.load(file)
losers = [
{'symbol': item['symbol'], 'name': item['name'], 'price': item['price'],
'changesPercentage': item['changesPercentage'], 'marketCap': item['marketCap']}
for item in data[:5]
]
market_status = check_market_hours() market_movers = {'gainers': gainers, 'losers': losers}
if market_status == 0: except:
try: market_movers = {}
with open(f"json/market-movers/markethours/gainers.json", 'r') as file: elif market_status == 2:
gainers = ujson.load(file) try:
with open(f"json/market-movers/markethours/losers.json", 'r') as file: with open("json/market-movers/afterhours/gainers.json", 'r') as file:
losers = ujson.load(file) data = ujson.load(file)
market_movers = {'gainers': gainers['1D'][:5], 'losers': losers['1D'][:5]} gainers = [
except: {'symbol': item['symbol'], 'name': item['name'], 'price': item['price'],
market_movers = {} 'changesPercentage': item['changesPercentage'], 'marketCap': item['marketCap']}
elif market_status == 1: for item in data[:5]
try: ]
with open(f"json/market-movers/premarket/gainers.json", 'r') as file:
data = ujson.load(file)
gainers = [{ 'symbol': item['symbol'], 'name': item['name'], 'price': item['price'], 'changesPercentage': item['changesPercentage'], 'marketCap': item['marketCap']} for item in data[:5]]
with open(f"json/market-movers/premarket/losers.json", 'r') as file: with open("json/market-movers/afterhours/losers.json", 'r') as file:
data = ujson.load(file) data = ujson.load(file)
losers = [{ 'symbol': item['symbol'], 'name': item['name'], 'price': item['price'], 'changesPercentage': item['changesPercentage'], 'marketCap': item['marketCap']} for item in data[:5]] losers = [
{'symbol': item['symbol'], 'name': item['name'], 'price': item['price'],
'changesPercentage': item['changesPercentage'], 'marketCap': item['marketCap']}
for item in data[:5]
]
market_movers={'gainers': gainers, 'losers': losers} market_movers = {'gainers': gainers, 'losers': losers}
except: except:
market_movers = {} market_movers = {}
elif market_status == 2:
try:
with open(f"json/market-movers/afterhours/gainers.json", 'r') as file:
data = ujson.load(file)
gainers = [{ 'symbol': item['symbol'], 'name': item['name'], 'price': item['price'], 'changesPercentage': item['changesPercentage'], 'marketCap': item['marketCap']} for item in data[:5]]
with open(f"json/market-movers/afterhours/losers.json", 'r') as file: data = {
data = ujson.load(file) 'marketMovers': market_movers,
losers = [{ 'symbol': item['symbol'], 'name': item['name'], 'price': item['price'], 'changesPercentage': item['changesPercentage'], 'marketCap': item['marketCap']} for item in data[:5]] 'marketStatus': market_status,
'optionsFlow': options_flow,
'recentEarnings': recent_earnings,
'upcomingEarnings': upcoming_earnings,
'analystReport': recent_analyst_report,
}
market_movers={'gainers': gainers, 'losers': losers} if len(data) > 0:
await save_json(data)
except:
market_movers = {}
data = {
'marketMovers': market_movers,
'marketStatus': market_status,
'optionsFlow': options_flow,
'recentEarnings': recent_earnings,
'upcomingEarnings': upcoming_earnings,
'analystReport': recent_analyst_report,
}
if len(data) > 0:
await save_json(data)
try: try:

View File

@ -344,6 +344,7 @@ fastify.register(async function (fastify) {
}); });
fastify.register(async function (fastify) { fastify.register(async function (fastify) {
fastify.get( fastify.get(
"/multiple-realtime-data", "/multiple-realtime-data",
@ -351,8 +352,8 @@ fastify.register(async function (fastify) {
(connection, req) => { (connection, req) => {
let tickers = []; let tickers = [];
let sendInterval; let sendInterval;
// Mapping for each ticker's `isSend` status to avoid duplicate sends // Store the last sent data for each ticker
const tickerStatus = {}; const lastSentData = {};
// Function to send data for all tickers as a list // Function to send data for all tickers as a list
const sendData = async () => { const sendData = async () => {
@ -370,31 +371,33 @@ fastify.register(async function (fastify) {
const fileData = fs.readFileSync(filePath, "utf8"); const fileData = fs.readFileSync(filePath, "utf8");
const jsonData = JSON.parse(fileData); const jsonData = JSON.parse(fileData);
// Only send data if conditions are met // Only send data if conditions are met and data has changed
if ( if (
jsonData?.ap != null && jsonData?.ap != null &&
jsonData?.t != null && jsonData?.t != null &&
["Q", "T"].includes(jsonData?.type) && ["Q", "T"].includes(jsonData?.type) &&
connection.socket.readyState === WebSocket.OPEN && connection.socket.readyState === WebSocket.OPEN
!tickerStatus[symbol]
) { ) {
// Collect data to send later // Check if the current data is different from the last sent data
dataToSend.push({ const currentDataSignature = `${jsonData.ap}`;
symbol, // Include the ticker symbol in the sent data const lastSentSignature = lastSentData[symbol];
ap: jsonData?.ap,
});
// Set ticker as "sent" and reset after 500ms if (currentDataSignature !== lastSentSignature) {
tickerStatus[symbol] = true; // Collect data to send
setTimeout(() => { dataToSend.push({
tickerStatus[symbol] = false; symbol, // Include the ticker symbol in the sent data
}, 500); ap: jsonData.ap,
});
// Update the last sent data for this ticker
lastSentData[symbol] = currentDataSignature;
}
} }
} else { } else {
console.error("File not found for ticker:", symbol); console.error("File not found for ticker:", symbol);
} }
} catch (err) { } catch (err) {
console.error("Error sending data for ticker:", symbol, err); console.error("Error processing data for ticker:", symbol, err);
} }
} }
@ -411,9 +414,9 @@ fastify.register(async function (fastify) {
tickers = JSON.parse(message.toString("utf-8")); tickers = JSON.parse(message.toString("utf-8"));
console.log("Received tickers from client:", tickers); console.log("Received tickers from client:", tickers);
// Initialize ticker status for each symbol // Reset last sent data for new tickers
tickers.forEach((ticker) => { tickers?.forEach((ticker) => {
tickerStatus[ticker] = false; lastSentData[ticker] = null;
}); });
// Start periodic data sending if not already started // Start periodic data sending if not already started
@ -461,7 +464,6 @@ fastify.register(async function (fastify) {
// Function to start the server // Function to start the server
function startServer() { function startServer() {
if (!serverRunning) { if (!serverRunning) {