Alligator Channel

Author: Copyright © 2021, Vladimir Karputov
Indicators Used
Bill Williams Alligator
1 Views
0 Downloads
0 Favorites
Alligator Channel
ÿþ//+------------------------------------------------------------------+

//|                                            Alligator Channel.mq5 |

//|                              Copyright © 2021, Vladimir Karputov |

//|                      https://www.mql5.com/en/users/barabashkakvn |

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

#property copyright "Copyright © 2021, Vladimir Karputov"

#property link      "https://www.mql5.com/en/users/barabashkakvn"

#property version   "1.000"

//---

#property indicator_chart_window

#property indicator_buffers 5

#property indicator_plots   4

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

//--- plot Channel

#property indicator_label4  "Channel"

#property indicator_type4   DRAW_FILLING

#property indicator_color4  clrWhite,clrWhite

#property indicator_width4  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_MA_METHOD       Inp_Alligator_ma_method       = MODE_SMMA;      // Alligator: type of smoothing

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

//--- indicator buffers

double   JawsBuffer[];

double   TeethBuffer[];

double   LipsBuffer[];

double   ChannelBuffer1[];

double   ChannelBuffer2[];

//---

int      start=0;

int      handle_iAlligator;               // variable for storing the handle of the iAlligator indicator

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

bool     m_init_error         = false;    // error on InInit

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

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

   SetIndexBuffer(3,ChannelBuffer1,INDICATOR_DATA);

   SetIndexBuffer(4,ChannelBuffer2,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 Channel(%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);

//--- Set the display of the symbol

   PlotIndexSetString(3,PLOT_LABEL,"Ceiling;"+"Floor");

//--- create handle of the indicator iAlligator

   handle_iAlligator=iAlligator(Symbol(),Period(),

                                Inp_Alligator_jaw_period,Inp_Alligator_jaw_shift,

                                Inp_Alligator_teeth_period,Inp_Alligator_teeth_shift,

                                Inp_Alligator_lips_period,Inp_Alligator_lips_shift,

                                Inp_Alligator_ma_method,Inp_Alligator_applied_price);

//--- if the handle is not created

   if(handle_iAlligator==INVALID_HANDLE)

     {

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

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

                  Symbol(),

                  EnumToString(Period()),

                  GetLastError());

      //--- the indicator is stopped early

      m_init_error=true;

      return(INIT_SUCCEEDED);

     }

//---

   start=0;

   if(Inp_Alligator_jaw_shift>start)

      start=Inp_Alligator_jaw_shift;

   if(Inp_Alligator_teeth_shift>start)

      start=Inp_Alligator_teeth_shift;

   if(Inp_Alligator_lips_shift>start)

      start=Inp_Alligator_lips_shift;

//---

   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<start+3)

      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=BarsCalculated(handle_iAlligator);

   if(calculated<=0)

     {

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

      return(0);

     }

//--- 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 arrays with values of the Alligator indicator

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

   if(!FillArraysFromBuffers(JawsBuffer,Inp_Alligator_jaw_shift,TeethBuffer,Inp_Alligator_teeth_shift,LipsBuffer,Inp_Alligator_lips_shift,handle_iAlligator,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)

     {

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

        {

         ChannelBuffer1[i]=0.0;

         ChannelBuffer2[i]=0.0;

        }

      limit=start;

     }

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

     {

      double highest=DBL_MIN;

      if(JawsBuffer[i-Inp_Alligator_jaw_shift]>highest)

         highest=JawsBuffer[i-Inp_Alligator_jaw_shift];

      if(TeethBuffer[i-Inp_Alligator_teeth_shift]>highest)

         highest=TeethBuffer[i-Inp_Alligator_teeth_shift];

      if(LipsBuffer[i-Inp_Alligator_lips_shift]>highest)

         highest=LipsBuffer[i-Inp_Alligator_lips_shift];

      //---

      double lowest=DBL_MAX;

      if(JawsBuffer[i-Inp_Alligator_jaw_shift]<lowest)

         lowest=JawsBuffer[i-Inp_Alligator_jaw_shift];

      if(TeethBuffer[i-Inp_Alligator_teeth_shift]<lowest)

         lowest=TeethBuffer[i-Inp_Alligator_teeth_shift];

      if(LipsBuffer[i-Inp_Alligator_lips_shift]<lowest)

         lowest=LipsBuffer[i-Inp_Alligator_lips_shift];

      ChannelBuffer1[i]=highest;

      ChannelBuffer2[i]=lowest;

     }

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

   return(rates_total);

  }

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

//| Filling indicator buffers from the iAlligator indicator          |

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

bool FillArraysFromBuffers(double &jaws_buffer[],  // indicator buffer for the Jaw line

                           int j_shift,            // shift of the Jaw line

                           double &teeth_buffer[], // indicator buffer for the Teeth line

                           int t_shift,            // shift of the Teeth line

                           double &lips_buffer[],  // indicator buffer for the Lips line

                           int l_shift,            // shift of the Lips line

                           int ind_handle,         // handle of the iAlligator indicator

                           int amount              // number of copied values

                          )

  {

//--- reset error code

   ResetLastError();

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

   if(CopyBuffer(ind_handle,0,-j_shift,amount,jaws_buffer)<0)

     {

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

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

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

      return(false);

     }

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

   if(CopyBuffer(ind_handle,1,-t_shift,amount,teeth_buffer)<0)

     {

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

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

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

      return(false);

     }

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

   if(CopyBuffer(ind_handle,2,-l_shift,amount,lips_buffer)<0)

     {

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

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

      IndicatorRelease(handle_iAlligator);

  }

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

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