Alligator Histogram Four Colors

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

//|                              Alligator Histogram Four Colors.mq5 |

//|                              Copyright © 2021, Vladimir Karputov |

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

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

#property copyright "Copyright © 2021, Vladimir Karputov"

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

#property version   "1.000"

#property indicator_separate_window

#property indicator_buffers 4

#property indicator_plots   1

//--- plot Alligator

#property indicator_label1  "Alligator"

#property indicator_type1   DRAW_COLOR_HISTOGRAM

#property indicator_color1  clrSilver,clrRed,clrBlue,clrOrange,clrYellow

#property indicator_style1  STYLE_SOLID

#property indicator_width1  2

//--- input parameters

input group             "Alligator"

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   AlligatorBuffer[];

double   AlligatorColors[];

double   JawsBuffer[];

double   LipsBuffer[];

//---

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

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- indicator buffers mapping

   SetIndexBuffer(0,AlligatorBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,AlligatorColors,INDICATOR_COLOR_INDEX);

   SetIndexBuffer(2,JawsBuffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(3,LipsBuffer,INDICATOR_CALCULATIONS);

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

   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,Inp_Alligator_jaw_period+Inp_Alligator_jaw_shift);

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

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

  {

   if(rates_total<Inp_Alligator_jaw_period+Inp_Alligator_jaw_shift)

      return(0);

   int values_to_copy;

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

   int calculated=BarsCalculated(handle_iAlligator);

   if(calculated<=0)

     {

      PrintFormat("BarsCalculated() 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,LipsBuffer,Inp_Alligator_lips_shift,handle_iAlligator,values_to_copy))

      return(0);

//--- memorize the number of values in the Alligator indicator

   bars_calculated=calculated;

//--- main loop

   int limit=prev_calculated-1;

   if(prev_calculated==0)

      limit=1;

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

     {

      AlligatorBuffer[i]=LipsBuffer[i]-JawsBuffer[i];

      /*

      'AlligatorBuffer' > previous AlligatorBuffer && 'AlligatorBuffer' < 0.0 -> 'Color'=1.0

      'AlligatorBuffer' > previous AlligatorBuffer && 'AlligatorBuffer' > 0.0 -> 'Color'=2.0

      'AlligatorBuffer' < previous AlligatorBuffer && 'AlligatorBuffer' > 0.0 -> 'Color'=3.0

      'AlligatorBuffer' < previous AlligatorBuffer && 'AlligatorBuffer' < 0.0 -> 'Color'=4.0

      */

      if(AlligatorBuffer[i]>AlligatorBuffer[i-1] && AlligatorBuffer[i]<0.0)

         AlligatorColors[i]=1.0;

      else

         if(AlligatorBuffer[i]>AlligatorBuffer[i-1] && AlligatorBuffer[i]>0.0)

            AlligatorColors[i]=2.0;

         else

            if(AlligatorBuffer[i]<AlligatorBuffer[i-1] && AlligatorBuffer[i]>0.0)

               AlligatorColors[i]=3.0;

            else

               if(AlligatorBuffer[i]<AlligatorBuffer[i-1] && AlligatorBuffer[i]<0.0)

                  AlligatorColors[i]=4.0;

     }

//--- 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 &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,GATORJAW_LINE,-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 LipsBuffer array with values from the indicator buffer that has index 2

   if(CopyBuffer(ind_handle,GATORLIPS_LINE,-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 ---