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.