It’s a repainting indicator, i.e. if it NOW detects a signal on a PAST candle, it goes back to that candle and plots the signal on it!
This is visually acceptable, but CAN’T be used in trading, as you cannor enter a trade back in the past!
I am attaching a custom version of DPO which returns the CURRENT value which CAN be used in automatic trading (BLACK line), it also plots the DPO as it is usually done, and as it is done by PRT (FUCHSIA line, which CAN’T be used in automatic trading).
Pic Dpo1 shows the repainting signals circled in ORANGE, while the CURRENT signal is circled in RED.
Pic Dpo2 shows where the REPAINTING signal is plotted and where it really is as CURRENT. There’s a 10-bar distance because the indicator was set with 10 periods., The more the periods, the bigger the gap, the less the periods the smaller the gap.
This is the code:
// DPO - Detrended Price Oscillator
//
// Formula and calculation:
//
// The DPO is calculated by subtracting the simple moving average over an "n" day period and shifted
// n/2+1 days back from the price.
//
// To calculate the detrended price oscillator:
//
// Decide on the time frame that you wish to analyze. Set "n" as half of that cycle period.
// Calculate a simple moving average for n periods.
// Calculate (n / 2 + 1)
// Subtract the moving average, from (n / 2 + 1) days ago, from the closing price:
// DPO = Close - Simple moving average [from (n / 2 + 1) days ago]
//
Periods = 21
MAtype = 0 //(sma)
Arrows = 1
Periods = max(1,min(999,Periods))
MAtype = max(0,min(8,MAtype))
src = CustomClose
n = round((Periods / 2) - 0.5) + 1
MyDPO = src[n] - Average[Periods,MAtype](src)
// Plot shifted DPO (somewhat like Ichimoku's Chikou)
DrawSegment(BarIndex - n -1,MyDpo[1],BarIndex - n,MyDPO) coloured(255,0,255,255) //Fuchsia
//
Bullish = close > open
Bearish = close < open
// If ARROWS are ticked then display signals
IF Arrows THEN
IF MyDPO CROSSES OVER MyDPO[n] AND Bullish THEN
DrawArrowUP(BarIndex,MyDPO) coloured(0,255,0,180) //Green
ENDIF
IF MyDPO CROSSES UNDER MyDPO[n] AND Bearish THEN
DrawArrowDOWN(BarIndex,MyDPO) coloured(255,0,0,180) //Red
ENDIF
ENDIF
//
RETURN MyDPO AS "DPO",0 AS "Zero"