将自定义多时间帧数据导入 backtrader
import custom multitimeframe data into backtrader
我需要将几个时间框架导入到一个交易策略中,但我不确定如何进行。
这是我的数据:
df['M1'].tail(3)
volume complete o h l c
time
2021-08-23 23:27:00+00:00 1 True 1.26520 1.26520 1.26520 1.26520
2021-08-23 23:28:00+00:00 2 True 1.26517 1.26520 1.26517 1.26520
2021-08-23 23:29:00+00:00 2 True 1.26517 1.26519 1.26517 1.26519
df['M5'].tail(3)
volume complete o h l c
time
2021-08-23 23:15:00+00:00 25 True 1.26506 1.26512 1.26506 1.26506
2021-08-23 23:20:00+00:00 8 True 1.26508 1.26524 1.26508 1.26521
2021-08-23 23:25:00+00:00 11 True 1.26518 1.26520 1.26513 1.26519
df['M15'].tail(3)
volume complete o h l c
time
2021-08-23 22:45:00+00:00 64 True 1.26474 1.26520 1.26472 1.26516
2021-08-23 23:00:00+00:00 64 True 1.26514 1.26534 1.26506 1.26508
2021-08-23 23:15:00+00:00 44 True 1.26506 1.26524 1.26506 1.26519
这是我正在做的事情的基本模板。
我不确定如何将我输入 'data' 的数据输入脑波,并且我不确定如何在我的策略中引用多个时间范围。
如有任何帮助,我们将不胜感激。
class firstStrategy(bt.Strategy):
def __init__(self):
self.rsi = bt.indicators.RSI_SMA(self.data.close, period=21)
def next(self):
if not self.position:
if self.rsi < 30:
self.buy(size=100)
else:
if self.rsi > 70:
self.sell(size=100)
#Variable for our starting cash
startcash = 10000
#Create an instance of cerebro
cerebro = bt.Cerebro()
#Add our strategy
cerebro.addstrategy(firstStrategy)
cerebro.adddata(data)
# Set our desired cash start
cerebro.broker.setcash(startcash)
# Run over everything
cerebro.run()
如果一个代码有多个时间范围,您可以使用最小的并在 backtrader 中重新采样:
class firstStrategy(bt.Strategy):
def __init__(self):
self.rsi1m = bt.indicators.RSI_SMA(self.data0.close, period=21) # rsi 1m
self.rsi5m = bt.indicators.RSI_SMA(self.data1.close, period=21) # rsi 5m
self.rsi15m = bt.indicators.RSI_SMA(self.data2.close, period=21) # rsi 15m
def next(self):
if not self.position:
if self.rsi1m < 30:
self.buy(size=100)
else:
if self.rsi1m > 70:
self.sell(size=100)
#Variable for our starting cash
startcash = 10000
#Create an instance of cerebro
cerebro = bt.Cerebro()
#Add our strategy
cerebro.addstrategy(firstStrategy)
data = bt.feeds.PandasData(
dataname=df['M1'],
open='o',
high='h',
low='l',
close='c',
openinterest=-1,
timeframe=bt.TimeFrame.Minutes,
compression=1,
)
# 1m data
cerebro.adddata(data, name='1m')
# 5m data
cerebro.resampledata(
data,
timeframe=bt.TimeFrame.Minutes,
compression=5,
name='5m'
)
# 15m data
cerebro.resampledata(
data,
timeframe=bt.TimeFrame.Minutes,
compression=15,
name='15m'
)
# Set our desired cash start
cerebro.broker.setcash(startcash)
# Run over everything
cerebro.run()
我需要将几个时间框架导入到一个交易策略中,但我不确定如何进行。
这是我的数据:
df['M1'].tail(3)
volume complete o h l c
time
2021-08-23 23:27:00+00:00 1 True 1.26520 1.26520 1.26520 1.26520
2021-08-23 23:28:00+00:00 2 True 1.26517 1.26520 1.26517 1.26520
2021-08-23 23:29:00+00:00 2 True 1.26517 1.26519 1.26517 1.26519
df['M5'].tail(3)
volume complete o h l c
time
2021-08-23 23:15:00+00:00 25 True 1.26506 1.26512 1.26506 1.26506
2021-08-23 23:20:00+00:00 8 True 1.26508 1.26524 1.26508 1.26521
2021-08-23 23:25:00+00:00 11 True 1.26518 1.26520 1.26513 1.26519
df['M15'].tail(3)
volume complete o h l c
time
2021-08-23 22:45:00+00:00 64 True 1.26474 1.26520 1.26472 1.26516
2021-08-23 23:00:00+00:00 64 True 1.26514 1.26534 1.26506 1.26508
2021-08-23 23:15:00+00:00 44 True 1.26506 1.26524 1.26506 1.26519
这是我正在做的事情的基本模板。
我不确定如何将我输入 'data' 的数据输入脑波,并且我不确定如何在我的策略中引用多个时间范围。
如有任何帮助,我们将不胜感激。
class firstStrategy(bt.Strategy):
def __init__(self):
self.rsi = bt.indicators.RSI_SMA(self.data.close, period=21)
def next(self):
if not self.position:
if self.rsi < 30:
self.buy(size=100)
else:
if self.rsi > 70:
self.sell(size=100)
#Variable for our starting cash
startcash = 10000
#Create an instance of cerebro
cerebro = bt.Cerebro()
#Add our strategy
cerebro.addstrategy(firstStrategy)
cerebro.adddata(data)
# Set our desired cash start
cerebro.broker.setcash(startcash)
# Run over everything
cerebro.run()
如果一个代码有多个时间范围,您可以使用最小的并在 backtrader 中重新采样:
class firstStrategy(bt.Strategy):
def __init__(self):
self.rsi1m = bt.indicators.RSI_SMA(self.data0.close, period=21) # rsi 1m
self.rsi5m = bt.indicators.RSI_SMA(self.data1.close, period=21) # rsi 5m
self.rsi15m = bt.indicators.RSI_SMA(self.data2.close, period=21) # rsi 15m
def next(self):
if not self.position:
if self.rsi1m < 30:
self.buy(size=100)
else:
if self.rsi1m > 70:
self.sell(size=100)
#Variable for our starting cash
startcash = 10000
#Create an instance of cerebro
cerebro = bt.Cerebro()
#Add our strategy
cerebro.addstrategy(firstStrategy)
data = bt.feeds.PandasData(
dataname=df['M1'],
open='o',
high='h',
low='l',
close='c',
openinterest=-1,
timeframe=bt.TimeFrame.Minutes,
compression=1,
)
# 1m data
cerebro.adddata(data, name='1m')
# 5m data
cerebro.resampledata(
data,
timeframe=bt.TimeFrame.Minutes,
compression=5,
name='5m'
)
# 15m data
cerebro.resampledata(
data,
timeframe=bt.TimeFrame.Minutes,
compression=15,
name='15m'
)
# Set our desired cash start
cerebro.broker.setcash(startcash)
# Run over everything
cerebro.run()