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/

    The next() call isn't called.

    General Code/Help
    1
    1
    156
    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.
    • T H
      T H last edited by

      Hi there.
      Basically, I want to use 5 seconds dataframe, but I want to use 8 hours dataframe for only the indicator.
      But the next() call isn't called when I run the following code.(But The prenext() call is called.)
      Am i doing something wrong?

      Thank in advance!!

      
      class St(bt.Strategy):
          def __init__(self):
              self.rsi =rsi= bt.indicators.RelativeStrengthIndex(self.datas[1], upperband=80, lowerband=20)
              rsi.plotinfo.plot = False
              # rsi1 = rsi()
              # self.rsi_val = self.data0.close < rsi1.rsi
      
          def prenext(self):
              print("PRE-NEXT", self.data.close[0], self.rsi+0,
                      self.data.datetime.datetime().strftime('%m-%d %H:%M:%S'),
                      self.data1.datetime.datetime().strftime('%m-%d %H:%M:%S')
                    )
      
          def next(self):
              print("NEXT", self.data.close[0], self.rsi+0,
                      self.data.datetime.datetime().strftime('%m-%d %H:%M:%S'),
                      self.data1.datetime.datetime().strftime('%m-%d %H:%M:%S')
                    )
              
      def runstrategy():
          # Create a cerebro
          cerebro = bt.Cerebro()
      
          storekwargs = dict(
              token="xxx",
              account="xxx",
              practice=False
          )
      
          store = StoreCls(**storekwargs)
      
          DataFactory = store.getdata
          datakwargs = dict(
              timeframe=3, compression=5,
              qcheck=0.5,
              historical=False,
              fromdate=datetime.datetime(2020, 5, 1, 0, 0),
              todate=datetime.datetime(2020, 5, 2, 0, 0),
              bidask=None,
              useask=None,
              backfill_start=True,
              backfill=True,
              tz='Asia/Tokyo'
          )
      
          data0 = DataFactory(dataname="USD_JPY", **datakwargs)
          data1 = cerebro.resampledata(data0, timeframe=bt.TimeFrame.Seconds, compression=5)
          data2 = cerebro.replaydata(data1, timeframe=bt.TimeFrame.Minutes, compression=480)
      
          # Add the strategy
          cerebro.addstrategy(St)
      
          # cerebro.run(exactbars=args.exactbars, stdstats=False, runonce=False,quicknotify=True)
          cerebro.run(exactbars=args.exactbars)
       
          print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
          plt.rcParams['axes.unicode_minus']=False
          plt.rcParams['figure.figsize']=[18, 16]
          plt.rcParams['figure.dpi']=200
          plt.rcParams['figure.facecolor']='w'
          plt.rcParams['figure.edgecolor']='k'
          cerebro.plot(iplot=False, style="candle")
      
      runstrategy()
      

      output

      PRE-NEXT 106.92 nan 05-01 00:00:00 05-01 00:00:00
      PRE-NEXT 106.927 nan 05-01 00:00:05 05-01 00:00:05
      PRE-NEXT 106.946 nan 05-01 00:00:10 05-01 00:00:10
      PRE-NEXT 106.956 nan 05-01 00:00:15 05-01 00:00:15
      :
      :
      :
      
      
      1 Reply Last reply Reply Quote 0
      • 1 / 1
      • First post
        Last post
      Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors