From ecca70ffeafabe43001191cc4a3bb7932c3dae4c Mon Sep 17 00:00:00 2001 From: MuslemRahimi Date: Thu, 3 Apr 2025 23:00:13 +0200 Subject: [PATCH] add ytd --- app/cron_historical_price.py | 6 ++++++ app/cron_revenue.py | 4 ++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/app/cron_historical_price.py b/app/cron_historical_price.py index fa56856..b0215f9 100755 --- a/app/cron_historical_price.py +++ b/app/cron_historical_price.py @@ -12,6 +12,8 @@ from dotenv import load_dotenv load_dotenv() api_key = os.getenv('FMP_API_KEY') +current_year = datetime.today().year + # Helper to ensure directories exist and write JSON files asynchronously async def write_json(path, data): os.makedirs(os.path.dirname(path), exist_ok=True) @@ -65,10 +67,14 @@ async def get_historical_data(ticker, query_con, session): df_max = pd.read_sql_query(query, query_con, params=(start_date_max, end_date)) df_max = df_max.round(2).rename(columns={"date": "time"}) + max_list = ujson.loads(df_max.to_json(orient="records")) + ytd_data = [entry for entry in max_list if datetime.strptime(entry["time"], "%Y-%m-%d").year == current_year] + # Prepare file-writing tasks tasks = [ write_json(f"json/historical-price/one-week/{ticker}.json", ujson.loads(data[0])), write_json(f"json/historical-price/one-month/{ticker}.json", ujson.loads(data[1])), + write_json(f"json/historical-price/ytd/{ticker}.json", ytd_data), write_json(f"json/historical-price/six-months/{ticker}.json", ujson.loads(df_6m.to_json(orient="records"))), write_json(f"json/historical-price/one-year/{ticker}.json", ujson.loads(df_1y.to_json(orient="records"))), write_json(f"json/historical-price/five-years/{ticker}.json", ujson.loads(df_5y.to_json(orient="records"))), diff --git a/app/cron_revenue.py b/app/cron_revenue.py index b10f4ad..805d204 100644 --- a/app/cron_revenue.py +++ b/app/cron_revenue.py @@ -69,7 +69,6 @@ async def get_data(symbol): ] stats = await get_statistics(symbol) - print(stats) res_dict = {**stats, 'annual': annual_data, 'quarter': quarter_data} if annual_data and quarter_data: @@ -84,7 +83,8 @@ async def run(): con.close() #Testing mode - #total_symbols = ['AAPL'] + #total_symbols = ['DGICA'] + for symbol in tqdm(total_symbols): try: await get_data(symbol)