From 9a4865d228a238715434113b69a32a50db0fa3d6 Mon Sep 17 00:00:00 2001 From: MuslemRahimi Date: Sat, 15 Jun 2024 21:47:03 +0200 Subject: [PATCH] add endpoint most-retail-volume --- app/cron_retail_volume.py | 26 +++++++++++++++++++++++--- app/main.py | 16 ++++++++++++++++ 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/app/cron_retail_volume.py b/app/cron_retail_volume.py index fb09f5c..00b093e 100644 --- a/app/cron_retail_volume.py +++ b/app/cron_retail_volume.py @@ -4,6 +4,8 @@ import aiohttp import sqlite3 from datetime import datetime,timedelta from tqdm import tqdm +import pandas as pd + from dotenv import load_dotenv import os load_dotenv() @@ -14,6 +16,14 @@ api_key = os.getenv('NASDAQ_API_KEY') today = datetime.now() # Calculate the date six months ago six_months_ago = today - timedelta(days=6*30) # Rough estimate, can be refined +query_template = """ + SELECT + name, marketCap, netIncome + FROM + stocks + WHERE + symbol = ? +""" async def save_json(symbol, data): @@ -59,8 +69,6 @@ async def run(): etf_cursor.execute("SELECT DISTINCT symbol FROM etfs") etf_symbols = [row[0] for row in etf_cursor.fetchall()] - con.close() - etf_con.close() total_symbols = stocks_symbols+etf_symbols @@ -86,8 +94,17 @@ async def run(): try: filtered_data = [item for item in transformed_data if symbol == item['symbol']] res = filter_past_six_months(filtered_data) - most_retail_volume.append({'assetType': 'stocks' if res[-1]['symbol'] in stocks_symbols else 'etf','symbol': res[-1]['symbol'], 'traded': res[-1]['traded'], 'sentiment': res[-1]['sentiment']}) await save_json(symbol, res) + + #Add stocks for most retail volume + if symbol in stocks_symbols: + data = pd.read_sql_query(query_template, con, params=(symbol,)) + name = data['name'].iloc[0] + net_income = int(data['netIncome'].iloc[0]) + market_cap = int(data['marketCap'].iloc[0]) + + most_retail_volume.append({'symbol': res[-1]['symbol'], 'name': name, 'traded': res[-1]['traded'], 'sentiment': res[-1]['sentiment'], 'marketCap': market_cap, 'netIncome': net_income}) + except: pass @@ -96,6 +113,9 @@ async def run(): with open(f"json/retail-volume/data.json", 'w') as file: ujson.dump(most_retail_volume, file) + con.close() + etf_con.close() + try: asyncio.run(run()) except Exception as e: diff --git a/app/main.py b/app/main.py index 10e70c2..06e2dfa 100755 --- a/app/main.py +++ b/app/main.py @@ -2824,6 +2824,22 @@ async def get_most_shorted_stocks(): redis_client.expire(cache_key, 3600*3600) # Set cache expiration time to 1 day return res +@app.get("/most-retail-volume") +async def get_most_retail_volume(): + cache_key = f"most-retail-volume" + cached_result = redis_client.get(cache_key) + if cached_result: + return ujson.loads(cached_result) + try: + with open(f"json/retail-volume/data.json", 'r') as file: + res = ujson.load(file) + except: + res = [] + + redis_client.set(cache_key, ujson.dumps(res)) + redis_client.expire(cache_key, 3600*3600) # Set cache expiration time to 1 day + return res + @app.post("/retail-volume") async def get_retail_volume(data:TickerData):