Control the Analyzer to start
-
I have 3 years of data adding to the cerebro. I only interest on the performance from the 2nd year to the end of the data. Is there a way to tell the Analyzer to start from the 2nd year?
-
Without knowing the details it would seem that even if you want the analyzer (which?) to look at the data from the 2nd and later years, you are actually doing something (trades) during the 1st year.
Analyzers are actually looking all the time at whatever it happens in the system (for example at the current value of the portfolio to calculate returns)
TimeReturn
provides a parameter (_doprenext
) to actually only calculate returns from the point at which the strategy could formally start trading (whennext
is called) This is especially useful when using it asBenchmark
(@dimitar-petrov found an issue with resampled data and this hook a couple of days ago), because benchmarking against a data or some other asset only happens whennext
is active.With that in mind and if
TimeReturn
is in use you could add an indicator to the strategy which forcesprenext
to be called until the end of the 1st year. Setting_doprenext=False
would achieve the effect.Yes, the problem is that such an indicator which may for for example be
SimpleMovingAverage
withperiod=p
, requiresp
to be calculated in advance at the start for it to be completely generic (you can always set a fixed value if you use the same data feed several times), which is not possible if the code has to work with data feeds of unknown length. -
@backtrader said in Control the Analyzer to start:
_doprenext
Thank you very much. _doprenext=False is working in my case. Is it also apply to other built-in analyzer ? it looks like analyzers.SharpeRatio does not support _doprenext. Any suggestion?
-
The
SharpeRatio
analyzer doesn't do rolling calculations, it does the calculation when everything is over.You can subclass it, override
__init__
and create the sub analyzerTimeReturn
analyzer with your_doprenext
of choice.