Skip to content

Commit f67613d

Browse files
committed
Export metrics
1 parent 77be6d0 commit f67613d

File tree

6 files changed

+120
-12
lines changed

6 files changed

+120
-12
lines changed

investing_algorithm_framework/__init__.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
AWSS3StorageStateHandler
2525
from .create_app import create_app
2626
from .download_data import download
27-
from .services.metrics import get_annual_volatility, get_sortino_ratio, \
27+
from .services import get_annual_volatility, get_sortino_ratio, \
2828
get_drawdown_series, get_max_drawdown, get_equity_curve, \
2929
get_price_efficiency_ratio, get_sharpe_ratio, \
3030
get_profit_factor, get_cumulative_profit_factor_series, \
@@ -41,7 +41,8 @@
4141
get_trades_per_year, get_average_monthly_return_losing_months, \
4242
get_average_monthly_return_winning_months, get_percentage_winning_years, \
4343
get_rolling_sharpe_ratio, create_backtest_metrics, get_growth, \
44-
get_growth_percentage, get_cumulative_exposure
44+
get_growth_percentage, get_cumulative_exposure, get_median_return, \
45+
get_average_return, get_risk_free_rate_us
4546

4647

4748
__all__ = [
@@ -165,5 +166,8 @@
165166
"get_growth_percentage",
166167
"BacktestEvaluationFocus",
167168
"combine_backtests",
168-
"PositionSize"
169+
"PositionSize",
170+
"get_median_return",
171+
"get_average_return",
172+
"get_risk_free_rate_us"
169173
]

investing_algorithm_framework/services/__init__.py

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,27 @@
1212
from .positions import PositionService, PositionSnapshotService
1313
from .repository_service import RepositoryService
1414
from .trade_service import TradeService
15-
from .metrics import get_risk_free_rate_us
15+
from .metrics import get_annual_volatility, \
16+
get_sortino_ratio, get_drawdown_series, get_max_drawdown, \
17+
get_equity_curve, get_price_efficiency_ratio, get_sharpe_ratio, \
18+
get_profit_factor, get_cumulative_profit_factor_series, \
19+
get_rolling_profit_factor_series, \
20+
get_cagr, get_standard_deviation_returns, \
21+
get_standard_deviation_downside_returns, \
22+
get_total_return, get_cumulative_exposure, get_exposure_ratio, \
23+
get_yearly_returns, get_monthly_returns, get_best_year, \
24+
get_best_month, get_worst_year, get_worst_month, get_best_trade, \
25+
get_worst_trade, get_average_yearly_return, get_average_gain, \
26+
get_average_loss, get_average_monthly_return, \
27+
get_percentage_winning_months, get_average_trade_duration, \
28+
get_trade_frequency, get_win_rate, get_win_loss_ratio, \
29+
get_calmar_ratio, get_max_drawdown_absolute, \
30+
get_max_drawdown_duration, get_max_daily_drawdown, get_trades_per_day, \
31+
get_trades_per_year, get_average_monthly_return_losing_months, \
32+
get_average_monthly_return_winning_months, get_percentage_winning_years, \
33+
get_rolling_sharpe_ratio, create_backtest_metrics, get_growth, \
34+
get_growth_percentage, get_risk_free_rate_us, get_median_return, \
35+
get_average_return
1636

1737
__all__ = [
1838
"OrderService",
@@ -35,5 +55,59 @@
3555
"PortfolioProviderLookup",
3656
"TradeOrderEvaluator",
3757
"DefaultTradeOrderEvaluator",
38-
"get_risk_free_rate_us"
58+
"get_risk_free_rate_us",
59+
"get_annual_volatility",
60+
"get_sortino_ratio",
61+
"get_drawdown_series",
62+
"get_max_drawdown",
63+
"get_equity_curve",
64+
"get_price_efficiency_ratio",
65+
"get_sharpe_ratio",
66+
"get_profit_factor",
67+
"get_cumulative_profit_factor_series",
68+
"get_rolling_profit_factor_series",
69+
"get_sharpe_ratio",
70+
"get_cagr",
71+
"get_standard_deviation_returns",
72+
"get_standard_deviation_downside_returns",
73+
"get_max_drawdown_absolute",
74+
"get_total_return",
75+
"get_cumulative_exposure",
76+
"get_exposure_ratio",
77+
"get_average_trade_duration",
78+
"get_win_rate",
79+
"get_win_loss_ratio",
80+
"get_calmar_ratio",
81+
"get_trade_frequency",
82+
"get_yearly_returns",
83+
"get_monthly_returns",
84+
"get_best_year",
85+
"get_best_month",
86+
"get_worst_year",
87+
"get_worst_month",
88+
"get_best_trade",
89+
"get_worst_trade",
90+
"get_average_yearly_return",
91+
"get_average_gain",
92+
"get_average_loss",
93+
"get_average_monthly_return",
94+
"get_percentage_winning_months",
95+
"get_average_trade_duration",
96+
"get_trade_frequency",
97+
"get_win_rate",
98+
"get_win_loss_ratio",
99+
"get_calmar_ratio",
100+
"get_max_drawdown_duration",
101+
"get_max_daily_drawdown",
102+
"get_trades_per_day",
103+
"get_trades_per_year",
104+
"get_average_monthly_return_losing_months",
105+
"get_average_monthly_return_winning_months",
106+
"get_percentage_winning_years",
107+
"get_rolling_sharpe_ratio",
108+
"get_growth_percentage",
109+
"create_backtest_metrics",
110+
"get_growth",
111+
"get_median_return",
112+
"get_average_return"
39113
]

investing_algorithm_framework/services/metrics/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
get_percentage_winning_months, get_average_monthly_return_losing_months, \
2222
get_average_monthly_return_winning_months, get_growth, \
2323
get_percentage_winning_years, get_worst_year, \
24-
get_growth_percentage
24+
get_growth_percentage, get_median_return, get_average_return
2525
from .exposure import get_average_trade_duration, \
2626
get_trade_frequency, get_trades_per_day, get_trades_per_year, \
2727
get_cumulative_exposure, get_exposure_ratio
@@ -85,4 +85,6 @@
8585
"get_growth",
8686
"get_growth_percentage",
8787
"get_risk_free_rate_us",
88+
"get_median_return",
89+
"get_average_return"
8890
]

investing_algorithm_framework/services/metrics/drawdown.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from typing import List, Tuple
1414
import pandas as pd
1515
from datetime import datetime
16-
from investing_algorithm_framework.domain import PortfolioSnapshot
16+
from investing_algorithm_framework.domain import PortfolioSnapshot, Trade
1717
from .equity_curve import get_equity_curve
1818

1919

investing_algorithm_framework/services/metrics/exposure.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,10 @@
66
Low exposure (<1) means capital is mostly idle or only partially invested.
77
"""
88

9+
from datetime import datetime, timedelta
910
from typing import List
10-
from datetime import timedelta, datetime
11-
from investing_algorithm_framework.domain import Trade
12-
1311

14-
from typing import List
15-
from datetime import datetime, timedelta
12+
from investing_algorithm_framework.domain import Trade
1613

1714

1815
def get_exposure_ratio(

investing_algorithm_framework/services/metrics/returns.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,37 @@ def get_average_return(trades: List[Trade]) -> Tuple[float, float]:
154154
return average_return, percentage
155155

156156

157+
def get_median_return(trades: List[Trade]) -> Tuple[float, float]:
158+
"""
159+
Calculate the median return from a list of trades.
160+
161+
The median return is calculated as the median of all returns.
162+
163+
Args:
164+
trades (List[Trade]): List of trades.
165+
166+
Returns:
167+
Tuple[float, float]: The median return
168+
percentage of the median return
169+
"""
170+
171+
if not trades:
172+
return 0.0, 0.0
173+
174+
sorted_returns = sorted(t.net_gain for t in trades)
175+
n = len(sorted_returns)
176+
mid = n // 2
177+
178+
if n % 2 == 0:
179+
median_return = (sorted_returns[mid - 1] + sorted_returns[mid]) / 2
180+
else:
181+
median_return = sorted_returns[mid]
182+
183+
cost = sum(t.cost for t in trades)
184+
percentage = (median_return / cost) if cost > 0 else 0.0
185+
return median_return, percentage
186+
187+
157188
def get_best_trade(trades: List[Trade]) -> Trade:
158189
"""
159190
Get the trade with the highest net gain.

0 commit comments

Comments
 (0)