update dashboard
This commit is contained in:
parent
bdb4b829bc
commit
020480d2bd
@ -250,8 +250,9 @@ def create_politician_db(data, stock_symbols, stock_raw_data, etf_symbols, etf_r
|
|||||||
# Calculate top sectors
|
# Calculate top sectors
|
||||||
sector_list = []
|
sector_list = []
|
||||||
industry_list = []
|
industry_list = []
|
||||||
for item2 in item:
|
for holding in item:
|
||||||
symbol = item2['symbol']
|
try:
|
||||||
|
symbol = holding['symbol']
|
||||||
ticker_data = stock_screener_data_dict.get(symbol, {})
|
ticker_data = stock_screener_data_dict.get(symbol, {})
|
||||||
|
|
||||||
|
|
||||||
@ -264,6 +265,8 @@ def create_politician_db(data, stock_symbols, stock_raw_data, etf_symbols, etf_r
|
|||||||
sector_list.append(sector)
|
sector_list.append(sector)
|
||||||
if industry:
|
if industry:
|
||||||
industry_list.append(industry)
|
industry_list.append(industry)
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
# Get the top 3 most common sectors and industries
|
# Get the top 3 most common sectors and industries
|
||||||
sector_counts = Counter(sector_list)
|
sector_counts = Counter(sector_list)
|
||||||
@ -375,7 +378,7 @@ async def run():
|
|||||||
crypto_con.close()
|
crypto_con.close()
|
||||||
|
|
||||||
total_symbols = crypto_symbols +etf_symbols + stock_symbols
|
total_symbols = crypto_symbols +etf_symbols + stock_symbols
|
||||||
chunk_size = 100
|
chunk_size = 200
|
||||||
politician_list = []
|
politician_list = []
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@ -384,7 +387,7 @@ async def run():
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
||||||
connector = aiohttp.TCPConnector(limit=100) # Adjust the limit as needed
|
connector = aiohttp.TCPConnector(limit=200) # Adjust the limit as needed
|
||||||
async with aiohttp.ClientSession(connector=connector) as session:
|
async with aiohttp.ClientSession(connector=connector) as session:
|
||||||
for i in tqdm(range(0, len(total_symbols), chunk_size)):
|
for i in tqdm(range(0, len(total_symbols), chunk_size)):
|
||||||
try:
|
try:
|
||||||
@ -392,7 +395,7 @@ async def run():
|
|||||||
data = await get_congress_data(symbols_chunk,session)
|
data = await get_congress_data(symbols_chunk,session)
|
||||||
politician_list +=data
|
politician_list +=data
|
||||||
print('sleeping')
|
print('sleeping')
|
||||||
await asyncio.sleep(30)
|
await asyncio.sleep(10)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(e)
|
print(e)
|
||||||
pass
|
pass
|
||||||
|
|||||||
@ -109,27 +109,44 @@ if tomorrow.weekday() >= 5: # 5 = Saturday, 6 = Sunday
|
|||||||
|
|
||||||
tomorrow = tomorrow.strftime('%Y-%m-%d')
|
tomorrow = tomorrow.strftime('%Y-%m-%d')
|
||||||
|
|
||||||
async def get_upcoming_earnings(session, end_date):
|
async def get_upcoming_earnings(session, end_date, filter_today=False):
|
||||||
url = "https://api.benzinga.com/api/v2.1/calendar/earnings"
|
url = "https://api.benzinga.com/api/v2.1/calendar/earnings"
|
||||||
importance_list = ["1", "2", "3", "4", "5"]
|
importance_list = ["1", "2", "3", "4", "5"]
|
||||||
res_list = []
|
res_list = []
|
||||||
|
today = date.today().strftime('%Y-%m-%d')
|
||||||
|
|
||||||
for importance in importance_list:
|
for importance in importance_list:
|
||||||
querystring = {"token": benzinga_api_key,"parameters[importance]":importance,"parameters[date_from]":today,"parameters[date_to]":end_date,"parameters[date_sort]":"date"}
|
querystring = {
|
||||||
|
"token": benzinga_api_key,
|
||||||
|
"parameters[importance]": importance,
|
||||||
|
"parameters[date_from]": today,
|
||||||
|
"parameters[date_to]": end_date,
|
||||||
|
"parameters[date_sort]": "date"
|
||||||
|
}
|
||||||
try:
|
try:
|
||||||
async with session.get(url, params=querystring, headers=headers) as response:
|
async with session.get(url, params=querystring, headers=headers) as response:
|
||||||
res = ujson.loads(await response.text())['earnings']
|
res = ujson.loads(await response.text())['earnings']
|
||||||
res = [e for e in res if datetime.strptime(e['date'], "%Y-%m-%d").date() != date.today() or datetime.strptime(e['time'], "%H:%M:%S").time() >= datetime.strptime("16:00:00", "%H:%M:%S").time()]
|
|
||||||
|
# Apply the time filter if filter_today is True
|
||||||
|
if filter_today:
|
||||||
|
res = [
|
||||||
|
e for e in res if
|
||||||
|
datetime.strptime(e['date'], "%Y-%m-%d").date() != date.today() or
|
||||||
|
datetime.strptime(e['time'], "%H:%M:%S").time() >= datetime.strptime("16:00:00", "%H:%M:%S").time()
|
||||||
|
]
|
||||||
|
|
||||||
for item in res:
|
for item in res:
|
||||||
try:
|
try:
|
||||||
symbol = item['ticker']
|
symbol = item['ticker']
|
||||||
name = item['name']
|
name = item['name']
|
||||||
time = item['time']
|
time = item['time']
|
||||||
is_today = True if item['date'] == datetime.today().strftime('%Y-%m-%d') else False
|
is_today = item['date'] == today
|
||||||
eps_prior = float(item['eps_prior']) if item['eps_prior'] != '' else 0
|
eps_prior = float(item['eps_prior']) if item['eps_prior'] != '' else 0
|
||||||
eps_est = float(item['eps_est']) if item['eps_est'] != '' else 0
|
eps_est = float(item['eps_est']) if item['eps_est'] != '' else 0
|
||||||
revenue_est = float(item['revenue_est']) if item['revenue_est'] != '' else 0
|
revenue_est = float(item['revenue_est']) if item['revenue_est'] != '' else 0
|
||||||
revenue_prior = float(item['revenue_prior']) if item['revenue_prior'] != '' else 0
|
revenue_prior = float(item['revenue_prior']) if item['revenue_prior'] != '' else 0
|
||||||
if symbol in stock_symbols and revenue_est != 0 and revenue_prior != 0 and eps_prior != 0 and eps_est != 0:
|
|
||||||
|
if symbol in stock_symbols and revenue_est and revenue_prior and eps_prior and eps_est:
|
||||||
df = pd.read_sql_query(query_template, con, params=(symbol,))
|
df = pd.read_sql_query(query_template, con, params=(symbol,))
|
||||||
market_cap = float(df['marketCap'].iloc[0]) if df['marketCap'].iloc[0] != '' else 0
|
market_cap = float(df['marketCap'].iloc[0]) if df['marketCap'].iloc[0] != '' else 0
|
||||||
res_list.append({
|
res_list.append({
|
||||||
@ -149,18 +166,16 @@ async def get_upcoming_earnings(session, end_date):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(e)
|
print(e)
|
||||||
pass
|
pass
|
||||||
|
|
||||||
try:
|
try:
|
||||||
res_list = remove_duplicates(res_list)
|
res_list = remove_duplicates(res_list)
|
||||||
res_list.sort(key=lambda x: x['marketCap'], reverse=True)
|
res_list.sort(key=lambda x: x['marketCap'], reverse=True)
|
||||||
#res_list = [{k: v for k, v in d.items() if k != 'marketCap'} for d in res_list]
|
|
||||||
return res_list[:10]
|
return res_list[:10]
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(e)
|
print(e)
|
||||||
return []
|
return []
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
async def get_recent_earnings(session):
|
async def get_recent_earnings(session):
|
||||||
url = "https://api.benzinga.com/api/v2.1/calendar/earnings"
|
url = "https://api.benzinga.com/api/v2.1/calendar/earnings"
|
||||||
res_list = []
|
res_list = []
|
||||||
@ -263,10 +278,17 @@ async def get_recent_dividends(session):
|
|||||||
async def run():
|
async def run():
|
||||||
async with aiohttp.ClientSession() as session:
|
async with aiohttp.ClientSession() as session:
|
||||||
recent_earnings = await get_recent_earnings(session)
|
recent_earnings = await get_recent_earnings(session)
|
||||||
upcoming_earnings = await get_upcoming_earnings(session, today)
|
|
||||||
|
upcoming_earnings = await get_upcoming_earnings(session, today, filter_today=True)
|
||||||
|
# If results are less than 5, try without the time filter.
|
||||||
|
if len(upcoming_earnings) < 5:
|
||||||
|
upcoming_earnings = await get_upcoming_earnings(session, today, filter_today=False)
|
||||||
|
|
||||||
|
# If still less than 5 results, try fetching for tomorrow.
|
||||||
if len(upcoming_earnings) < 5:
|
if len(upcoming_earnings) < 5:
|
||||||
upcoming_earnings = await get_upcoming_earnings(session, tomorrow)
|
upcoming_earnings = await get_upcoming_earnings(session, tomorrow)
|
||||||
|
|
||||||
|
|
||||||
recent_dividends = await get_recent_dividends(session)
|
recent_dividends = await get_recent_dividends(session)
|
||||||
|
|
||||||
#Avoid clashing of recent and upcoming earnings
|
#Avoid clashing of recent and upcoming earnings
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user