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,7 +82,12 @@ async def get_quote_of_stocks(ticker_list):
df = await response.json() df = await response.json()
return df 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(): async def get_gainer_loser_active_stocks():
#Database read 1y and 3y data #Database read 1y and 3y data
@ -218,7 +223,9 @@ async def get_gainer_loser_active_stocks():
# Iterate through time periods, categories, and symbols # Iterate through time periods, categories, and symbols
for time_period in data.keys(): for time_period in data.keys():
for category in data[time_period].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"] symbol = stock_data["symbol"]
unique_symbols.add(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) latest_quote = await get_quote_of_stocks(unique_symbols_list)
# Updating values in the data list based on matching symbols from the quote list # Updating values in the data list based on matching symbols from the quote list
for time_period in data.keys(): for time_period in data.keys():
for category in data[time_period].keys(): # Only proceed if the time period is "1D"
for stock_data in data[time_period][category]: if time_period == "1D":
symbol = stock_data["symbol"] for category in data[time_period].keys():
quote_stock = next((item for item in latest_quote if item["symbol"] == symbol), None) for stock_data in data[time_period][category]:
if quote_stock: symbol = stock_data["symbol"]
stock_data['price'] = quote_stock['price'] quote_stock = next((item for item in latest_quote if item["symbol"] == symbol), None)
stock_data['changesPercentage'] = quote_stock['changesPercentage'] if quote_stock:
stock_data['marketCap'] = quote_stock['marketCap'] stock_data['price'] = quote_stock['price']
stock_data['volume'] = quote_stock['volume'] stock_data['changesPercentage'] = quote_stock['changesPercentage']
stock_data['marketCap'] = quote_stock['marketCap']
stock_data['volume'] = quote_stock['volume']
return data return data
@ -310,17 +318,20 @@ try:
#Filter out tickers #Filter out tickers
symbols = [symbol for symbol in symbols if symbol != "STEC"] 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()) data = asyncio.run(get_gainer_loser_active_stocks())
with open(f"json/market-movers/data.json", 'w') as file: with open(f"json/market-movers/data.json", 'w') as file:
ujson.dump(data, 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)) data = asyncio.run(get_pre_post_market_movers(symbols))
with open(f"json/market-movers/pre-post-data.json", 'w') as file: with open(f"json/market-movers/pre-post-data.json", 'w') as file:
ujson.dump(data, file) ujson.dump(data, file)
'''
con.close() con.close()
except Exception as e: except Exception as e:

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

@ -1,152 +1,157 @@
import sqlite3 import sqlite3
from datetime import datetime, timedelta from datetime import datetime, timedelta
import pandas as pd import pandas as pd
import json import json
import time import time
class Past_Market_Movers: class Past_Market_Movers:
def __init__(self): def __init__(self):
self.con = sqlite3.connect('backup_db/stocks.db') self.con = sqlite3.connect('backup_db/stocks.db')
self.cursor = self.con.cursor() self.cursor = self.con.cursor()
self.cursor.execute("PRAGMA journal_mode = wal") self.cursor.execute("PRAGMA journal_mode = wal")
self.symbols = self.get_stock_symbols() self.symbols = self.get_stock_symbols()
def get_stock_symbols(self): def get_stock_symbols(self):
self.cursor.execute("SELECT DISTINCT symbol FROM stocks WHERE symbol != ?", ('%5EGSPC',)) self.cursor.execute("SELECT DISTINCT symbol FROM stocks WHERE symbol != ?", ('%5EGSPC',))
return [row[0] for row in self.cursor.fetchall()] return [row[0] for row in self.cursor.fetchall()]
@staticmethod @staticmethod
def check_if_holiday(): def check_if_holiday():
holidays = { holidays = {
datetime(2023, 5, 29): 'memorial_day', datetime(2023, 5, 29): 'memorial_day',
datetime(2023, 6, 19): 'independence_day', datetime(2023, 6, 19): 'independence_day',
datetime(2023, 6, 20): 'independence_day+1', datetime(2023, 6, 20): 'independence_day+1',
datetime(2023, 9, 4): 'labor_day', datetime(2023, 9, 4): 'labor_day',
} }
current_datetime = datetime.today() current_datetime = datetime.today()
for holiday_date, holiday_name in holidays.items(): for holiday_date, holiday_name in holidays.items():
if current_datetime == holiday_date: if current_datetime == holiday_date:
return holiday_name return holiday_name
return None return None
def correct_weekday_interval(self, prev_day): def correct_weekday_interval(self, prev_day):
holiday = self.check_if_holiday() holiday = self.check_if_holiday()
if holiday: if holiday:
if holiday == 'memorial_day': if holiday == 'memorial_day':
start_date = datetime(2023, 5, 26) start_date = datetime(2023, 5, 26)
elif holiday in ('independence_day', 'independence_day+1'): elif holiday in ('independence_day', 'independence_day+1'):
start_date = datetime(2023, 6, 16) start_date = datetime(2023, 6, 16)
else: else:
start_date = datetime(2023, 9, 1) start_date = datetime(2023, 9, 1)
else: else:
current_date = datetime.today() - timedelta(prev_day) current_date = datetime.today() - timedelta(prev_day)
current_weekday = current_date.weekday() current_weekday = current_date.weekday()
if current_weekday in (5, 6): # Saturday or Sunday if current_weekday in (5, 6): # Saturday or Sunday
start_date = current_date - timedelta(days=current_weekday % 5 + 1) start_date = current_date - timedelta(days=current_weekday % 5 + 1)
else: else:
start_date = current_date start_date = current_date
return start_date.strftime("%Y-%m-%d") 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 = [] performance_data = []
query_template = """ query_template = """
SELECT date, close, volume FROM "{ticker}" WHERE date >= ? SELECT date, close, volume FROM "{ticker}" WHERE date >= ?
""" """
query_fundamental_template = """ query_fundamental_template = """
SELECT marketCap, name FROM stocks WHERE symbol = ? SELECT marketCap, name FROM stocks WHERE symbol = ?
""" """
gainer_json = {} gainer_json = {}
loser_json = {} loser_json = {}
active_json = {} active_json = {}
for time_period in time_periods: for time_period in time_periods:
performance_data = [] performance_data = []
high_volume = [] high_volume = []
gainer_data = [] gainer_data = []
loser_data = [] loser_data = []
active_data = [] active_data = []
start_date = self.correct_weekday_interval(time_period) start_date = self.correct_weekday_interval(time_period)
for ticker in self.symbols: for ticker in self.symbols:
try: try:
query = query_template.format(ticker=ticker) query = query_template.format(ticker=ticker)
df = pd.read_sql_query(query, self.con, params=(start_date,)) df = pd.read_sql_query(query, self.con, params=(start_date,))
if not df.empty: if not df.empty:
fundamental_data = pd.read_sql_query(query_fundamental_template, self.con, params=(ticker,)) fundamental_data = pd.read_sql_query(query_fundamental_template, self.con, params=(ticker,))
avg_volume = df['volume'].mean() avg_volume = df['volume'].mean()
if avg_volume > 1E6 and df['close'].mean() > 1: market_cap = int(fundamental_data['marketCap'].iloc[0])
changes_percentage = ((df['close'].iloc[-1] - df['close'].iloc[0]) / df['close'].iloc[0]) * 100 if avg_volume > 1E6 and df['close'].mean() > 1 and market_cap >=50E6:
performance_data.append((ticker, fundamental_data['name'].iloc[0], df['close'].iloc[-1], changes_percentage, avg_volume, int(fundamental_data['marketCap'].iloc[0]))) changes_percentage = ((df['close'].iloc[-1] - df['close'].iloc[0]) / df['close'].iloc[0]) * 100
except: performance_data.append((ticker, fundamental_data['name'].iloc[0], df['close'].iloc[-1], changes_percentage, avg_volume, market_cap))
pass except:
pass
# Sort the stocks by percentage change in descending order
performance_data.sort(key=lambda x: x[3], reverse=True) # Sort the stocks by percentage change in descending order
high_volume = sorted(performance_data, key=lambda x: x[4], reverse=True) performance_data.sort(key=lambda x: x[3], reverse=True)
high_volume = sorted(performance_data, key=lambda x: x[4], reverse=True)
for symbol, name, price, changes_percentage, volume, market_cap in performance_data[:20]:
gainer_data.append({'symbol': symbol, 'name': name, 'price': price, 'changesPercentage': changes_percentage, 'volume': volume, 'marketCap': market_cap}) for symbol, name, price, changes_percentage, volume, market_cap in performance_data[:20]:
for symbol, name, price, changes_percentage, volume, market_cap in performance_data[-20:]: gainer_data.append({'symbol': symbol, 'name': name, 'price': price, 'changesPercentage': changes_percentage, 'volume': volume, 'marketCap': market_cap})
loser_data.append({'symbol': symbol, 'name': name, 'price': price, 'changesPercentage': changes_percentage, 'volume': volume, 'marketCap': market_cap}) for symbol, name, price, changes_percentage, volume, market_cap in performance_data[-20:]:
for symbol, name, price, changes_percentage, volume, market_cap in high_volume[:20]: loser_data.append({'symbol': symbol, 'name': name, 'price': price, 'changesPercentage': changes_percentage, 'volume': volume, 'marketCap': market_cap})
active_data.append({'symbol': symbol, 'name': name, 'price': price, 'changesPercentage': changes_percentage, 'volume': volume, 'marketCap': market_cap}) for symbol, name, price, changes_percentage, volume, market_cap in high_volume[:20]:
active_data.append({'symbol': symbol, 'name': name, 'price': price, 'changesPercentage': changes_percentage, 'volume': volume, 'marketCap': market_cap})
if time_period == 7:
gainer_json['1W'] = gainer_data if time_period == 7:
loser_json['1W'] = loser_data gainer_json['1W'] = gainer_data
active_json['1W'] = active_data loser_json['1W'] = loser_data
elif time_period == 30: active_json['1W'] = active_data
gainer_json['1M'] = gainer_data elif time_period == 20:
loser_json['1M'] = loser_data gainer_json['1M'] = gainer_data
active_json['1M'] = active_data loser_json['1M'] = loser_data
elif time_period == 90: active_json['1M'] = active_data
gainer_json['3M'] = gainer_data elif time_period == 252:
loser_json['3M'] = loser_data gainer_json['1Y'] = gainer_data
active_json['3M'] = active_data loser_json['1Y'] = loser_data
elif time_period == 180: active_json['1Y'] = active_data
gainer_json['6M'] = gainer_data elif time_period == 756:
loser_json['6M'] = loser_data gainer_json['3Y'] = gainer_data
active_json['6M'] = active_data loser_json['3Y'] = loser_data
active_json['3Y'] = active_data
return gainer_json, loser_json, active_json elif time_period == 1260:
gainer_json['5Y'] = gainer_data
loser_json['5Y'] = loser_data
def create_table(self): active_json['5Y'] = active_data
"""
Create the 'market_movers' table if it doesn't exist and add 'gainer', 'loser', and 'most_active' columns. return gainer_json, loser_json, active_json
"""
query_drop = "DROP TABLE IF EXISTS market_movers"
self.con.execute(query_drop) def create_table(self):
query_create = """ """
CREATE TABLE IF NOT EXISTS market_movers ( Create the 'market_movers' table if it doesn't exist and add 'gainer', 'loser', and 'most_active' columns.
gainer TEXT, """
loser TEXT, query_drop = "DROP TABLE IF EXISTS market_movers"
most_active TEXT self.con.execute(query_drop)
) query_create = """
""" CREATE TABLE IF NOT EXISTS market_movers (
self.con.execute(query_create) gainer TEXT,
self.con.commit() loser TEXT,
most_active TEXT
)
def update_database(self, gainer_json, loser_json, active_json): """
""" self.con.execute(query_create)
Update the 'gainer', 'loser', and 'most_active' columns in the 'market_movers' table with the provided JSON data. self.con.commit()
"""
query = "INSERT INTO market_movers (gainer, loser, most_active) VALUES (?, ?, ?)"
gainer_json_str = json.dumps(gainer_json) def update_database(self, gainer_json, loser_json, active_json):
loser_json_str = json.dumps(loser_json) """
active_json_str = json.dumps(active_json) Update the 'gainer', 'loser', and 'most_active' columns in the 'market_movers' table with the provided JSON data.
self.con.execute(query, (gainer_json_str, loser_json_str, active_json_str)) """
self.con.commit() query = "INSERT INTO market_movers (gainer, loser, most_active) VALUES (?, ?, ?)"
gainer_json_str = json.dumps(gainer_json)
def close_database_connection(self): loser_json_str = json.dumps(loser_json)
self.con.close() active_json_str = json.dumps(active_json)
self.con.execute(query, (gainer_json_str, loser_json_str, active_json_str))
if __name__ == "__main__": self.con.commit()
analyzer = Past_Market_Movers()
analyzer.create_table() # Create the 'market_movers' table with the 'gainer', 'loser', and 'most_active' columns def close_database_connection(self):
gainer_json, loser_json, active_json = analyzer.run() # Retrieve the gainer_json, loser_json, and active_json data self.con.close()
analyzer.update_database(gainer_json, loser_json, active_json) # Update the 'gainer', 'loser', and 'most_active' columns with the respective data
analyzer.close_database_connection() if __name__ == "__main__":
analyzer = Past_Market_Movers()
analyzer.create_table() # Create the 'market_movers' table with the 'gainer', 'loser', and 'most_active' columns
gainer_json, loser_json, active_json = analyzer.run() # Retrieve the gainer_json, loser_json, and active_json data
analyzer.update_database(gainer_json, loser_json, active_json) # Update the 'gainer', 'loser', and 'most_active' columns with the respective data
analyzer.close_database_connection()