Running multiple strategies individually
I found that if multiple strategies are added to cerebro, they will work on the total position instead of their own positions, which makes them interact with eachother.
Is there a switch or something so that I am able to let multiple strategies just handle the order and positions from their own? I just would like to see the result of simple sum of multiple strategies.
Position is maintained by broker - so if you need multiple strategies each working with its own broker - the option I see is to use multiple cerebro engine instances.
(I'm now aware of a switch or option supporting multiple brokers or assigning broker per strategy)
After checking the implement in strategy.py, I am afraid it is not easy to identify specific broker when buy or sell, even if multiple brokers are added to cerebro.
It seems that only one broker is active even if multiple brokers are added, is it true?
AFAIK there is no way to add multiple brokers to cerebro engine. The only method of adding a broker is
setbroker, which just sets the
_brokerinstance var in cerebro ( there is no even a space to store multiple brokers there )
def setbroker(self, broker): ''' Sets a specific ``broker`` instance for this strategy, replacing the one inherited from cerebro. ''' self._broker = broker broker.cerebro = self return broker
Alternatively you may try to cheat and add multiple data feeds (with the same data but different names), such that each strategy will know which data it is supposed to work on.
This way the cerebro's broker will maintain separate positions for each data feed.
However you still need to manage a common cash - which could be a showstopper depending on your needs.
Got it. Thank you.
BTW, if cerebro separate position and orders for each data feed, does it mean if different timeframe data of same instrument are added, the strategy cannot manage the total position or orders, but these of a single data?
( it is like the opposite problem of the original one)
We are talking only about built-in broker class used for backtracking. Real-live broker classes (Oanda, IB, ...) get the position information directly from the real broker , so it doesn't matter if there will be multiple data feeds of the same instrument with different time-frames - the orders will be processed for a specific instrument and the position will be managed appropriately.
As for the BackBroker class (built-in broker used for backtracking) - although I never tried to trade on multiple time-frames simultaneously myself - AFAIU the position is maintained per data feed. I'm not sure there is a code in Backtrader that could identify that different data feeds actually belong to the same instrument.
Probably more experienced members on this forum could prove otherwise and point us to the corresponding source code.