For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See:

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?

  • administrators

    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 (when next is called) This is especially useful when using it as Benchmark (@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 when next is active.

    With that in mind and if TimeReturn is in use you could add an indicator to the strategy which forces prenext 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 with period=p, requires p 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:


    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?

  • administrators

    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 analyzer TimeReturn analyzer with your _doprenext of choice.

Log in to reply