Detrended Price Oscillator in Python — From Scratch (pandas)

Displacing the moving average, and the shift that trips people up

Detrended Price Oscillator computed in Python
Detrended Price Oscillator computed in Python

The Detrended Price Oscillator is a moving average and a shift — that’s genuinely it. The only thing to get right is the direction and size of the shift, which is exactly where implementations disagree. See the explainer.

What you’ll need

  • Python 3.9+, pandas/numpy
  • A close price series.

From scratch

import pandas as pd


def dpo(close, period=20):
    shift = period // 2 + 1
    sma = close.rolling(period).mean()
    # Non-centered: subtract the SMA value from `shift` bars ago.
    return (close - sma.shift(shift)).rename("dpo")

close.rolling(period).mean() is the N-period SMA; sma.shift(shift) displaces it (N/2 + 1) bars into the past; subtracting it from the current close leaves the detrended cycle — the line oscillating around zero in the output chart. For a 20-period DPO, shift is 11.

The shift is the whole game

Everything about the DPO lives in that shift(period // 2 + 1). Two mistakes are common:

  • Forgetting the + 1. The displacement is N/2 + 1, not N/2. It’s a small difference but it’s the definition — StockCharts, TradingView and most platforms use the +1.
  • Shifting the wrong way. sma.shift(+shift) pulls past SMA values to the present (non-centered). This is the real-time-plottable convention and what the chart above uses.

Centered vs non-centered

The version above is non-centered — it has a value on every bar including the latest, so it’s what you use for anything real-time. The centered convention shifts the whole DPO forward instead, centring it on its data but leaving the last (N/2 + 1) bars blank:

def dpo_centered(close, period=20):
    shift = period // 2 + 1
    sma = close.rolling(period).mean()
    return (close - sma).shift(-shift)   # plotted in the past; right edge is NaN

Neither is “wrong” — they’re display conventions. Just know which one your platform draws before comparing charts, because they don’t line up.

Estimating the cycle

The DPO’s real job is cycle length. A quick-and-dirty peak spacing:

from scipy.signal import find_peaks
d = dpo(df["close"], 20).dropna()
peaks, _ = find_peaks(d.values)
avg_cycle = pd.Series(peaks).diff().mean()   # average bars between DPO peaks

That average peak-to-peak distance is your estimated dominant cycle — the thing the DPO exists to find, and a parameter you can test in AlgoGen.

Gotchas

  • It’s not momentum. Don’t wire the latest DPO value into a real-time signal as if it were RSI; it’s a cycle/detrend tool.
  • shift = N//2 + 1. Mind the +1 and the integer division.
  • Right edge. Non-centered has a latest value but it’s still detrended, not predictive; centered leaves the right edge blank entirely.
  • SMA, not EMA. The standard DPO uses a simple moving average.

The lazy one-liner

import pandas_ta as ta
df["dpo"] = ta.dpo(df["close"], length=20, centered=False)

Same indicator elsewhere: MQL5, Pine Script, EasyLanguage, NinjaScript.


This post is educational, not financial advice. Indicators describe the past; they don’t predict the future. Backtest anything before you risk real money on it.

Sources and further reading

  1. Windowing operations (pandas documentation)

Historical research from the Algogen archive. Not investment advice.

Blog

Have a strategy idea?

Describe it in plain English, preview where your rules fire, then decide whether the evidence is worth a backtest.

Keep me postedHow it works