Dist Ma

Overview of the Detrended Price Oscillator

This script is a custom technical analysis tool designed to help traders visualize price momentum by filtering out long-term "noise" and focusing on how current prices behave relative to a moving average. Instead of looking at raw price fluctuations, it provides a specialized count that reveals whether the market is showing persistent strength or weakness over a specific timeframe.

How the Logic Works

The core functionality of this indicator relies on three main steps:

1. Establishing the Baseline The script first identifies a long-term "average" price based on a user-defined period (the baseline). By calculating this moving average, the tool creates a smoothed representation of the market's trend, making it easier to see the "true" direction of the price rather than getting distracted by small, everyday ups and downs.

2. Detecting Trend Crosses The logic continuously monitors the relationship between the current closing price and that moving average. It specifically looks for "crossover" points—moments where the price moves from below the average to above it, or vice versa. These crossovers act as reset triggers, signaling a potential shift in market sentiment.

3. Momentum Counting Unlike standard oscillators that might just show a simple line, this script employs a counting mechanism:

  • When the price stays consistently above the moving average, the counter increases.
  • When the price stays consistently below the moving average, the counter also tracks that movement.
  • Whenever the price crosses the average (the "reset" mentioned above), the counter returns to zero and begins tracking the new direction from scratch.

What This Means for Traders

The resulting visual output is a representation of persistence.

A rising value on the indicator suggests that the price is not just briefly touching the average but is moving decisively and consistently above it. Conversely, a steady movement in the opposite direction indicates sustained downward pressure. Because the counter resets every time the price crosses the baseline, the indicator effectively "cleans" the data, allowing you to see how long a specific trend has been maintained without the interference of historical price data that is no longer relevant.

Indicators Used
Moving average indicator
Miscellaneous
Implements a curve of type %1
11 Views
0 Downloads
0 Favorites
Dist Ma
//+------------------------------------------------------------------+
//|                                  Detrended Price Oscillator.mq4  |
//|                                       Ramdass - Conversion only  |
//+------------------------------------------------------------------+

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 DodgerBlue
#property indicator_level1 500
#property indicator_levelcolor Red

//----
extern int x_prd = 800;
extern int CountBars = 15000;
//---- buffers
double dpo[];
double dpo1[];
//+------------------------------------------------------------------+
//| 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, MA, count, dir;
//----
   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)
     {
       MA=iMA(NULL,0,x_prd,0,MODE_SMA,PRICE_CLOSE,i);
      
           
      if (Close[i] > MA  && Close[i+1] < MA || Close[i] < MA && Close[i+1] > MA )
        count = 0;
      
      
           
      if (Close[i] < MA )
         count=count+1; 
       
              else count=0;
        
        
         
         if (Close[i] > MA)
         count = count +1;
        
        
                        
        dpo[i] = count; 
       i--;
     }
   return(0);
  }
//+------------------------------------------------------------------+

Comments