Apply multiple strategies



  • 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?


  • administrators

    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:



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


  • administrators

    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.



  • 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.


  • administrators

    @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.



  • 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.



  • 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?


  • administrators

    @Maxim-Korobov

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



  • 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?


  • administrators

    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


Log in to reply
 

Looks like your connection to Backtrader Community was lost, please wait while we try to reconnect.