Backtrader Community

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    1. Home
    2. Robin Dhillon
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/
    • Profile
    • Following 0
    • Followers 0
    • Topics 5
    • Posts 31
    • Best 7
    • Controversial 0
    • Groups 0

    Robin Dhillon

    @Robin Dhillon

    9
    Reputation
    365
    Profile views
    31
    Posts
    0
    Followers
    0
    Following
    Joined Last Online
    Location Canada

    Robin Dhillon Unfollow Follow

    Best posts made by Robin Dhillon

    • RE: I save in file it's ok, but I want disable the on-screen display ?

      Hello @mamos so there is a workaround this. You would need to make some changes to the original plot functions. I have this code written up for you. put this function at the end of your cerebro initialization and you should be good.

      def saveplots(cerebro, numfigs=1, iplot=True, start=None, end=None,
                   width=16, height=9, dpi=300, tight=True, use=None, file_path = '', **kwargs):
      
              from backtrader import plot
              if cerebro.p.oldsync:
                  plotter = plot.Plot_OldSync(**kwargs)
              else:
                  plotter = plot.Plot(**kwargs)
      
              figs = []
              for stratlist in cerebro.runstrats:
                  for si, strat in enumerate(stratlist):
                      rfig = plotter.plot(strat, figid=si * 100,
                                          numfigs=numfigs, iplot=iplot,
                                          start=start, end=end, use=use)
                      figs.append(rfig)
      
              for fig in figs:
                  for f in fig:
                      f.savefig(file_path, bbox_inches='tight')
              return figs
      
      saveplots(cerebro, file_path = 'savefig.png') #run it
      
      posted in General Code/Help
      Robin Dhillon
      Robin Dhillon
    • RE: Plot cerebro in a non-blocking way

      Hello @Rubén-Briones, if you are just trying to save the chart, here's the code:

          def saveplots(cerebro, numfigs=1, iplot=True, start=None, end=None,
                   width=16, height=9, dpi=300, tight=True, use=None, file_path = '', **kwargs):
      
              from backtrader import plot
              if cerebro.p.oldsync:
                  plotter = plot.Plot_OldSync(**kwargs)
              else:
                  plotter = plot.Plot(**kwargs)
      
              figs = []
              for stratlist in cerebro.runstrats:
                  for si, strat in enumerate(stratlist):
                      rfig = plotter.plot(strat, figid=si * 100,
                                          numfigs=numfigs, iplot=iplot,
                                          start=start, end=end, use=use)
                      figs.append(rfig)
      
                  # this blocks code execution
                  # plotter.show()
      
              for fig in figs:
                  for f in fig:
                      f.savefig(file_path, bbox_inches='tight')
              return figs
      
              cerebro.plot(style = 'candlestick')
      
          saveplots(cerebro,file_path = 'savefig.png')
      
      posted in General Discussion
      Robin Dhillon
      Robin Dhillon
    • RE: Plot (matplotlib?) Problem

      Hello Alain, I recommend uninstalling the Matplotlib module and try reinstalling it from the source itself as it default has the backend active. TkAgg usually has this error when it isnt active when you install the module on your system. If all else fails try installing with anaconda.

      posted in General Code/Help
      Robin Dhillon
      Robin Dhillon
    • RE: Plot (matplotlib?) Problem

      Just if you are curious, check out this documentation, outlines how to assign the backend needed without having to hardcode it every time in your imports. https://matplotlib.org/users/customizing.html#the-matplotlibrc-file

      posted in General Code/Help
      Robin Dhillon
      Robin Dhillon
    • RE: Make an Indicator off a Custom 'Line'

      I dont quite understand what you mean by using a custom data line, because you can literally use any line data as a input for your indicators. So to put this into context.
      suppose I have an indicator which depends on the SMA12 and SMA26 of a line dataset, lets call it cumulative_average.

      def __ init__ (self):
            sma12 = bt.indicators.SimpleMovingAverage(self.datas[0], period=12)
            sma26 = bt.indicators.SimpleMovingAverage(self.datas[0], period=26)
            cumulative_average = sma26 - sma12 + self.datas[0].close
            another_average = bt.indicators.SimpleMovingAverage(cumulative_average, period = 14)
      

      So basically you performed some arithmetic operation on your line data and assigned it to cumulative_average and found the SMA of the line data. Essentially you made a new line data which you can further implement in your next method.
      You can refer to the docs for more info.

      posted in General Code/Help
      Robin Dhillon
      Robin Dhillon
    • RE: Unable to get indicators to work

      You have to import the indicators as such:

      bt.indicators.SimpleMovingAverage(self.datas[0].close, period = 40)
      

      You will be benefitted greatly if you just took a look at the docs and navigate to the quickstart section.

      posted in General Code/Help
      Robin Dhillon
      Robin Dhillon
    • RE: Getting IndexError: array assignment index out of range in cerebro.run()

      I am not too sure what would the problem is here, however, I think it may be linked to your feeding of data. I would suggest taking a look at the values, see where the error might be.

      posted in General Code/Help
      Robin Dhillon
      Robin Dhillon

    Latest posts made by Robin Dhillon

    • RE: Plot cerebro in a non-blocking way

      Hello @Rubén-Briones, if you are just trying to save the chart, here's the code:

          def saveplots(cerebro, numfigs=1, iplot=True, start=None, end=None,
                   width=16, height=9, dpi=300, tight=True, use=None, file_path = '', **kwargs):
      
              from backtrader import plot
              if cerebro.p.oldsync:
                  plotter = plot.Plot_OldSync(**kwargs)
              else:
                  plotter = plot.Plot(**kwargs)
      
              figs = []
              for stratlist in cerebro.runstrats:
                  for si, strat in enumerate(stratlist):
                      rfig = plotter.plot(strat, figid=si * 100,
                                          numfigs=numfigs, iplot=iplot,
                                          start=start, end=end, use=use)
                      figs.append(rfig)
      
                  # this blocks code execution
                  # plotter.show()
      
              for fig in figs:
                  for f in fig:
                      f.savefig(file_path, bbox_inches='tight')
              return figs
      
              cerebro.plot(style = 'candlestick')
      
          saveplots(cerebro,file_path = 'savefig.png')
      
      posted in General Discussion
      Robin Dhillon
      Robin Dhillon
    • RE: Bracket Orders Behavior

      the purpose of a bracket order is as follows:

      1. A mainside order: Basically the set price you want to enter in, could be Limit Price or StopLimit order
      2. A lowside order: basically an order to make sure you get out a trade in the event it goes against your initial analysis
      3. A highside order: When you enter, you want to make sure that you get out in time, given the trade goes in the favorable decision

      All in all, it's not magic how these get executed, its all depends on which order gets hit first when you enter with your mainside order.

      posted in General Code/Help
      Robin Dhillon
      Robin Dhillon
    • RE: Do Operators use Lazy evaluation?

      AND refers to both the condition holding true/false and or refers to one of the conditions holding true/false.
      for example:

      val1 = 5.0
      val2 = 6.0
      if val1 > 2.0 and val2 < 10.0:
          print(True)
      

      this would fail if val2 was infact greater than 10.0
      for the 'OR' case:

      val1 = 5.0
      val2 = 6.0
      if val1 > 2.0 or val2 == 10.0:
          print('only condition one is true')
      
      posted in General Code/Help
      Robin Dhillon
      Robin Dhillon
    • RE: Taking the sign on a indicator signal

      Well, technically you can do that pretty easy. Make a custom indicator that follows the same formulation as your target one, and normalize it to output +1, if it goes above a certain threshold value and vice-versa for the -1. Take a look at the indicator reference and see how you can go about doing this.

      posted in General Code/Help
      Robin Dhillon
      Robin Dhillon
    • RE: Getting IndexError: array assignment index out of range in cerebro.run()

      I am not too sure what would the problem is here, however, I think it may be linked to your feeding of data. I would suggest taking a look at the values, see where the error might be.

      posted in General Code/Help
      Robin Dhillon
      Robin Dhillon
    • RE: I save in file it's ok, but I want disable the on-screen display ?

      Hello @mamos so there is a workaround this. You would need to make some changes to the original plot functions. I have this code written up for you. put this function at the end of your cerebro initialization and you should be good.

      def saveplots(cerebro, numfigs=1, iplot=True, start=None, end=None,
                   width=16, height=9, dpi=300, tight=True, use=None, file_path = '', **kwargs):
      
              from backtrader import plot
              if cerebro.p.oldsync:
                  plotter = plot.Plot_OldSync(**kwargs)
              else:
                  plotter = plot.Plot(**kwargs)
      
              figs = []
              for stratlist in cerebro.runstrats:
                  for si, strat in enumerate(stratlist):
                      rfig = plotter.plot(strat, figid=si * 100,
                                          numfigs=numfigs, iplot=iplot,
                                          start=start, end=end, use=use)
                      figs.append(rfig)
      
              for fig in figs:
                  for f in fig:
                      f.savefig(file_path, bbox_inches='tight')
              return figs
      
      saveplots(cerebro, file_path = 'savefig.png') #run it
      
      posted in General Code/Help
      Robin Dhillon
      Robin Dhillon
    • RE: I save in file it's ok, but I want disable the on-screen display ?

      Just dont plot. Write up the savefig method and not the cerebro.plot. Try to read into the docs to see the plotting features.

      posted in General Code/Help
      Robin Dhillon
      Robin Dhillon
    • RE: How do i check an order type?

      @hamid-zargar the best way to resolve your issue is to head to the docs. You can essentially add order info to whenever you are generating an order so for example in a long situation:

      long_order = self.buy()
      long_order.addinfo(name = 'long entry')
      

      or you can essentially add a log statement/print each time you are sending an order in next()that is long or short.
      either way, give the docs a good read.
      Cheers

      posted in General Code/Help
      Robin Dhillon
      Robin Dhillon
    • RE: Unable to get indicators to work

      You have to import the indicators as such:

      bt.indicators.SimpleMovingAverage(self.datas[0].close, period = 40)
      

      You will be benefitted greatly if you just took a look at the docs and navigate to the quickstart section.

      posted in General Code/Help
      Robin Dhillon
      Robin Dhillon
    • RE: Dynamically set params and lines attribute of class PandasData

      @lammy the initialization all depends on you so for example:

      I have my close prices loaded into BT as:
      self.dataclose = self.datas[0].close

      Now I my strategy buys the asset/instrument when (self.dataclose x 2) is greater than 'x' value.

      self.close2 = self.dataclose*2
      So essentially, all I did was take the pandas column and just explicitly times it by two and assigned it to another object self.close2

      Now you can take this object use it in your next() method and create your buy/sell orders

      posted in General Code/Help
      Robin Dhillon
      Robin Dhillon