add pre-post market gainers losers
This commit is contained in:
parent
5c1e1860dc
commit
b0eda2701b
@ -13,6 +13,38 @@ import sqlite3
|
||||
|
||||
headers = {"accept": "application/json"}
|
||||
|
||||
def check_market_hours():
|
||||
|
||||
holidays = [
|
||||
"2024-01-01", "2024-01-15", "2024-02-19", "2024-03-29",
|
||||
"2024-05-27", "2024-06-19", "2024-07-04", "2024-09-02",
|
||||
"2024-11-28", "2024-12-25"
|
||||
]
|
||||
|
||||
# Get the current date and time in ET (Eastern Time)
|
||||
et_timezone = pytz.timezone('America/New_York')
|
||||
current_time = datetime.now(et_timezone)
|
||||
current_date_str = current_time.strftime('%Y-%m-%d')
|
||||
current_hour = current_time.hour
|
||||
current_minute = current_time.minute
|
||||
current_day = current_time.weekday() # Monday is 0, Sunday is 6
|
||||
|
||||
# Check if the current date is a holiday or weekend
|
||||
is_weekend = current_day >= 5 # Saturday (5) or Sunday (6)
|
||||
is_holiday = current_date_str in holidays
|
||||
|
||||
# Determine the market status
|
||||
if is_weekend or is_holiday:
|
||||
return 0 #Closed
|
||||
elif current_hour < 9 or (current_hour == 9 and current_minute < 30):
|
||||
return 1 # Pre-Market
|
||||
elif 9 <= current_hour < 16 or (current_hour == 16 and current_minute == 0):
|
||||
return 0 #"Market hours."
|
||||
elif 16 <= current_hour < 24:
|
||||
return 2 #"After-market hours."
|
||||
else:
|
||||
return 0 #"Market is closed."
|
||||
|
||||
|
||||
load_dotenv()
|
||||
benzinga_api_key = os.getenv('BENZINGA_API_KEY')
|
||||
@ -308,37 +340,35 @@ async def run():
|
||||
except Exception as e:
|
||||
print(e)
|
||||
options_flow = {}
|
||||
try:
|
||||
with open(f"json/wiim/rss-feed/data.json", 'r') as file:
|
||||
wiim_feed = ujson.load(file)[0:5]
|
||||
|
||||
except:
|
||||
wiim_feed = []
|
||||
|
||||
'''
|
||||
try:
|
||||
with open(f"json/market-movers/data.json", 'r') as file:
|
||||
data = ujson.load(file)
|
||||
market_mover = {'winner': data['gainers']['1D'][0], 'loser': data['losers']['1D'][0], 'active': data['active']['1D'][0]}
|
||||
market_movers = {'winner': data['gainers']['1D'][0], 'loser': data['losers']['1D'][0], 'active': data['active']['1D'][0]}
|
||||
except:
|
||||
market_mover = {}
|
||||
|
||||
'''
|
||||
try:
|
||||
with open(f"json/most-shorted-stocks/data.json", 'r') as file:
|
||||
data = ujson.load(file)[0]
|
||||
shorted_stock = {key: data[key] for key in ['symbol', 'shortOutStandingPercent']}
|
||||
|
||||
except:
|
||||
shorted_stock = {}
|
||||
market_movers = {}
|
||||
'''
|
||||
market_status = check_market_hours()
|
||||
|
||||
quick_info = {**market_mover, 'topSector': top_sector}
|
||||
if market_status == 0:
|
||||
try:
|
||||
with open(f"json/market-movers/data.json", 'r') as file:
|
||||
data = ujson.load(file)
|
||||
market_movers = {'gainers': data['gainers']['1D'][:5], 'losers': data['losers']['1D'][:5]}
|
||||
except:
|
||||
market_movers = {}
|
||||
else:
|
||||
try:
|
||||
with open(f"json/market-movers/pre-post-data.json", 'r') as file:
|
||||
market_movers = ujson.load(file)
|
||||
except:
|
||||
market_movers = {}
|
||||
|
||||
data = {
|
||||
'quickInfo': quick_info,
|
||||
'marketMovers': market_movers,
|
||||
'marketStatus': market_status,
|
||||
'optionsFlow': options_flow,
|
||||
#'retailTracker': retail_tracker,
|
||||
'wiimFeed': wiim_feed,
|
||||
'marketNews': benzinga_news,
|
||||
'recentEarnings': recent_earnings,
|
||||
'upcomingEarnings': upcoming_earnings,
|
||||
|
||||
@ -253,6 +253,44 @@ async def get_historical_data():
|
||||
|
||||
return res_list
|
||||
|
||||
async def get_pre_post_market_movers(symbols):
|
||||
res_list = []
|
||||
|
||||
# Loop through the symbols and load the corresponding JSON files
|
||||
for symbol in symbols:
|
||||
try:
|
||||
# Load the main quote JSON file
|
||||
with open(f"json/quote/{symbol}.json", "r") as file:
|
||||
data = ujson.load(file)
|
||||
market_cap = int(data.get('marketCap', 0))
|
||||
name = data.get('name',None)
|
||||
# If market cap is >= 10 million, proceed to load pre-post quote data
|
||||
if market_cap >= 10**7:
|
||||
try:
|
||||
with open(f"json/pre-post-quote/{symbol}.json", "r") as file:
|
||||
pre_post_data = ujson.load(file)
|
||||
price = pre_post_data.get("price", None)
|
||||
changes_percentage = pre_post_data.get("changesPercentage", None)
|
||||
if price and changes_percentage:
|
||||
res_list.append({
|
||||
"symbol": symbol,
|
||||
"name": name,
|
||||
"price": price,
|
||||
"changesPercentage": changes_percentage
|
||||
})
|
||||
except:
|
||||
pass
|
||||
|
||||
except:
|
||||
pass
|
||||
|
||||
|
||||
# Sort the list by changesPercentage in descending order and slice the top 10
|
||||
top_5_gainers = sorted(res_list, key=lambda x: x['changesPercentage'], reverse=True)[:5]
|
||||
top_5_losers = sorted(res_list, key=lambda x: x['changesPercentage'], reverse=False)[:5]
|
||||
|
||||
return {'gainers': top_5_gainers, 'losers': top_5_losers}
|
||||
|
||||
|
||||
try:
|
||||
con = sqlite3.connect('stocks.db')
|
||||
@ -268,6 +306,12 @@ try:
|
||||
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_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:
|
||||
print(e)
|
||||
Loading…
x
Reference in New Issue
Block a user