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/

    Apply multiple strategies

    General Code/Help
    2
    11
    6011
    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.
    • Maxim Korobov
      Maxim Korobov last edited by

      I have multiple strategies in my arsenal. Now I want to run them all together to find best points to enter and exit the market.
      For each strategy addstrategy called to cerebro new plot was added. How to plot all strategies action on one figure?

      1 Reply Last reply Reply Quote 0
      • B
        backtrader administrators last edited by

        The problem with the standard window managers in matplotlib is that window scrolling is not there and that's the main reason to plot strategies in different figures (or else every item of a chart would be crushed to very thing stripes)

        Your option, untested, is to execute inside a Jupyter Notebook. The automatic behavior is to activate inline plotting and the different figures should be plotted at least vertically aligned and the charts can be scrolled vertically.

        See the blog post which documented the introduction of the behavior:

        • https://www.backtrader.com/blog/posts/2016-09-17-notebook-inline/notebook-inline.html
        1 Reply Last reply Reply Quote 0
        • Maxim Korobov
          Maxim Korobov last edited by

          Why not add option to draw buy/sell point on one window and select which idicator to plot on it?

          1 Reply Last reply Reply Quote 0
          • B
            backtrader administrators last edited by

            You cannot do that as a default because the objects don't exist. It would have to be done manually afterwards on a per-line basis by assigning the master to which you want to plot to.

            1 Reply Last reply Reply Quote 0
            • Maxim Korobov
              Maxim Korobov last edited by

              Could you add more information about such solution?

              I see other idea to make one plot for many strategies at one cerebro run - create Strategy group class, which will be iterately asking each of grouped strategy about their actions. In theory, it sounds easy, but I feel a little pain, which comes when start to implement the idea.

              B 1 Reply Last reply Reply Quote 0
              • B
                backtrader administrators @Maxim Korobov last edited by

                @Maxim-Korobov said in Apply multiple strategies:

                Could you add more information about such solution?

                There is no information, it would have to be implemented from scratch.

                1 Reply Last reply Reply Quote 0
                • Maxim Korobov
                  Maxim Korobov last edited by

                  I found an idea:

                  • Create callback in base strategy, which is called when one strategy decided to buy/sell. Create parameter, which forbids strategy to make orders.
                  • Make Strategy aggregator class, which contains collection of strategies, make buy/sell actions when gets signals from more than one strategy included (possibly add weights for all of strategies).

                  When it'll done, I'll write result and possible PR on github.

                  1 Reply Last reply Reply Quote 0
                  • Maxim Korobov
                    Maxim Korobov last edited by Maxim Korobov

                    I tried the idea and stuck with substrategy instantiation inside __init__ of strategy class. Then I tried to split substrategy into substrategy + subindicator to use subindicator not by substrategy, but by someone else - strategy group.

                    Every works just fine except for subindicator plotting. Imagine hierarchy:
                    Strategy -> Custom logic indicator -> Basic indicator like SMA.

                    Basic indicators are plot one the separated subplots under the price subplot. How to add basic indicators back to price subplot?

                    B 1 Reply Last reply Reply Quote 0
                    • B
                      backtrader administrators @Maxim Korobov last edited by

                      @Maxim-Korobov

                      You seem to be looking for subplot. Docs - Plotting or depending on the complexity of the hierarchy may be also for plotmaster

                      1 Reply Last reply Reply Quote 0
                      • Maxim Korobov
                        Maxim Korobov last edited by Maxim Korobov

                        Tried with no success. SMA and BBands are already plotted over the data.

                        Look at the code:

                        class BBands(BaseStrategy):
                        ...
                            def __init__(self):
                        	super().__init__()
                        
                        	self.mode = self.params.mode
                        
                        	self.b_bands = bt.ind.BollingerBands()
                        	self.rsi = bt.ind.RelativeStrengthIndex(period=self.params.rsi, upperband=70, lowerband=30)
                        	# self.bb = BBandsIndicator(mode=self.params.mode)
                        ...
                        

                        Result:
                        oops

                        It's OK. Now disable b_bands, enable bb:

                        class BBands(BaseStrategy):
                        ...
                            def __init__(self):
                                super().__init__()
                        
                                self.mode = self.params.mode
                        
                                # self.b_bands = bt.ind.BollingerBands()
                                self.rsi = bt.ind.RelativeStrengthIndex(period=self.params.rsi, upperband=70, lowerband=30)
                                self.bb = BBandsIndicator(mode=self.params.mode)
                        ...
                        
                        class BBandsIndicator(bt.Indicator):
                            lines = ('open_position', 'close_position')
                        
                            def __init__(self):
                                self.mode = self.params.mode
                        
                                self.b_bands = bt.ind.BollingerBands()
                                self.rsi = bt.ind.RelativeStrengthIndex(period=self.params.rsi, upperband=70, lowerband=30)
                        

                        Result:
                        oops

                        BBandsIndicator added. But where is b_bands declared in BBandsIndicator?

                        1 Reply Last reply Reply Quote 0
                        • B
                          backtrader administrators last edited by

                          The problem is difficult to comprehend, because things seem to work like they:

                          Snippet1

                                  self.b_bands = bt.ind.BollingerBands()
                          	self.rsi = bt.ind.RelativeStrengthIndex(period=self.params.rsi, upperband=70, lowerband=30)
                          

                          Two (2) indicators are declared in the __init__ method of the Strategy and two (2) indicators show up in the char

                          • BollingerBands which defaults to plotting along the data (because the values from this indicator are meant to be in line with the value)
                          • RSI which defaults to be plotted on a subplot because it has fixed values ranging from 0 to 100

                          Snippet 2

                                  # self.b_bands = bt.ind.BollingerBands()
                                  self.rsi = bt.ind.RelativeStrengthIndex(period=self.params.rsi, upperband=70, lowerband=30)
                                  self.bb = BBandsIndicator(mode=self.params.mode)
                          

                          And again two (2) indicators there. The story of RSI is already known.

                          • BBandsIndicator is a direct descendent of Indicator and like most indicators the default is to plot on a subplot

                          Both charts are ok

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