Indicators - different subplot values for different lines



  • Hello

    I have developed an indicator that can provide signals when a price hits a simple trend line.

    My signal line is currently on a subplot . However I would also like to plot my trend line on the main plot so I can easily see afterwards what is going on.

    In other words:

    Signal Line: subplot = True
    Trend Line: subplot = False

    I didn't spot anything in the line specific plotting options in the docs (https://www.backtrader.com/docu/plotting/plotting.html)

    Thanks!



  • @ThatBlokeDave from my understanding subplot is applicable to whole indicator, observer or data, but not to the particular line. So looks like you may want to have two indicators - trend and signal (which can call trend indicator inside) and plot them with different subplot options.


  • administrators

    As pointed out by @ab_trader, subplot only applies to what one could call first class citizens, i.e.: objects that hold lines themselves (Indicators and Observers) Data Feeds are also first class citizens, but they don't support subplot because they are the target of such action (when False)

    Rather than developing a new indicator for that line, a quick solution can be:

    • Creating a SimpleMovingAverage with period=1 of the line in question.
    • Setting plotmaster=datax for that SimpleMovingAverage

    Or ApplyN also with period=1 and a func=lambda x: x[0] (basically the identity function for the unique value in the array). See Docs - Indicators for ApplyN

    Plot master is also documented in Docs - Plotting


  • administrators

    Or even better, use the functionality that was specifically created to support this use case: LinePlotterIndicator

    See: Docs - Using Indicators



  • @ab_trader @backtrader

    Thanks for the suggestions.

    I managed to get the SimpleMovingAverage suggestion working by creating the SMA inside the strategy and feeding it the line.

    I also tried applying the SMA idea inside my custom indicators __init__ method but could not manage to get anything to plot. I guess I cannot plot indicators within indicators (even though the SMA is calculated fine.)

    It would be nice to be able to get the line to plot from within my custom indicator code if possible. So I do not need to create a second indicator in the strategy.

    I also failed in my attempts to get a the LinePlotterIndicator working. I think that is because I am not performing a calculation on the lines themselves. I am simply extracting the date from the line, converting it to a timestamp and calculating Y (price on the chart) from the slope and offset that is calculated during the indicators __init__ from values passed to it.

    Having said that, It is late here and it is likely I am misinterpreting the docs!


  • administrators

    @ThatBlokeDave said in Indicators - different subplot values for different lines:

    I also tried applying the SMA idea inside my custom indicators init method but could not manage to get anything to plot. I guess I cannot plot indicators within indicators (even though the SMA is calculated fine.)

    That cannot be done. Strategies plot any indicators directly created by itself. And Indicators plot the lines they have.



  • @backtrader

    Ok noted, thanks.


Log in to reply
 

Looks like your connection to Backtrader Community was lost, please wait while we try to reconnect.