From 9411c1c976e9847b8130d326b318ce3e12c9566a Mon Sep 17 00:00:00 2001 From: Marius Date: Tue, 18 Nov 2025 01:49:14 +0100 Subject: [PATCH] WIP for calculating the value across all tickers --- main.py | 164 ++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 101 insertions(+), 63 deletions(-) diff --git a/main.py b/main.py index 6e644ac..9381a54 100644 --- a/main.py +++ b/main.py @@ -227,7 +227,6 @@ def push_trmnl_update_chart(wklydict_numbers, dict_chart, trmnl_page_id): ''' - # ------------------# # LEVEL 2 FUNCTIONS # # ------------------# @@ -430,6 +429,7 @@ def calc_current_value_per_trade(trades, history_per_trade): print ("[SUCCESS] Calculating current value per trade") return trades + # ------------------# # LEVEL 3 FUNCTIONS # # ------------------# @@ -446,65 +446,6 @@ def filter_history_by_list(history, dates_list): print ("[SUCCESS] Filtering History") return filtered_history -# CALC THE HISTORY OVERALL -def calc_history_total(history_per_trade, trades): - - # ------------------ CREATE JSON OBJECT - # Create core layer - values = {} - values['current_invested'] = 0.0 - values['current_value'] = 0.0 - values['current_irr'] = 0.0 - values['current_performance'] = 0.0 - - # Create dates-list - index_date = get_date_open_oldest_trade(trades) - dates = [] - while index_date <= datetime.date.today(): - dates.append(index_date.isoformat()) - index_date = index_date + datetime.timedelta(days=1) - - # Create dates-layer - history_total = {} - for date in dates: - history_total[date] = values - - # ------------------ AGGREGATE DATA - # Loop over all dates - i = 0 - for index_date in history_per_trade: - - # Trade values stored in history per trade - trade_values = history_per_trade[index_date] - - # Loop over all trades - for trade_id in trade_values: - - # Individual values stored per trade and day - values = trade_values[trade_id] - - # Aggregate data in history_total - history_total[index_date]['current_invested'] = history_total[index_date]['current_invested'] + values['current_invested'] - history_total[index_date]['current_value'] = history_total[index_date]['current_value'] + values['current_value'] - - # Weighted irr sum - weighted_irr_sum = values['current_irr'] * values['current_invested'] - history_total[index_date]['current_irr'] = history_total[index_date]['current_irr'] + weighted_irr_sum - - # Weighted irr & total performance - if history_total[index_date]['current_invested'] == 0.0: - history_total[index_date]['current_irr'] = 0.0 - else: - history_total[index_date]['current_irr'] = history_total[index_date]['current_irr'] / history_total[index_date]['current_invested'] - history_total[index_date]['total_performance'] = history_total[index_date]['current_value'] + history_total[index_date]['current_invested'] - - # Increase index - i = i+1 - - # Logging - print("[SUCCESS] Calculating history overall") - return history_total - # CALC HISTORY PER TRADE def calc_history_per_trade(trades, yf_data): # Create support variables @@ -572,7 +513,7 @@ def calc_history_per_trade(trades, yf_data): total_performanance = current_value_with_dividends - current_invested if current_value_with_dividends == 0: - print("0-value Error with ticker: {}").format(ticker) + print("0-value Error with ticker: {}".format(ticker)) else: # Write 0, if trade is not relevant for current timeframe @@ -631,6 +572,94 @@ def calc_history_per_trade(trades, yf_data): return history_per_trade +# CALC THE HISTORY PER TRADE & OVERALL +def calc_history_per_ticker(history_per_trade, tickers, trades): + + # ------------------ CREATE JSON OBJECT + # Create the json-dict + history_per_trade = {} + + # Loop over each date entry in the history + for date_entry in history_per_trade: + + # Set initial values to avoid NULL errors + initial_values = {} + initial_values["invested"] = 0 + initial_values["dividends"] = 0 + initial_values["value"] = 0 + initial_values["irr"] = 0 + initial_values["course"] = 0 + initial_values["performance"] = 0 + + # Create a dict to store the results per day and ticker + dict_daily = {} + for ticker in tickers: + dict_daily[ticker] = initial_values + # Added only for ticker entries, not for the "total" value + dict_daily[ticker]["amount"] = 0 + initial_values["course"] = 0 + + # Add values for a ticker called "total" + dict_daily["total"] = initial_values + + # Loop over each trade-entry for that day + for trade_id in date_entry: + + # Extract data from the history_per_trade + trade_amount = history_per_trade[date_entry][trade_id]['current_amount'] + trade_invested = history_per_trade[date_entry][trade_id]['current_invested'] + trade_dividends = history_per_trade[date_entry][trade_id]['total_dividends'] + trade_value = history_per_trade[date_entry][trade_id]['current_value'] + trade_irr = history_per_trade[date_entry][trade_id]['current_irr'] + trade_course = history_per_trade[date_entry][trade_id]['current_course'] + trade_performanance = history_per_trade[date_entry][trade_id]['total_performanance'] + + # Lookup the ticker by the trade-id + ticker = trades[trade_id] + + # Extract data from the history_per_ticker + ticker_amount = dict_daily[ticker]['current_amount'] + ticker_invested = dict_daily[ticker]['current_invested'] + ticker_dividends = dict_daily[ticker]['total_dividends'] + ticker_value = dict_daily[ticker]['current_value'] + ticker_irr = dict_daily[ticker]['current_irr'] + ticker_performanance = dict_daily[ticker]['total_performanance'] + + # Overwrite the values in the history_per_ticker + dict_daily[ticker]['current_amount'] = ticker_amount + trade_amount # Simple addition works + dict_daily[ticker]['current_invested'] = ticker_invested + trade_invested + dict_daily[ticker]['total_dividends'] = ticker_dividends + trade_dividends + dict_daily[ticker]['current_value'] = ticker_value + trade_value + dict_daily[ticker]['current_irr'] = (ticker_irr * ticker_invested + trade_irr * trade_amount) / (ticker_invested + trade_amount) + # --> IRR is adjusted based on the trade values. This way a trade of 25% of the initial trade volume has only a 25% influence on the irr + dict_daily[ticker]['current_course'] = trade_course # Simple overwrite is fine, as the course is the same for all trades + dict_daily[ticker]['total_performanance'] = ticker_performanance + trade_performanance + + # Calculate the "total" entry after finishing with all the trades + for ticker in tickers: + + # Same logic as above, but shortended code + dict_daily["total"]['total_dividends'] = dict_daily["total"]['total_dividends'] + dict_daily[ticker]['total_dividends'] + dict_daily["total"]['current_value'] = dict_daily["total"]['current_value'] + dict_daily[ticker]['current_value'] + dict_daily["total"]['total_performanance'] = dict_daily["total"]['total_performanance'] + dict_daily[ticker]['total_performanance'] + + # Extracting the values before rewriting them, to preserve them for the IRR calculation + total_invested = dict_daily["total"]['current_invested'] + ticker_invested = dict_daily[ticker]['current_invested'] + dict_daily["total"]['current_invested'] = total_invested + ticker_invested + + # Extracting the values before rewriting them, to preserve them for the IRR calculation + total_irr = dict_daily["total"]['current_irr'] + ticker_irr = dict_daily[ticker]['current_irr'] + dict_daily["total"]['current_irr'] = (total_irr * total_invested + ticker_irr * ticker_invested) / (total_invested + ticker_invested) + + # Finally, write the results for this day-entry to the history_per_ticker + history_per_trade[date_entry] = dict_daily + print("[INFO] Calculated total history for date{}".format(date_entry)) + + # Logging + print("[SUCCESS] Calculating history overall") + return history_total ### -------------------- MAIN PROGRAMM -------------------- @@ -661,8 +690,9 @@ while True: if error == False: # Calculates & stores a history per trade indexed by the notion database_id - # Errors in calculating the history should not stop the remaining steps history_per_trade = calc_history_per_trade(trades, yf_data) + if history_per_trade == True: + error = True if error == False: # Selects the most current values for the irr, current course etc. and saves them to the trades-object @@ -675,11 +705,19 @@ while True: push_notion_trades_update(trades) if error == False: - history_total = calc_history_total(history_per_trade, trades) + # Calculates & stores a history per ticker AND a total across all tickers indexed by the ticker name + # Errors in calculating the history should not stop the remaining steps + history_total = calc_history_per_ticker(history_per_trade, tickers, trades) + else: + # Just clean code practice, so that the parmateter is not undefined + history_total = 0 if error == False: # Creates a list containing one date per week list_wkl_dates = create_list_wkl_dates(trades) + else: + # Just clean code practice, so that the parmateter is not undefined + list_wkl_dates = 0 if error == False: history_total_wkl = filter_history_by_list(history_total, list_wkl_dates)