add helper function

This commit is contained in:
MuslemRahimi 2024-11-28 15:56:34 +01:00
parent dbf367783e
commit ba8d6ec24b
5 changed files with 42 additions and 34 deletions

View File

@ -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):

View File

@ -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')

Binary file not shown.

30
app/utils/helper.py Normal file
View File

@ -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."

View File

@ -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);