Step VHF adaptive VMA

Download script
Author: © mladen, 2018

This script is designed as a sophisticated visual tool for financial charting, specifically built to help traders identify market trends while filtering out unnecessary "noise." It combines several advanced mathematical concepts—specifically adaptive smoothing, volatility measurement, and price quantization—into a single cohesive indicator.

Core Logic Breakdown

1. Volatility Awareness (VHF Component) The script doesn't just look at the price; it looks at how much the price is moving relative to its recent history. It calculates the difference between the highest and lowest prices over a specific period and divides that by the "noise" (the total cumulative movement) of the market. This helps the indicator understand whether the market is in a state of high volatility or a quiet consolidation.

2. Adaptive Smoothing Unlike standard moving averages that treat all price data equally, this script uses an adaptive approach. By incorporating the volatility measurement mentioned above, the indicator can adjust its sensitivity. When the market moves strongly and clearly, the indicator reacts to capture the trend; when the market is choppy or erratic, the smoothing logic helps prevent the indicator from giving false signals.

3. The "Step" Mechanism (Price Quantization) One of the unique features of this script is the "Step Size." Rather than showing every tiny fluctuation in price, the script rounds the data into specific "steps" (like stairs).

  • If a price movement is smaller than your defined step size, the indicator ignores it.
  • This effectively flattens out minor market wobbles, making it much easier for a human trader to see the actual direction of the trend without being distracted by insignificant ticks.

4. Dynamic Visual Feedback The script provides three distinct layers of visual information:

  • The Shadow: A secondary line that represents the underlying calculations.
  • The Step VMA (Main Line): This is the primary visual. It changes color dynamically based on its direction. When the line moves up, it shifts to one color (Medium Sea Green); when it moves down, it shifts to another (Deep Pink).
  • Color Transitions: The logic tracks the direction of each "step." If the price stays flat, it maintains its current color; if it moves, the color updates immediately to reflect the new trend.

Summary for the User

In plain terms, this script acts as a "Trend Filter." It looks at the market's volatility to decide how much to trust the current price movement, smooths that data out so you don't get confused by "fake" moves, and then rounds those movements into clean, color-coded steps. It allows you to see the "true" path of the market while ignoring the minor distractions of daily price noise.

8 Views
0 Downloads
0 Favorites
Step VHF adaptive VMA
ÿþ//------------------------------------------------------------------

#property copyright   "© mladen, 2018"

#property link        "mladenfx@gmail.com"

#property description "Step VHF adaptive VMA"

//------------------------------------------------------------------

#property indicator_chart_window

#property indicator_buffers 4

#property indicator_plots   2

#property indicator_label1  "Shadow"

#property indicator_type1   DRAW_LINE

#property indicator_color1  clrGray

#property indicator_width1  6

#property indicator_label2  "Step VMA"

#property indicator_type2   DRAW_COLOR_LINE

#property indicator_color2  clrDarkGray,clrMediumSeaGreen,clrDeepPink

#property indicator_width2  2



//

//--- input parameters

//



input int                inpPeriod   = 14;          // VMA period

input int                inpPeriod2  =  0;          // VHF period (<=1 for same as VMA period)

input ENUM_APPLIED_PRICE inpPrice    = PRICE_CLOSE; // Price

input double             inpStepSize = 1.0;         // Step size (pips)



//

//--- indicator buffers

//



double val[],valc[],vals[],avg[];

double ª_alpha,ª_stepSize;; int ª_adpPeriod;



//------------------------------------------------------------------

//  Custom indicator initialization function

//------------------------------------------------------------------ 



int OnInit()

{

   //

   //--- indicator buffers mapping

   //

         SetIndexBuffer(0,vals ,INDICATOR_DATA);

         SetIndexBuffer(1,val  ,INDICATOR_DATA);

         SetIndexBuffer(2,valc ,INDICATOR_COLOR_INDEX);

         SetIndexBuffer(3,avg  ,INDICATOR_CALCULATIONS);

            ª_adpPeriod = (inpPeriod2<=1 ? inpPeriod : inpPeriod2);

            ª_alpha     = 2.0/(1.0+inpPeriod);

            ª_stepSize  = (inpStepSize>0?inpStepSize:0)*_Point*MathPow(10,_Digits%2);

   //         

   //--- indicator short name assignment

   //

   

         IndicatorSetString(INDICATOR_SHORTNAME,"VMA ("+(string)inpPeriod+")");

   return (INIT_SUCCEEDED);

}

void OnDeinit(const int reason)

{

}



//------------------------------------------------------------------

//  Custom indicator iteration function

//------------------------------------------------------------------

//

//---

//



#define _setPrice(_priceType,_target,_index) \

   { \

   switch(_priceType) \

   { \

      case PRICE_CLOSE:    _target = close[_index];                                              break; \

      case PRICE_OPEN:     _target = open[_index];                                               break; \

      case PRICE_HIGH:     _target = high[_index];                                               break; \

      case PRICE_LOW:      _target = low[_index];                                                break; \

      case PRICE_MEDIAN:   _target = (high[_index]+low[_index])/2.0;                             break; \

      case PRICE_TYPICAL:  _target = (high[_index]+low[_index]+close[_index])/3.0;               break; \

      case PRICE_WEIGHTED: _target = (high[_index]+low[_index]+close[_index]+close[_index])/4.0; break; \

      default : _target = 0; \

   }}

//

//---

//



int OnCalculate(const int rates_total,const int prev_calculated,const datetime &time[],

                const double &open[],

                const double &high[],

                const double &low[],

                const double &close[],

                const long &tick_volume[],

                const long &volume[],

                const int &spread[])

{

   int i = (prev_calculated>0 ? prev_calculated-1 : 0); for (; i<rates_total && !_StopFlag; i++)

   {

      double _price; _setPrice(inpPrice,_price,i);

      avg[i] = (i>0) ? avg[i-1]+(ª_alpha*iVhf(ª_adpPeriod,close,i,rates_total)*2.0)*(_price-avg[i-1]) : _price;

      

      //

      //---

      //

      

      if (i>0 && ª_stepSize>0)

      {

           double _diff = avg[i]-val[i-1];

           val[i] = val[i-1]+((_diff<ª_stepSize && _diff>-ª_stepSize) ? 0 : (int)(_diff/ª_stepSize)*ª_stepSize); 

      }

      else val[i]  = (ª_stepSize>0) ? MathRound(avg[i]/ª_stepSize)*ª_stepSize : avg[i]; 

           valc[i] = (i>0) ? (val[i]>val[i-1]) ? 1 : (val[i]<val[i-1]) ? 2 : valc[i-1] : 0 ;

           vals[i] = val[i];

   }

   return(i);

}



//------------------------------------------------------------------

//  Custom function(s)

//------------------------------------------------------------------

//

//---

//



#define _checkArrayReserve 500

#define _checkArraySize(_arrayName,_ratesTotal) static bool _arrayError=false; { static int _arrayResizedTo=0; if (_arrayResizedTo<_ratesTotal) { int _res = (_ratesTotal+_checkArrayReserve); _res -= ArrayResize(_arrayName,_ratesTotal+_checkArrayReserve); if (_res) _arrayError=true; else { _arrayResizedTo=_ratesTotal+_checkArrayReserve; }}}



//

//---

//





template <typename T>

double iVhf(int period, T& price[], int i, int bars, int _instance=0)

{

   //

   //---

   //

   

      #define _functionInstancesArraySize 2

      #ifdef  _functionInstances

            static double _workArray[][_functionInstancesArraySize*_functionInstances];

      #else static double _workArray[][_functionInstancesArraySize];

      #endif

         _checkArraySize(_workArray,bars); 



   //

   //---

   //

                  

      #ifdef _functionInstances 

                int _winst = _instance*_functionInstancesArraySize; 

      #else #define _winst   _instance

      #endif

      #define _diff   _winst+1

      #define _noise  _winst



      _workArray[i][_diff] = (i>0) ? (price[i]>price[i-1]) ? price[i]-price[i-1] : price[i-1]-price[i] : 0;

         if (i>period)

                 _workArray[i][_noise] = _workArray[i-1][_noise]-_workArray[i-period][_diff]+_workArray[i][_diff];

         else  { _workArray[i][_noise] = _workArray[i][_diff]; for(int k=1; k<period && i>=k; k++) _workArray[i][_noise] += _workArray[i-k][_diff]; }

   

         //

         //----

         //

         

         int start = i-period+1; if (start<0) start=0;

         double max = price[ArrayMaximum(price,start,period)];

         double min = price[ArrayMinimum(price,start,period)];

   return((_workArray[i][_noise]!=0) ? (max-min)/_workArray[i][_noise] : 0);  



   //

   //---

   //

   

   #undef _diff #undef _noise

   #undef _functionInstances #undef _functionInstancesArraySize

   

}

//------------------------------------------------------------------

Comments