Author: Copyright 2020, Serj_Che.
0 Views
0 Downloads
0 Favorites
Noise
ÿþ//+------------------------------------------------------------------+

//|                                                        Noise.mq5 |

//|                                        Copyright 2020, Serj_Che. |

//|                           https://www.mql5.com/ru/users/serj_che |

//+------------------------------------------------------------------+

#property copyright "Copyright 2020, Serj_Che."

#property link      "https://www.mql5.com/ru/users/serj_che"

#property version   "1.00"

#property indicator_separate_window

#property  description "Noise = SUM(ABS(CLOSE(i)-CLOSE(i-1),N) - ABS(CLOSE(i-N)-CLOSE(i-N-1)"



//--- indicator settings

#property indicator_buffers 3

#property indicator_plots   1

#property indicator_type1   DRAW_COLOR_HISTOGRAM

#property indicator_color1  clrRed,clrBlue

#property indicator_label1  "Noise"

#property indicator_style1  STYLE_SOLID 

#property indicator_width1  2 



//--- input parameters

input int                InpPeriod=25;              // Period

//--- indicator buffers

double ExtBuffer[];

double ColorBuffer[]; 

double SumBuffer[];

//+------------------------------------------------------------------+

//| Custom indicator initialization function                         |

//+------------------------------------------------------------------+

void OnInit()

  {

//--- indicator buffers mapping

   SetIndexBuffer(0,ExtBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,ColorBuffer,INDICATOR_COLOR_INDEX); 

   SetIndexBuffer(2,SumBuffer,INDICATOR_CALCULATIONS); 

//---

   IndicatorSetInteger(INDICATOR_DIGITS,_Digits+1);

//--- sets first bar from what index will be drawn

   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,InpPeriod-1);

//--- name for DataWindow

   IndicatorSetString(INDICATOR_SHORTNAME,"Noise("+string(InpPeriod)+")");

//--- initialization done

  }

//+------------------------------------------------------------------+

//| Custom indicator iteration function                              |

//+------------------------------------------------------------------+

int OnCalculate(const int rates_total,

                const int prev_calculated,

                const int begin,

                const double &price[])

  {

   int    i,limit;

//--- check for bars count

   if(rates_total<InpPeriod+1)

      return(0);// not enough bars for calculation

//--- preliminary calculations

   limit=prev_calculated-1;

   if(limit<InpPeriod+1)

     { 

      SumBuffer[0]=0;

      for(int p=1; p<=InpPeriod; p++)

        {

         SumBuffer[p]=SumBuffer[p-1]+MathAbs(price[p]-price[p-1]);

        }

      limit=InpPeriod+1;

     }

//--- the main loop of calculations

   for(i=limit;i<rates_total && !IsStopped();i++)

     {

      SumBuffer[i]=SumBuffer[i-1]+MathAbs(price[i]-price[i-1])-MathAbs(price[i-InpPeriod]-price[i-InpPeriod-1]);

      ExtBuffer[i]=(SumBuffer[i]-MathAbs(price[i-InpPeriod]-price[i]));

      if(ExtBuffer[i]>ExtBuffer[i-1]) ColorBuffer[i]=0;

      else ColorBuffer[i]=1;

     }

//--- done

   return(rates_total);

  }

//+------------------------------------------------------------------+

Comments