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

@ -356,83 +356,111 @@ async def run():
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.
upcoming_earnings = [
item for item in upcoming_earnings
if item['symbol'] not in [earning['symbol'] for earning in recent_earnings]
]
if len(upcoming_earnings) < 5: if len(upcoming_earnings) < 5:
upcoming_earnings = await get_upcoming_earnings(session, today, filter_today=True) upcoming_earnings = await get_upcoming_earnings(session, today, filter_today=True)
# If still less than 5 results, try fetching for tomorrow.
if len(upcoming_earnings) < 5: if len(upcoming_earnings) < 5:
upcoming_earnings = await get_upcoming_earnings(session, tomorrow, filter_today=True) upcoming_earnings = await get_upcoming_earnings(session, tomorrow, filter_today=True)
#recent_dividends = await get_recent_dividends(session)
recent_analyst_report = await get_analyst_report() recent_analyst_report = await get_analyst_report()
#Avoid clashing of recent and upcoming earnings upcoming_earnings = [
upcoming_earnings = [item for item in upcoming_earnings if item['symbol'] not in [earning['symbol'] for earning in recent_earnings]] item for item in upcoming_earnings
if item['symbol'] not in [earning['symbol'] for earning in recent_earnings]
]
try: try:
with open(f"json/retail-volume/data.json", 'r') as file: with open("json/options-flow/feed/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) options_flow = ujson.load(file)
# Filter the options_flow to include only items with ticker in total_symbol # 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] 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 = 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_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) highest_premium = sorted(options_flow, key=lambda x: int(x['cost_basis']), 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_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 = sorted(options_flow, key=lambda x: int(x['open_interest']), reverse=True) 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]] 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} options_flow = {
'premium': highest_premium,
'volume': highest_volume,
'openInterest': highest_open_interest
}
except Exception as e: except Exception as e:
print(e) print(e)
options_flow = {} options_flow = {}
market_status = check_market_hours() market_status = check_market_hours()
if market_status == 0: if market_status == 0:
try: try:
with open(f"json/market-movers/markethours/gainers.json", 'r') as file: with open("json/market-movers/markethours/gainers.json", 'r') as file:
gainers = ujson.load(file) gainers = ujson.load(file)
with open(f"json/market-movers/markethours/losers.json", 'r') as file: with open("json/market-movers/markethours/losers.json", 'r') as file:
losers = ujson.load(file) losers = ujson.load(file)
market_movers = {'gainers': gainers['1D'][:5], 'losers': losers['1D'][:5]} market_movers = {'gainers': gainers['1D'][:5], 'losers': losers['1D'][:5]}
except: except:
market_movers = {} market_movers = {}
elif market_status == 1: elif market_status == 1:
try: try:
with open(f"json/market-movers/premarket/gainers.json", 'r') as file: with open("json/market-movers/premarket/gainers.json", 'r') as file:
data = ujson.load(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]] 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/premarket/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: elif market_status == 2:
try: try:
with open(f"json/market-movers/afterhours/gainers.json", 'r') as file: with open("json/market-movers/afterhours/gainers.json", 'r') as file:
data = ujson.load(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]] 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: 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 = {}
@ -445,7 +473,6 @@ async def run():
'analystReport': recent_analyst_report, 'analystReport': recent_analyst_report,
} }
if len(data) > 0: if len(data) > 0:
await save_json(data) await save_json(data)

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
const currentDataSignature = `${jsonData.ap}`;
const lastSentSignature = lastSentData[symbol];
if (currentDataSignature !== lastSentSignature) {
// Collect data to send
dataToSend.push({ dataToSend.push({
symbol, // Include the ticker symbol in the sent data symbol, // Include the ticker symbol in the sent data
ap: jsonData?.ap, ap: jsonData.ap,
}); });
// Set ticker as "sent" and reset after 500ms // Update the last sent data for this ticker
tickerStatus[symbol] = true; lastSentData[symbol] = currentDataSignature;
setTimeout(() => { }
tickerStatus[symbol] = false;
}, 500);
} }
} 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) {