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 0000000..a3a84a3 Binary files /dev/null and b/app/utils/__pycache__/helper.cpython-310.pyc differ 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);