|
| 1 | +import json |
| 2 | +from Trading.model.trade import Trade |
| 3 | +from pathlib import Path |
| 4 | +from typing import List, Dict |
| 5 | +import os |
| 6 | + |
| 7 | +CURRENT_FILE_PATH = Path(os.path.dirname(os.path.realpath(__file__))) |
| 8 | +TRADES_JSON_PATH = CURRENT_FILE_PATH.joinpath("trades.json") |
| 9 | + |
| 10 | +def load_trades(): |
| 11 | + with open(TRADES_JSON_PATH) as f: |
| 12 | + data = json.load(f) |
| 13 | + for i, t in enumerate(data): |
| 14 | + data[i] = Trade(**t) |
| 15 | + return data |
| 16 | + |
| 17 | +def get_total_invested_raw(trades: List[Trade]): |
| 18 | + # This does not take into account reinvesting profits |
| 19 | + total = 0 |
| 20 | + for t in trades: |
| 21 | + total += t.volume * t.open_price |
| 22 | + return total |
| 23 | + |
| 24 | +def get_total_profit(trades: List[Trade]): |
| 25 | + total = 0 |
| 26 | + for t in trades: |
| 27 | + total += t.profit |
| 28 | + return total |
| 29 | + |
| 30 | +def get_number_of_days_open(trades: List[Trade]): |
| 31 | + earliest_open_date = None |
| 32 | + latest_close_date = None |
| 33 | + for t in trades: |
| 34 | + if earliest_open_date is None or t.entry_date < earliest_open_date: |
| 35 | + earliest_open_date = t.entry_date |
| 36 | + if latest_close_date is None or t.exit_date > latest_close_date: |
| 37 | + latest_close_date = t.exit_date |
| 38 | + if earliest_open_date is None or latest_close_date is None: |
| 39 | + return 0 |
| 40 | + from datetime import timedelta, date |
| 41 | + # parse string to date |
| 42 | + earliest_open_date = date.fromisoformat(earliest_open_date) |
| 43 | + latest_close_date = date.fromisoformat(latest_close_date) |
| 44 | + return (latest_close_date - earliest_open_date).days |
| 45 | + |
| 46 | +def get_cagr(trades: List[Trade]): |
| 47 | + total_invested = get_invested_money(trades) |
| 48 | + total_profit = get_total_profit(trades) |
| 49 | + return_rate = total_profit / total_invested |
| 50 | + number_of_days = get_number_of_days_open(trades) |
| 51 | + return return_rate * 365 / number_of_days |
| 52 | + |
| 53 | +def get_invested_money(trades: List[Trade]) -> float: |
| 54 | + """Calculate the total input money required to execute a series of trades, considering reinvestment of money obtained |
| 55 | + from closed trades when possible. The input money is calculated as the sum of the money required |
| 56 | + to open a trade, minus the money obtained from closed trades when possible. The money obtained from closed trades is |
| 57 | + calculated as the sum of the volume of the trade times the close price of the trade. The money required to open a trade |
| 58 | + is calculated as the volume of the trade times the open price of the trade. The money obtained from closed trades is |
| 59 | + calculated as the sum of the volume of the trade times the close price of the trade. |
| 60 | + Args: |
| 61 | + trades (List[Trade]): A list of performed trades |
| 62 | +
|
| 63 | + Returns: |
| 64 | + float: The total input money required to execute the trades |
| 65 | + """ |
| 66 | + input_money = 0 |
| 67 | + money_from_closed_trades = 0 |
| 68 | + trades.sort(key=lambda x: x.entry_date) |
| 69 | + |
| 70 | + # add property visited to trades |
| 71 | + for t in trades: |
| 72 | + t.visited = False |
| 73 | + |
| 74 | + def get_trades_that_closed_before(trades: List[Trade], date): |
| 75 | + return [t for t in trades if t.exit_date < date and not t.visited] |
| 76 | + |
| 77 | + for t in trades: |
| 78 | + if t.entry_date == t.exit_date: |
| 79 | + continue |
| 80 | + trades_that_closed_before = get_trades_that_closed_before(trades, t.entry_date) |
| 81 | + for trade in trades_that_closed_before: |
| 82 | + money_from_closed_trades += (trade.volume * trade.close_price) |
| 83 | + trade.visited = True |
| 84 | + money_required = t.volume * t.open_price |
| 85 | + if money_from_closed_trades >= money_required: |
| 86 | + money_from_closed_trades -= money_required |
| 87 | + else: |
| 88 | + input_money += (money_required - money_from_closed_trades) |
| 89 | + money_from_closed_trades = 0 |
| 90 | + |
| 91 | + print(f"Input money: {input_money:.2f}") |
| 92 | + print(f"Money from closed trades: {money_from_closed_trades:.2f}") |
| 93 | + return input_money |
| 94 | + |
| 95 | +def get_start_date(trades: List[Trade]): |
| 96 | + trades.sort(key=lambda x: x.entry_date) |
| 97 | + return trades[0].entry_date |
| 98 | + |
| 99 | +def get_end_date(trades: List[Trade]): |
| 100 | + trades.sort(key=lambda x: x.exit_date) |
| 101 | + return trades[-1].exit_date |
| 102 | + |
| 103 | +print(f"From {get_start_date(load_trades())} to {get_end_date(load_trades())}") |
| 104 | + |
| 105 | +total_invested = get_invested_money(load_trades()) |
| 106 | +print(f"Total invested: {round(total_invested, 2)}") |
| 107 | +total_profit = get_total_profit(load_trades()) |
| 108 | +print(f"Total profit: {round(total_profit, 2)}") |
| 109 | + |
| 110 | +return_rate = total_profit / total_invested |
| 111 | +print(f"Return rate: {round(return_rate * 100, 2)}%") |
| 112 | + |
| 113 | +n_days = get_number_of_days_open(load_trades()) |
| 114 | +print(f"Number of days open: {n_days}") |
| 115 | +cagr = get_cagr(load_trades()) * 100 |
| 116 | +cagr = round(cagr, 2) |
| 117 | +print(f"CAGR: {cagr}%") |
0 commit comments