==================
很多同学问如何用backtrader进行期货回测。以下给个例子,与普通股票回测不同的是佣金的设置方式。
1 佣金类设置
import datetime # For datetime objects
import backtrader as bt
import backtrader.feeds as btfeeds
import backtrader.indicators as btind
import pandas as pd
import numpy as np
from datetime import datetime
class Par_SAR(bt.Strategy):
params = (('period', 2), ('af', 0.02), ('afmax', 0.2))
def __init__(self):
pass
if __name__ == '__main__':
# rb 15 min data
brf_min_bar = bt.feeds.GenericCSVData(
dataname='E:/Quant/Backtrader/backtrader-master/China_Market/data/rb_15m.csv',
timeframe=bt.TimeFrame.Minutes,
#fromdate=datetime.datetime(2019, 5, 1),
#todate=datetime.datetime(2020, 6, 1),
nullvalue=0.0,
dtformat=('%Y-%m-%d %H:%M:%S'),
datetime=1,
time=-1,
high=4,
low=5,
open=3,
close=6,
volume=-1,
# openinterest=-1
)
# Add the Data Feed to Cerebro