How to let a Strategy trade on multiple/all datas in the mix?
Let's say I wrote a simple strategy that uses indicators and is meant to trade on a single data (first data in the mix:
self.data). So the normal use case is a mix that contains this one strategy and one data.
But now I want to let the strategy act upon a whole bunch of datas, let's say, 30. Important for me is that I can run the optimization in an efficient way also.
What would be the best way to do this?
Two ideas that I had myself but I did not like alot:
Add the same strategy multiple times: Add a parameter named
datato the strategy so I can pass an instance of the
dataI want each strategy instance to trade on. Then I can add the strategy once for every data I want it to trade using regular
addstrategycalls (everytime with a different value for
1 - The strategy code has to be modified to support the external
dataparameter. I would prefer it if "vanilla" strategies could be used without the need to add special behavior.
2 - when optimizing the parameter combinations will not be "aggregated" as each instance of the strategy will be treated independently. So the number of results/calculations will go through the roof. I would like that every optimization run uses the same parameter set for every strategy instance. So it gives the same number of result when trading on 1 data or on 10 data.
Modify the strategy so that it acts/trades on every data in the mix. The strategy has only to be added to the mix once.
Optimization will work well as we have only a single strategy in the mix and cerebro can run the optimization as usual
The strategy will need to be heavily modified. I would like to avoid pulling the logic of handling multples datas into the strategy. It would be great if the strategy would only have to know about how to trade on a single data.
Well, I hope I was able to make myself clear. :)
So what would be the best and cleanest way to let a strategy act upon all datas in the mix in parallel and also have the optimization work like there was only a single data?
Your goal is to have optimization to be done on all datafeeds at the same time. I really don't see any other options except option #2.
Modiying of the logic for multiple datafeeds is not that complex, just add one more loop. And modified code will cover both single and multiple data runs. More flexibility.
Thanks for your input. Yes it seems to be the best way at the moment.
Also tried with some sort of intermediate strategy that basically acts as a dispatcher that instantiates the underlying "real" strategies and feeds them accordingly.
Would be used as this:
cerebro_train.optstrategy(build_multidata_strategy(DipExploiter), stop_loss=np.arange(0.02, 0.15, 0.20), profit_target=np.arange(0.03, 0.12, 0.18), dip_thresh=np.arange(-0.01, -0.5, -0.2), adx_trend_thresh=np.arange(30, 60, 50), adx_period=np.arange(12, 16, 10) )
build_multidata_strategywould construct a strategy class at runtime. It would also "fake"
self.datasfor the underlying strategies so each strategy would only see one data and could act upon
self.dataas usual. But I could not get this to work (indicators were not called). I guess the problem is related to the metaclasses that are used by the strategies. I guess constructing strategy objects within other strategies is not supported.
backtrader administrators last edited by
Don't know that your
build_multidata_strategydoes, but there are two post detailing how to this (one or multiple data feeds is not important)
Thank you, as I understand it, that approach can handle multiple strategies by interpreting them as one optimization parameter. But frankly I did not understand how to use that or modify it so it could be used to let a single strategy work on all datas in the mix in a single run (and get an aggregated result over all datas).
I thought about using it to instantiate always the same strategy so that every instance could trade on different data but then they would still be individual runs and I would not get aggregated results. Maybe I just fail to see the relation, sorry :(
backtrader administrators last edited by
@vbs said in How to let a Strategy trade on multiple/all datas in the mix?:
as I understand it, that approach can handle multiple strategies by interpreting them as one optimization parameter
No. It delivers the strategy that is going to be delivered on the fly, i.e.: it is a strategy factory. Overriding the
__new__method of a class to deliver the instance of a different class is a standard Python idiom.
Huh? I think that this is what I mean too. Based on the parameter that is part of the optimization, on every optimization iteration a different strategy might be instantiated (depends on the value of the parameter). While that is a very interesting approach I fail to see how I could use that with my problem.