bugfixing
This commit is contained in:
parent
12b3dc44fe
commit
12ac8395e1
@ -204,62 +204,90 @@ def filter_latest_analyst_unique_rating(data):
|
|||||||
return [value['entry'] for value in latest_entries.values()]
|
return [value['entry'] for value in latest_entries.values()]
|
||||||
|
|
||||||
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 []
|
try:
|
||||||
data = filter_latest_analyst_unique_rating(data)
|
# Filter for top analysts with score >= 4
|
||||||
# Filter recent data from the last 12 months
|
filtered_data = []
|
||||||
recent_data = [
|
for item in data:
|
||||||
item for item in data
|
try:
|
||||||
if 'date' in item and datetime.strptime(item['date'], "%Y-%m-%d") >= one_year_ago
|
if item['analystScore'] >= 4:
|
||||||
][:30] # Consider only the last 30 ratings
|
filtered_data.append(item)
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
data = filtered_data
|
||||||
|
|
||||||
|
data = filter_latest_analyst_unique_rating(data)
|
||||||
|
|
||||||
|
# Filter recent data from last 12 months, limit to 30 most recent
|
||||||
|
recent_data = []
|
||||||
|
|
||||||
|
for item in data:
|
||||||
|
try:
|
||||||
|
if 'date' in item and datetime.strptime(item['date'], "%Y-%m-%d") >= one_year_ago:
|
||||||
|
recent_data.append(item)
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
|
recent_data = recent_data[:30]
|
||||||
|
|
||||||
|
if not recent_data:
|
||||||
|
return {
|
||||||
|
"topAnalystCounter": None,
|
||||||
|
"topAnalystPriceTarget": None,
|
||||||
|
"topAnalystUpside": None,
|
||||||
|
"topAnalystRating": None
|
||||||
|
}
|
||||||
|
|
||||||
# Count filtered analysts
|
|
||||||
if len(recent_data) > 0:
|
|
||||||
filtered_analyst_count = len(recent_data)
|
filtered_analyst_count = len(recent_data)
|
||||||
|
|
||||||
# Extract and filter price targets
|
# Extract valid price targets
|
||||||
price_targets = [
|
price_targets = []
|
||||||
float(item['adjusted_pt_current']) for item in recent_data
|
for item in recent_data:
|
||||||
if 'adjusted_pt_current' in item and item['adjusted_pt_current'] and not math.isnan(float(item['adjusted_pt_current']))
|
try:
|
||||||
]
|
pt = item.get('adjusted_pt_current')
|
||||||
|
if pt and not math.isnan(float(pt)):
|
||||||
|
price_targets.append(float(pt))
|
||||||
|
except (ValueError, TypeError):
|
||||||
|
continue
|
||||||
|
|
||||||
# Calculate median price target
|
# Calculate median price target
|
||||||
median_price_target = None
|
median_price_target = None
|
||||||
if price_targets:
|
if price_targets:
|
||||||
price_targets.sort()
|
price_targets.sort()
|
||||||
median_index = len(price_targets) // 2
|
mid = len(price_targets) // 2
|
||||||
median_price_target = (
|
median_price_target = (
|
||||||
price_targets[median_index]
|
price_targets[mid] if len(price_targets) % 2
|
||||||
if len(price_targets) % 2 != 0 else
|
else (price_targets[mid-1] + price_targets[mid]) / 2
|
||||||
(price_targets[median_index - 1] + price_targets[median_index]) / 2
|
|
||||||
)
|
)
|
||||||
if median_price_target <= 0:
|
if median_price_target <= 0:
|
||||||
median_price_target = None
|
median_price_target = None
|
||||||
# Calculate changes percentage
|
|
||||||
|
# Calculate upside percentage
|
||||||
upside = None
|
upside = None
|
||||||
if median_price_target != None and current_price is not None:
|
if median_price_target and current_price and current_price > 0:
|
||||||
upside = round(((median_price_target / current_price - 1) * 100), 2)
|
upside = round(((median_price_target / current_price - 1) * 100), 2)
|
||||||
|
|
||||||
# Define rating scores
|
# Rating scores mapping
|
||||||
rating_scores = {
|
rating_scores = {
|
||||||
"Strong Buy": 5,
|
"Strong Buy": 5,
|
||||||
"Buy": 4,
|
"Buy": 4,
|
||||||
"Hold": 3,
|
"Hold": 3,
|
||||||
"Sell": 2,
|
"Sell": 2,
|
||||||
"Strong Sell": 1,
|
"Strong Sell": 1
|
||||||
}
|
}
|
||||||
|
|
||||||
# Calculate total rating score
|
# Calculate average rating
|
||||||
total_rating_score = sum(
|
valid_ratings = [
|
||||||
rating_scores.get(item.get('rating_current'), 0) for item in recent_data
|
rating_scores.get(item.get('rating_current', ''), 0)
|
||||||
)
|
for item in recent_data
|
||||||
|
]
|
||||||
|
|
||||||
# Calculate average rating score
|
|
||||||
average_rating_score = (
|
average_rating_score = (
|
||||||
round(total_rating_score / filtered_analyst_count,2)
|
round(sum(valid_ratings) / len(valid_ratings), 2)
|
||||||
if filtered_analyst_count > 0 else 0
|
if valid_ratings else 0
|
||||||
)
|
)
|
||||||
|
|
||||||
# Determine consensus rating
|
# Map average score to consensus rating
|
||||||
|
consensus_rating = None
|
||||||
if average_rating_score >= 4.5:
|
if average_rating_score >= 4.5:
|
||||||
consensus_rating = "Strong Buy"
|
consensus_rating = "Strong Buy"
|
||||||
elif average_rating_score >= 3.5:
|
elif average_rating_score >= 3.5:
|
||||||
@ -270,21 +298,20 @@ def process_top_analyst_data(data, current_price):
|
|||||||
consensus_rating = "Sell"
|
consensus_rating = "Sell"
|
||||||
elif average_rating_score >= 1.0:
|
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,
|
||||||
"topAnalystPriceTarget": median_price_target,
|
"topAnalystPriceTarget": median_price_target,
|
||||||
"topAnalystUpside": upside,
|
"topAnalystUpside": upside,
|
||||||
"topAnalystRating": consensus_rating,
|
"topAnalystRating": consensus_rating
|
||||||
}
|
}
|
||||||
else:
|
|
||||||
|
except Exception as e:
|
||||||
return {
|
return {
|
||||||
"topAnalystCounter": None,
|
"topAnalystCounter": None,
|
||||||
"topAnalystPriceTarget": None,
|
"topAnalystPriceTarget": None,
|
||||||
"topAnalystUpside": None,
|
"topAnalystUpside": None,
|
||||||
"topAnalystRating": None,
|
"topAnalystRating": None
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -647,7 +674,9 @@ async def get_stock_screener(con):
|
|||||||
raw_data = cursor.fetchall()
|
raw_data = cursor.fetchall()
|
||||||
|
|
||||||
# Iterate through stock_screener_data and update 'price' and 'changesPercentage' if symbols match
|
# Iterate through stock_screener_data and update 'price' and 'changesPercentage' if symbols match
|
||||||
# Add VaR value to stock screener
|
#test mode
|
||||||
|
#filtered_data = [item for item in stock_screener_data if item['symbol'] == 'AMD']
|
||||||
|
|
||||||
for item in tqdm(stock_screener_data):
|
for item in tqdm(stock_screener_data):
|
||||||
symbol = item['symbol']
|
symbol = item['symbol']
|
||||||
|
|
||||||
@ -795,7 +824,6 @@ async def get_stock_screener(con):
|
|||||||
item['topAnalystRating'] = None
|
item['topAnalystRating'] = None
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with open(f"json/fail-to-deliver/companies/{symbol}.json", 'r') as file:
|
with open(f"json/fail-to-deliver/companies/{symbol}.json", 'r') as file:
|
||||||
res = orjson.loads(file.read())[-1]
|
res = orjson.loads(file.read())[-1]
|
||||||
@ -1630,6 +1658,12 @@ async def save_json_files():
|
|||||||
etf_symbols = [row[0] for row in etf_cursor.fetchall()]
|
etf_symbols = [row[0] for row in etf_cursor.fetchall()]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
stock_screener_data = await get_stock_screener(con)
|
||||||
|
with open(f"json/stock-screener/data.json", 'w') as file:
|
||||||
|
ujson.dump(stock_screener_data, file)
|
||||||
|
|
||||||
|
|
||||||
data = await get_congress_rss_feed(symbols, etf_symbols)
|
data = await get_congress_rss_feed(symbols, etf_symbols)
|
||||||
with open(f"json/congress-trading/rss-feed/data.json", 'w') as file:
|
with open(f"json/congress-trading/rss-feed/data.json", 'w') as file:
|
||||||
ujson.dump(data, file)
|
ujson.dump(data, file)
|
||||||
@ -1644,11 +1678,6 @@ async def save_json_files():
|
|||||||
with open(f"json/ipo-calendar/data.json", 'w') as file:
|
with open(f"json/ipo-calendar/data.json", 'w') as file:
|
||||||
ujson.dump(data, file)
|
ujson.dump(data, file)
|
||||||
|
|
||||||
stock_screener_data = await get_stock_screener(con)
|
|
||||||
with open(f"json/stock-screener/data.json", 'w') as file:
|
|
||||||
ujson.dump(stock_screener_data, file)
|
|
||||||
|
|
||||||
|
|
||||||
earnings_list = await get_earnings_calendar(con,symbols)
|
earnings_list = await get_earnings_calendar(con,symbols)
|
||||||
with open(f"json/earnings-calendar/calendar.json", 'w') as file:
|
with open(f"json/earnings-calendar/calendar.json", 'w') as file:
|
||||||
ujson.dump(earnings_list, file)
|
ujson.dump(earnings_list, file)
|
||||||
@ -1658,15 +1687,13 @@ async def save_json_files():
|
|||||||
with open(f"json/dividends-calendar/calendar.json", 'w') as file:
|
with open(f"json/dividends-calendar/calendar.json", 'w') as file:
|
||||||
ujson.dump(dividends_list, file)
|
ujson.dump(dividends_list, file)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
data = await etf_providers(etf_con, etf_symbols)
|
data = await etf_providers(etf_con, etf_symbols)
|
||||||
with open(f"json/all-etf-providers/data.json", 'w') as file:
|
with open(f"json/all-etf-providers/data.json", 'w') as file:
|
||||||
ujson.dump(data, file)
|
ujson.dump(data, file)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
con.close()
|
con.close()
|
||||||
etf_con.close()
|
etf_con.close()
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user