Weighted deviation

Download script
Author: © mladen, 2023

This script is a technical analysis tool designed for trading platforms to measure "Weighted Deviation." In simple terms, it calculates how much the current price is swinging away from its recent average, while giving more importance to the most recent data points.

Core Logic Overview

The indicator operates by analyzing historical price data over a user-defined period. Instead of treating every piece of historical data as equally important, it applies a "weighting" system. This means that prices occurring closer to the current moment have a greater influence on the final result than older prices.

How it Works Step-by-Step

1. Data Collection and Customization The script allows you to choose which specific price type to track (such as the Closing price, High, Low, or an average of these). It then gathers this data over a specific number of bars (the "Period").

2. Weighted Averaging The tool calculates a "Weighted Mean." To do this, it assigns a higher value to recent prices and lower values to older ones within the chosen period. This helps the indicator stay responsive to new market movements while still acknowledging historical context.

3. Deviation Calculation Once the weighted average is established, the script looks at how far each individual price point in that period sits away from that average. It squares these differences (to ensure all values are positive) and sums them up.

4. The Final Output (Standard Deviation) Finally, the script takes the square root of those squared differences relative to the total weight. The result is a "Weighted Deviation" value.

Practical Interpretation

When you see the line move on your chart:

  • High Values: Indicate that the price is currently experiencing significant volatility or is moving aggressively away from its recent weighted average.
  • Low Values: Indicate that the price is stabilizing and trading very close to its recent weighted average.

By focusing on "weighted" data, this script aims to provide a more relevant view of current market volatility than a standard deviation indicator, which treats all data in a period with equal importance.

8 Views
1 Downloads
0 Favorites
Weighted deviation
ÿþ//------------------------------------------------------------------------------------------------------------------------

#property copyright   "© mladen, 2023"

#property link        "mladenfx@gmail.com"

#property description "Weighted deviation"

#property version     "1.00"

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

#property indicator_separate_window

#property indicator_buffers 1

#property indicator_plots   1

#property indicator_label1  "Deviation"

#property indicator_type1   DRAW_LINE

#property indicator_color1  clrDeepSkyBlue

#property indicator_width1  2



//

//

//



input int                inpPeriod =  30;         // Period

input ENUM_APPLIED_PRICE inpPrice  = PRICE_CLOSE; // Price 



//

//

//



double val[];

struct sGlobalStruct

{

   int    period;

};

sGlobalStruct global;



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

//

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

//

//

//



int OnInit()

{

   SetIndexBuffer(0,val,INDICATOR_DATA);



         global.period = MathMax(inpPeriod,1);

      

   IndicatorSetString(INDICATOR_SHORTNAME,StringFormat("Weighted deviation (%i)",inpPeriod));

   return (INIT_SUCCEEDED);

}

void OnDeinit(const int reason) { }



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

//

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

//

//

//



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 limit = (prev_calculated>0) ? prev_calculated-1 : 0;

   

   //

   //

   //



      for(int i=limit; i<rates_total && !_StopFlag; i++)  val[i] = iDeviationWeighted(iGetPrice(inpPrice,open,high,low,close,i),global.period,i,rates_total);



   return (rates_total);

}



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

//

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

//

//

//



double iDeviationWeighted(double value, int period, int i, int bars)

{

   struct sCalcStruct

         {

               struct sWorkStruct

                     {

                        double value;

                        double valueSum;

                        double valueWeightSum;

                     };

               sWorkStruct data[];

               int         dataSize;                     

               double      weightSum;



               //

               //

               //

               

               sCalcStruct() : dataSize(-1) {}

         };

   static sCalcStruct m_work;               

                  if (m_work.dataSize<=bars) m_work.dataSize = ArrayResize(m_work.data,bars+500,5000);

                  if (period<1) period = 1;



   //

   //

   //



      m_work.data[i].value = value;

            if (i >= period)

                     {

                        m_work.data[i].valueSum       = m_work.data[i-1].valueSum       + m_work.data[i].value - m_work.data[i-period].value;

                        m_work.data[i].valueWeightSum = m_work.data[i-1].valueWeightSum - m_work.data[i-1].valueSum + m_work.data[i].value*(double)period;

                     }

            else

                     {

                        period = i+1;

                        m_work.weightSum              = (double)period;

                        m_work.data[i].valueWeightSum = (double)period*m_work.data[i].value;

                        m_work.data[i].valueSum       =                m_work.data[i].value;

                 

                        //

                        //

                        //

                  

                        for(int k=1; k<period && i>=k; k++)

                           {

                              double weight = period-k;

                                              m_work.weightSum              +=                        weight;

                                              m_work.data[i].valueWeightSum += m_work.data[i-k].value*weight;  

                                              m_work.data[i].valueSum       += m_work.data[i-k].value;  

                           }         

                     }



               //

               //

               //

               

               double mean = m_work.data[i].valueWeightSum/m_work.weightSum;

               double sums = 0;



               for (int k=0, weight=period; k<period && i>=k; k++,weight--)

                     {

                        sums += (double)weight*(m_work.data[i-k].value-mean)*(m_work.data[i-k].value-mean);

                     }



   //

   //

   //

         

   return(sqrt(sums/m_work.weightSum));

}



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

//

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

//

//

//



template <typename type>

double iGetPrice(ENUM_APPLIED_PRICE tprice, type& open[], type& high[], type& low[], type& close[], int i)

{

   switch(tprice)

      {

         case PRICE_CLOSE:     return(close[i]);

         case PRICE_OPEN:      return(open[i]);

         case PRICE_HIGH:      return(high[i]);

         case PRICE_LOW:       return(low[i]);

         case PRICE_MEDIAN:    return((high[i]+low[i])/2.0);

         case PRICE_TYPICAL:   return((high[i]+low[i]+close[i])/3.0);

         case PRICE_WEIGHTED:  return((high[i]+low[i]+close[i]+close[i])/4.0);

      }

   return(0);

}

Comments