From ca4322feba2e988c3355211632112aafafba638a Mon Sep 17 00:00:00 2001 From: MuslemRahimi Date: Sun, 9 Jun 2024 21:31:54 +0200 Subject: [PATCH] update cron job --- app/cron_share_statistics.py | 31 ++++++++++++++++++++++++------- app/main.py | 2 +- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/app/cron_share_statistics.py b/app/cron_share_statistics.py index 233cea2..98e2ca4 100644 --- a/app/cron_share_statistics.py +++ b/app/cron_share_statistics.py @@ -4,7 +4,8 @@ import asyncio import pandas as pd from tqdm import tqdm from datetime import datetime - +import yfinance as yf +import time async def save_as_json(symbol, data): @@ -33,6 +34,15 @@ def filter_data_quarterly(data): return filtered_data +def get_short_data(ticker, outstanding_shares, float_shares): + try: + data_dict = yf.Ticker(ticker).info + short_outstanding_percent = round((data_dict['sharesShort']/outstanding_shares)*100,2) + short_float_percent = round((data_dict['sharesShort']/float_shares)*100,2) + return {'sharesShort': data_dict['sharesShort'], 'shortRatio': data_dict['shortRatio'], 'sharesShortPriorMonth': data_dict['sharesShortPriorMonth'], 'shortOutStandingPercent': short_outstanding_percent, 'shortFloatPercent': short_float_percent} + except: + return {'sharesShort': '-', 'shortRatio': '-', 'sharesShortPriorMonth': '-', 'shortOutStandingPercent': '-', 'shortFloatPercent': '-'} + async def get_data(ticker, con): @@ -50,13 +60,20 @@ async def get_data(ticker, con): ] shareholder_statistics = sorted(shareholder_statistics, key=lambda x: datetime.strptime(x['date'], '%Y-%m-%d'), reverse=False) + + latest_outstanding_shares = shareholder_statistics[-1]['outstandingShares'] + latest_float_shares = shareholder_statistics[-1]['floatShares'] + # Filter out only quarter-end dates - shareholder_statistics = filter_data_quarterly(shareholder_statistics) + historical_shares = filter_data_quarterly(shareholder_statistics) + + short_data = get_short_data(ticker, latest_outstanding_shares, latest_float_shares) + res = {**short_data, 'latestOutstandingShares': latest_outstanding_shares, 'latestFloatShares': latest_float_shares,'historicalShares': historical_shares} except Exception as e: #print(e) - shareholder_statistics = [] + res = {} - return shareholder_statistics + return res async def run(): @@ -69,9 +86,9 @@ async def run(): stock_symbols = [row[0] for row in cursor.fetchall()] for ticker in tqdm(stock_symbols): - shareholder_statistics = await get_data(ticker, con) - if len(shareholder_statistics) > 0: - await save_as_json(ticker, shareholder_statistics) + data_dict = await get_data(ticker, con) + if data_dict.keys(): + await save_as_json(ticker, data_dict) con.close() diff --git a/app/main.py b/app/main.py index e2bc540..d1fafaf 100755 --- a/app/main.py +++ b/app/main.py @@ -2731,7 +2731,7 @@ async def get_enterprise_values(data:TickerData): with open(f"json/share-statistics/{ticker}.json", 'r') as file: res = ujson.load(file) except: - res = [] + res = {} redis_client.set(cache_key, ujson.dumps(res)) redis_client.expire(cache_key, 3600*3600) # Set cache expiration time to 1 day