From 9287d34e3aeeeb104c7f9898b39a99d1e37f3583 Mon Sep 17 00:00:00 2001 From: MuslemRahimi Date: Tue, 3 Sep 2024 13:44:52 +0200 Subject: [PATCH] bugfixing stockdeck --- app/create_stock_db.py | 57 ++--------------------------------------- app/cron_stockdeck.py | 2 +- app/primary_cron_job.py | 2 +- app/restart_json.py | 5 ++++ 4 files changed, 9 insertions(+), 57 deletions(-) diff --git a/app/create_stock_db.py b/app/create_stock_db.py index aa38a67..3966566 100755 --- a/app/create_stock_db.py +++ b/app/create_stock_db.py @@ -122,63 +122,9 @@ class StockDatabase: fundamental_data = {} - # Check if 'income' and 'income_growth' data already exist for the symbol - try: - self.cursor.execute("SELECT income, income_growth, balance, balance_growth, cashflow, cashflow_growth, ratios, stock_peers FROM stocks WHERE symbol = ?", (symbol,)) - existing_data = self.cursor.fetchone() - income_exists = existing_data and existing_data[0] is not None - income_growth_exists = existing_data and existing_data[1] is not None - balance_exists = existing_data and existing_data[0] is not None - balance_growth_exists = existing_data and existing_data[1] is not None - cashflow_exists = existing_data and existing_data[0] is not None - cashflow_growth_exists = existing_data and existing_data[1] is not None - ratios_exists = existing_data and existing_data[1] is not None - stock_peers_exists = existing_data and existing_data[0] is not None - esg_data_exists = existing_data and existing_data[0] is not None - esg_ratings_exists = existing_data and existing_data[0] is not None - except: - income_exists = False - income_growth_exists = False - balance_exists = False - balance_growth_exists = False - cashflow_exists = False - cashflow_growth_exists = False - ratios_exists = False - stock_peers_exists = False - esg_data_exists = False - esg_ratings_exists = False for url in urls: - # Skip the API calls if the data already exists - if 'income-statement/' in url and income_exists: - print(f"Skipping income-statement for {symbol} as it already exists.") - continue - elif 'income-statement-growth/' in url and income_growth_exists: - print(f"Skipping income-statement-growth for {symbol} as it already exists.") - continue - elif 'balance-sheet-statement/' in url and balance_exists: - print(f"Skipping balance-statement for {symbol} as it already exists.") - continue - elif 'balance-sheet-statement-growth/' in url and balance_growth_exists: - print(f"Skipping balance-statement-growth for {symbol} as it already exists.") - continue - elif 'cash-flow-statement/' in url and cashflow_exists: - print(f"Skipping cashflow-statement for {symbol} as it already exists.") - continue - elif 'cash-flow-statement-growth/' in url and cashflow_growth_exists: - print(f"Skipping cashflow-statement-growth for {symbol} as it already exists.") - continue - elif '/v3/ratios/' in url and ratios_exists: - print(f"Skipping ratios for {symbol} as it already exists.") - continue - - elif 'stock_peers/' in url and stock_peers_exists: - print(f"Skipping stock_peers for {symbol} as it already exists.") - continue - - - async with session.get(url) as response: data = await response.text() parsed_data = get_jsonparsed_data(data) @@ -307,7 +253,8 @@ class StockDatabase: elif "analyst-estimates" in url: # Handle list response, save as JSON object fundamental_data['analyst_estimates'] = ujson.dumps(parsed_data) - except: + except Exception as e: + print(e) pass diff --git a/app/cron_stockdeck.py b/app/cron_stockdeck.py index ed8564a..e971446 100755 --- a/app/cron_stockdeck.py +++ b/app/cron_stockdeck.py @@ -25,7 +25,7 @@ def clean_financial_data(self, list1, list2): query_template = """ SELECT profile, quote, - esg_ratings,esg_data,stock_split + stock_split FROM stocks WHERE diff --git a/app/primary_cron_job.py b/app/primary_cron_job.py index b805ecd..088f11d 100755 --- a/app/primary_cron_job.py +++ b/app/primary_cron_job.py @@ -604,7 +604,7 @@ schedule.every(15).minutes.do(run_threaded, run_market_moods).tag('market_moods_ #schedule.every(10).minutes.do(run_threaded, run_dark_pool_flow).tag('dark_pool_flow_job') schedule.every(2).hours.do(run_threaded, run_fda_calendar).tag('fda_calendar_job') -schedule.every(3).hours.do(run_threaded, run_json).tag('json_job') +schedule.every(6).hours.do(run_threaded, run_json).tag('json_job') #schedule.every(4).hours.do(run_threaded, run_share_statistics).tag('share_statistics_job') #schedule.every(2).days.at("01:00").do(run_borrowed_share).tag('borrowed_share_job') diff --git a/app/restart_json.py b/app/restart_json.py index 3313b8f..58f5b93 100755 --- a/app/restart_json.py +++ b/app/restart_json.py @@ -136,6 +136,11 @@ def get_financial_statements(item, symbol): item.update(process_financial_data(f"json/financial-statements/income-statement/annual/{symbol}.json", key_income)) item.update(process_financial_data(f"json/financial-statements/balance-sheet-statement/annual/{symbol}.json", key_balance_sheet)) + try: + item['freeCashFlowMargin'] = (item['freeCashFlow'] / item['revenue']) * 100 + except: + item['freeCashFlowMargin'] = None + return item async def get_stock_screener(con):