Using multiple strategies runs only one?
duality_ last edited by duality_
cerebro.addstrategyto add a strategy and it seems to work correctly, here's using SMA crossover.
The problem is if I add two strategies. Even though one strategy is the same as before, none of the plots look the same as the one above.
Here's the app code plus the two strategies: link.
Any idea why this would be happening? Both strategies also finish with the same broker value.
Both strategies also finish with the same broker value.
It would be a surprise if they didn't. There is only one broker and it doesn't have different accounts.
The two strategies are running or else you wouldn't have 2 plots. You are probably staking too much in the belief that each strategy has the full amount of cash and runs separate from the other one.
You're right, I did think that each strategy would run separate from the others.
So for my use case where I'd like to test different strategies and plot their effectiveness in terms of PNL, Sharpe ratio, max drawdown etc., what's the best way to approach this? Do I have to create multiple
Cerebroobjects, each for one strategy? This would probably not be the best in terms of performance...
ab_trader last edited by
In the similar case I've done the following - set strategy object as a separate python module, set main script with
cerebroand data initialization with strategy import from strategy module, set ability to use strategy name as script argument. Then create
batfile like this:
python main.py strategy1 python main.py strategy2 python main.py strategy3
This run all strategies. I didn't use it with the
plotfunction, but it seems to me it should work also.
This would just run the strategies in a separate process. What I would like to do is run different strategies and compare the results programatically, not with manual inspection.
Sohail last edited by
@duality_ so create a function that takes in your strategy + params and run that way:
def runBacktest(strategyClass,**strategyArgs): ... cerebero.addstrategy(strategyClass,**strategyArgs) ... cerebro.run() ... return stats
ab_trader last edited by ab_trader
I was writing results from different strategy runs in the same text file in csv format as single row for single strategy. Then another script read that file, analyzed results and built diagrams.
Another possibility is to use the Strategy Selection pattern. See
Sohail last edited by Sohail
Basically it persists all configured observers and datas to a SQLite database. It's a little slow, but I need it for comparing live trades to backtests.
ab_trader last edited by
@sohail I think it will get good extension for backtrader.
Strategy Selection pattern looks like something I need. It is a bit convoluted that one needs to do this in the first place. It's not intuitive and code maintenance suffers. I think this use case is crucial for using Backtrader.
It's not intuitive and code maintenance suffers
Let me disagree. You have a unique Strategy producer in which you can plug any set of strategies for comparison. The pattern remain constant for your testing script and data feeds whilst you can change amongst several thousands of tests.
__new__to deliver a different object instance (usually related as it is the case) is a classing Python pattern and idiomatic.
It's okay from the Python's point-of-view, I don't see it intuitive from the library consumer point-of-view. Find me a new developer that will figure out by themselves what they need to do to compare different strategies using Backtrader.
Not a big deal though, now that I know it's not hard to use. Thanks so much for the help, appreciate it!
Find me a new developer
Nobody had said that things are for new developers.
what they need to do to compare different strategies using Backtrader
You run one strategy and write down the results. You run the next strategy and write down the results. And you then compare the results. (writing down can be be to paper, to a file, to a database, to a socket ...)
Running multiple strategies simultaneously from a single source to compare them doesn't really seem suited for new developers.
I mean new Backtrader developers, not developers in general. I'm still struggling to get something that I would call basic running, namely running multiple strategies on multiple markets and comparing the results of the strategies. This use case is what brought me into Backtrader.