|
""" |
|
运行此脚本可以得到回测结果。 |
|
你需要先修改此脚本中的data_dir和save_dir、以及calculate_pnl.py中的fee_data、volume_data、inst_data五个地方的路径为你本地机器的路径, |
|
之后就可以运行成功。 |
|
""" |
|
|
|
from calculate_pnl import calculate_pnl, portfolio |
|
import pandas as pd |
|
import os |
|
from datetime import date, timedelta |
|
|
|
|
|
product_para = { |
|
|
|
|
|
"FG": {"exchange": "CZCE", "open_threshold": 0.25, "close_threshold": 0.15}, |
|
"sc": {"exchange": "DCE", "open_threshold": 0.46, "close_threshold": 0.322}, |
|
} |
|
|
|
|
|
|
|
|
|
data_dir = "./res/" |
|
save_dir = "./backtest/" |
|
|
|
|
|
if __name__ == "__main__": |
|
|
|
product = "FG" |
|
exchange = product_para[product]["exchange"] |
|
open_threshold = product_para[product]["open_threshold"] |
|
close_threshold = product_para[product]["close_threshold"] |
|
max_pos = 1 |
|
|
|
|
|
start_day = date(2023, 12, 18) |
|
end_day = date(2023, 12, 18) |
|
rebate_ratio = 0.4 |
|
|
|
|
|
day = start_day |
|
backtest_data = {} |
|
while day <= end_day: |
|
path = os.path.join(data_dir, product, "{}.csv".format(day)) |
|
print(path, os.path.exists(path)) |
|
if not os.path.exists(path): |
|
day += timedelta(days = 1) |
|
continue |
|
tmp = pd.read_csv(path) |
|
backtest_data[day] = calculate_pnl( |
|
product = product, |
|
exchange = exchange, |
|
data = tmp, |
|
max_pos = max_pos, |
|
open_threshold = open_threshold, |
|
close_threshold = close_threshold, |
|
) |
|
day += timedelta(days = 1) |
|
continue |
|
|
|
|
|
portfolio( |
|
trade_data = backtest_data, |
|
max_pos = max_pos, |
|
threshold = (open_threshold, close_threshold), |
|
rebate_ratio = rebate_ratio, |
|
save_dir = os.path.join(save_dir, product), |
|
) |
|
|