Implementing calculation of a performance per ticker
This commit is contained in:
58
main.py
58
main.py
@@ -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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user