简体   繁体   中英

import custom multitimeframe data into backtrader

I need to import several timeframes into a single trading strategy and I am unsure how to proceed.

Here is my data:

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

Here is a basic template for what im doing.

I am not sure how to get the data that i have into 'data' to stick into cerebro, and am not sure how to reference the multiple timeframes once its in my strategy.

Any help would be appreciated.

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()


If you have several timeframes from one ticker you can use smallest and resample it in 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM