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:
#recent_dividends = await get_recent_dividends(session) upcoming_earnings = await get_upcoming_earnings(session, today, filter_today=True)
recent_analyst_report = await get_analyst_report()
#Avoid clashing of recent and upcoming earnings if len(upcoming_earnings) < 5:
upcoming_earnings = [item for item in upcoming_earnings if item['symbol'] not in [earning['symbol'] for earning in recent_earnings]] upcoming_earnings = await get_upcoming_earnings(session, tomorrow, filter_today=True)
try: recent_analyst_report = await get_analyst_report()
with open(f"json/retail-volume/data.json", 'r') as file:
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
options_flow = [item for item in options_flow if item['ticker'] in stock_symbols]
highest_volume = sorted(options_flow, key=lambda x: int(x['volume']), 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 = sorted(options_flow, key=lambda x: int(x['cost_basis']), reverse=True) upcoming_earnings = [
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]] item for item in upcoming_earnings
if item['symbol'] not in [earning['symbol'] for earning in recent_earnings]
highest_open_interest = sorted(options_flow, key=lambda x: int(x['open_interest']), reverse=True) ]
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]]
options_flow = {'premium': highest_premium, 'volume': highest_volume, 'openInterest':highest_open_interest}
except Exception as e:
print(e)
options_flow = {}
market_status = check_market_hours()
if market_status == 0: try:
try: with open("json/options-flow/feed/data.json", 'r') as file:
with open(f"json/market-movers/markethours/gainers.json", 'r') as file: options_flow = ujson.load(file)
gainers = ujson.load(file)
with open(f"json/market-movers/markethours/losers.json", 'r') as file: # Filter the options_flow to include only items with ticker in total_symbol
losers = ujson.load(file) options_flow = [item for item in options_flow if item['ticker'] in stock_symbols]
market_movers = {'gainers': gainers['1D'][:5], 'losers': losers['1D'][:5]}
except: highest_volume = sorted(options_flow, key=lambda x: int(x['volume']), reverse=True)
market_movers = {} highest_volume = [
elif market_status == 1: {key: item[key] for key in ['cost_basis', 'ticker', 'underlying_type', 'date_expiration', 'put_call', 'volume', 'strike_price']}
try: for item in highest_volume[0:4]
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: highest_premium = sorted(options_flow, key=lambda x: int(x['cost_basis']), reverse=True)
data = ujson.load(file) highest_premium = [
losers = [{ 'symbol': item['symbol'], 'name': item['name'], 'price': item['price'], 'changesPercentage': item['changesPercentage'], 'marketCap': item['marketCap']} for item in data[:5]] {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]
market_movers={'gainers': gainers, 'losers': losers} ]
except:
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: highest_open_interest = sorted(options_flow, key=lambda x: int(x['open_interest']), reverse=True)
data = ujson.load(file) highest_open_interest = [
losers = [{ 'symbol': item['symbol'], 'name': item['name'], 'price': item['price'], 'changesPercentage': item['changesPercentage'], 'marketCap': item['marketCap']} for item in data[:5]] {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]
market_movers={'gainers': gainers, 'losers': losers} ]
except: options_flow = {
market_movers = {} 'premium': highest_premium,
'volume': highest_volume,
'openInterest': highest_open_interest
}
except Exception as e:
print(e)
options_flow = {}
data = { market_status = check_market_hours()
'marketMovers': market_movers, if market_status == 0:
'marketStatus': market_status, try:
'optionsFlow': options_flow, with open("json/market-movers/markethours/gainers.json", 'r') as file:
'recentEarnings': recent_earnings, gainers = ujson.load(file)
'upcomingEarnings': upcoming_earnings, with open("json/market-movers/markethours/losers.json", 'r') as file:
'analystReport': recent_analyst_report, 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:
if len(data) > 0: data = ujson.load(file)
await save_json(data) 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}
except:
market_movers = {}
elif market_status == 2:
try:
with open("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("json/market-movers/afterhours/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_movers = {'gainers': gainers, 'losers': losers}
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,71 +352,73 @@ 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 () => {
const dataToSend = []; const dataToSend = [];
// Iterate over tickers and collect data // Iterate over tickers and collect data
for (const symbol of tickers) { for (const symbol of tickers) {
const filePath = path.join( const filePath = path.join(
__dirname, __dirname,
`../app/json/websocket/companies/${symbol}.json` `../app/json/websocket/companies/${symbol}.json`
); );
try { try {
if (fs.existsSync(filePath)) { if (fs.existsSync(filePath)) {
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,
}); if (currentDataSignature !== lastSentSignature) {
// Collect data to send
// Set ticker as "sent" and reset after 500ms dataToSend.push({
tickerStatus[symbol] = true; symbol, // Include the ticker symbol in the sent data
setTimeout(() => { ap: jsonData.ap,
tickerStatus[symbol] = false; });
}, 500);
// 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);
} }
} }
// Send all collected data as a single message // Send all collected data as a single message
if (dataToSend.length > 0 && connection.socket.readyState === WebSocket.OPEN) { if (dataToSend.length > 0 && connection.socket.readyState === WebSocket.OPEN) {
connection.socket.send(JSON.stringify(dataToSend)); connection.socket.send(JSON.stringify(dataToSend));
} }
}; };
// Start receiving messages from the client // Start receiving messages from the client
connection.socket.on("message", (message) => { connection.socket.on("message", (message) => {
try { try {
// Parse message as JSON to get tickers array // Parse message as JSON to get tickers array
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
if (!sendInterval) { if (!sendInterval) {
sendInterval = setInterval(sendData, 5000); sendInterval = setInterval(sendData, 5000);
@ -424,14 +427,14 @@ fastify.register(async function (fastify) {
console.error("Failed to parse tickers from client message:", err); console.error("Failed to parse tickers from client message:", err);
} }
}); });
// Handle client disconnect // Handle client disconnect
connection.socket.on("close", () => { connection.socket.on("close", () => {
console.log("Client disconnected"); console.log("Client disconnected");
clearInterval(sendInterval); clearInterval(sendInterval);
removeProcessListeners(); removeProcessListeners();
}); });
// Handle server crash cleanup // Handle server crash cleanup
const closeHandler = () => { const closeHandler = () => {
console.log("Server is closing. Cleaning up resources..."); console.log("Server is closing. Cleaning up resources...");
@ -439,14 +442,14 @@ fastify.register(async function (fastify) {
connection.socket.close(); connection.socket.close();
removeProcessListeners(); removeProcessListeners();
}; };
// Add close handler to process events // Add close handler to process events
process.on("exit", closeHandler); process.on("exit", closeHandler);
process.on("SIGINT", closeHandler); process.on("SIGINT", closeHandler);
process.on("SIGTERM", closeHandler); process.on("SIGTERM", closeHandler);
process.on("uncaughtException", closeHandler); process.on("uncaughtException", closeHandler);
process.on("unhandledRejection", closeHandler); process.on("unhandledRejection", closeHandler);
// Function to remove process listeners to avoid memory leaks // Function to remove process listeners to avoid memory leaks
const removeProcessListeners = () => { const removeProcessListeners = () => {
process.off("exit", closeHandler); process.off("exit", closeHandler);
@ -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) {