简单波动率策略,实现+网格搜索
This commit is contained in:
@@ -9,7 +9,7 @@ from ..core_data import PortfolioSnapshot, Trade, Bar
|
||||
|
||||
|
||||
def calculate_metrics(
|
||||
snapshots: List[PortfolioSnapshot], trades: List[Trade], initial_capital: float
|
||||
snapshots: List[PortfolioSnapshot], trades: List[Trade], initial_capital: float
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
纯函数:根据投资组合快照和交易历史计算关键绩效指标。
|
||||
@@ -124,11 +124,27 @@ def calculate_metrics(
|
||||
"亏损交易次数": losing_count,
|
||||
"平均每次盈利": avg_profit_per_trade,
|
||||
"平均每次亏损": avg_loss_per_trade, # 这个值是负数
|
||||
"InitialCapital": initial_capital,
|
||||
"FinalCapital": final_value,
|
||||
"TotalReturn": total_return,
|
||||
"AnnualizedReturn": annualized_return,
|
||||
"MaxDrawdown": max_drawdown,
|
||||
"SharpeRatio": sharpe_ratio,
|
||||
"CalmarRatio": calmar_ratio,
|
||||
"TotalTrades": len(trades), # All buy and sell trades
|
||||
"TransactionCosts": total_commissions,
|
||||
"TotalRealizedPNL": total_realized_pnl, # New
|
||||
"WinRate": win_rate,
|
||||
"ProfitLossRatio": profit_loss_ratio,
|
||||
"WinningTradesCount": winning_count,
|
||||
"LosingTradesCount": losing_count,
|
||||
"AvgProfitPerTrade": avg_profit_per_trade,
|
||||
"AvgLossPerTrade": avg_loss_per_trade, # This value is negative
|
||||
}
|
||||
|
||||
|
||||
def plot_equity_and_drawdown_chart(snapshots: List[PortfolioSnapshot], initial_capital: float,
|
||||
title: str = "Portfolio Equity and Drawdown Curve") -> None:
|
||||
title: str = "Portfolio Equity and Drawdown Curve") -> None:
|
||||
"""
|
||||
Plots the portfolio equity curve and drawdown. X-axis points are equally spaced.
|
||||
|
||||
@@ -145,7 +161,7 @@ def plot_equity_and_drawdown_chart(snapshots: List[PortfolioSnapshot], initial_c
|
||||
{'datetime': s.datetime, 'total_value': s.total_value}
|
||||
for s in snapshots
|
||||
])
|
||||
|
||||
|
||||
equity_curve = df_equity['total_value'] / initial_capital
|
||||
|
||||
rolling_max = equity_curve.cummax()
|
||||
@@ -203,7 +219,7 @@ def plot_close_price_chart(bars: List[Bar], title: str = "Close Price Chart") ->
|
||||
])
|
||||
|
||||
plt.style.use('seaborn-v0_8-darkgrid')
|
||||
fig, ax = plt.subplots(1, 1, figsize=(14, 7)) # Single subplot
|
||||
fig, ax = plt.subplots(1, 1, figsize=(14, 7)) # Single subplot
|
||||
|
||||
x_axis_indices = np.arange(len(df_prices))
|
||||
|
||||
@@ -228,7 +244,7 @@ def plot_close_price_chart(bars: List[Bar], title: str = "Close Price Chart") ->
|
||||
|
||||
# 辅助函数:计算单笔交易的盈亏
|
||||
def calculate_trade_pnl(
|
||||
trade: Trade, entry_price: float, exit_price: float, direction: str
|
||||
trade: Trade, entry_price: float, exit_price: float, direction: str
|
||||
) -> float:
|
||||
if direction == "LONG":
|
||||
pnl = (exit_price - entry_price) * trade.volume
|
||||
|
||||
Reference in New Issue
Block a user