update top analyst rule

This commit is contained in:
MuslemRahimi 2024-12-22 22:03:26 +01:00
parent 4c90dc0a77
commit 3a2d4e7e95
3 changed files with 10 additions and 16 deletions

View File

@ -187,9 +187,8 @@ def get_top_stocks():
# Filter analysts with a score >= 4 # Filter analysts with a score >= 4
filtered_data = [item for item in analyst_stats_list if item['analystScore'] >= 4] filtered_data = [item for item in analyst_stats_list if item['analystScore'] >= 4]
# Define the date range for the past 12 months
end_date = datetime.now().date() end_date = datetime.now().date()
# Define the date range for the past 12 months
start_date = end_date - timedelta(days=365) start_date = end_date - timedelta(days=365)
# Track unique analyst-stock pairs and get the latest Strong Buy for each pair # Track unique analyst-stock pairs and get the latest Strong Buy for each pair
@ -203,10 +202,7 @@ def get_top_stocks():
rating_date = datetime.strptime(rating['date'], '%Y-%m-%d').date() rating_date = datetime.strptime(rating['date'], '%Y-%m-%d').date()
ticker = rating['ticker'] ticker = rating['ticker']
if ( if rating['rating_current'] == 'Strong Buy' and start_date <= rating_date:
rating['rating_current'] == 'Strong Buy' and
start_date <= rating_date <= end_date
):
# Keep the latest rating for each stock by this analyst # Keep the latest rating for each stock by this analyst
if ticker not in ticker_ratings or rating_date > ticker_ratings[ticker]['date']: if ticker not in ticker_ratings or rating_date > ticker_ratings[ticker]['date']:
ticker_ratings[ticker] = { ticker_ratings[ticker] = {
@ -264,7 +260,7 @@ def get_top_stocks():
'topAnalystUpside': round((info['median'] / info.get('price') - 1) * 100, 2) if info.get('price') else None, 'topAnalystUpside': round((info['median'] / info.get('price') - 1) * 100, 2) if info.get('price') else None,
'topAnalystPriceTarget': info['median'], 'topAnalystPriceTarget': info['median'],
'topAnalystCounter': len(info['analyst_ids']), 'topAnalystCounter': len(info['analyst_ids']),
'analystRating': "Strong Buy", 'topAnalystRating': "Strong Buy",
'marketCap': info['marketCap'], 'marketCap': info['marketCap'],
'name': info['name'] 'name': info['name']
} }

View File

@ -323,8 +323,7 @@ try:
with open(f"json/analyst/all-analyst-data.json", 'r') as file: with open(f"json/analyst/all-analyst-data.json", 'r') as file:
analyst_stats_list = ujson.load(file) analyst_stats_list = ujson.load(file)
chunk_size = len(stock_symbols) // 100 # Divide the list into N chunks chunk_size = len(stock_symbols) // 300 # Divide the list into N chunks
chunks = [stock_symbols[i:i + chunk_size] for i in range(0, len(stock_symbols), chunk_size)] chunks = [stock_symbols[i:i + chunk_size] for i in range(0, len(stock_symbols), chunk_size)]
#chunks = [['NVDA']] #chunks = [['NVDA']]
for chunk in chunks: for chunk in chunks:

View File

@ -60,6 +60,8 @@ time_frames = {
'change3Y': (datetime.now() - timedelta(days=365 * 3)).strftime('%Y-%m-%d'), 'change3Y': (datetime.now() - timedelta(days=365 * 3)).strftime('%Y-%m-%d'),
} }
one_year_ago = datetime.now() - timedelta(days=365)
def calculate_price_changes(symbol, item, con): def calculate_price_changes(symbol, item, con):
try: try:
# Loop through each time frame to calculate the change # Loop through each time frame to calculate the change
@ -202,12 +204,7 @@ def filter_latest_analyst_unique_rating(data):
def process_top_analyst_data(data, current_price): def process_top_analyst_data(data, current_price):
data = [item for item in data if item.get('analystScore', 0) >= 4] if data else [] data = [item for item in data if item.get('analystScore', 0) >= 4] if data else []
if symbol == 'AMD':
print(data)
data = filter_latest_analyst_unique_rating(data) data = filter_latest_analyst_unique_rating(data)
one_year_ago = datetime.now() - timedelta(days=365)
# Filter recent data from the last 12 months # Filter recent data from the last 12 months
recent_data = [ recent_data = [
item for item in data item for item in data
@ -257,7 +254,7 @@ def process_top_analyst_data(data, current_price):
# Calculate average rating score # Calculate average rating score
average_rating_score = ( average_rating_score = (
total_rating_score / filtered_analyst_count round(total_rating_score / filtered_analyst_count,2)
if filtered_analyst_count > 0 else 0 if filtered_analyst_count > 0 else 0
) )
@ -270,8 +267,10 @@ def process_top_analyst_data(data, current_price):
consensus_rating = "Hold" consensus_rating = "Hold"
elif average_rating_score >= 1.5: elif average_rating_score >= 1.5:
consensus_rating = "Sell" consensus_rating = "Sell"
else: elif average_rating_score >= 1.0:
consensus_rating = "Strong Sell" consensus_rating = "Strong Sell"
else:
consensus_rating = None
return { return {
"topAnalystCounter": filtered_analyst_count, "topAnalystCounter": filtered_analyst_count,