Full Strategy Model & Market Integration
-
Hi,
I am reasonably new to Backtrader, and I am a bit confused as to the best way to go about building a full model which can perform both backtesting, and live stock trading.
I have seen many examples on analysing individual stocks, and even assessing multiple stocks for buy sell recommendations (eg. @mementum post here: http://quant.stackexchange.com/questions/27559/open-source-software-for-stock-screening-and-scanning-using-technical-analysis
and this analyzer post here:https://www.backtrader.com/blog/posts/2016-08-15-stock-screening/stock-screening.html?highlight=tsla)However, I cant seem to find any good examples of a full strategy modeling, i.e. imagine a basic strategy where I buy/ sell when the close crosses a SMA...but how do I test that strategy over an entire stock universe, or even just the top 200 stocks, through history (say last 10-20 years) and model the performance of buying and selling specific amounts of each stock through time... to then get average portfolio return, culm returns, share ratios (annually, daily), etc etc. I believe Pyfolio may be a good option? but do not wish to invest the time if that is the wrong direction.
I then need this strategy to be able to be implemented in live trading (say interactive brokers).
Please note I am using my own stock data CSV file source.
Are there any large examples covering the above? Also lots of commentary to explain would be useful given I am new to this library!
Thank you!
CWSE -
@cwse said in Full Strategy Model & Market Integration:
.but how do I test that strategy over an entire stock universe, or even just the top 200 stocks
This is unclear. Do you have a strategy which looks at the entire stock universe and cherry picks or do you want to try the same approach on the entire stock universe and then select the stocks that happen to be better suited for the strategy?
-
Entire stock universe altogether
- You keep on adding stocks to
Cerebro
usingcerebro.adddata
until you exhaust the universe and thencerebro.run
. The strategy will of course be fairly complex in that you'll be looking at a huge number of assets and juggling them.
- You keep on adding stocks to
-
One at a time
- You create a
Cerebro
instance,adddata
once, add a strategy and thencerebro.run
- Go back to the previous step until you exhaust the universe
In both cases the analysis is about plugging in
Analyzers
:SharpeRatio
,TimeReturn
, logarithmic returns,VWR
,SQN
,TradeAnalyzer
and you can plug your own.The general analyzer documentation and reference
Specifically on
pyfolio
Several samples and blog posts use analyzer:
- Blog - Benchmarking
- Blog - MACD Settings
- Blog - Stock Screening
Which constructs a custom analyzer to gather the results - Blog - Variability Weighted Return
You can see also the following samples in the sources:
analyzer-annualreturn
sharpe-timereturn
writer-test
which also use performance measuring analyzers.
-
-
With regards to
live
implementation (to keep it separate). The platform makes no distinction between data feeds and also no distinction between brokers.You obviously need to load a different data feed when connecting to Interactive Brokers and plug-in the Interactive Brokers broke entity, to let the system know to stop using the broker simulation.
But the logic in the strategy doesn't know that you are getting data from different feeds and talking to a different broker.
But even with all that in mind you have to develop your logic taking into account that you will be connecting to a live feed. Example:
- Some people activate the
cheat-on-close
mode to buy theclose
price of daily bars in the simulation - That won't work in the reality, because the bar is closed. It may be handy to get an idea of what happens if you run the system 5 minutes before the end and decide to execute a manual order right before the market closes.
- Some people activate the
-
@backtrader thank you for the information. However, I am trying to find a full worked example which links all these steps. Everything I have seen is just piecemeal (including those stock screening blog posts). And the documentation on Analyzers doesn't have comprehensive examples.
Where you say: "In both cases the analysis is about plugging in Analyzers: SharpeRatio, TimeReturn, logarithmic returns, VWR, SQN, TradeAnalyzer and you can plug your own."
...are there any examples which cover ALL of these steps in one script?Also - is it reasonable to use JUST 'pyfolio' instead of the other backtrader analyzers? It appears pyfolio covers most these fundamentals?
Thank you!
CWSE -
@cwse
I will see about posting a complete system here to give an idea of what I am doing, however, below is my standard block of analyzers that I am putting in every strategy I am working on:# Own analyzerset cerebro.addanalyzer(bt.analyzers.TradeAnalyzer,) cerebro.addanalyzer(bt.analyzers.TimeReturn, timeframe=bt.TimeFrame.Years) cerebro.addanalyzer(bt.analyzers.SharpeRatio, timeframe=bt.TimeFrame.Years) cerebro.addanalyzer(bt.analyzers.SQN,) cerebro.addanalyzer(bt.analyzers.DrawDown)
I've looked at the output from pyfolio and frankly find it to be too much information. I'm sure there is some value in there, but if I get good numbers out of the above analyzer set, that is enough for me to take the next step.
I would also recommend taking a look in the samples directory at the pyfolio sample as it gives a pretty complete start as to how to use it and some of the other options.
-
@RandyT thank you, looking forward to an example! :-)
-
Probably is just some kind of "lost in translation", but the
pyfoliotest
sample provides you with a working example that buys, sells and has thepyfolio
analyzer plugged in and later fetched to look at the results.It seems a rather complete example. The rest of the analyzers are all present in the documentation and can be added and later fetched like
pyfolio
.And the other docs, samples that were pointed do the same with some other analyzers (trade to give the analyzer a meaningful role in the sample)
@randyt has added a list of those other analyzers, which are also present in other samples, but it's just a matter of taste which analyzers to use. Settling for
pyfolio
may be perfectly fine for you and not for other people. Yes it's rather complete and yet some people may not like that it doesn't work with sub-day data (at least it didn't when implemented) or because it doesn't implement what people are looking for. -
@backtrader, how do you get a backtrader strategy to assess ALL stocks added to the cerebro engine.. i.e. I loop through each CSV and adddata... then within the Strategy next() statement do I just refer to "datas" and it will asses each stock?
Or what if I have a specific list of stocks which I want to assess within the Strategy (out of the total universe of stocks which has been added already)?
I would much appreciate at least a snippet of sample code here! Thank you in advance!
CWSE
-
@cwse check out this post, it might help.
https://community.backtrader.com/post/967 -
@ab_trader Thanks! One more piece in the puzzle...
-
The choice as how to do it is yours. The
datas
array gives you access to the data feeds which were added to the system.The blog has a post with 2 data sources: https://www.backtrader.com/blog/posts/2015-09-03-multidata-strategy/multidata-strategy.html
-
So I can loop through all my data in the Next...
How do I plot the portfolio performance? instead of backtrader printing 1000 different charts...
Is there an integrated solution to do this in Backtrader? Haven't seen anything on it but it should be fundamental to any back testing system... example code would be appreciated!
Thank you,
CWE -
So I can loop through all my data in the Next..
self.datas
is an array and you can loop over it.How do I plot the portfolio performance? instead of backtrader printing 1000 different charts...
Depends on what you have defined as performance for your portfolio. There is for example a
TimeReturn
observer which is intended to plot exactly what the name implies.If you want to plot other performance indicator, you will need to define it as an indicator or observer
The default is to plot the evolution of the
cash
and the actual portfoliovalue
which should give a quick indication as to where the system isAnd only a single chart will be printed per strategy.
Is there an integrated solution to do this in Backtrader?
There are several samples, blog and documentation pages. See for example:
Here the performance is defined as the comparison against another asset.