From 92bfb07ee3e6256403fb3359c1882d5c78ac0340 Mon Sep 17 00:00:00 2001 From: m5l14i11 Date: Thu, 19 Sep 2024 14:15:53 +0300 Subject: [PATCH] upd --- core/events/portfolio.py | 2 +- core/models/entity/_base.py | 4 +- core/models/{ => entity}/portfolio.py | 82 ++------------------------- portfolio/_portfolio.py | 2 +- 4 files changed, 9 insertions(+), 81 deletions(-) rename core/models/{ => entity}/portfolio.py (75%) diff --git a/core/events/portfolio.py b/core/events/portfolio.py index 65e0c2a3..c748f6cd 100644 --- a/core/events/portfolio.py +++ b/core/events/portfolio.py @@ -1,6 +1,6 @@ from dataclasses import dataclass, field -from core.models.portfolio import Performance +from core.models.entity.portfolio import Performance from core.models.strategy import Strategy from core.models.symbol import Symbol from core.models.timeframe import Timeframe diff --git a/core/models/entity/_base.py b/core/models/entity/_base.py index 3cc7f790..df604867 100644 --- a/core/models/entity/_base.py +++ b/core/models/entity/_base.py @@ -1,6 +1,7 @@ import json from dataclasses import dataclass, fields from enum import Enum +from functools import cached_property from typing import Any, Dict, List @@ -12,7 +13,8 @@ def to_dict(self) -> Dict[str, Any]: property_dict = { k: getattr(self, k) for k in dir(self) - if isinstance(getattr(self.__class__, k, None), property) + if isinstance(getattr(self.__class__, k, None), (property, cached_property)) + and hasattr(self, k) } result = {**field_dict, **property_dict} diff --git a/core/models/portfolio.py b/core/models/entity/portfolio.py similarity index 75% rename from core/models/portfolio.py rename to core/models/entity/portfolio.py index 8039b579..1612bed4 100644 --- a/core/models/portfolio.py +++ b/core/models/entity/portfolio.py @@ -1,15 +1,17 @@ -from dataclasses import dataclass, field, replace +from dataclasses import field, replace from datetime import datetime from functools import cached_property import numpy as np from scipy.stats import kurtosis, norm, skew +from ._base import Entity + TOTAL_TRADES_THRESHOLD = 3 GAMMA = 0.57721566 -@dataclass(frozen=True) +@Entity class Performance: _account_size: float _risk_per_trade: float @@ -533,79 +535,3 @@ def _penalty(pnl) -> float: corr = [((num - x) / num) * coef**x for x in range(1, num)] return np.sqrt(1 + 2 * np.sum(corr)) - - def to_dict(self): - return { - "account_size": self._account_size, - "total_trades": self.total_trades, - "total_pnl": self.total_pnl, - "total_fee": self.total_fee, - "average_pnl": self.average_pnl, - "average_profit": self.average_profit, - "average_loss": self.average_loss, - "profit": self.profit, - "loss": self.loss, - "total_profit": self.total_profit, - "total_loss": self.total_loss, - "equity": self.equity, - "runup": self.runup, - "max_runup": self.max_runup, - "drawdown": self.drawdown, - "max_drawdown": self.max_drawdown, - "sharpe_ratio": self.sharpe_ratio, - "smart_sharpe_ratio": self.smart_sharpe_ratio, - "deflated_sharpe_ratio": self.deflated_sharpe_ratio, - "sortino_ratio": self.sortino_ratio, - "smart_sortino_ratio": self.smart_sortino_ratio, - "calmar_ratio": self.calmar_ratio, - "payoff_ratio": self.payoff_ratio, - "cagr": self.cagr, - "kelly": self.kelly, - "expected_return": self.expected_return, - "time_weighted_return": self.time_weighted_return, - "geometric_holding_period_return": self.geometric_holding_period_return, - "annualized_volatility": self.ann_volatility, - "annualized_sharpe_ratio": self.ann_sharpe_ratio, - "recovery_factor": self.recovery_factor, - "profit_factor": self.profit_factor, - "risk_of_ruin": self.risk_of_ruin, - "skew": self.skew, - "kurtosis": self.kurtosis, - "var": self.var, - "cvar": self.cvar, - "ulcer_index": self.ulcer_index, - "upi": self.upi, - "lake_ratio": self.lake_ratio, - "burke_ratio": self.burke_ratio, - "rachev_ratio": self.rachev_ratio, - "sterling_ratio": self.sterling_ratio, - "tail_ratio": self.tail_ratio, - "omega_ratio": self.omega_ratio, - "martin_ratio": self.martin_ratio, - "kappa_three_ratio": self.kappa_three_ratio, - "max_consecutive_wins": self.max_consecutive_wins, - "max_consecutive_losses": self.max_consecutive_losses, - "hit_ratio": self.hit_ratio, - "cpc_ratio": self.cpc_ratio, - "common_sense_ratio": self.common_sense_ratio, - } - - def __str__(self): - return ( - f"total_trades={self.total_trades}, hit_ratio={self.hit_ratio}, profit_factor={self.profit_factor}, profit={self.profit}, loss={self.loss}, " - f"total_profit={self.total_profit}, total_loss={self.total_loss}, total_pnl={self.total_pnl}, average_pnl={self.average_pnl}, average_profit={self.average_profit}, average_loss={self.average_loss}, " - f"total_fee={self.total_fee}, max_consecutive_wins={self.max_consecutive_wins}, max_consecutive_losses={self.max_consecutive_losses}, " - f"equity={self.equity}, max_runup={self.max_runup}, max_drawdown={self.max_drawdown}, " - f"sharpe_ratio={self.sharpe_ratio}, smart_sharpe_ratio={self.smart_sharpe_ratio}, deflated_sharpe_ratio={self.deflated_sharpe_ratio}, sortino_ratio={self.sortino_ratio}, smart_sortino_ratio={self.smart_sortino_ratio}, calmar_ratio={self.calmar_ratio}, " - f"expected_return={self.expected_return}, cagr={self.cagr}, time_weighted_return={self.time_weighted_return}, geometric_holding_period_return={self.geometric_holding_period_return}, " - f"annualized_volatility={self.ann_volatility}, annualized_sharpe_ratio={self.ann_sharpe_ratio}, " - f"recovery_factor={self.recovery_factor}, risk_of_ruin={self.risk_of_ruin}, " - f"skew={self.skew}, kurtosis={self.kurtosis}, var={self.var}, cvar={self.cvar}, " - f"ulcer_index={self.ulcer_index}, upi={self.upi}, " - f"kelly={self.kelly}, lake_ratio={self.lake_ratio}, burke_ratio={self.burke_ratio}, rachev_ratio={self.rachev_ratio}, kappa_three_ratio={self.kappa_three_ratio}, " - f"payoff_ratio={self.payoff_ratio}, sterling_ratio={self.sterling_ratio}, tail_ratio={self.tail_ratio}, omega_ratio={self.omega_ratio}, " - f"cpc_ratio={self.cpc_ratio}, common_sense_ratio={self.common_sense_ratio}" - ) - - def __repr__(self): - return f"Performance({self})" diff --git a/portfolio/_portfolio.py b/portfolio/_portfolio.py index cfcd9605..9de5b362 100644 --- a/portfolio/_portfolio.py +++ b/portfolio/_portfolio.py @@ -2,7 +2,7 @@ from contextlib import asynccontextmanager from typing import Dict, Tuple -from core.models.portfolio import Performance +from core.models.entity.portfolio import Performance from core.models.position import Position from core.models.strategy import Strategy from core.models.symbol import Symbol