From ba8d6ec24b2e77ae59a77150ce7730be89461600 Mon Sep 17 00:00:00 2001 From: MuslemRahimi Date: Thu, 28 Nov 2024 15:56:34 +0100 Subject: [PATCH] add helper function --- app/cron_one_day_price.py | 29 +----------------- app/cron_options_flow.py | 7 ++++- app/utils/__pycache__/helper.cpython-310.pyc | Bin 0 -> 844 bytes app/utils/helper.py | 30 +++++++++++++++++++ fastify/app.js | 10 +++---- 5 files changed, 42 insertions(+), 34 deletions(-) create mode 100644 app/utils/__pycache__/helper.cpython-310.pyc create mode 100644 app/utils/helper.py diff --git a/app/cron_one_day_price.py b/app/cron_one_day_price.py index 3339321..6772353 100755 --- a/app/cron_one_day_price.py +++ b/app/cron_one_day_price.py @@ -3,42 +3,15 @@ import asyncio import aiohttp import sqlite3 from datetime import datetime, timedelta, time -import pytz import pandas as pd from GetStartEndDate import GetStartEndDate from dotenv import load_dotenv import os +from utils.helper import check_market_hours load_dotenv() api_key = os.getenv('FMP_API_KEY') -def check_market_hours(): - - holidays = [ - "2024-01-01", "2024-01-15", "2024-02-19", "2024-03-29", - "2024-05-27", "2024-06-19", "2024-07-04", "2024-09-02", - "2024-11-28", "2024-12-25" - ] - - # Get the current date and time in ET (Eastern Time) - et_timezone = pytz.timezone('America/New_York') - current_time = datetime.now(et_timezone) - current_date_str = current_time.strftime('%Y-%m-%d') - current_hour = current_time.hour - current_minute = current_time.minute - current_day = current_time.weekday() # Monday is 0, Sunday is 6 - - # Check if the current date is a holiday or weekend - is_weekend = current_day >= 5 # Saturday (5) or Sunday (6) - is_holiday = current_date_str in holidays - - # Determine the market status - if is_weekend or is_holiday: - return False #"Market is closed." - elif 9 <= current_hour < 16 or (current_hour == 17 and current_minute == 0): - return True #"Market hours." - else: - return False #"Market is closed." async def save_price_data(symbol, data): diff --git a/app/cron_options_flow.py b/app/cron_options_flow.py index 9dc685f..c73ed4c 100755 --- a/app/cron_options_flow.py +++ b/app/cron_options_flow.py @@ -6,6 +6,7 @@ from datetime import datetime from GetStartEndDate import GetStartEndDate from dotenv import load_dotenv from benzinga import financial_data +from utils.helper import check_market_hours # Load environment variables load_dotenv() @@ -100,4 +101,8 @@ async def main(): # Run the async event loop if __name__ == "__main__": - asyncio.run(main()) + market_open = check_market_hours() + if market_open: + asyncio.run(main()) + else: + print('market closed') diff --git a/app/utils/__pycache__/helper.cpython-310.pyc b/app/utils/__pycache__/helper.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..a3a84a38f0f7a9ee1eeea6769448647ae9d53601 GIT binary patch literal 844 zcmYjPOOMkq5VjpBb<N zE;s&?ublc9I592_TRZdRn}=scW9O%*Hi9+Ye7PM{gnnz~co9&pL5kM^9C4hW6irZy zCpaY&g0+n##>C(TryDe(oZKMC+(9&Q48?d*iZV)tYHQ>oDFa0{9fdliS#2N&$S06u z18@tae&QOHXp1?y$2Bg=)~GNrQjsm?1~8){cVmJosWpNstb*nNe^aItb zY86h!P*^Gry6h04hCrD8=Xzwd8@ z&hmv+RvKlCQmEEah?oZ}r>)p5Pa>d&>WFgMD!$`(g*)O@1w?l z$lqynNUZFrnCf~W<9mK6_zTHr(CD8tZN8$2pL>fk zN(yf#lDUv?=PUIjoQW`=rh$y1Yu!Tuj~Nv&0T{7cHny<^n_*^Xq4*W1m=gH{0vvNc mBB|e>)W@VA?B2C+=?I>KL$MEO&KHTe(gQCnfTxJz6Y>{rSk@W< literal 0 HcmV?d00001 diff --git a/app/utils/helper.py b/app/utils/helper.py new file mode 100644 index 0000000..aeb823a --- /dev/null +++ b/app/utils/helper.py @@ -0,0 +1,30 @@ +from datetime import datetime, timedelta, time +import pytz + +def check_market_hours(): + + holidays = [ + "2024-01-01", "2024-01-15", "2024-02-19", "2024-03-29", + "2024-05-27", "2024-06-19", "2024-07-04", "2024-09-02", + "2024-11-28", "2024-12-25" + ] + + # Get the current date and time in ET (Eastern Time) + et_timezone = pytz.timezone('America/New_York') + current_time = datetime.now(et_timezone) + current_date_str = current_time.strftime('%Y-%m-%d') + current_hour = current_time.hour + current_minute = current_time.minute + current_day = current_time.weekday() # Monday is 0, Sunday is 6 + + # Check if the current date is a holiday or weekend + is_weekend = current_day >= 5 # Saturday (5) or Sunday (6) + is_holiday = current_date_str in holidays + + # Determine the market status + if is_weekend or is_holiday: + return False #"Market is closed." + elif 9 <= current_hour < 16 or (current_hour == 17 and current_minute == 0): + return True #"Market hours." + else: + return False #"Market is closed." diff --git a/fastify/app.js b/fastify/app.js index 8a29dcf..3200457 100755 --- a/fastify/app.js +++ b/fastify/app.js @@ -361,14 +361,14 @@ fastify.register(async function (fastify) { // Iterate over tickers and collect data for (const symbol of tickers) { - const filePath = path.join( + const filePath = path?.join( __dirname, `../app/json/websocket/companies/${symbol}.json` ); try { - if (fs.existsSync(filePath)) { - const fileData = fs.readFileSync(filePath, "utf8"); + if (fs?.existsSync(filePath)) { + const fileData = fs?.readFileSync(filePath, "utf8"); const jsonData = JSON?.parse(fileData); // Only send data if conditions are met and data has changed if ( @@ -386,7 +386,7 @@ fastify.register(async function (fastify) { if (currentDataSignature !== lastSentSignature) { // Collect data to send - dataToSend.push({ + dataToSend?.push({ symbol, // Include the ticker symbol in the sent data ap: jsonData?.ap, }); @@ -396,7 +396,7 @@ fastify.register(async function (fastify) { } } } else { - console.error("File not found for ticker:", symbol); + //console.error("File not found for ticker:", symbol); } } catch (err) { console.error("Error processing data for ticker:", symbol, err);