AMA Envelopes Custom

Author: Copyright © 2020, Vladimir Karputov
Price Data Components
0 Views
0 Downloads
0 Favorites
AMA Envelopes Custom
ÿþ//+------------------------------------------------------------------+

//|                                         AMA Envelopes Custom.mq5 |

//|                              Copyright © 2020, Vladimir Karputov |

//|                     https://www.mql5.com/ru/market/product/43516 |

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

#property copyright "Copyright © 2020, Vladimir Karputov"

#property link      "https://www.mql5.com/ru/market/product/43516"

#property version   "1.000"

#property indicator_chart_window

#property indicator_buffers 2

#property indicator_plots   2

#property indicator_type1   DRAW_LINE

#property indicator_type2   DRAW_LINE

#property indicator_color1  clrBlue

#property indicator_color2  clrRed

#property indicator_label1  "Upper band"

#property indicator_label2  "Lower band"

#property tester_indicator "Envelopes Upper"

#property tester_indicator "Envelopes Lower"

//--- input parameters

input group             "Envelopes"

input int                 Inp_Env_ma_period        = 14;          // Envelopes: averaging period

input int                 Inp_Env_ma_shift         = 0;           // Envelopes: horizontal shift

input ENUM_MA_METHOD      Inp_Env_ma_method        = MODE_SMA;    // Envelopes: smoothing type

input ENUM_APPLIED_PRICE  Inp_Env_applied_price    = PRICE_CLOSE; // Envelopes: type of price

input double              Inp_Env_deviation        = 0.1;         // Envelopes: deviation of boundaries from the midline (in percents)

input group             "AMA"

input int                  Inp_AMA_ama_period      = 15;          // AMA: average period for AMA

input int                  Inp_AMA_fast_ma_period  = 2;           // AMA: fast MA period

input int                  Inp_AMA_slow_ma_period  = 30;          // AMA: slow MA period

input int                  Inp_AMA_ama_shift       = 0;           // AMA: horizontal shift of the indicator

//--- indicator buffers

double   UpperBuffer[];

double   LowerBuffer[];

//---

int    handle_iEnvelopes_Upper;              // variable for storing the handle of the Envelopes Upper indicator

int    handle_iEnvelopes_Lower;              // variable for storing the handle of the Envelopes Lower indicator

int    handle_iAMA_Upper;                    // variable for storing the handle of the iAMA indicator

int    handle_iAMA_Lower;                    // variable for storing the handle of the iAMA indicator

int    bars_calculated=0;                    // we will keep the number of values in the Adaptive Moving Average indicator

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- indicator buffers mapping

   SetIndexBuffer(0,UpperBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,LowerBuffer,INDICATOR_DATA);

//---

   IndicatorSetInteger(INDICATOR_DIGITS,Digits()+1);

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

   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,Inp_Env_ma_period-1);

//--- name for DataWindow

   string short_name=StringFormat("AMA Env(%d)",Inp_Env_ma_period);

   IndicatorSetString(INDICATOR_SHORTNAME,short_name);

   PlotIndexSetString(0,PLOT_LABEL,short_name+" Upper");

   PlotIndexSetString(1,PLOT_LABEL,short_name+" Lower");

//--- line shifts when drawing

   PlotIndexSetInteger(0,PLOT_SHIFT,Inp_Env_ma_shift);

   PlotIndexSetInteger(1,PLOT_SHIFT,Inp_Env_ma_shift);

//--- create handle of the indicator Envelopes Upper

   handle_iEnvelopes_Upper=iCustom(Symbol(),Period(),"Envelopes Upper",Inp_Env_ma_period,Inp_Env_ma_shift,Inp_Env_ma_method,Inp_Env_applied_price,Inp_Env_deviation);

//--- if the handle is not created

   if(handle_iEnvelopes_Upper==INVALID_HANDLE)

     {

      //--- tell about the failure and output the error code

      PrintFormat("Failed to create handle of the Envelopes Upper indicator for the symbol %s/%s, error code %d",

                  Symbol(),

                  EnumToString(Period()),

                  GetLastError());

      //--- the indicator is stopped early

      return(INIT_FAILED);

     }

//--- create handle of the indicator Envelopes Upper

   handle_iEnvelopes_Lower=iCustom(Symbol(),Period(),"Envelopes Lower",Inp_Env_ma_period,Inp_Env_ma_shift,Inp_Env_ma_method,Inp_Env_applied_price,Inp_Env_deviation);

//--- if the handle is not created

   if(handle_iEnvelopes_Lower==INVALID_HANDLE)

     {

      //--- tell about the failure and output the error code

      PrintFormat("Failed to create handle of the Envelopes Lower indicator for the symbol %s/%s, error code %d",

                  Symbol(),

                  EnumToString(Period()),

                  GetLastError());

      //--- the indicator is stopped early

      return(INIT_FAILED);

     }

//--- create handle of the indicator iAMA

   handle_iAMA_Upper=iAMA(Symbol(),Period(),Inp_AMA_ama_period,Inp_AMA_fast_ma_period,

                          Inp_AMA_slow_ma_period,Inp_AMA_ama_shift,handle_iEnvelopes_Upper);

//--- if the handle is not created

   if(handle_iAMA_Upper==INVALID_HANDLE)

     {

      //--- tell about the failure and output the error code

      PrintFormat("Failed to create handle of the iAMA Upper indicator for the symbol %s/%s, error code %d",

                  Symbol(),

                  EnumToString(Period()),

                  GetLastError());

      //--- the indicator is stopped early

      return(INIT_FAILED);

     }

//--- create handle of the indicator iAMA

   handle_iAMA_Lower=iAMA(Symbol(),Period(),Inp_AMA_ama_period,Inp_AMA_fast_ma_period,

                          Inp_AMA_slow_ma_period,Inp_AMA_ama_shift,handle_iEnvelopes_Lower);

//--- if the handle is not created

   if(handle_iAMA_Lower==INVALID_HANDLE)

     {

      //--- tell about the failure and output the error code

      PrintFormat("Failed to create handle of the iAMA Lower indicator for the symbol %s/%s, error code %d",

                  Symbol(),

                  EnumToString(Period()),

                  GetLastError());

      //--- the indicator is stopped early

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

  {

//--- number of values copied from the iAMA indicator

   int values_to_copy;

//--- determine the number of values calculated in the indicator

   int calculated_upper=BarsCalculated(handle_iAMA_Upper);

   if(calculated_upper<=0)

     {

      PrintFormat("BarsCalculated() returned %d, error code %d",calculated_upper,GetLastError());

      return(0);

     }

   int calculated_lower=BarsCalculated(handle_iAMA_Lower);

   if(calculated_lower<=0)

     {

      PrintFormat("BarsCalculated() returned %d, error code %d",calculated_lower,GetLastError());

      return(0);

     }

   if(calculated_upper!=calculated_lower)

     {

      PrintFormat("BarsCalculated(handle_iAMA_Upper) returned %d ",calculated_upper);

      PrintFormat("BarsCalculated(handle_iAMA_Lower) returned %d ",calculated_lower);

      return(0);

     }

   int calculated=calculated_upper;

//--- if it is the first start of calculation of the indicator or if the number of values in the iAMA indicator changed

//---or if it is necessary to calculated the indicator for two or more bars (it means something has changed in the price history)

   if(prev_calculated==0 || calculated!=bars_calculated || rates_total>prev_calculated+1)

     {

      //--- if the iAMABuffer array is greater than the number of values in the iAMA indicator for symbol/period, then we don't copy everything

      //--- otherwise, we copy less than the size of indicator buffers

      if(calculated>rates_total)

         values_to_copy=rates_total;

      else

         values_to_copy=calculated;

     }

   else

     {

      //--- it means that it's not the first time of the indicator calculation, and since the last call of OnCalculate()

      //--- for calculation not more than one bar is added

      values_to_copy=(rates_total-prev_calculated)+1;

     }

//--- fill the arrays with values of the Adaptive Moving Average indicator

//--- if FillArraysFromBuffer returns false, it means the information is nor ready yet, quit operation

   if(!FillArrayFromBuffer(UpperBuffer,Inp_AMA_ama_shift,handle_iAMA_Upper,values_to_copy))

      return(0);

   if(!FillArrayFromBuffer(LowerBuffer,Inp_AMA_ama_shift,handle_iAMA_Lower,values_to_copy))

      return(0);

//--- memorize the number of values in the Adaptive Moving Average indicator

   bars_calculated=calculated;

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

   return(rates_total);

  }

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

//| Filling indicator buffer from the iAMA indicator                 |

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

bool FillArrayFromBuffer(double &ama_buffer[],  // indicator buffer of the AMA line

                         int a_shift,           // shift of the AMA line

                         int ind_handle,        // handle of the iAMA indicator

                         int amount             // number of copied values

                        )

  {

//--- reset error code

   ResetLastError();

//--- fill a part of the iAMABuffer array with values from the indicator buffer that has 0 index

   if(CopyBuffer(ind_handle,0,-a_shift,amount,ama_buffer)<0)

     {

      //--- if the copying fails, tell the error code

      PrintFormat("Failed to copy data from the iAMA indicator, error code %d",GetLastError());

      //--- quit with zero result - it means that the indicator is considered as not calculated

      return(false);

     }

//--- everything is fine

   return(true);

  }

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

//| Indicator deinitialization function                              |

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

void OnDeinit(const int reason)

  {

   if(handle_iEnvelopes_Upper!=INVALID_HANDLE)

      IndicatorRelease(handle_iEnvelopes_Upper);

   if(handle_iEnvelopes_Lower!=INVALID_HANDLE)

      IndicatorRelease(handle_iEnvelopes_Lower);

   if(handle_iAMA_Upper!=INVALID_HANDLE)

      IndicatorRelease(handle_iAMA_Upper);

   if(handle_iAMA_Lower!=INVALID_HANDLE)

      IndicatorRelease(handle_iAMA_Lower);

  }

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

Comments