bugfixing analyst ticker

This commit is contained in:
MuslemRahimi 2024-11-23 15:04:29 +01:00
parent 7de22df924
commit 6ee0260c76

View File

@ -75,14 +75,16 @@ def get_summary(res_list):
# Filter the data for the last 12 months and consider the last N ratings # Filter the data for the last 12 months and consider the last N ratings
#Furthermore consider only the last rating of the analyst if he provided multiple in the last 12 months #Furthermore consider only the last rating of the analyst if he provided multiple in the last 12 months
#filtered data is needed for the recommendation list
filtered_data = [item for item in res_list if start_date_12m <= datetime.strptime(item['date'], '%Y-%m-%d').date() <= end_date] filtered_data = [item for item in res_list if start_date_12m <= datetime.strptime(item['date'], '%Y-%m-%d').date() <= end_date]
filtered_data = filter_latest_entries(filtered_data)[:30] #unique list is needed for analyst summary rating
unique_filtered_data = filter_latest_entries(filtered_data)[:30]
# Initialize dictionary to store the latest price target for each analyst # Initialize dictionary to store the latest price target for each analyst
latest_pt_current = defaultdict(list) latest_pt_current = defaultdict(list)
# Iterate through the filtered data to collect pt_current for each analyst # Iterate through the filtered data to collect pt_current for each analyst
for item in filtered_data: for item in unique_filtered_data:
if 'adjusted_pt_current' in item and item['adjusted_pt_current']: if 'adjusted_pt_current' in item and item['adjusted_pt_current']:
analyst_name = item['analyst_name'] analyst_name = item['analyst_name']
try: try:
@ -149,7 +151,7 @@ def get_summary(res_list):
# Compute consensus ratings (similar to previous implementation) # Compute consensus ratings (similar to previous implementation)
consensus_ratings = defaultdict(str) consensus_ratings = defaultdict(str)
for item in filtered_data: for item in unique_filtered_data:
if 'rating_current' in item and item['rating_current'] and 'analyst_name' in item and item['analyst_name']: if 'rating_current' in item and item['rating_current'] and 'analyst_name' in item and item['analyst_name']:
try: try:
analyst_name = item['analyst_name'] analyst_name = item['analyst_name']
@ -173,7 +175,7 @@ def get_summary(res_list):
# Count unique analysts # Count unique analysts
unique_analyst_names = set() unique_analyst_names = set()
numOfAnalyst = len(filtered_data) numOfAnalyst = len(unique_filtered_data)
''' '''
for item in filtered_data: for item in filtered_data:
if item['analyst_name'] not in unique_analyst_names: if item['analyst_name'] not in unique_analyst_names:
@ -197,14 +199,14 @@ def get_summary(res_list):
res = {**stats, **categorical_ratings} res = {**stats, **categorical_ratings}
return res return res
def run(chunk,analyst_list, con): def run(chunk, analyst_list, con):
start_date = datetime(2015,1,1) start_date = datetime(2015, 1, 1)
end_date_str = end_date.strftime('%Y-%m-%d') end_date_str = end_date.strftime('%Y-%m-%d')
start_date_str = start_date.strftime('%Y-%m-%d') start_date_str = start_date.strftime('%Y-%m-%d')
company_tickers = ','.join(chunk) company_tickers = ','.join(chunk)
res_list = [] res_list = []
for page in range(0, 500): for page in range(0, 500):
try: try:
data = fin.ratings(company_tickers=company_tickers, page=page, pagesize=1000, date_from=start_date_str, date_to=end_date_str) data = fin.ratings(company_tickers=company_tickers, page=page, pagesize=1000, date_from=start_date_str, date_to=end_date_str)
@ -214,8 +216,6 @@ def run(chunk,analyst_list, con):
break break
res_list = [item for item in res_list if item.get('analyst_name')] res_list = [item for item in res_list if item.get('analyst_name')]
#print(res_list[-15])
for ticker in chunk: for ticker in chunk:
try: try:
ticker_filtered_data = [item for item in res_list if item['ticker'] == ticker] ticker_filtered_data = [item for item in res_list if item['ticker'] == ticker]
@ -223,6 +223,8 @@ def run(chunk,analyst_list, con):
for item in ticker_filtered_data: for item in ticker_filtered_data:
if item['rating_current'] == 'Strong Sell' or item['rating_current'] == 'Strong Buy': if item['rating_current'] == 'Strong Sell' or item['rating_current'] == 'Strong Buy':
pass pass
elif item['rating_current'] == 'Accumulate' and item['rating_prior'] == 'Buy':
item['rating_current'] = 'Buy'
elif item['rating_current'] == 'Neutral': elif item['rating_current'] == 'Neutral':
item['rating_current'] = 'Hold' item['rating_current'] = 'Hold'
elif item['rating_current'] == 'Equal-Weight' or item['rating_current'] == 'Sector Weight' or item['rating_current'] == 'Sector Perform': elif item['rating_current'] == 'Equal-Weight' or item['rating_current'] == 'Sector Weight' or item['rating_current'] == 'Sector Perform':
@ -235,7 +237,7 @@ def run(chunk,analyst_list, con):
item['rating_current'] = 'Sell' item['rating_current'] = 'Sell'
elif (item['rating_current'] == 'Outperform' or item['rating_current'] == 'Overweight') and (item['action_company'] == 'Reiterates' or item['action_company'] == 'Initiates Coverage On'): elif (item['rating_current'] == 'Outperform' or item['rating_current'] == 'Overweight') and (item['action_company'] == 'Reiterates' or item['action_company'] == 'Initiates Coverage On'):
item['rating_current'] = 'Buy' item['rating_current'] = 'Buy'
item['action_comapny'] = 'Initiates' item['action_company'] = 'Initiates'
elif item['rating_current'] == 'Market Outperform' and (item['action_company'] == 'Maintains' or item['action_company'] == 'Reiterates'): elif item['rating_current'] == 'Market Outperform' and (item['action_company'] == 'Maintains' or item['action_company'] == 'Reiterates'):
item['rating_current'] = 'Buy' item['rating_current'] = 'Buy'
elif item['rating_current'] == 'Outperform' and (item['action_company'] == 'Maintains' or item['action_pt'] == 'Announces' or item['action_company'] == 'Upgrades'): elif item['rating_current'] == 'Outperform' and (item['action_company'] == 'Maintains' or item['action_pt'] == 'Announces' or item['action_company'] == 'Upgrades'):
@ -256,16 +258,16 @@ def run(chunk,analyst_list, con):
item['rating_current'] = 'Hold' item['rating_current'] = 'Hold'
elif item['rating_prior'] == 'Outperform' and item['action_company'] == 'Downgrades': elif item['rating_prior'] == 'Outperform' and item['action_company'] == 'Downgrades':
item['rating_current'] = 'Hold' item['rating_current'] = 'Hold'
elif item['rating_current'] == 'Peer Perform' and item['rating_prior'] == 'Peer Perfrom': elif item['rating_current'] == 'Peer Perform' and item['rating_prior'] == 'Peer Perform':
item['rating_current'] = 'Hold' item['rating_current'] = 'Hold'
elif item['rating_current'] == 'Peer Perform' and item['action_pt'] == 'Announces': elif item['rating_current'] == 'Peer Perform' and item['action_pt'] == 'Announces':
item['rating_current'] = 'Hold' item['rating_current'] = 'Hold'
item['action_comapny'] = 'Initiates' item['action_company'] = 'Initiates'
summary = get_summary(ticker_filtered_data) summary = get_summary(ticker_filtered_data)
try: try:
#Add historical price for the last 12 month # Add historical price for the last 12 months
query = query_template.format(ticker=ticker) query = query_template.format(ticker=ticker)
df_12m = pd.read_sql_query(query, con, params=(start_date_12m, end_date)).round(2) df_12m = pd.read_sql_query(query, con, params=(start_date_12m, end_date)).round(2)
df_12m['date'] = pd.to_datetime(df_12m['date']) df_12m['date'] = pd.to_datetime(df_12m['date'])
@ -276,16 +278,11 @@ def run(chunk,analyst_list, con):
except: except:
summary["pastPriceList"] = [] summary["pastPriceList"] = []
# Get ratings of each analyst
#get ratings of each analyst
with open(f"json/analyst/summary/{ticker}.json", 'w') as file: with open(f"json/analyst/summary/{ticker}.json", 'w') as file:
ujson.dump(summary, file) ujson.dump(summary, file)
for item1 in ticker_filtered_data: for item1 in ticker_filtered_data:
#item1['analystId'] = ''
#item1['analystScore'] = 0
#item1['adjusted_pt_current'] = 0
#item1['adjusted_pt_prior'] = 0
for item2 in analyst_stats_list: for item2 in analyst_stats_list:
if item1['analyst'] == item2['companyName'] and item1['analyst_name'] == item2['analystName']: if item1['analyst'] == item2['companyName'] and item1['analyst_name'] == item2['analystName']:
item1['analystId'] = item2['analystId'] item1['analystId'] = item2['analystId']
@ -295,15 +292,6 @@ def run(chunk,analyst_list, con):
item1['analystId'] = item2['analystId'] item1['analystId'] = item2['analystId']
item1['analystScore'] = item2['analystScore'] item1['analystScore'] = item2['analystScore']
break break
#Bug: Benzinga does not give me reliable all analyst names and hence score.
# Compute in those cases the analyst score separately for each analyst
'''
if 'analystScore' not in item1: #or item1['analystScore'] == 0:
one_sample_list = get_one_sample_analyst_data(item1['analyst_name'], item1['analyst'])
item1['analystId'] = one_sample_list[0]['id']
item1['analystScore'] = one_sample_list[0]['analystScore']
'''
desired_keys = ['date', 'action_company', 'rating_current', 'adjusted_pt_current', 'adjusted_pt_prior', 'analystId', 'analystScore', 'analyst', 'analyst_name'] desired_keys = ['date', 'action_company', 'rating_current', 'adjusted_pt_current', 'adjusted_pt_prior', 'analystId', 'analystScore', 'analyst', 'analyst_name']
@ -312,9 +300,6 @@ def run(chunk,analyst_list, con):
for item in ticker_filtered_data for item in ticker_filtered_data
] ]
#print(ticker_filtered_data[0])
#time.sleep(10000)
with open(f"json/analyst/history/{ticker}.json", 'w') as file: with open(f"json/analyst/history/{ticker}.json", 'w') as file:
ujson.dump(ticker_filtered_data, file) ujson.dump(ticker_filtered_data, file)
except Exception as e: except Exception as e:
@ -322,7 +307,6 @@ def run(chunk,analyst_list, con):
try: try:
con = sqlite3.connect('stocks.db') con = sqlite3.connect('stocks.db')
stock_cursor = con.cursor() stock_cursor = con.cursor()