DPO
Download scriptThe Detrended Price Oscillator (DPO) is a technical analysis tool designed to help traders identify price cycles and momentum by stripping away the long-term "trend" from the current market data. In simpler terms, it isolates the short-term fluctuations of an asset's price, making it easier to see whether the market is currently overextended or if it is returning to a mean.
Core Logic Overview
Most trading indicators try to show you where the price is going in the long run. The DPO does the opposite: it removes that long-term direction to show you what is happening "right now" relative to the recent past. It achieves this through a specific three-step mathematical process:
- Price Tracking: The script monitors the closing price of an asset over a user-defined period (defaulting to 14 units).
- Trend Identification: It calculates a Simple Moving Average (SMA) based on that same period. This average represents the "baseline" or the general trend of the price during that timeframe.
- The Calculation (Detrending): The script subtracts this moving average from the current closing price. By subtracting the "trend" from the "actual price," the resulting value is an oscillator.
How to Interpret the Results
Because the tool removes the upward or downward tilt of a trend, the resulting line oscillates around a zero point:
- Positive Values: When the indicator moves above the zero line, it signifies that the current price is trading higher than its recent moving average (bullish momentum).
- Negative Values: When the indicator moves below the zero line, it signifies that the current price is trading lower than its recent moving average (bearish momentum).
- Cycle Identification: For traders, this is particularly useful for identifying "peaks" and "troughs" in cyclical markets. It helps visualize when a price movement has become exhausted relative to its average path.
User Customization
The tool allows for two primary adjustments to refine the analysis:
- Period (x_prd): This determines how far back the script looks to calculate the trend. A shorter period makes the indicator more sensitive to immediate price changes, while a longer period smooths out the data to highlight broader cycles.
- Data Visibility (CountBars): This manages how much historical data is rendered on your screen at one time, ensuring the chart remains clean and focused on the most relevant timeframe.
//+------------------------------------------------------------------+
//| Detrended Price Oscillator.mq4 |
//| Ramdass - Conversion only |
//+------------------------------------------------------------------+
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 DodgerBlue
//----
extern int x_prd = 14;
extern int CountBars = 300;
//---- buffers
double dpo[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
string short_name;
//---- indicator line
SetIndexStyle(0, DRAW_LINE);
SetIndexBuffer(0, dpo);
//---- name for DataWindow and indicator subwindow label
short_name = "DPO(" + x_prd + ")";
IndicatorShortName(short_name);
SetIndexLabel(0, short_name);
//----
if(CountBars >= Bars)
CountBars = Bars;
SetIndexDrawBegin(0, Bars - CountBars + x_prd + 1);
//----
return(0);
}
//+------------------------------------------------------------------+
//| DPO |
//+------------------------------------------------------------------+
int start()
{
int i, counted_bars=IndicatorCounted();
double t_prd;
//----
if(Bars <= x_prd)
return(0);
//---- initial zero
if(counted_bars < x_prd)
{
for(i = 1; i <= x_prd; i++)
dpo[CountBars-i] = 0.0;
}
//----
i = CountBars - x_prd - 1;
t_prd = x_prd / 2 + 1;
//----
while(i >= 0)
{
dpo[i] = Close[i] - iMA(NULL, 0, x_prd, t_prd, MODE_SMA, PRICE_CLOSE, i);
i--;
}
return(0);
}
//+------------------------------------------------------------------+
Comments