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/

    How to backtest a group of stocks?

    General Code/Help
    4
    12
    563
    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.
    • B
      bilibili last edited by

      Hi, It seems only one stock could be backtested at one time. how to backtest a stock group?
      For example, all NYSE stocks in a backtest? Thanks in advance.

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

        External blog:

        https://backtest-rookies.com/2017/08/22/backtrader-multiple-data-feeds-indicators/

        Internal blog:
        https://www.backtrader.com/blog/posts/2017-04-09-multi-example/multi-example/

        Community post(s):
        https://community.backtrader.com/topic/2032/manage-multiple-assets/8

        This is just one sample, but there are multiple others - try to search.

        1 Reply Last reply Reply Quote 4
        • B
          bilibili last edited by

          Thanks for your help. but it seems that is not what i wanted.
          I want to execute the same strategy on different stocks, then we could evaluate the strategy on each stock.
          for example, we have strategy A and stock group {stock 1, stock 2, stock 3...}
          I want something like this.
          init cash=1000, -> strategy A -> stock 1 final cash ? max money draw?
          init cash=1000, -> strategy A -> stock 2 final cash ? max money draw?
          init cash=1000, -> strategy A -> stock 3 final cash? max money draw?

          I don't want to distribute init cash 1000 to several stocks and calculate the final return.
          But, these blogs tell us to add data at the same time, and cerebro will append each data.
          that means, we have to buy/sell multi-stocks in one day. that is not a group of stock backtest. It's more like position management. Please refer to adddata below.

          def adddata(self, data, name=None):
              '''
              Adds a ``Data Feed`` instance to the mix.
          
              If ``name`` is not None it will be put into ``data._name`` which is
              meant for decoration/plotting purposes.
              '''
              if name is not None:
                  data._name = name
          
              data._id = next(self._dataid)
              data.setenvironment(self)
          
              self.datas.append(data)
              self.datasbyname[data._name] = data
              feed = data.getfeed()
              if feed and feed not in self.feeds:
                  self.feeds.append(feed)
          
              if data.islive():
                  self._dolive = True
          1 Reply Last reply Reply Quote 0
          • vladisld
            vladisld last edited by

            In this case, I'd just run the whole Cerebro logic for each stock separately ( in a loop) - at least that's what I'm doing in my project.

            Probably others may have better advice.

            B run-out 3 Replies Last reply Reply Quote 3
            • A
              ab_trader last edited by

              You can also run optimization having data feed as an optimization parameter. In this case it will be single cerebro run.

              B 1 Reply Last reply Reply Quote 4
              • B
                bilibili @vladisld last edited by

                @vladisld I've tried the same way, but as the "adddata" code above, cerebro will append new data each time. then, It seems we can not run a single data cerebro each time.

                1 Reply Last reply Reply Quote 0
                • B
                  bilibili @ab_trader last edited by

                  @ab_trader sounds like a good idea :) Have you tried it before? Thanks in advance.

                  1 Reply Last reply Reply Quote 0
                  • A
                    ab_trader last edited by

                    I didn't try it, but seen somebody on the forum used this approach.

                    B 1 Reply Last reply Reply Quote 0
                    • B
                      bilibili @ab_trader last edited by

                      @ab_trader Any keywords ? then I could search it on this forum. Thanks :)

                      1 Reply Last reply Reply Quote 0
                      • A
                        ab_trader last edited by

                        I don't remember. The approach is approximately the following:

                        • add data feeds as usual into cerebro at the same time adding them into the list datafeeds
                        • strategy will have parameter dfeed
                        • in the strategy you use self.p.dfeed as a data feed for orders and indicator development
                        • then you call cerebro.optstrategy(YourStrategyName, dfeed=dfeeds)

                        Also you can use list of data feed names, pass them as parameter, and use getdatabyname to retrieve data feed in the strategy.

                        1 Reply Last reply Reply Quote 1
                        • run-out
                          run-out @vladisld last edited by

                          @vladisld That's what I do... :)

                          1 Reply Last reply Reply Quote 1
                          • B
                            bilibili @vladisld last edited by

                            @vladisld said in How to backtest a group of stocks?:

                            Cerebro logic for each stock separately ( in a loop)

                            It works! I misunderstood what you meant. I'm just an idiot without your help. Thanks for your help.

                            1 Reply Last reply Reply Quote 0
                            • 1 / 1
                            • First post
                              Last post
                            Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
                            $(document).ready(function () { app.coldLoad(); }); }