From 3a2d4e7e951a8cb322a6c38d0487718116e1ee3c Mon Sep 17 00:00:00 2001 From: MuslemRahimi Date: Sun, 22 Dec 2024 22:03:26 +0100 Subject: [PATCH] update top analyst rule --- app/cron_analyst_db.py | 10 +++------- app/cron_analyst_ticker.py | 3 +-- app/restart_json.py | 13 ++++++------- 3 files changed, 10 insertions(+), 16 deletions(-) diff --git a/app/cron_analyst_db.py b/app/cron_analyst_db.py index 86c7d1c..90ebfb8 100755 --- a/app/cron_analyst_db.py +++ b/app/cron_analyst_db.py @@ -187,9 +187,8 @@ def get_top_stocks(): # Filter analysts with a score >= 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() + # Define the date range for the past 12 months start_date = end_date - timedelta(days=365) # 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() ticker = rating['ticker'] - if ( - rating['rating_current'] == 'Strong Buy' and - start_date <= rating_date <= end_date - ): + if rating['rating_current'] == 'Strong Buy' and start_date <= rating_date: # Keep the latest rating for each stock by this analyst if ticker not in ticker_ratings or rating_date > ticker_ratings[ticker]['date']: 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, 'topAnalystPriceTarget': info['median'], 'topAnalystCounter': len(info['analyst_ids']), - 'analystRating': "Strong Buy", + 'topAnalystRating': "Strong Buy", 'marketCap': info['marketCap'], 'name': info['name'] } diff --git a/app/cron_analyst_ticker.py b/app/cron_analyst_ticker.py index 3a86268..772d7e7 100755 --- a/app/cron_analyst_ticker.py +++ b/app/cron_analyst_ticker.py @@ -323,8 +323,7 @@ try: with open(f"json/analyst/all-analyst-data.json", 'r') as 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 = [['NVDA']] for chunk in chunks: diff --git a/app/restart_json.py b/app/restart_json.py index fae363d..5cbc3ba 100755 --- a/app/restart_json.py +++ b/app/restart_json.py @@ -60,6 +60,8 @@ time_frames = { '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): try: # 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): 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) - - one_year_ago = datetime.now() - timedelta(days=365) - # Filter recent data from the last 12 months recent_data = [ item for item in data @@ -257,7 +254,7 @@ def process_top_analyst_data(data, current_price): # Calculate 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 ) @@ -270,8 +267,10 @@ def process_top_analyst_data(data, current_price): consensus_rating = "Hold" elif average_rating_score >= 1.5: consensus_rating = "Sell" - else: + elif average_rating_score >= 1.0: consensus_rating = "Strong Sell" + else: + consensus_rating = None return { "topAnalystCounter": filtered_analyst_count,