排查回测挣钱,干跑赔钱的问题, 对两边交易结果进行逐条校验

This commit is contained in:
zhangkun9038@dingtalk.com 2025-05-13 01:42:39 +00:00
parent 037bb3fd28
commit de6c47c3f8
6 changed files with 54 additions and 1 deletions

1
.gitignore vendored
View File

@ -130,3 +130,4 @@ result/backtest-result-2025-05-12_09-59-10.meta.json
result/backtest-result-2025-05-12_09-59-10_TheForceV7.py
result/output_filted.log
-r
result/

View File

@ -28,3 +28,7 @@ cd -
sed -i 's/\x1B\[[0-9;]*m//g' output.log
python3 filter.py
cp output_filted.log result/ -f
source .venv/bin/activate
cd tools/
python tradestocsv.py
cd -

View File

@ -83,5 +83,6 @@ services:
--config /freqtrade/config_examples/theforcev7.json
--strategy-path /freqtrade/templates
--strategy TheForceV7
--export trades
--fee 0.0008
--export trades

View File

@ -70,7 +70,7 @@ services:
--config /freqtrade/config_examples/config_my_hyperopt.json
--strategy-path /freqtrade/templates
--strategy TheForceV7
--timerange 20240101-20250413
--timerange 20250510-20250513
--breakdown week month
--export trades
--fee 0.0008

BIN
tools/.tradestocsv.py.swp Normal file

Binary file not shown.

47
tools/tradestocsv.py Normal file
View File

@ -0,0 +1,47 @@
import json
import pandas as pd
import os
from pathlib import Path
# 定义结果目录
result_dir = Path('../result')
# 确保结果目录存在
if not result_dir.exists():
raise FileNotFoundError(f"Directory {result_dir} does not exist")
# 寻找文件名包含 'backtest-result-' 的 JSON 文件
json_files = [f for f in result_dir.glob('*.json') if 'backtest-result-' in f.name]
if not json_files:
raise FileNotFoundError("No JSON files with 'backtest-result-' in name found in ../result")
# 找到文件大小最大的 JSON 文件
largest_file = max(json_files, key=lambda x: x.stat().st_size)
# 读取最大的 JSON 文件
with open(largest_file) as f:
data = json.load(f)
# 提取交易记录
trades = data['strategy']['TheForceV7']['trades']
trades_df = pd.DataFrame(trades)
# 选择关键字段
trades_df = trades_df[[
'pair', 'open_date', 'close_date', 'open_rate', 'close_rate', 'amount',
'profit_ratio', 'profit_abs', 'exit_reason', 'fee_open', 'fee_close',
'trade_duration', 'min_rate', 'max_rate'
]]
# 转换为本地时间(可选)
trades_df['open_date'] = pd.to_datetime(trades_df['open_date'])
trades_df['close_date'] = pd.to_datetime(trades_df['close_date'])
# 定义输出 CSV 文件路径(在 ../result 目录下)
output_csv = result_dir / 'backtest_trades.csv'
# 保存为 CSV
trades_df.to_csv(output_csv, index=False)
print(f"Successfully converted {largest_file} to {output_csv}")