同步回测与tqsdk

This commit is contained in:
2025-07-01 10:14:28 +08:00
parent 70c3b8186a
commit 4c243a4b47
7 changed files with 27641 additions and 25887 deletions

View File

@@ -240,7 +240,7 @@ if __name__ == "__main__":
# symbol='SHFE.rb2510', # symbol='SHFE.rb2510',
# symbol='KQ.i@SHFE.bu', # symbol='KQ.i@SHFE.bu',
freq="min60", freq="min60",
start_date_str="2023-01-01", start_date_str="2022-01-01",
end_date_str="2025-06-22", end_date_str="2025-06-22",
mode="backtest", # 指定为回测模式 mode="backtest", # 指定为回测模式
tq_user=TQ_USER_NAME, tq_user=TQ_USER_NAME,

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

20897
main.ipynb

File diff suppressed because one or more lines are too long

View File

@@ -51,9 +51,14 @@ def calculate_metrics(
total_days = (df_values.index.max() - df_values.index.min()).days total_days = (df_values.index.max() - df_values.index.min()).days
if total_days > 0: if total_days > 0:
annualized_return = (1 + total_return) ** (252 / total_days) - 1 if total_return > -1:
annualized_return = (1 + total_return) ** (252 / total_days) - 1
else:
annualized_return = -9999999
else: else:
annualized_return = 0.0 annualized_return = 0.0
print(f'total_return: {total_return}, annualized_return:{annualized_return}, 252 / total_days:{252 / total_days}')
rolling_max = df_values["total_value"].cummax() rolling_max = df_values["total_value"].cummax()
daily_drawdown = (rolling_max - df_values["total_value"]) / rolling_max daily_drawdown = (rolling_max - df_values["total_value"]) / rolling_max
@@ -63,7 +68,10 @@ def calculate_metrics(
daily_volatility = excess_daily_returns.std() daily_volatility = excess_daily_returns.std()
if daily_volatility > 0: if daily_volatility > 0:
sharpe_ratio = np.sqrt(252) * (excess_daily_returns.mean() / daily_volatility) if total_return > -1:
sharpe_ratio = np.sqrt(252) * (excess_daily_returns.mean() / daily_volatility)
else:
sharpe_ratio = -10
else: else:
sharpe_ratio = 0.0 sharpe_ratio = 0.0

View File

@@ -330,6 +330,8 @@ class SimpleLimitBuyStrategyShort(Strategy):
# 只有当有持仓时才考虑平仓 # 只有当有持仓时才考虑平仓
if current_pos_volume < 0: # 假设只做多,所以持仓量 > 0 if current_pos_volume < 0: # 假设只做多,所以持仓量 > 0
avg_entry_price = self.get_average_position_price(self.symbol) avg_entry_price = self.get_average_position_price(self.symbol)
self.log(f'avg_entry_price {avg_entry_price}, bar.open {bar.open}')
if avg_entry_price is not None: if avg_entry_price is not None:
pnl_per_unit = ( pnl_per_unit = (
avg_entry_price - bar.open avg_entry_price - bar.open

File diff suppressed because one or more lines are too long