joshuaulrich / TTR

Technical analysis and other functions to construct technical trading rules with R
GNU General Public License v2.0
326 stars 102 forks source link

DPO documentation is confusing #47

Closed FrancisHChen closed 5 years ago

FrancisHChen commented 7 years ago

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:10

However, 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:

    Date        Close   MA5      DPO5
1   2017-05-30  0.7465  NA       NA
2   2017-05-31  0.7430  NA      -0.00098
3   2017-06-01  0.7374  NA      -0.00740
4   2017-06-02  0.7443  NA      -0.00288
5   2017-06-05  0.7487  0.74398 -0.00194
6   2017-06-06  0.7506  0.74480 -0.00172
7   2017-06-07  0.7549  0.74718  0.00150
8   2017-06-08  0.7547  0.75064  0.00068
9   2017-06-09  0.7527  0.75232 -0.00204
10  2017-06-12  0.7541  0.75340 -0.00128
11  2017-06-13  0.7537  0.75402 -0.00356
12  2017-06-14  0.7585  0.75474  0.00006
13  2017-06-15  0.7579  0.75538 -0.00140
14  2017-06-16  0.7621  0.75726  0.00344
15  2017-06-19  0.7600  0.75844  0.00208
16  2017-06-20  0.7580  0.75930  0.00114
17  2017-06-21  0.7553  0.75866 -0.00126
18  2017-06-22  0.7542  0.75792  NA
19  2017-06-23  0.7568  0.75686  NA
20  2017-06-26  0.7585  0.75656  NA

This looks into future.

joshuaulrich commented 7 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.

FrancisHChen commented 7 years ago

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!!!

joshuaulrich commented 7 years ago

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.

FrancisHChen commented 7 years ago

I see. Thanks! But now I think the three refereces are all different with each other. Totally confused.

  1. https://www.metastock.com/customer/resources/taaz/?c=3&p=48 Formula: DPO = Close - (Moving Average ((n/2)+1) days ago)

  2. http://www.fmlabs.com/reference/default.htm?url=DPO.htm Formula: DPO[i] = Close[i-1] - Mean(Close[i-n:i])

  3. 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.

joshuaulrich commented 7 years ago

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.

FrancisHChen commented 7 years ago

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.

joshuaulrich commented 6 years ago

Re-opening so I remember to update the documentation.