Alligator MA Color N Bars

Author: Copyright © 2020, Vladimir Karputov
Indicators Used
Moving average indicator
0 Views
0 Downloads
0 Favorites
Alligator MA Color N Bars
ÿþ//+------------------------------------------------------------------+

//|                                    Alligator MA Color N Bars.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 6

#property indicator_plots   3

//--- plot Jaws

#property indicator_label1  "Jaws"

#property indicator_type1   DRAW_COLOR_LINE

#property indicator_color1  clrPaleTurquoise,clrBlue

#property indicator_style1  STYLE_SOLID

#property indicator_width1  2

//--- plot Teeth

#property indicator_label2  "Teeth"

#property indicator_type2   DRAW_COLOR_LINE

#property indicator_color2  clrPaleTurquoise,clrRed

#property indicator_style2  STYLE_SOLID

#property indicator_width2  2

//--- plot Lips

#property indicator_label3  "Lips"

#property indicator_type3   DRAW_COLOR_LINE

#property indicator_color3  clrPaleTurquoise,clrLime

#property indicator_style3  STYLE_SOLID

#property indicator_width3  2

//--- input parameters

input group             "Jaws"

input int                  Inp_MA_Jaws_ma_period      = 13;          // MA Jaws: averaging period

input int                  Inp_MA_Jaws_ma_shift       = 8;           // MA Jaws: horizontal shift

input ENUM_MA_METHOD       Inp_MA_Jaws_ma_method      = MODE_EMA;    // MA Jaws: smoothing type

input ENUM_APPLIED_PRICE   Inp_MA_Jaws_applied_price  = PRICE_MEDIAN;// MA Jaws: type of price

input group             "Teeth"

input int                  Inp_MA_Teeth_ma_period     = 8;           // MA Teeth: averaging period

input int                  Inp_MA_Teeth_ma_shift      = 5;           // MA Teeth: horizontal shift

input ENUM_MA_METHOD       Inp_MA_Teeth_ma_method     = MODE_EMA;    // MA Teeth: smoothing type

input ENUM_APPLIED_PRICE   Inp_MA_Teeth_applied_price = PRICE_MEDIAN;// MA Teeth: type of price

input group             "Lips"

input int                  Inp_MA_Lips_ma_period      = 5;           // MA Lips: averaging period

input int                  Inp_MA_Lips_ma_shift       = 3;           // MA Lips: horizontal shift

input ENUM_MA_METHOD       Inp_MA_Lips_ma_method      = MODE_EMA;    // MA Lips: smoothing type

input ENUM_APPLIED_PRICE   Inp_MA_Lips_applied_price  = PRICE_MEDIAN;// MA Lips: type of price

input group             "Trend"

input uchar                Inp_MA_trend_n_bars        = 3;           // MA: trend N Bars

//--- indicator buffers

double   JawsBuffer[];

double   JawsColors[];

double   TeethBuffer[];

double   TeethColors[];

double   LipsBuffer[];

double   LipsColors[];

//---

int      handle_iMA_Jaws;                          // variable for storing the handle of the iMA indicator

int      handle_iMA_Teeth;                         // variable for storing the handle of the iMA indicator

int      handle_iMA_Lips;                          // variable for storing the handle of the iMA indicator

int      bars_calculated=0;                        // we will keep the number of values in the MAs indicators

int      m_start=0;

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- indicator buffers mapping

   SetIndexBuffer(0,JawsBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,JawsColors,INDICATOR_COLOR_INDEX);

   SetIndexBuffer(2,TeethBuffer,INDICATOR_DATA);

   SetIndexBuffer(3,TeethColors,INDICATOR_COLOR_INDEX);

   SetIndexBuffer(4,LipsBuffer,INDICATOR_DATA);

   SetIndexBuffer(5,LipsColors,INDICATOR_COLOR_INDEX);

//--- set shift of each line

   PlotIndexSetInteger(0,PLOT_SHIFT,Inp_MA_Jaws_ma_shift);

   PlotIndexSetInteger(1,PLOT_SHIFT,Inp_MA_Teeth_ma_shift);

   PlotIndexSetInteger(2,PLOT_SHIFT,Inp_MA_Lips_ma_shift);

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

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

                                  Inp_MA_Jaws_ma_period,Inp_MA_Jaws_ma_shift,

                                  Inp_MA_Teeth_ma_period,Inp_MA_Teeth_ma_shift,

                                  Inp_MA_Lips_ma_period,Inp_MA_Lips_ma_shift);

   IndicatorSetString(INDICATOR_SHORTNAME,short_name);

//--- create handle of the indicator iMA

   handle_iMA_Jaws=iMA(Symbol(),Period(),Inp_MA_Jaws_ma_period,Inp_MA_Jaws_ma_shift,

                       Inp_MA_Jaws_ma_method,Inp_MA_Jaws_applied_price);

//--- if the handle is not created

   if(handle_iMA_Jaws==INVALID_HANDLE)

     {

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

      PrintFormat("Failed to create handle of the iMA indicator ('Jaws') 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 iMA

   handle_iMA_Teeth=iMA(Symbol(),Period(),Inp_MA_Teeth_ma_period,Inp_MA_Teeth_ma_shift,

                        Inp_MA_Teeth_ma_method,Inp_MA_Teeth_applied_price);

//--- if the handle is not created

   if(handle_iMA_Teeth==INVALID_HANDLE)

     {

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

      PrintFormat("Failed to create handle of the iMA indicator ('Teeth') 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 iMA

   handle_iMA_Lips=iMA(Symbol(),Period(),Inp_MA_Lips_ma_period,Inp_MA_Lips_ma_shift,

                       Inp_MA_Lips_ma_method,Inp_MA_Lips_applied_price);

//--- if the handle is not created

   if(handle_iMA_Lips==INVALID_HANDLE)

     {

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

      PrintFormat("Failed to create handle of the iMA indicator ('Lips') for the symbol %s/%s, error code %d",

                  Symbol(),

                  EnumToString(Period()),

                  GetLastError());

      //--- the indicator is stopped early

      return(INIT_FAILED);

     }

//---

   m_start=(Inp_MA_Jaws_ma_period+Inp_MA_Jaws_ma_shift>m_start)?Inp_MA_Jaws_ma_period+Inp_MA_Jaws_ma_shift:m_start;

   m_start=(Inp_MA_Teeth_ma_period+Inp_MA_Teeth_ma_shift>m_start)?Inp_MA_Teeth_ma_period+Inp_MA_Teeth_ma_shift:m_start;

   m_start=(Inp_MA_Lips_ma_period+Inp_MA_Lips_ma_shift>m_start)?Inp_MA_Lips_ma_period+Inp_MA_Lips_ma_shift:m_start;

//---

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

  {

   if(rates_total+3<m_start)

      return(0);

//--- 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_iMA_Jaws);

   if(calculated_jaw<=0)

     {

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

      return(0);

     }

   int calculated_teeth=BarsCalculated(handle_iMA_Teeth);

   if(calculated_teeth<=0)

     {

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

      return(0);

     }

   int calculated_lips=BarsCalculated(handle_iMA_Teeth);

   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=calculated_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_MA_Jaws_ma_shift,handle_iMA_Jaws,values_to_copy))

      return(0);

   if(!FillArrayFromBuffer(TeethBuffer,Inp_MA_Teeth_ma_shift,handle_iMA_Teeth,values_to_copy))

      return(0);

   if(!FillArrayFromBuffer(LipsBuffer,Inp_MA_Lips_ma_shift,handle_iMA_Lips,values_to_copy))

      return(0);

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

   bars_calculated=calculated;

//--- main loop

   int limit=prev_calculated-1;

   if(prev_calculated==0)

     {

      limit=m_start;

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

        {

         JawsColors[i]=0.0;

         TeethColors[i]=0.0;

         LipsColors[i]=0.0;

        }

     }

   for(int i=limit; i<rates_total; i++)

     {

      int trend_jaws=0,trend_teeth=0,trend_lips=0;

      for(int j=i-Inp_MA_trend_n_bars; j<i; j++)

        {

         if(JawsBuffer[j]<JawsBuffer[j+1])

            trend_jaws++;

         else

            trend_jaws--;

         if(TeethBuffer[j]<TeethBuffer[j+1])

            trend_teeth++;

         else

            trend_teeth--;

         if(LipsBuffer[j]<LipsBuffer[j+1])

            trend_lips++;

         else

            trend_lips--;

        }

      if(trend_jaws==Inp_MA_trend_n_bars || -trend_jaws==Inp_MA_trend_n_bars)

         JawsColors[i]=1;

      else

         JawsColors[i]=0;

      if(trend_teeth==Inp_MA_trend_n_bars || -trend_teeth==Inp_MA_trend_n_bars)

         TeethColors[i]=1;

      else

         TeethColors[i]=0;

      if(trend_lips==Inp_MA_trend_n_bars || -trend_lips==Inp_MA_trend_n_bars)

         LipsColors[i]=1;

      else

         LipsColors[i]=0;

     }

//--- 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_iMA_Jaws!=INVALID_HANDLE)

      IndicatorRelease(handle_iMA_Jaws);

   if(handle_iMA_Teeth!=INVALID_HANDLE)

      IndicatorRelease(handle_iMA_Teeth);

   if(handle_iMA_Lips!=INVALID_HANDLE)

      IndicatorRelease(handle_iMA_Lips);

  }

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

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 ---