update market movers

This commit is contained in:
MuslemRahimi 2024-11-06 14:31:33 +01:00
parent 59ab55c8cf
commit 15da925dc4
2 changed files with 183 additions and 167 deletions

View File

@ -82,6 +82,11 @@ async def get_quote_of_stocks(ticker_list):
df = await response.json()
return df
def add_rank(data):
for key in data:
for index, item in enumerate(data[key], start=1):
item['rank'] = index
return data
async def get_gainer_loser_active_stocks():
@ -218,7 +223,9 @@ async def get_gainer_loser_active_stocks():
# Iterate through time periods, categories, and symbols
for time_period in data.keys():
for category in data[time_period].keys():
for stock_data in data[time_period][category]:
# Add rank and process symbols
for index, stock_data in enumerate(data[time_period][category], start=1):
stock_data['rank'] = index # Add rank field
symbol = stock_data["symbol"]
unique_symbols.add(symbol)
@ -230,16 +237,17 @@ async def get_gainer_loser_active_stocks():
latest_quote = await get_quote_of_stocks(unique_symbols_list)
# Updating values in the data list based on matching symbols from the quote list
for time_period in data.keys():
for category in data[time_period].keys():
for stock_data in data[time_period][category]:
symbol = stock_data["symbol"]
quote_stock = next((item for item in latest_quote if item["symbol"] == symbol), None)
if quote_stock:
stock_data['price'] = quote_stock['price']
stock_data['changesPercentage'] = quote_stock['changesPercentage']
stock_data['marketCap'] = quote_stock['marketCap']
stock_data['volume'] = quote_stock['volume']
# Only proceed if the time period is "1D"
if time_period == "1D":
for category in data[time_period].keys():
for stock_data in data[time_period][category]:
symbol = stock_data["symbol"]
quote_stock = next((item for item in latest_quote if item["symbol"] == symbol), None)
if quote_stock:
stock_data['price'] = quote_stock['price']
stock_data['changesPercentage'] = quote_stock['changesPercentage']
stock_data['marketCap'] = quote_stock['marketCap']
stock_data['volume'] = quote_stock['volume']
return data
@ -310,17 +318,20 @@ try:
#Filter out tickers
symbols = [symbol for symbol in symbols if symbol != "STEC"]
data = asyncio.run(get_historical_data())
with open(f"json/mini-plots-index/data.json", 'w') as file:
ujson.dump(data, file)
data = asyncio.run(get_gainer_loser_active_stocks())
with open(f"json/market-movers/data.json", 'w') as file:
ujson.dump(data, file)
'''
data = asyncio.run(get_historical_data())
with open(f"json/mini-plots-index/data.json", 'w') as file:
ujson.dump(data, file)
data = asyncio.run(get_pre_post_market_movers(symbols))
with open(f"json/market-movers/pre-post-data.json", 'w') as file:
ujson.dump(data, file)
'''
con.close()
except Exception as e:

29
app/market_movers.py Executable file → Normal file
View File

@ -48,7 +48,7 @@ class Past_Market_Movers:
start_date = current_date
return start_date.strftime("%Y-%m-%d")
def run(self, time_periods=[7,30,90,180]):
def run(self, time_periods=[7,20,252,756,1260]):
performance_data = []
query_template = """
SELECT date, close, volume FROM "{ticker}" WHERE date >= ?
@ -75,9 +75,10 @@ class Past_Market_Movers:
if not df.empty:
fundamental_data = pd.read_sql_query(query_fundamental_template, self.con, params=(ticker,))
avg_volume = df['volume'].mean()
if avg_volume > 1E6 and df['close'].mean() > 1:
market_cap = int(fundamental_data['marketCap'].iloc[0])
if avg_volume > 1E6 and df['close'].mean() > 1 and market_cap >=50E6:
changes_percentage = ((df['close'].iloc[-1] - df['close'].iloc[0]) / df['close'].iloc[0]) * 100
performance_data.append((ticker, fundamental_data['name'].iloc[0], df['close'].iloc[-1], changes_percentage, avg_volume, int(fundamental_data['marketCap'].iloc[0])))
performance_data.append((ticker, fundamental_data['name'].iloc[0], df['close'].iloc[-1], changes_percentage, avg_volume, market_cap))
except:
pass
@ -97,18 +98,22 @@ class Past_Market_Movers:
gainer_json['1W'] = gainer_data
loser_json['1W'] = loser_data
active_json['1W'] = active_data
elif time_period == 30:
elif time_period == 20:
gainer_json['1M'] = gainer_data
loser_json['1M'] = loser_data
active_json['1M'] = active_data
elif time_period == 90:
gainer_json['3M'] = gainer_data
loser_json['3M'] = loser_data
active_json['3M'] = active_data
elif time_period == 180:
gainer_json['6M'] = gainer_data
loser_json['6M'] = loser_data
active_json['6M'] = active_data
elif time_period == 252:
gainer_json['1Y'] = gainer_data
loser_json['1Y'] = loser_data
active_json['1Y'] = active_data
elif time_period == 756:
gainer_json['3Y'] = gainer_data
loser_json['3Y'] = loser_data
active_json['3Y'] = active_data
elif time_period == 1260:
gainer_json['5Y'] = gainer_data
loser_json['5Y'] = loser_data
active_json['5Y'] = active_data
return gainer_json, loser_json, active_json