update earnings calendar endpoint

This commit is contained in:
MuslemRahimi 2024-10-15 15:21:18 +02:00
parent a6637256b2
commit 05f504642d
2 changed files with 84 additions and 72 deletions

View File

@ -1094,7 +1094,7 @@ async def get_indicator_data(data: IndicatorListData, api_key: str = Security(ge
rule_of_list = data.ruleOfList rule_of_list = data.ruleOfList
ticker_list = data.tickerList ticker_list = data.tickerList
combined_results = [] # List to store the combined results combined_results = [] # List to store the combined results
print(rule_of_list)
# Keys that should be read from the quote files if they are in rule_of_list # Keys that should be read from the quote files if they are in rule_of_list
quote_keys_to_include = ['volume', 'marketCap', 'changesPercentage', 'price', 'symbol', 'name'] quote_keys_to_include = ['volume', 'marketCap', 'changesPercentage', 'price', 'symbol', 'name']
@ -1149,7 +1149,7 @@ async def get_indicator_data(data: IndicatorListData, api_key: str = Security(ge
except Exception as e: except Exception as e:
print(f"An error occurred while merging data: {e}") print(f"An error occurred while merging data: {e}")
print(combined_results)
res = orjson.dumps(combined_results) res = orjson.dumps(combined_results)
compressed_data = gzip.compress(res) compressed_data = gzip.compress(res)

View File

@ -771,84 +771,97 @@ async def get_dividends_calendar(con,symbols):
return filtered_data return filtered_data
async def get_earnings_calendar(con, symbols):
async def get_earnings_calendar(con, stock_symbols):
def remove_duplicates(elements):
seen = set()
unique_elements = []
for element in elements:
if element['symbol'] not in seen:
seen.add(element['symbol'])
unique_elements.append(element)
return unique_elements
BENZINGA_API_KEY = os.getenv('BENZINGA_API_KEY')
headers = {"accept": "application/json"}
url = "https://api.benzinga.com/api/v2.1/calendar/earnings"
importance_list = ["0", "1", "2", "3", "4", "5"]
berlin_tz = pytz.timezone('Europe/Berlin') berlin_tz = pytz.timezone('Europe/Berlin')
today = datetime.now(berlin_tz) today = datetime.now(berlin_tz)
# Start date: 2 weeks ago, rounded to the previous Monday
start_date = today - timedelta(weeks=2)
start_date -= timedelta(days=start_date.weekday()) # Reset to Monday
# Calculate the start date (Monday) 4 weeks before # End date: 2 weeks ahead, rounded to the following Friday
start_date = today - timedelta(weeks=4) end_date = today + timedelta(weeks=2)
start_date = start_date - timedelta(days=(start_date.weekday() - 0) % 7) end_date += timedelta(days=(4 - end_date.weekday())) # Set to Friday
# Calculate the end date (Friday) 4 weeks after
end_date = today + timedelta(weeks=4)
end_date = end_date + timedelta(days=(4 - end_date.weekday()) % 7)
# Format dates as strings in 'YYYY-MM-DD' format
start_date = start_date.strftime('%Y-%m-%d')
end_date = end_date.strftime('%Y-%m-%d')
res_list = []
async with aiohttp.ClientSession() as session: async with aiohttp.ClientSession() as session:
query_template = """ query_template = """
SELECT SELECT name, marketCap FROM stocks WHERE symbol = ?
name,marketCap,eps
FROM
stocks
WHERE
symbol = ?
""" """
while start_date <= end_date:
url = f"https://financialmodelingprep.com/api/v3/earning_calendar?from={start_date}&to={end_date}&apikey={api_key}" date_str = start_date.strftime('%Y-%m-%d') # Same date for start and end in API call
async with session.get(url) as response: for importance in importance_list:
data = await response.json() querystring = {
filtered_data = [{k: v for k, v in stock.items() if stock['symbol'] in symbols and '.' not in stock['symbol'] and '-' not in stock['symbol']} for stock in data] "token": BENZINGA_API_KEY,
#filtered_data = [entry for entry in filtered_data if entry] "parameters[importance]": importance,
for entry in filtered_data: "parameters[date_from]": date_str,
"parameters[date_to]": date_str
}
try: try:
symbol = entry['symbol'] async with session.get(url, params=querystring, headers=headers) as response:
data = ujson.loads(await response.text())['earnings']
res = [item for item in data if item['ticker'] in stock_symbols and '.' not in item['ticker'] and '-' not in item['ticker']]
for item in res:
try: try:
with open(f"json/financial-statements/income-statement/annual/{symbol}.json", 'rb') as file: symbol = item['ticker']
entry['revenue'] = orjson.loads(file.read())[0]['revenue'] eps_prior = float(item['eps_prior']) if item['eps_prior'] else None
except: eps_est = float(item['eps_est']) if item['eps_est'] else None
entry['revenue'] = None revenue_est = float(item['revenue_est']) if item['revenue_est'] else None
revenue_prior = float(item['revenue_prior']) if item['revenue_prior'] else None
fundamental_data = pd.read_sql_query(query_template, con, params=(symbol,)) # Time-based release type
entry['name'] = fundamental_data['name'].iloc[0] time = datetime.strptime(item['time'], "%H:%M:%S").time()
entry['marketCap'] = int(fundamental_data['marketCap'].iloc[0]) if time < datetime.strptime("09:30:00", "%H:%M:%S").time():
entry['eps'] = float(fundamental_data['eps'].iloc[0]) release = "bmo"
except: elif time > datetime.strptime("16:30:00", "%H:%M:%S").time():
entry['marketCap'] = 'n/a' release = "amc"
entry['marketCap'] = None else:
entry['revenue'] = None release = "during"
entry['eps'] = None
filtered_data = [item for item in filtered_data if 'date' in item] # Only include valid data
df = pd.read_sql_query(query_template, con, params=(symbol,))
seen_symbols = set() market_cap = float(df['marketCap'].iloc[0]) if df['marketCap'].iloc[0] else 0
unique_data = [] res_list.append({
'symbol': symbol,
for item in filtered_data: 'name': item['name'],
symbol = item.get('symbol') 'date': item['date'],
try: 'marketCap': market_cap,
with open(f"json/quote/{symbol}.json", 'r') as file: 'epsPrior': eps_prior,
quote = ujson.load(file) 'epsEst': eps_est,
try: 'revenuePrior': revenue_prior,
earnings_date = datetime.strptime(quote['earningsAnnouncement'].split('T')[0], '%Y-%m-%d').strftime('%Y-%m-%d') 'revenueEst': revenue_est,
except: 'release': release
earnings_date = '-' })
except Exception as e: except Exception as e:
earnings_date = '-' print(f"Error processing item for symbol {symbol}: {e}")
print(e) continue
if symbol is None or symbol not in seen_symbols: res_list = remove_duplicates(res_list)
#bug in fmp endpoint. Double check that earnings date is the same as in quote endpoint res_list.sort(key=lambda x: x['marketCap'], reverse=True)
if item['date'] == earnings_date:
#print(symbol, item['date'], earnings_date)
unique_data.append(item)
seen_symbols.add(symbol)
return unique_data except:
#print(f"Error fetching data from API: {e}")
continue
start_date += timedelta(days=1) # Increment date by one day
return res_list
async def get_stock_splits_calendar(con,symbols): async def get_stock_splits_calendar(con,symbols):
@ -1919,11 +1932,14 @@ async def save_json_files():
stock_screener_data = await get_stock_screener(con) stock_screener_data = await get_stock_screener(con)
with open(f"json/stock-screener/data.json", 'w') as file: with open(f"json/stock-screener/data.json", 'w') as file:
ujson.dump(stock_screener_data, file) ujson.dump(stock_screener_data, file)
earnings_list = await get_earnings_calendar(con,symbols)
with open(f"json/earnings-calendar/calendar.json", 'w') as file:
ujson.dump(earnings_list, file)
economic_list = await get_economic_calendar() economic_list = await get_economic_calendar()
if len(economic_list) > 0: if len(economic_list) > 0:
with open(f"json/economic-calendar/calendar.json", 'w') as file: with open(f"json/economic-calendar/calendar.json", 'w') as file:
@ -1934,11 +1950,6 @@ async def save_json_files():
ujson.dump(dividends_list, file) ujson.dump(dividends_list, file)
earnings_list = await get_earnings_calendar(con,symbols)
with open(f"json/earnings-calendar/calendar.json", 'w') as file:
ujson.dump(earnings_list, file)
data = await get_most_shorted_stocks(con) data = await get_most_shorted_stocks(con)
with open(f"json/most-shorted-stocks/data.json", 'w') as file: with open(f"json/most-shorted-stocks/data.json", 'w') as file:
ujson.dump(data, file) ujson.dump(data, file)
@ -2000,6 +2011,7 @@ async def save_json_files():
with open(f"json/magnificent-seven/data.json", 'w') as file: with open(f"json/magnificent-seven/data.json", 'w') as file:
ujson.dump(data, file) ujson.dump(data, file)
con.close() con.close()
etf_con.close() etf_con.close()
crypto_con.close() crypto_con.close()