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 differentsubplot
options. -
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 supportsubplot
because they are the target of such action (whenFalse
)Rather than developing a new indicator for that line, a quick solution can be:
- Creating a
SimpleMovingAverage
withperiod=1
of the line in question. - Setting
plotmaster=datax
for thatSimpleMovingAverage
Or
ApplyN
also withperiod=1
and afunc=lambda x: x[0]
(basically the identity function for the unique value in the array). See Docs - Indicators forApplyN
Plot master is also documented in Docs - Plotting
- Creating a
-
Or even better, use the functionality that was specifically created to support this use case:
LinePlotterIndicator
-
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!
-
@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.
-
Ok noted, thanks.