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/

    LinesCoupler is plotted on different figure when passing line instead of data to SMA indicator

    General Code/Help
    1
    1
    48
    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.
    • P
      paul_lam last edited by

      I am currently trying to implement a strategy which uses a larger timeframe indicators. I started to learn from the "Mixing Timeframes in Indicators" article and have the following amended code to achieve the goal. In particular, I would like to have resample data using 7-day instead of month timeframe, and have a SMA indicator.

      import argparse
      
      import backtrader as bt
      import backtrader.feeds as btfeeds
      import backtrader.indicators as btind
      import backtrader.utils.flushfile
      
      
      class St(bt.Strategy):
          params = dict(multi=True)
      
          def __init__(self):
              self.sma = sma = btind.SMA(self.data1)
              sma.plotinfo.plot = True  # show plotting
      
              if self.p.multi:
                  sma1 = sma()  # couple the entire indicators
                  self.sellsignal = self.data0.close < sma1.sma
              else:
                  self.sellsignal = self.data0.close < sma.sma()
      
          def next(self):
              txt = ','.join(
                  ['%04d' % len(self),
                   '%04d' % len(self.data0),
                   '%04d' % len(self.data1),
                   self.data.datetime.date(0).isoformat(),
                   '%.2f' % self.data0.close[0],
                   '%.2f' % self.sma.sma[0],
                   '%.2f' % self.sellsignal[0]])
      
              print(txt)
      
      
      def runstrat():
          args = parse_args()
      
          cerebro = bt.Cerebro()
          data = btfeeds.BacktraderCSVData(dataname=args.data)
          cerebro.adddata(data)
          cerebro.resampledata(data, timeframe=bt.TimeFrame.Days, compression=7)
      
          cerebro.addstrategy(St, multi=args.multi)
      
          cerebro.run(stdstats=False, runonce=False)
          if args.plot:
              cerebro.plot(style='bar')
      
      
      def parse_args():
          parser = argparse.ArgumentParser(
              formatter_class=argparse.ArgumentDefaultsHelpFormatter,
              description='Sample for pivot point and cross plotting')
      
          parser.add_argument('--data', required=False,
                              default='../../datas/2005-2006-day-001.txt',
                              help='Data to be read in')
      
          parser.add_argument('--multi', required=False, action='store_true',
                              help='Couple all lines of the indicator')
      
          parser.add_argument('--plot', required=False, action='store_true',
                              help=('Plot the result'))
      
          return parser.parse_args()
      
      
      if __name__ == '__main__':
          runstrat()
      
      

      The following figure shows the result of plot and everything is working as excepted after amendments.
      figure01.png

      Since I would like to have a SMA of different prices such as open and high, I found this useful post, and I further changed
      self.sma = sma = btind.SMA(self.data1)
      into
      self.sma = sma = btind.SMA(self.data1.open)

      In that case, the LinesCoupler is plotted on 7-Day figure as shown below.
      figure02.png

      Although I believe that this may not affect my mogic and calculation, as I am able to verify values of lines from the print(txt), I would like to know if there is way to put LinesCoupler back to the first plot in order to better presentation.

      1 Reply Last reply Reply Quote 0
      • 1 / 1
      • First post
        Last post
      Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors