Skip to content

Commit

Permalink
change retail volume cron job
Browse files Browse the repository at this point in the history
  • Loading branch information
MuslemRahimi committed Jun 18, 2024
1 parent d016fc8 commit b7fadd7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
1 change: 1 addition & 0 deletions app/create_stock_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ async def save_fundamental_data(self, session, symbol):
'price': parsed_data[0]['price'],
'changesPercentage': round(parsed_data[0]['changesPercentage'],2),
'marketCap': parsed_data[0]['marketCap'],
'volume': parsed_data[0]['volume'],
'avgVolume': parsed_data[0]['avgVolume'],
'eps': parsed_data[0]['eps'],
'pe': parsed_data[0]['pe'],
Expand Down
18 changes: 16 additions & 2 deletions app/cron_retail_volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from datetime import datetime,timedelta
from tqdm import tqdm
import pandas as pd
import time

from dotenv import load_dotenv
import os
Expand All @@ -18,7 +19,7 @@
six_months_ago = today - timedelta(days=6*30) # Rough estimate, can be refined
query_template = """
SELECT
name, marketCap, netIncome
name, marketCap, netIncome, price, avgVolume
FROM
stocks
WHERE
Expand Down Expand Up @@ -94,7 +95,20 @@ async def run():
try:
filtered_data = [item for item in transformed_data if symbol == item['symbol']]
res = filter_past_six_months(filtered_data)
await save_json(symbol, res)

#Compute strength of retail investors
last_trade = res[-1]['traded']
last_sentiment = int(res[-1]['sentiment'])
last_date = res[-1]['date']
data = pd.read_sql_query(query_template, con, params=(symbol,))
price = float(data['price'].iloc[0])
retail_volume = int(last_trade/price)
total_volume = int(data['avgVolume'].iloc[0])
retailer_strength = round(((retail_volume/total_volume))*100,2)

company_data = {'lastDate': last_date, 'lastTrade': last_trade, 'lastSentiment': last_sentiment, 'retailStrength': retailer_strength, 'history': res}

await save_json(symbol, company_data)

#Add stocks for most retail volume
if symbol in stocks_symbols:
Expand Down
2 changes: 1 addition & 1 deletion app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2852,7 +2852,7 @@ async def get_retail_volume(data:TickerData):
with open(f"json/retail-volume/companies/{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 b7fadd7

Please sign in to comment.