Closed FrancisHChen closed 5 years ago
Closing as duplicate of #1. If you truly believe the calculation in the 3 references provided in ?DPO
are incorrect, please provide some evidence of the correct calculation.
I think the way TTR does is wrong. You didn't look at the details I provided.
The reference shows: DPO = Close - (Moving Average ((n/2)+1) days ago)
But what TTR does is using Moving Average ((n/2)+1) days in future!!!
I did look at the details you provided, and they're not the correct way to calculate the DPO indicator. Since you agree that all the references are correct, I'll quote some sections of the StockCharts reference.
Here they talk about the "displaced" moving average, which "centers" the moving average. Centered moving averages look into the future, by definition.
Displaced Moving Average The moving average displacement actually centers the moving average. Consider a 20-day simple moving average offset 11 days to the left. There are 10 days in front of the moving average, 1 day at the moving average and 9 days behind the moving average. In reality, this moving average is in the middle of its look-back period. Roughly half the prices used in the calculation are to the right and half are to the left.
They also explicitly say it is not designed for momentum signals because the displaced (centered) moving average is set in the past.
Using DPO Even though this indicator looks like a classic oscillator, it is not designed for momentum signals. The displaced moving average is set in the past and this is why the DPO is shown in the past.
Then they explicitly say that what you suggest defeats the purpose of the DPO indicator.
To Shift or not to Shift It is possible to displace the Detrended Price Oscillator (DPO) with a horizontal shift to the right. If DPO is set at 20, then an 11 period shift is needed to place it in line with the most recent price. This displacement number comes from the formula at the top (20/2 + 1) = 11. While shifting may seem like a good idea, it really defeats the purpose of this indicator, which is to identify cycles.
I see. Thanks! But now I think the three refereces are all different with each other. Totally confused.
https://www.metastock.com/customer/resources/taaz/?c=3&p=48 Formula: DPO = Close - (Moving Average ((n/2)+1) days ago)
http://www.fmlabs.com/reference/default.htm?url=DPO.htm Formula: DPO[i] = Close[i-1] - Mean(Close[i-n:i])
http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:detrended_price_osci displace MA n/2+1 days in the middle of close
I think the third way is only useful for visualization, otherwise it does not make sense technically.
Yes, the references are confusing. I should probably remove the MetaStock and FM Labs references, since they aren't internally consistent. The chart on the MetaStock page suggests a centered MA is used, but that is not clear from the text. The FM Labs text says, "Note that the calculation shifts the results (shift = term / 2 + 1) periods, so the last shift periods will be zero." The only way the last "shift" periods are zero are if you use a centered MA. But there's nothing in the formula that suggests the MA is centered.
Thanks for your reply.
I searched google scholar, and found out most papers (even though very few) used the first way (curent price - moving average n/2+1 days ago), which is based on this paper:
Achelis, S. B. (2001). Technical Analysis from A to Z. New York: McGraw Hill. http://freetradingdownloads.com/Technical%20Analysis%20from%20A%20to%20Z.pdf
cited 589 times.
The displacement way is very likely from this book:
Di Lorenzo, Renato. Basic Technical Analysis of Financial Markets: A Modern Approach. Springer Science & Business Media, 2013.
I looked at the origin. It says:
**15.7 Detrended Price Oscillator**
The detrended price oscillator is basically a SMA, i.e., a simple moving average,
materially shifted back along the time axis to eliminate the delay, just like a centered
average already seen in the first part of this treatise.
Therefore one needs to decide in advance how many bars (n) the cycles of the
security that we are considering can last, then he may materially take back along
the time axis the SMA of n/2+1 bars.
Obviously enough, we will not have any indication in the last part of the graph,
as it is evident from Fig. 15.24.
Some peaks have been highlighted, i.e., those perfectly matching both in the
oscillator chart and in the price chart, but… the last part, as said, does not report
any indication at all, and this makes such an oscillator not suitable for the construction
of a trading system whatsoever.
This explaination is almost the same as: http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:detrended_price_osci
Let's review this from StockCharts:
While shifting may seem like a good idea, it really defeats the purpose of this indicator, which is to identify cycles.
This DPO can be calculated by any n period and shift to past. If I can look into future, any cycle can be identified. Look at the e.g. figure for Nasdaq 100 ETF (QQQQ). Cycles in the past are perfect, but it ends with displaced SMA. Then there is no indication that whether recent prices are in a cycle or not. So it is not an technical indicator but more like a visualization tool.
Overall, I still doubt the effectiveness of this method.
Re-opening so I remember to update the documentation.
The way of calculating DPO: Detrended Price Oscillator is confusing!
For Simple Moving Average, if I have a series
v <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
ma <- TTR::SMA(v, n=5)
NA NA NA NA 3 4 5 6 7 8
It is calculated by rolling mean from 1:5 -> 5:10However, when I do DPO, it gives
TTR::DPO(v,n=5)
NA -1 -1 -1 -1 -1 -1 NA NA NA
It is calculated by: v[1:7] - ma[4:10], shifted by (n/2+1=3)It uses current price to substract future moving average. The correct way is: v[4:10] - ma[1:7], using current price to substract previous moving average, and the results should be:
NA NA NA NA NA NA NA 5 5 5
A real example here using TTR::SMA and TTR::DPO:
This looks into future.