Standard_Error_Bands

Author: Copyright 2018, MetaQuotes Software Corp.
Price Data Components
Indicators Used
Moving average indicator
0 Views
0 Downloads
0 Favorites
Standard_Error_Bands
ÿþ//+------------------------------------------------------------------+

//|                                         Standard_Error_Bands.mq5 |

//|                        Copyright 2018, MetaQuotes Software Corp. |

//|                                                 https://mql5.com |

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

#property copyright "Copyright 2018, MetaQuotes Software Corp."

#property link      "https://mql5.com"

#property version   "1.00"

#property description "Standard Error Bands indicator"

#property indicator_chart_window

#property indicator_buffers 7

#property indicator_plots   3

//--- plot Top

#property indicator_label1  "StdErr Top"

#property indicator_type1   DRAW_LINE

#property indicator_color1  clrGreen

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- plot Central

#property indicator_label2  "StdErr Central"

#property indicator_type2   DRAW_LINE

#property indicator_color2  clrBlue

#property indicator_style2  STYLE_SOLID

#property indicator_width2  1

//--- plot Bottom

#property indicator_label3  "StdErr Bottom"

#property indicator_type3   DRAW_LINE

#property indicator_color3  clrRed

#property indicator_style3  STYLE_SOLID

#property indicator_width3  1

//--- input parameters

input uint                 InpPeriod         =  21;            // Regression period

input uint                 InpPeriodSM       =  3;             // Smoothing period

input double               InpMultiplier     =  2.0;           // Multiplier

input ENUM_APPLIED_PRICE   InpAppliedPrice   =  PRICE_CLOSE;   // Applied price

//--- indicator buffers

double         BufferTop[];

double         BufferCentral[];

double         BufferBottom[];

double         BufferLreg[];

double         BufferMA[];

double         BufferDev[];

double         BufferMADevTmp[];

//--- global variables

double         multiplier;

int            period_reg;

int            period_max;

int            period_sm;

int            handle_ma;

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- set global variables

   multiplier=InpMultiplier;

   period_reg=int(InpPeriod<2 ? 2 : InpPeriod);

   period_sm=int(InpPeriodSM<2 ? 2 : InpPeriodSM);

   period_max=fmax(period_reg,period_sm);

//--- indicator buffers mapping

   SetIndexBuffer(0,BufferTop,INDICATOR_DATA);

   SetIndexBuffer(1,BufferCentral,INDICATOR_DATA);

   SetIndexBuffer(2,BufferBottom,INDICATOR_DATA);

   SetIndexBuffer(3,BufferLreg,INDICATOR_CALCULATIONS);

   SetIndexBuffer(4,BufferMA,INDICATOR_CALCULATIONS);

   SetIndexBuffer(5,BufferDev,INDICATOR_CALCULATIONS);

   SetIndexBuffer(6,BufferMADevTmp,INDICATOR_CALCULATIONS);

//--- setting indicator parameters

   IndicatorSetString(INDICATOR_SHORTNAME,"Standard Error Bands ("+(string)period_reg+","+(string)period_sm+","+DoubleToString(multiplier,1)+")");

   IndicatorSetInteger(INDICATOR_DIGITS,Digits());

//--- setting plot buffer parameters

   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,period_max+2);

   PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,period_max+2);

   PlotIndexSetInteger(2,PLOT_DRAW_BEGIN,period_max+2);

//--- setting buffer arrays as timeseries

   ArraySetAsSeries(BufferTop,true);

   ArraySetAsSeries(BufferCentral,true);

   ArraySetAsSeries(BufferBottom,true);

   ArraySetAsSeries(BufferLreg,true);

   ArraySetAsSeries(BufferMA,true);

   ArraySetAsSeries(BufferDev,true);

   ArraySetAsSeries(BufferMADevTmp,true);

//--- create MA's handles

   ResetLastError();

   handle_ma=iMA(NULL,PERIOD_CURRENT,1,0,MODE_SMA,InpAppliedPrice);

   if(handle_ma==INVALID_HANDLE)

     {

      Print("The iMA(1) by ",EnumToString(InpAppliedPrice)," object was not created: Error ",GetLastError());

      return INIT_FAILED;

     }

//---

   return(INIT_SUCCEEDED);

  }

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

//| Custom indicator iteration function                              |

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

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[])

  {

//--- @>25@:0 8 @0AGQB :>;8G5AB20 ?@>AG8BK205<KE 10@>2

   if(rates_total<fmax(period_max,4)) return 0;

//--- @>25@:0 8 @0AGQB :>;8G5AB20 ?@>AG8BK205<KE 10@>2

   int limit=rates_total-prev_calculated;

   if(limit>1)

     {

      limit=rates_total-period_max-1;

      ArrayInitialize(BufferTop,EMPTY_VALUE);

      ArrayInitialize(BufferCentral,EMPTY_VALUE);

      ArrayInitialize(BufferBottom,EMPTY_VALUE);

      ArrayInitialize(BufferLreg,0);

      ArrayInitialize(BufferMA,0);

      ArrayInitialize(BufferDev,0);

      ArrayInitialize(BufferMADevTmp,0);

     }

//--- >43>B>2:0 40==KE

   int count=(limit>1 ? rates_total : 1);

   int copied=CopyBuffer(handle_ma,0,0,count,BufferMA);

   if(copied!=count) return 0;

   for(int i=limit; i>=0 && !IsStopped(); i--)

     {

      double x=0, y=0, xy=0, x2=0;

      for(int j=0; j<period_reg; j++)

        {

         y+=BufferMA[i+j];

         xy+=BufferMA[i+j]*j;

         x+=j;

         x2+=j*j;

        }

      double Temp=period_reg*x2-x*x;

      double m=(period_reg*xy-x*y)/Temp;

      double yint=(y+m*x)/period_reg;

      BufferLreg[i]=yint-m*period_reg;

     }

   if(StdDevOnArray(rates_total,prev_calculated,0,period_sm,MODE_SMA,BufferMADevTmp,BufferLreg,BufferDev)==0)

      return 0;

   

//---  0AGQB 8=48:0B>@0

   for(int i=limit; i>=0 && !IsStopped(); i--)

     {

      BufferCentral[i]=GetSMA(rates_total,i,period_sm,BufferLreg);

      double deviation=BufferDev[i];

      double dev=multiplier*deviation;

      BufferTop[i]=BufferCentral[i]+dev;

      BufferBottom[i]=BufferCentral[i]-dev;

     }



//--- return value of prev_calculated for next call

   return(rates_total);

  }

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

//| Simple Moving Average                                            |

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

double GetSMA(const int rates_total,const int index,const int period,const double &price[],const bool as_series=true)

  {

//---

   double result=0.0;

//--- check position

   bool check_index=(as_series ? index<=rates_total-period-1 : index>=period-1);

   if(period<1 || !check_index)

      return 0;

//--- calculate value

   for(int i=0; i<period; i++)

      result=result+(as_series ? price[index+i]: price[index-i]);

//---

   return(result/period);

  }

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

//| Standart Deviation on array                                      |

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

#include <MovingAverages.mqh>

template<typename T>

int StdDevOnArray(const int rates_total,

                  const int prev_calculated,

                  const int begin,

                  const int period,

                  const ENUM_MA_METHOD method,

                  double &buffer_ma_tmp[],

                  const T &src_array[],

                  double &dest_buffer[]

                  )

  {

//--- variables of indicator

   int pos=0;

//--- check for rates count

   if(rates_total<period) return(0);

//--- save as_series flags

   bool as_series_arr=ArrayGetAsSeries(src_array);

   bool as_series_dev=ArrayGetAsSeries(dest_buffer);

   bool as_series_ma=ArrayGetAsSeries(buffer_ma_tmp);

   if(as_series_arr)

      ArraySetAsSeries(src_array,false);

   if(as_series_dev)

      ArraySetAsSeries(dest_buffer,false);

   if(as_series_ma)

      ArraySetAsSeries(buffer_ma_tmp,false);



//--- starting work

   pos=prev_calculated-1;

//--- correct position for first iteration

   if(pos<period)

     {

      pos=period-1;

      ArrayInitialize(dest_buffer,0.0);

      ArrayInitialize(buffer_ma_tmp,0.0);

     }

//--- main cycle

   switch(method)

     {

      case  MODE_EMA :

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

           {

            if(i==period-1)

               buffer_ma_tmp[i]=SimpleMA(i,period,src_array);

            else

               buffer_ma_tmp[i]=ExponentialMA(i,period,buffer_ma_tmp[i-1],src_array);

            //--- Calculate StdDev

            dest_buffer[i]=StdDevFunc(src_array,buffer_ma_tmp,period,i);

           }

         break;

      case MODE_SMMA :

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

           {

            if(i==period-1)

               buffer_ma_tmp[i]=SimpleMA(i,period,src_array);

            else

               buffer_ma_tmp[i]=SmoothedMA(i,period,buffer_ma_tmp[i-1],src_array);

            //--- Calculate StdDev

            dest_buffer[i]=StdDevFunc(src_array,buffer_ma_tmp,period,i);

           }

         break;

      case MODE_LWMA :

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

           {

            buffer_ma_tmp[i]=LinearWeightedMA(i,period,src_array);

            dest_buffer[i]=StdDevFunc(src_array,buffer_ma_tmp,period,i);

           }

         break;

      default   :

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

           {

            buffer_ma_tmp[i]=SimpleMA(i,period,src_array);

            //--- Calculate StdDev

            dest_buffer[i]=StdDevFunc(src_array,buffer_ma_tmp,period,i);

           }

     }

//--- restore as_series flags

   if(as_series_arr)

      ArraySetAsSeries(src_array,true);

   if(as_series_dev)

      ArraySetAsSeries(dest_buffer,true);

   if(as_series_ma)

      ArraySetAsSeries(buffer_ma_tmp,true);

//---- OnCalculate done. Return new prev_calculated.

   return(rates_total);

  }

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

//| Calculate Standard Deviation                                     |

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

template<typename T>

double StdDevFunc(const T &price[],const double &price_ma[],const int period,int position)

  {

   double dev=0.0;

   for(int i=0; i<period; i++) dev+=pow(price[position-i]-price_ma[position],2);

   dev=sqrt(dev/period);

   return(dev);

  }

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

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---