Implementing calculation of a performance per ticker

This commit is contained in:
2025-11-18 10:31:03 +01:00
parent 9411c1c976
commit b90903f424
5 changed files with 550662 additions and 137349 deletions

411314
history_per_ticker.json Normal file

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

58
main.py
View File

@@ -5,7 +5,6 @@ import json
import yfinance as yf
import pandas as pd
import requests
import os
@@ -227,6 +226,7 @@ def push_trmnl_update_chart(wklydict_numbers, dict_chart, trmnl_page_id):
'''
# ------------------#
# LEVEL 2 FUNCTIONS #
# ------------------#
@@ -430,6 +430,7 @@ def calc_current_value_per_trade(trades, history_per_trade):
return trades
# ------------------#
# LEVEL 3 FUNCTIONS #
# ------------------#
@@ -577,33 +578,32 @@ def calc_history_per_ticker(history_per_trade, tickers, trades):
# ------------------ CREATE JSON OBJECT
# Create the json-dict
history_per_trade = {}
history_per_ticker = {}
# 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
initial_values["current_invested"] = 0
initial_values["total_dividends"] = 0
initial_values["current_value"] = 0
initial_values["current_irr"] = 0
initial_values["total_performanance"] = 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
dict_daily[ticker]["current_amount"] = 0
initial_values["current_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:
for trade_id in history_per_trade[date_entry]:
# Extract data from the history_per_trade
trade_amount = history_per_trade[date_entry][trade_id]['current_amount']
@@ -615,7 +615,7 @@ def calc_history_per_ticker(history_per_trade, tickers, trades):
trade_performanance = history_per_trade[date_entry][trade_id]['total_performanance']
# Lookup the ticker by the trade-id
ticker = trades[trade_id]
ticker = trades[trade_id]["ticker"]
# Extract data from the history_per_ticker
ticker_amount = dict_daily[ticker]['current_amount']
@@ -630,10 +630,14 @@ def calc_history_per_ticker(history_per_trade, tickers, trades):
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
dict_daily[ticker]['current_course'] = trade_course # Simple overwrite is fine, as the course is the same for all trades
if ticker_invested == 0 and trade_invested == 0:
dict_daily[ticker]['current_irr'] = 0
# Catch 0 values
else:
dict_daily[ticker]['current_irr'] = (ticker_irr * ticker_invested + trade_irr * trade_invested) / (ticker_invested + trade_invested)
# --> 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
# Calculate the "total" entry after finishing with all the trades
for ticker in tickers:
@@ -651,15 +655,21 @@ def calc_history_per_ticker(history_per_trade, tickers, trades):
# 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)
if ticker_invested == 0 and total_invested == 0:
dict_daily["total"]['current_irr'] = 0
else:
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))
history_per_ticker[date_entry] = dict_daily
# Logging
print("[SUCCESS] Calculating history overall")
return history_total
print("[SUCCESS] Calculating history per ticker")
data = json.dumps(history_per_ticker, indent=2) # Converts a python-dictionary into a json
with open("history_per_ticker.json", "w") as f:
f.write(data)
return history_per_ticker
### -------------------- MAIN PROGRAMM --------------------
@@ -699,10 +709,6 @@ while True:
# This object stores notion pages with all properties
# By "overwriting" the output properties (i.e. irr) and then overwriting the notion-pages, i can store fresh data
trades = calc_current_value_per_trade(trades, history_per_trade)
if error == False:
# Updates / replaces all pages in the notion "trades"
push_notion_trades_update(trades)
if error == False:
# Calculates & stores a history per ticker AND a total across all tickers indexed by the ticker name
@@ -722,6 +728,10 @@ while True:
if error == False:
history_total_wkl = filter_history_by_list(history_total, list_wkl_dates)
if error == False:
# Updates / replaces all pages in the notion "trades"
push_notion_trades_update(trades)
# if error == False:
# push_trmnl_update(test_data, trmnl_id)

View File

@@ -1,200 +1,200 @@
{'LYYA.DE': Close Dividends
Date
2009-11-13 63.441936 0.0
2009-11-16 64.206802 0.0
2009-11-17 64.117287 0.0
2009-11-18 63.743000 0.0
2009-11-19 62.929321 0.0
2009-11-13 63.441959 0.0
2009-11-16 64.206795 0.0
2009-11-17 64.117294 0.0
2009-11-18 63.742992 0.0
2009-11-19 62.929337 0.0
... ... ...
2025-11-10 378.290009 0.0
2025-11-11 379.589996 0.0
2025-11-12 381.549988 0.0
2025-11-13 376.359985 0.0
2025-11-14 375.850006 0.0
2025-11-17 374.269989 0.0
[4066 rows x 2 columns], 'NVD.DE': Close Dividends
[4067 rows x 2 columns], 'NVD.DE': Close Dividends
Date
2007-12-13 23.393524 0.0
2007-12-14 23.393524 0.0
2007-12-17 23.393524 0.0
2007-12-18 23.393524 0.0
2007-12-19 23.393524 0.0
2007-12-13 23.393522 0.0
2007-12-14 23.393522 0.0
2007-12-17 23.393522 0.0
2007-12-18 23.393522 0.0
2007-12-19 23.393522 0.0
... ... ...
2025-11-10 168.039993 0.0
2025-11-11 165.800003 0.0
2025-11-12 166.639999 0.0
2025-11-13 166.639999 0.0
2025-11-14 163.500000 0.0
2025-11-17 161.699997 0.0
[4552 rows x 2 columns], 'ISPA.DE': Close Dividends
[4553 rows x 2 columns], 'ISPA.DE': Close Dividends
Date
2009-09-25 8.835647 0.0
2009-09-28 8.890593 0.0
2009-09-29 8.978509 0.0
2009-09-30 9.005982 0.0
2009-10-01 8.934552 0.0
2009-09-25 8.835642 0.0
2009-09-28 8.890589 0.0
2009-09-29 8.978510 0.0
2009-09-30 9.005983 0.0
2009-10-01 8.934550 0.0
... ... ...
2025-11-10 33.000000 0.0
2025-11-11 33.215000 0.0
2025-11-12 33.395000 0.0
2025-11-13 33.255001 0.0
2025-11-14 33.205002 0.0
2025-11-17 33.060001 0.0
[4100 rows x 2 columns], 'EUNY.DE': Close Dividends
[4101 rows x 2 columns], 'EUNY.DE': Close Dividends
Date
2011-11-25 12.068275 0.0
2011-11-28 12.304699 0.0
2011-11-29 12.368515 0.0
2011-11-28 12.304696 0.0
2011-11-29 12.368513 0.0
2011-11-30 12.701229 0.0
2011-12-01 12.940397 0.0
2011-12-01 12.940396 0.0
... ... ...
2025-11-10 15.128000 0.0
2025-11-11 15.258000 0.0
2025-11-12 15.298000 0.0
2025-11-13 15.194000 0.0
2025-11-14 15.208000 0.0
2025-11-17 15.132000 0.0
[3548 rows x 2 columns], 'MSF.DE': Close Dividends
[3549 rows x 2 columns], 'MSF.DE': Close Dividends
Date
2007-12-28 16.091715 0.0
2008-01-02 15.795918 0.0
2008-01-03 15.717033 0.0
2008-01-04 15.145140 0.0
2008-01-07 15.454091 0.0
2007-12-28 16.091713 0.0
2008-01-02 15.795903 0.0
2008-01-03 15.717032 0.0
2008-01-04 15.145144 0.0
2008-01-07 15.454095 0.0
... ... ...
2025-11-10 431.250000 0.0
2025-11-11 434.549988 0.0
2025-11-12 433.000000 0.0
2025-11-13 437.799988 0.0
2025-11-14 437.649994 0.0
2025-11-17 437.899994 0.0
[4544 rows x 2 columns], 'IQQL.DE': Close Dividends
[4545 rows x 2 columns], 'IQQL.DE': Close Dividends
Date
2008-01-07 8.660799 0.000000
2008-01-14 8.216021 0.000000
2008-01-21 7.511789 0.000000
2008-01-31 8.055408 0.000000
2008-02-05 8.463118 0.000000
2008-01-31 8.055406 0.000000
2008-02-05 8.463119 0.000000
... ... ...
2025-11-10 30.087978 0.000000
2025-11-11 30.289515 0.000000
2025-11-12 30.348501 0.000000
2025-11-13 29.400000 0.521498
2025-11-14 29.325001 0.000000
2025-11-17 29.075001 0.000000
[4370 rows x 2 columns], 'IQQP.DE': Close Dividends
[4371 rows x 2 columns], 'IQQP.DE': Close Dividends
Date
2008-12-23 11.590864 0.0
2008-12-23 11.590865 0.0
2008-12-29 11.916176 0.0
2008-12-30 11.753519 0.0
2009-01-02 12.566789 0.0
2009-01-05 13.061826 0.0
2008-12-30 11.753521 0.0
2009-01-02 12.566793 0.0
2009-01-05 13.061823 0.0
... ... ...
2025-11-10 30.260000 0.0
2025-11-11 30.530001 0.0
2025-11-12 30.785000 0.0
2025-11-13 30.895000 0.0
2025-11-14 30.500000 0.0
2025-11-17 30.565001 0.0
[4290 rows x 2 columns], 'XUHY.DE': Close Dividends
[4291 rows x 2 columns], 'XUHY.DE': Close Dividends
Date
2018-02-14 7.248336 0.0
2018-02-14 7.248337 0.0
2018-02-15 7.256043 0.0
2018-02-16 7.256043 0.0
2018-02-19 7.256043 0.0
2018-02-20 7.256043 0.0
... ... ...
2025-11-10 11.363500 0.0
2025-11-11 11.364000 0.0
2025-11-12 11.321500 0.0
2025-11-13 11.252500 0.0
2025-11-14 11.278000 0.0
2025-11-17 11.271500 0.0
[1970 rows x 2 columns], 'ASML.AS': Close Dividends
[1971 rows x 2 columns], 'ASML.AS': Close Dividends
Date
1998-07-20 3.681718 0.0
1998-07-21 3.625336 0.0
1998-07-20 3.681719 0.0
1998-07-21 3.625335 0.0
1998-07-22 3.585870 0.0
1998-07-23 3.185560 0.0
1998-07-23 3.185559 0.0
1998-07-24 3.067160 0.0
... ... ...
2025-11-10 886.700012 0.0
2025-11-11 886.900024 0.0
2025-11-12 891.799988 0.0
2025-11-13 881.500000 0.0
2025-11-14 874.099976 0.0
2025-11-17 876.400024 0.0
2025-11-18 867.500000 0.0
[7035 rows x 2 columns], 'XDN0.DE': Close Dividends
[7037 rows x 2 columns], 'XDN0.DE': Close Dividends
Date
2013-09-04 18.432314 0.0
2013-09-05 18.409676 0.0
2013-09-06 18.467722 0.0
2013-09-09 18.567812 0.0
2013-09-10 18.888399 0.0
2013-09-06 18.467720 0.0
2013-09-09 18.567806 0.0
2013-09-10 18.888397 0.0
... ... ...
2025-11-10 47.720001 0.0
2025-11-11 48.660000 0.0
2025-11-12 49.115002 0.0
2025-11-13 48.849998 0.0
2025-11-14 48.279999 0.0
2025-11-17 47.980000 0.0
[3097 rows x 2 columns], 'VGWL.DE': Close Dividends
[3098 rows x 2 columns], 'VGWL.DE': Close Dividends
Date
2017-10-26 60.565231 0.0
2017-10-27 61.300976 0.0
2017-10-30 61.049957 0.0
2017-10-31 61.049957 0.0
2017-11-01 61.508717 0.0
2017-10-26 60.565212 0.0
2017-10-27 61.300964 0.0
2017-10-30 61.049961 0.0
2017-10-31 61.049961 0.0
2017-11-01 61.508728 0.0
... ... ...
2025-11-10 141.539993 0.0
2025-11-11 142.080002 0.0
2025-11-12 142.699997 0.0
2025-11-13 140.860001 0.0
2025-11-14 140.720001 0.0
2025-11-17 140.080002 0.0
[2046 rows x 2 columns], 'UEFE.DE': Close Dividends
[2047 rows x 2 columns], 'UEFE.DE': Close Dividends
Date
2018-09-05 10.201477 0.0
2018-09-06 10.226524 0.0
2018-09-07 10.285321 0.0
2018-09-05 10.201478 0.0
2018-09-06 10.226525 0.0
2018-09-07 10.285319 0.0
2018-09-10 10.244708 0.0
2018-09-11 10.219944 0.0
... ... ...
2025-11-10 11.380000 0.0
2025-11-11 11.374500 0.0
2025-11-12 11.393500 0.0
2025-11-13 11.371000 0.0
2025-11-14 11.395500 0.0
2025-11-17 11.390000 0.0
[1829 rows x 2 columns], 'APC.DE': Close Dividends
[1830 rows x 2 columns], 'APC.DE': Close Dividends
Date
2000-01-03 2.844365 0.000000
2000-01-04 0.798163 0.000000
2000-01-05 0.702383 0.000000
2000-01-06 0.740115 0.000000
2000-01-07 0.732859 0.000000
2000-01-03 2.844365 0.0
2000-01-04 0.798163 0.0
2000-01-05 0.702383 0.0
2000-01-06 0.740115 0.0
2000-01-07 0.732859 0.0
... ... ...
2025-11-10 232.750000 0.223652
2025-11-11 234.899994 0.000000
2025-11-12 236.699997 0.000000
2025-11-13 236.699997 0.000000
2025-11-14 235.649994 0.000000
2025-11-11 234.899994 0.0
2025-11-12 236.699997 0.0
2025-11-13 236.699997 0.0
2025-11-14 235.649994 0.0
2025-11-17 231.149994 0.0
[6615 rows x 2 columns], 'QCI.DE': Close Dividends
[6616 rows x 2 columns], 'QCI.DE': Close Dividends
Date
2007-12-18 15.819970 0.0
2007-12-19 15.819970 0.0
2007-12-20 15.819970 0.0
2007-12-21 15.819970 0.0
2007-12-27 15.819970 0.0
2007-12-18 15.819973 0.0
2007-12-19 15.819973 0.0
2007-12-20 15.819973 0.0
2007-12-21 15.819973 0.0
2007-12-27 15.819973 0.0
... ... ...
2025-11-10 147.539993 0.0
2025-11-11 149.699997 0.0
2025-11-12 152.160004 0.0
2025-11-13 150.240005 0.0
2025-11-14 150.919998 0.0
2025-11-17 145.720001 0.0
[4549 rows x 2 columns], 'VWCE.DE': Close Dividends
[4550 rows x 2 columns], 'VWCE.DE': Close Dividends
Date
2019-07-29 71.849998 0.0
2019-07-30 71.440002 0.0
@@ -202,27 +202,27 @@ Date
2019-08-01 71.989998 0.0
2019-08-02 69.639999 0.0
... ... ...
2025-11-10 144.539993 0.0
2025-11-11 145.020004 0.0
2025-11-12 145.740005 0.0
2025-11-13 143.820007 0.0
2025-11-14 143.679993 0.0
2025-11-17 143.020004 0.0
[1605 rows x 2 columns], 'EXSG.DE': Close Dividends
[1606 rows x 2 columns], 'EXSG.DE': Close Dividends
Date
2008-01-02 16.657269 0.0
2008-01-03 16.594925 0.0
2008-01-02 16.657274 0.0
2008-01-03 16.594921 0.0
2008-01-04 16.454641 0.0
2008-01-07 16.522179 0.0
2008-01-08 16.615705 0.0
2008-01-07 16.522184 0.0
2008-01-08 16.615711 0.0
... ... ...
2025-11-10 20.334999 0.0
2025-11-11 20.465000 0.0
2025-11-12 20.705000 0.0
2025-11-13 20.715000 0.0
2025-11-14 20.555000 0.0
2025-11-17 20.420000 0.0
[4540 rows x 2 columns], 'MIVB.DE': Close Dividends
[4541 rows x 2 columns], 'MIVB.DE': Close Dividends
Date
2018-11-20 47.334999 0.0
2018-11-21 47.334999 0.0
@@ -230,13 +230,13 @@ Date
2018-11-23 47.334999 0.0
2018-11-26 47.334999 0.0
... ... ...
2025-11-10 83.870003 0.0
2025-11-11 85.080002 0.0
2025-11-12 85.599998 0.0
2025-11-13 84.830002 0.0
2025-11-14 83.949997 0.0
2025-11-17 83.379997 0.0
[1775 rows x 2 columns], 'AMD.DE': Close Dividends
[1776 rows x 2 columns], 'AMD.DE': Close Dividends
Date
2007-12-28 5.200000 0.0
2008-01-02 4.880000 0.0
@@ -244,41 +244,41 @@ Date
2008-01-04 4.260000 0.0
2008-01-07 4.250000 0.0
... ... ...
2025-11-10 210.750000 0.0
2025-11-11 205.949997 0.0
2025-11-12 225.600006 0.0
2025-11-13 225.600006 0.0
2025-11-14 216.100006 0.0
2025-11-17 210.199997 0.0
[4544 rows x 2 columns], 'EXX5.DE': Close Dividends
[4545 rows x 2 columns], 'EXX5.DE': Close Dividends
Date
2008-01-02 33.756733 0.0
2008-01-03 33.661068 0.0
2008-01-04 32.674915 0.0
2008-01-02 33.756721 0.0
2008-01-03 33.661072 0.0
2008-01-04 32.674923 0.0
2008-01-07 33.006100 0.0
2008-01-08 32.652840 0.0
2008-01-08 32.652847 0.0
... ... ...
2025-11-10 85.919998 0.0
2025-11-11 86.769997 0.0
2025-11-12 87.209999 0.0
2025-11-13 86.779999 0.0
2025-11-14 86.059998 0.0
2025-11-17 85.959999 0.0
[4541 rows x 2 columns], 'INL.DE': Close Dividends
[4542 rows x 2 columns], 'INL.DE': Close Dividends
Date
2007-12-28 9.958893 0.0
2008-01-02 9.323332 0.0
2007-12-28 9.958892 0.0
2008-01-02 9.323335 0.0
2008-01-03 9.021710 0.0
2008-01-04 8.375380 0.0
2008-01-07 8.316133 0.0
2008-01-04 8.375382 0.0
2008-01-07 8.316132 0.0
... ... ...
2025-11-10 33.080002 0.0
2025-11-11 32.764999 0.0
2025-11-12 32.735001 0.0
2025-11-13 31.320000 0.0
2025-11-14 30.885000 0.0
2025-11-17 30.570000 0.0
[4544 rows x 2 columns], 'DFEN.DE': Close Dividends
[4545 rows x 2 columns], 'DFEN.DE': Close Dividends
Date
2023-04-05 18.525999 0.0
2023-04-06 18.690001 0.0
@@ -286,13 +286,13 @@ Date
2023-04-12 18.955999 0.0
2023-04-13 18.959999 0.0
... ... ...
2025-11-10 53.689999 0.0
2025-11-11 53.189999 0.0
2025-11-12 53.000000 0.0
2025-11-13 52.380001 0.0
2025-11-14 52.160000 0.0
2025-11-17 52.340000 0.0
[663 rows x 2 columns], 'DBPG.DE': Close Dividends
[664 rows x 2 columns], 'DBPG.DE': Close Dividends
Date
2010-03-18 12.664600 0.0
2010-03-19 12.535500 0.0
@@ -300,10 +300,10 @@ Date
2010-03-23 12.845900 0.0
2010-03-24 12.704800 0.0
... ... ...
2025-11-10 252.350006 0.0
2025-11-11 254.199997 0.0
2025-11-12 256.450012 0.0
2025-11-13 250.100006 0.0
2025-11-14 249.449997 0.0
2025-11-17 247.449997 0.0
[3979 rows x 2 columns]}
[3980 rows x 2 columns]}

View File

@@ -3978,3 +3978,4 @@ Close,Dividends
256.45001220703125,0.0
250.10000610351562,0.0
249.4499969482422,0.0
247.4499969482422,0.0
1 Close Dividends
3978 256.45001220703125 0.0
3979 250.10000610351562 0.0
3980 249.4499969482422 0.0
3981 247.4499969482422 0.0