Skip to content

Commit

Permalink
update cron job
Browse files Browse the repository at this point in the history
  • Loading branch information
MuslemRahimi committed Jun 9, 2024
1 parent fe574eb commit ca4322f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
31 changes: 24 additions & 7 deletions app/cron_share_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
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):
Expand Down Expand Up @@ -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):

Expand All @@ -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():
Expand All @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ca4322f

Please sign in to comment.