bugfixing dividends

This commit is contained in:
MuslemRahimi 2025-02-22 16:46:20 +01:00
parent cd388202a5
commit f29cb1471c

View File

@ -24,8 +24,10 @@ async def save_as_json(symbol, data, file_name):
with open(f"{file_name}/{symbol}.json", 'w') as file: with open(f"{file_name}/{symbol}.json", 'w') as file:
ujson.dump(data, file) ujson.dump(data, file)
async def get_data(ticker, con, etf_con, stock_symbols, etf_symbols): async def get_data(ticker, con, etf_con, stock_symbols, etf_symbols):
try: try:
# Choose the appropriate table and column names
if ticker in etf_symbols: if ticker in etf_symbols:
table_name = 'etfs' table_name = 'etfs'
column_name = 'etf_dividend' column_name = 'etf_dividend'
@ -33,68 +35,82 @@ async def get_data(ticker, con, etf_con, stock_symbols, etf_symbols):
table_name = 'stocks' table_name = 'stocks'
column_name = 'stock_dividend' column_name = 'stock_dividend'
# Build and execute the SQL query
query_template = f""" query_template = f"""
SELECT {column_name}, quote SELECT {column_name}
FROM {table_name} FROM {table_name}
WHERE symbol = ? WHERE symbol = ?
""" """
df = pd.read_sql_query(
df = pd.read_sql_query(query_template, query_template,
etf_con if table_name == 'etfs' else con, etf_con if table_name == 'etfs' else con,
params=(ticker,)) params=(ticker,)
)
dividend_data = orjson.loads(df[column_name].iloc[0]) # Load the JSON data
res = dividend_data.get('historical', []) res = orjson.loads(df[column_name].iloc[0])
# Filter out records that do not have a recordDate or paymentDate
filtered_res = [item for item in res if item['recordDate'] and item['paymentDate']] filtered_res = [item for item in res if item['recordDate'] and item['paymentDate']]
# Get the current and previous year if not filtered_res:
today = datetime.today() raise ValueError("No valid dividend records found.")
current_year = str(today.year)
previous_year = str(today.year - 1)
# Compute the previous year's total dividend # Extract payout frequency and dividend yield from the first valid record
previous_year_records = [item for item in filtered_res if previous_year in item['recordDate']] payout_frequency = filtered_res[0]['frequency']
previous_annual_dividend = round(sum(float(item['adjDividend']) for item in previous_year_records), 2) if previous_year_records else 0 dividend_yield = filtered_res[0]['yield']
# Calculate payout frequency # Determine the period for the last year using the maximum record date
current_year_records = [item for item in filtered_res if current_year in item['recordDate']] max_record_date = max(datetime.fromisoformat(item['recordDate']) for item in filtered_res)
record_dates = sorted([datetime.strptime(item['recordDate'], '%Y-%m-%d') for item in current_year_records]) one_year_ago = max_record_date - timedelta(days=365)
def map_frequency_to_standard(calculated_frequency): # Calculate dividend growth rate
if calculated_frequency >= 45: # Approximately weekly # Sort records by record date
return 5 sorted_records = sorted(filtered_res, key=lambda x: datetime.fromisoformat(x['recordDate']))
elif calculated_frequency >= 10: # More frequent than quarterly but less than weekly
return 5 # Default to weekly for very frequent payments
elif calculated_frequency >= 3: # Approximately quarterly
return 4
elif calculated_frequency >= 1.5: # Approximately semi-annual
return 2
else: # Annual or less frequent
return 1
if len(record_dates) > 1: # Get the year of the latest dividend
total_days = (record_dates[-1] - record_dates[0]).days latest_year = datetime.fromisoformat(sorted_records[-1]['recordDate']).year
intervals = len(record_dates) - 1
average_interval = total_days / intervals if intervals > 0 else None
raw_frequency = round(365 / average_interval) if average_interval and average_interval > 0 else len(record_dates)
estimated_frequency = map_frequency_to_standard(raw_frequency)
else:
estimated_frequency = 1 # Default to annual if only one record exists
# Process quote data # Find the first dividend in the current year and the first dividend from previous year
quote_data = orjson.loads(df['quote'].iloc[0])[0] latest_dividend = None
eps = quote_data.get('eps') previous_year_dividend = None
current_price = quote_data.get('price')
dividend_yield = round((previous_annual_dividend / current_price) * 100, 2) if current_price else None for record in sorted_records:
payout_ratio = round((1 - (eps - previous_annual_dividend) / eps) * 100, 2) if eps else None record_date = datetime.fromisoformat(record['recordDate'])
if record_date.year == latest_year and latest_dividend is None:
latest_dividend = record['adjDividend']
elif record_date.year == latest_year - 1 and previous_year_dividend is None:
previous_year_dividend = record['adjDividend']
# Break if we found both dividends
if latest_dividend is not None and previous_year_dividend is not None:
break
# Calculate growth rate if both values exist
dividend_growth = None
if latest_dividend is not None and previous_year_dividend is not None and previous_year_dividend != 0:
dividend_growth = round(((latest_dividend - previous_year_dividend) / previous_year_dividend) * 100, 2)
# Sum up all adjDividend values for records in the last year
annual_dividend = sum(
item['adjDividend']
for item in filtered_res
if datetime.fromisoformat(item['recordDate']) >= one_year_ago
)
with open(f"json/quote/{ticker}.json","r") as file:
try:
quote_data = orjson.loads(file.read())
eps = quote_data['eps']
payout_ratio = round((1 - (eps - annual_dividend) / eps) * 100, 2) if eps else None
except:
payout_ratio = None
return { return {
'payoutFrequency': estimated_frequency, 'payoutFrequency': payout_frequency,
'annualDividend': previous_annual_dividend, 'annualDividend': round(annual_dividend,2) if annual_dividend != None else annual_dividend,
'dividendYield': dividend_yield, 'dividendYield': round(dividend_yield,2) if dividend_yield != None else dividend_yield,
'payoutRatio': payout_ratio, 'payoutRatio': round(payout_ratio,2) if payout_ratio != None else payout_ratio,
'dividendGrowth': None, 'dividendGrowth': dividend_growth,
'history': filtered_res, 'history': filtered_res,
} }
@ -102,6 +118,7 @@ async def get_data(ticker, con, etf_con, stock_symbols, etf_symbols):
print(f"Error processing ticker {ticker}: {e}") print(f"Error processing ticker {ticker}: {e}")
return {} return {}
async def run(): async def run():
con = sqlite3.connect('stocks.db') con = sqlite3.connect('stocks.db')
cursor = con.cursor() cursor = con.cursor()
@ -114,7 +131,7 @@ async def run():
etf_cursor.execute("SELECT DISTINCT symbol FROM etfs") etf_cursor.execute("SELECT DISTINCT symbol FROM etfs")
etf_symbols = [row[0] for row in etf_cursor.fetchall()] etf_symbols = [row[0] for row in etf_cursor.fetchall()]
total_symbols = ['AAPL'] #stock_symbols + etf_symbols total_symbols = ['QQQY'] #stock_symbols + etf_symbols
for ticker in tqdm(total_symbols): for ticker in tqdm(total_symbols):
res = await get_data(ticker, con, etf_con, stock_symbols, etf_symbols) res = await get_data(ticker, con, etf_con, stock_symbols, etf_symbols)