bugfixing price target

This commit is contained in:
MuslemRahimi 2024-10-15 17:08:46 +02:00
parent 05f504642d
commit 695eae90ca

View File

@ -8,6 +8,7 @@ import time
import sqlite3
import ujson
import math
import statistics
import os
from dotenv import load_dotenv
@ -31,43 +32,35 @@ def remove_duplicates(data, key):
def get_summary(res_list):
#Get Latest Summary of ratings from the last 12 months
# -Number of Analyst, -Price Target, -Consensus Rating
# Get the latest summary of ratings from the last 12 months
end_date = date.today()
start_date = end_date - timedelta(days=365) # end_date is today
# Filter the data for the last 12 months
filtered_data = [item for item in res_list if start_date <= datetime.strptime(item['date'], '%Y-%m-%d').date() <= end_date]
#Compute Average Price Target
# Initialize dictionary to store the latest price target for each analyst
latest_pt_current = defaultdict(int)
# Iterate through the data to update the latest pt_current for each analyst
# Iterate through the filtered data to update the latest pt_current for each analyst
for item in filtered_data:
if 'adjusted_pt_current' in item and item['adjusted_pt_current']:
analyst_name = item['analyst_name']
# Convert pt_current to float and check if it's a valid number
try:
pt_current_value = float(item['pt_current'])
# Check if the value is float or int
pt_current_value = float(item['adjusted_pt_current'])
# Update with the maximum value for each analyst
if isinstance(pt_current_value, (float, int)):
# Initialize the analyst entry if it doesn't exist
if analyst_name not in latest_pt_current:
latest_pt_current[analyst_name] = pt_current_value
else:
# Update with the maximum value
latest_pt_current[analyst_name] = max(latest_pt_current[analyst_name], pt_current_value)
latest_pt_current[analyst_name] = max(latest_pt_current.get(analyst_name, pt_current_value), pt_current_value)
except (ValueError, TypeError):
print(f"Invalid pt_current value for analyst '{analyst_name}': {item['pt_current']}")
# Compute the average pt_current based on the latest values
# Get the price target values
pt_current_values = list(latest_pt_current.values())
average_pt_current = sum(pt_current_values) / len(pt_current_values) if pt_current_values else 0
#print("Average pt_current:", round(average_pt_current, 2))
# Compute the median pt_current if there are values, otherwise set to 0
median_pt_current = statistics.median(pt_current_values) if pt_current_values else 0
#print("Median pt_current:", round(median_pt_current, 2))
# Compute Consensus Rating
consensus_ratings = defaultdict(str)
# Define the rating hierarchy
rating_hierarchy = {'Strong Sell': 0, 'Sell': 1, 'Hold': 2, 'Buy': 3, 'Strong Buy': 4}
@ -102,7 +95,6 @@ def get_summary(res_list):
sell_total = data_dict.get('Strong Sell', 0) + data_dict.get('Sell', 0)
hold_total = data_dict.get('Hold', 0)
unique_analyst_names = set()
numOfAnalyst = 0
@ -112,12 +104,13 @@ def get_summary(res_list):
numOfAnalyst += 1
#print("Number of unique analyst names:", numOfAnalyst)
stats = {'numOfAnalyst': numOfAnalyst, 'consensusRating': consensus_rating, 'priceTarget': round(average_pt_current, 2)}
stats = {'numOfAnalyst': numOfAnalyst, 'consensusRating': consensus_rating, 'priceTarget': round(median_pt_current, 2)}
categorical_ratings = {'Buy': buy_total, 'Sell': sell_total, 'Hold': hold_total}
res = {**stats, **categorical_ratings}
return res
def run(chunk,analyst_list):
end_date = date.today()
start_date = datetime(2015,1,1)
@ -247,7 +240,7 @@ try:
chunk_size = len(stock_symbols) // 40 # 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']]
#chunks = [['CMG']]
for chunk in chunks:
run(chunk, analyst_stats_list)