Multiple strategies on single data
-
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. -
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/)
-
@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. -
@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. -
@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 ?
-
@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.
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.
-
@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.
-
@chen-xu what is the reason not to implement this logic in a single strategy?
-
@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.
-
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.