This commit is contained in:
27942
2025-10-14 13:38:01 +08:00
parent c01f0465a4
commit 03cc1cf986

View File

@@ -21,20 +21,20 @@ from models.weex import Weex15, Weex1
class BacktestConfig:
"""回测配置类"""
# 交易参数
take_profit: float = 8.0 # 止盈点数
stop_loss: float = -1.0 # 止损点数
take_profit: float = 8.0 # 止盈点数
stop_loss: float = -1.0 # 止损点数
contract_size: float = 10000 # 合约规模
open_fee: float = 5.0 # 开仓手续费
open_fee: float = 5.0 # 开仓手续费
close_fee_rate: float = 0.0005 # 平仓手续费率
# 回测日期范围
start_date: str = "2025-7-1"
end_date: str = "2025-7-31"
# 信号参数
enable_bear_bull_engulf: bool = True # 涨包跌信号
enable_bull_bear_engulf: bool = True # 跌包涨信号
def __post_init__(self):
"""验证配置参数"""
if self.take_profit <= 0:
@@ -65,12 +65,12 @@ class SignalStats:
count: int = 0
wins: int = 0
total_profit: float = 0.0
@property
def win_rate(self) -> float:
"""胜率计算"""
return (self.wins / self.count * 100) if self.count > 0 else 0.0
@property
def avg_profit(self) -> float:
"""平均盈利"""
@@ -191,9 +191,18 @@ def simulate_trade(direction, entry_price, entry_time, next_15min_time, tp=8, sl
# ===============================================================
def backtest(dates, tp=8, sl=-1):
"""
datas日期的列表
:param dates:
:param tp:
:param sl:
:return:
"""
all_data = []
for date_str in dates:
all_data.extend(get_data_by_date(Weex15, date_str))
all_data.extend(get_data_by_date(Weex15, date_str)) # 获取天的数据15分钟k线数据
all_data.sort(key=lambda x: x['id'])
@@ -205,8 +214,8 @@ def backtest(dates, tp=8, sl=-1):
trades = []
for idx in range(1, len(all_data) - 1):
prev, curr = all_data[idx - 1], all_data[idx]
entry_candle = all_data[idx + 1]
prev, curr = all_data[idx - 1], all_data[idx] # 前一笔,当前一笔
entry_candle = all_data[idx + 1] # 开仓
direction, signal = check_signal(prev, curr)
if not direction: