Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    Multi-timeframe position size

    General Code/Help
    1
    1
    88
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Z
      zetra_trader last edited by

      Hi All,

      I'm working on a multi-timeframe data example and I'm getting unexpected results for the position size. I set the buy size to the same fixed value for all timeframes. My expectation is that for each timeframe only the given size would be bought. However, this is not the case. I get different positions for each timeframe. Please see code and output below. Can someone please explain to me what I'm missing. Thanks.

      Code:

      import backtrader as bt
              
      class MyStrategy(bt.Strategy):
              def log(self, txt, dt=None, tm=None):
                  ''' Logging function fot this strategy'''
                  dt = dt or self.datas[0].datetime.date(0)
                  tm = tm or self.datas[0].datetime.time(0)
                  print('%s %s, %s' % (dt.isoformat(), tm.isoformat(), txt))
          
          
              def __init__(self):
                  
                  
      
                  self.all_ind = {}
                  
                  for i,d in enumerate(self.datas):
                      self.all_ind[i] = bt.talib.EMA(self.datas[i], period=10)    
                  
                              
              def next(self):
                   for i, d in enumerate(self.datas):
                        pos    = self.getposition(d).size
                        print("Position {} {}".format(pos,d._name))
                        if not pos:
                             if self.getdatabyname(d._name).close[0] > self.all_ind[i][0] :
                                 self.o = self.buy(data=d,size=10)
                                 
                              
                       
      if __name__ == '__main__':
          
          
          cerebro = bt.Cerebro()
      
          data0 = bt.feeds.GenericCSVData(timeframe=bt.TimeFrame.Minutes,dataname=".../DAT_ASCII_USDJPY_M1_2001.csv",                                  separator=';',
                                        nullvalue=float('NaN'),
                                        dtformat=('%Y%m%d %H%M%S'),
                                        tmformat=('%H%M%S'),
                                        datetime=0,
                                        time=-1,
                                        open=1,
                                        high=2,
                                        low=3,
                                        close=4,
                                        volume=-1,
                                        openinterest=-1,
                                        compression=1)
          
          cerebro.adddata(data0, name="data0")
          cerebro.resampledata(data0,name="data1", timeframe=bt.TimeFrame.Minutes,compression=15)    
          cerebro.resampledata(data0,name="data2", timeframe=bt.TimeFrame.Minutes,compression=30)    
      
          startcash = 10000
           
           # Set our desired cash start
          cerebro.broker.setcash(startcash)
      
           # Set the strategy
          cerebro.addstrategy(MyStrategy)
      
      

      Now the output:

      Position 10 data0
      Position 30 data1
      Position 40 data2
      Position 10 data0
      Position 30 data1
      Position 40 data2
      Position 10 data0
      Position 30 data1
      Position 40 data2
      Position 10 data0
      Position 30 data1
      Position 40 data2
      Position 10 data0
      Position 30 data1
      Position 40 data2
      Position 10 data0
      Position 30 data1
      Position 40 data2
      Position 10 data0
      Position 30 data1
      Position 40 data2
      Position 10 data0
      Position 30 data1
      Position 40 data2
      Position 10 data0
      Position 30 data1
      Position 40 data2
      Position 10 data0
      Position 30 data1
      Position 40 data2
      Position 10 data0
      Position 30 data1
      Position 40 data2
      
      
      1 Reply Last reply Reply Quote 0
      • 1 / 1
      • First post
        Last post
      Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors