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/

    Multiple strategies on single data

    Indicators/Strategies/Analyzers
    4
    11
    2734
    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.
    • Yury Tsar
      Yury Tsar last edited by

      Hi,
      does anyone have idea how to implement different strategies on single data feed ?
      For example if I want to do backtesting with different stop loss % and get results through analyzers.

      B 1 Reply Last reply Reply Quote 0
      • vladisld
        vladisld last edited by

        If your strategies are completely different you may just add all of them to cerebro - each one will be given the same data feed upon start, also each strategy's next method will be called once for each update in the data feed.
        Alternatively you may use a more advanced technique described here: (https://www.backtrader.com/blog/posts/2016-10-29-strategy-selection/strategy-selection/) and here: (https://www.backtrader.com/blog/posts/2017-05-16-stsel-revisited/stsel-revisited/)

        In case the only difference between your strategies is in a single parameter (% stop loss) you may set it as a parameter of the same strategy like:

        class MyStrategy(bt.Strategy):
            params = (
                ('stop_loss_percent', 0.96),
            )
        

        and use the cerebro.optstrategy instead of using cerebro.addstrategy. The 'optstrategy' method accepts a range for each strategy's parameter and will instantiate the same strategy for each value of the parameter in the above range. See more info here: (https://www.backtrader.com/docu/cerebro/)

        Yury Tsar 2 Replies Last reply Reply Quote 2
        • Yury Tsar
          Yury Tsar @vladisld last edited by

          @vladisld
          It doesn't really work.
          Strategies exactly same, just with different stop loss.
          In case as you described, Cerebro will share same position across multiple strategies.

          B 1 Reply Last reply Reply Quote 0
          • Yury Tsar
            Yury Tsar @vladisld last edited by

            @vladisld
            question was how to run them simultaneously, not using iteration 1 after another.
            If you are working with very big dataset of OHLC you want to avoid candles iteration as much as possible, if only stop loss pct is different.

            vladisld 1 Reply Last reply Reply Quote 0
            • vladisld
              vladisld @Yury Tsar last edited by

              @Yury-Tsar said in Multiple strategies on single data:

              @vladisld
              question was how to run them simultaneously, not using iteration 1 after another.

              This wasn't clear from the original question:

              @Yury-Tsar said in Multiple strategies on single data:

              how to implement different strategies on single data feed

              In case you would like to backtest the same strategy with a different parameters, where each run of the strategy will have its own position, and all of them will be run simultaneously, but still having the same "big OHLC" data loaded just once - this is exactly were the cerebro.optstrategy comes to rescue. Cerebro engine could be setup to preload the data feed (so that it will be read just once) and each strategy instance will run in its own process - all of them running simultaneously as you wished.

              Am I still getting your question wrong ?

              1 Reply Last reply Reply Quote 2
              • B
                backtrader administrators @Yury Tsar last edited by

                @Yury-Tsar said in Multiple strategies on single data:

                For example if I want to do backtesting with different stop loss % and get results through analyzers

                You obviously want to optimize.

                • https://www.backtrader.com/docu/quickstart/quickstart/#lets-optimize

                The data (if pre-loadable) is only loaded once and shared across the strategies, which will in turn use all your cores unless you explicitly disable that behavior.

                The data has to be iterated because there is ML (Magic Logic) or AI (Awesome Implementation) which can do it otherwise.

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

                  @Yury-Tsar said in Multiple strategies on single data:

                  In case as you described, Cerebro will share same position across multiple strategies.

                  What @vladisld pointed you to won't share the same position across multiple strategies. You may want to read the articles in detail. It doesn't in case apply to your single stop-loss optimization goal.

                  1 Reply Last reply Reply Quote 0
                  • Chen Xu
                    Chen Xu last edited by

                    Thanks for the tips.

                    But what about another use case:

                    • one data feed say SPY

                    • two strategies trading it in a same cerebro instance thus about one broker account: SMA and RSI

                    • a mother sizer decide X% goes to SMA and the rest 1-X% goes to RSI , by, say, maximizing the rolling Sharpe ratio by tracking the returns from SMA and RSI from last n periods.

                    vladisld 1 Reply Last reply Reply Quote 0
                    • vladisld
                      vladisld @Chen Xu last edited by

                      @chen-xu what is the reason not to implement this logic in a single strategy?

                      Chen Xu 1 Reply Last reply Reply Quote 0
                      • Chen Xu
                        Chen Xu @vladisld last edited by

                        @vladisld thanks for the reply and keeping leading the work to make BT(2) alive!

                        The reason is b/c of that I need to track the position, value, and etc for each separate strategy so that I can solve some optimization problem to decide optimally how much of my capital needs to go to each strategy.

                        I am trying to use the wonderful trackers, say analyzers and observers, already built in BT, which is currently more for a whole broker account and thus for one cerebro instance.

                        I am actually already working on building these as some sub-strategy's under one strategy and create a few class elements under the mother strategy to track the position, value, return and etc for each separate sub-strategy. There is also an added benefit: when there is some cross-over order i.e. one substrategy try to sell and some other substrategy try to buy the same stock then I don't need to place the order for them at all. (kind of I trade with myself)

                        I am also curious, what is the use case(s) for adding two strategies to one cerebro instance? I understand in the optstrategy() case it is more for the purpose of speeding things up by loading data and iterating over bars once.

                        Chen Xu 1 Reply Last reply Reply Quote 0
                        • Chen Xu
                          Chen Xu @Chen Xu last edited by

                          @chen-xu

                          need to correct myself: analyzers are coordinated by (cerebro, strategy), not associated with the whole cerebro only. But analyzers are not lines so it does not fulfill my use case as I need to track last n bars metrics to do my optimization.

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