How to backtest a group of stocks?
-
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. -
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/8This is just one sample, but there are multiple others - try to search.
-
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
-
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.
-
You can also run optimization having data feed as an optimization parameter. In this case it will be single
cerebro
run. -
@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.
-
@ab_trader sounds like a good idea :) Have you tried it before? Thanks in advance.
-
I didn't try it, but seen somebody on the forum used this approach.
-
@ab_trader Any keywords ? then I could search it on this forum. Thanks :)
-
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 listdatafeeds
- 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. - add data feeds as usual into
-
@vladisld That's what I do... :)
-
@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.