Detrended Price Oscillator (DPO)
Summary
Detrended Price Oscillator: the price a half-cycle back, less the moving average that spans the cycle. Removing the average removes the trend, leaving the shorter oscillation that the trend was hiding.
It crosses zero as price crosses its own average, so peaks and troughs mark the cycle rather than the direction of the market. The distance between successive peaks estimates the cycle length, and the amplitude is in price units, so it is comparable across time only for one instrument.
Chart
Chart metadata i
Outputs
outReal:line·display_shift·zero·positive·negative
Formula
Let t = optInTimePeriod / 2 + 1, an integer division, so a period and its odd successor share the same displacement.
DPO[i] = P[i - t] - SMA(P, optInTimePeriod)[i]
Notes
- The value is emitted at the bar whose moving average produced it. Charting packages usually draw it
tbars to the left instead, which is a plotting convention rather than a different series; a caller wanting that view shiftsoutRealitself, by the display shift the function reports,-t. - A causal variant,
P[i] - SMA(P, optInTimePeriod)[i - t], displaces the average instead of the price. It is a genuinely different series, not a re-indexing of this one, and is not implemented here.
Inputs
inReal— Source price/value series, canonically the close
Outputs
outReal— Detrended Price Oscillator value, in the units of the input
Parameters
| Parameter | Type | Default | Accepted values | Description |
|---|---|---|---|---|
optInTimePeriod | integer | 20 | 2–100000 | Number of bars spanned by the moving average being removed; the displacement is derived from it |
Properties
Numerical Stability: Start-Independent
| ☐ Overlap Input |
| ✅ Independent Y-Axis i |
| ☐ Candlestick |
| ✅ Display Shift i |
| ☐ Can Output NaN or ±Inf |
| ☐ Identity at Period 1 |
| ☐ Uses Transcendental |
| ☐ Input Must Be Positive |
Example
#include <stdio.h>
#include <ta_libc.h>
int n = 252;
double series[252]; /* your data, oldest first */
double out[252];
int outBegIdx, outNBElement;
TA_Initialize();
TA_DPO( 0, n - 1, series, 20, &outBegIdx, &outNBElement, out );
printf( "%d values from bar %d: [%.4f, ..., %.4f]\n", outNBElement, outBegIdx, out[0], out[outNBElement - 1] );use ta_lib::Core;
let series = vec![0.0; 252]; // your data, oldest first
let mut out = vec![0.0; 252];
let core = Core::new();
let range = core.dpo(0, series.len() - 1, &series, 20, &mut out)?;
println!("{} values from bar {}: [{:.4}, ..., {:.4}]", range.count, range.beg_idx, out[0], out[range.count - 1]);import io.github.talib.Core;
import io.github.talib.OutRange;
import java.util.Locale;
double[] series = new double[252]; // your data, oldest first
double[] out = new double[252];
Core core = Core.DEFAULT;
OutRange r = core.dpo(0, series.length - 1, series, 20, out);
System.out.printf(Locale.ROOT, "%d values from bar %d: [%.4f, ..., %.4f]%n", r.count(), r.begIdx(), out[0], out[r.count() - 1]);using System;
using TALib;
var series = new double[252]; // your data, oldest first
var outReal = new double[252];
var core = Core.Default;
OutRange r = core.Dpo(0, series.Length - 1, series, 20, outReal);
Console.WriteLine(FormattableString.Invariant($"{r.Count} values from bar {r.BegIdx}: [{outReal[0]:F4}, ..., {outReal[r.Count - 1]:F4}]"));import { dpo } from 'ta-lib';
const series = new Float64Array(252); // your data, oldest first
const { begIdx, values } = dpo(series, { timePeriod: 20 });
console.log(values.length + ' values from bar ' + begIdx + ': [' + values[0].toFixed(4) + ', ..., ' + values[values.length - 1].toFixed(4) + ']');import (
"fmt"
"log"
talib "ta-lib.github.io/go"
)
series := make([]float64, 252) // your data, oldest first
out := make([]float64, 252)
core := talib.NewCore()
r, err := core.DPO(0, len(series)-1, series, 20, out)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%d values from bar %d: [%.4f, ..., %.4f]\n", r.NBElement, r.BegIdx, out[0], out[r.NBElement-1])Implementation
Added in 0.8.1 (ta-lib-python 0.8.1).
TA-Lib Definition: dpo.c · dpo.yaml
| Native | File |
|---|---|
| C | ta_DPO.c |
| Rust | dpo.rs |
| Java | Core_DPO.java |
| C# | Core_DPO.cs |
| TypeScript | dpo.ts |
| Go | dpo.go |
Python: talib.DPO (ta-lib-python 0.8.1 or later), see ta-lib-python.
TA-Lib is also available for R and more using a wrapper.
Aliases
Detrended Price Oscillator
See Also
References
- Steven B. Achelis, Technical Analysis from A to Z, McGraw-Hill (p. 119)
- StockCharts ChartSchool: Detrended Price Oscillator