Alligator iFrAMA

Author: Copyright © 2020, Vladimir Karputov
0 Views
0 Downloads
0 Favorites
Alligator iFrAMA
ÿþ//+------------------------------------------------------------------+

//|                                             Alligator iFrAMA.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 3

#property indicator_plots   3

//--- plot Jaws

#property indicator_label1  "Jaws"

#property indicator_type1   DRAW_LINE

#property indicator_color1  clrBlue

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- plot Teeth

#property indicator_label2  "Teeth"

#property indicator_type2   DRAW_LINE

#property indicator_color2  clrRed

#property indicator_style2  STYLE_SOLID

#property indicator_width2  1

//--- plot Lips

#property indicator_label3  "Lips"

#property indicator_type3   DRAW_LINE

#property indicator_color3  clrLime

#property indicator_style3  STYLE_SOLID

#property indicator_width3  1

//--- input parameters

input int                  Inp_Alligator_jaw_period      = 13;             // Alligator: period for the calculation of jaws

input int                  Inp_Alligator_jaw_shift       = 8;              // Alligator: horizontal shift of jaws

input int                  Inp_Alligator_teeth_period    = 8;              // Alligator: period for the calculation of teeth

input int                  Inp_Alligator_teeth_shift     = 5;              // Alligator: horizontal shift of teeth

input int                  Inp_Alligator_lips_period     = 5;              // Alligator: period for the calculation of lips

input int                  Inp_Alligator_lips_shift      = 3;              // Alligator: horizontal shift of lips

input ENUM_APPLIED_PRICE   Inp_Alligator_applied_price   = PRICE_MEDIAN;   // Alligator: type of price

//--- indicator buffers

double   JawsBuffer[];

double   TeethBuffer[];

double   LipsBuffer[];

//---

int      handle_iFrAMA_jaw;                        // variable for storing the handle of the iFrAMA indicator

int      handle_iFrAMA_teeth;                      // variable for storing the handle of the iFrAMA indicator

int      handle_iFrAMA_lips;                       // variable for storing the handle of the iFrAMA indicator

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

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

//| Expert initialization function                                   |

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

int OnInit()

  {

//--- assignment of arrays to indicator buffers

   SetIndexBuffer(0,JawsBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,TeethBuffer,INDICATOR_DATA);

   SetIndexBuffer(2,LipsBuffer,INDICATOR_DATA);

//--- set shift of each line

   PlotIndexSetInteger(0,PLOT_SHIFT,Inp_Alligator_jaw_shift);

   PlotIndexSetInteger(1,PLOT_SHIFT,Inp_Alligator_teeth_shift);

   PlotIndexSetInteger(2,PLOT_SHIFT,Inp_Alligator_lips_shift);

//--- show the symbol/timeframe the Alligator indicator is calculated for

   string short_name=StringFormat("iAlligator iFrAMA(%d,%d,%d,%d,%d,%d)",

                                  Inp_Alligator_jaw_period,Inp_Alligator_jaw_shift,

                                  Inp_Alligator_teeth_period,Inp_Alligator_teeth_shift,

                                  Inp_Alligator_lips_period,Inp_Alligator_lips_shift);

   IndicatorSetString(INDICATOR_SHORTNAME,short_name);

//--- create handle of the indicator

   handle_iFrAMA_jaw=iFrAMA(Symbol(),Period(),Inp_Alligator_jaw_period,

                            Inp_Alligator_jaw_shift,Inp_Alligator_applied_price);

//--- if the handle is not created

   if(handle_iFrAMA_jaw==INVALID_HANDLE)

     {

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

      PrintFormat("Failed to create handle of the iFrAMA (\"Jaw\") 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

   handle_iFrAMA_teeth=iFrAMA(Symbol(),Period(),Inp_Alligator_teeth_period,

                              Inp_Alligator_teeth_shift,Inp_Alligator_applied_price);

//--- if the handle is not created

   if(handle_iFrAMA_teeth==INVALID_HANDLE)

     {

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

      PrintFormat("Failed to create handle of the iFrAMA (\"Teeth\") 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

   handle_iFrAMA_lips=iFrAMA(Symbol(),Period(),Inp_Alligator_lips_period,

                             Inp_Alligator_lips_shift,Inp_Alligator_applied_price);

//--- if the handle is not created

   if(handle_iFrAMA_lips==INVALID_HANDLE)

     {

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

      PrintFormat("Failed to create handle of the iFrAMA (\"Lips\") 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 iAlligator indicator

   int values_to_copy;

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

   int calculated_jaw=BarsCalculated(handle_iFrAMA_jaw);

   if(calculated_jaw<=0)

     {

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

      return(0);

     }

   int calculated_teeth=BarsCalculated(handle_iFrAMA_teeth);

   if(calculated_teeth<=0)

     {

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

      return(0);

     }

   int calculated_lips=BarsCalculated(handle_iFrAMA_lips);

   if(calculated_lips<=0)

     {

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

      return(0);

     }

   if(calculated_jaw!=calculated_teeth || calculated_teeth!=calculated_lips)

     {

      PrintFormat("BarsCalculated(Jaw) returned %d, BarsCalculated(Teeth) returned %d, BarsCalculated(Lips) returned %d, error code %d",

                  calculated_jaw,calculated_teeth,calculated_lips);

      return(0);

     }

   int calculated=BarsCalculated(handle_iFrAMA_jaw);

//--- if it is the first start of calculation of the indicator or if the number of values in the iAlligator 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 JawsBuffer array is greater than the number of values in the iAlligator 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 iFrAMABuffer array with values of the Fractal Adaptive Moving Average indicator

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

   if(!FillArrayFromBuffer(JawsBuffer,Inp_Alligator_jaw_shift,handle_iFrAMA_jaw,values_to_copy))

      return(0);

   if(!FillArrayFromBuffer(TeethBuffer,Inp_Alligator_teeth_shift,handle_iFrAMA_teeth,values_to_copy))

      return(0);

   if(!FillArrayFromBuffer(LipsBuffer,Inp_Alligator_lips_shift,handle_iFrAMA_lips,values_to_copy))

      return(0);

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

   bars_calculated=calculated;

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

   return(rates_total);

  }

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

//| Filling indicator buffers from the iFrAMA indicator              |

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

bool FillArrayFromBuffer(double &values[],  // indicator buffer of Fractal Adaptive Moving Average values

                         int shift,         // shift

                         int ind_handle,    // handle of the iFrAMA indicator

                         int amount         // number of copied values

                        )

  {

//--- reset error code

   ResetLastError();

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

   if(CopyBuffer(ind_handle,0,-shift,amount,values)<0)

     {

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

      PrintFormat("Failed to copy data from the iFrAMA 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_iFrAMA_jaw!=INVALID_HANDLE)

      IndicatorRelease(handle_iFrAMA_jaw);

   if(handle_iFrAMA_teeth!=INVALID_HANDLE)

      IndicatorRelease(handle_iFrAMA_teeth);

   if(handle_iFrAMA_lips!=INVALID_HANDLE)

      IndicatorRelease(handle_iFrAMA_lips);

  }

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

Comments