iAlligator Alert

Author: Copyright © 2020, Vladimir Karputov
Price Data Components
Indicators Used
Moving average indicator
Miscellaneous
It issuies visual alerts to the screen
0 Views
0 Downloads
0 Favorites
iAlligator Alert
ÿþ//+------------------------------------------------------------------+

//|                                             iAlligator Alert.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 5

#property indicator_plots   5

//--- plot "Jaws"

#property indicator_label1  "Jaws"

#property indicator_type1   DRAW_LINE

#property indicator_color1  clrBlue

#property indicator_width1  1

//--- plot "Teeth"

#property indicator_label2  "Teeth"

#property indicator_type2   DRAW_LINE

#property indicator_color2  clrRed

#property indicator_width2  1

//--- plot "Lips"

#property indicator_label3  "Lips"

#property indicator_type3   DRAW_LINE

#property indicator_color3  clrLime

#property indicator_width3  1

//--- plot UP

#property indicator_label4  "UP"

#property indicator_type4   DRAW_ARROW

#property indicator_color4  clrRoyalBlue

#property indicator_style4  STYLE_SOLID

#property indicator_width4  1

//--- plot DOWN

#property indicator_label5  "DOWN"

#property indicator_type5   DRAW_ARROW

#property indicator_color5  clrRed

#property indicator_style5  STYLE_SOLID

#property indicator_width5  1

//--- Alligator 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   UPBuffer[];

double   DOWNBuffer[];

//--- handles for moving averages

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

//--- bars minimum for calculation

int      m_bars_minimum;

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- indicator buffers mapping

   SetIndexBuffer(0,JawsBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,TeethBuffer,INDICATOR_DATA);

   SetIndexBuffer(2,LipsBuffer,INDICATOR_DATA);

   SetIndexBuffer(3,UPBuffer,INDICATOR_DATA);

   SetIndexBuffer(4,DOWNBuffer,INDICATOR_DATA);

//--- set accuracy

   IndicatorSetInteger(INDICATOR_DIGITS,Digits());

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

   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,Inp_Alligator_jaw_period-1);

   PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,Inp_Alligator_teeth_period-1);

   PlotIndexSetInteger(2,PLOT_DRAW_BEGIN,Inp_Alligator_lips_period-1);

   PlotIndexSetInteger(3,PLOT_DRAW_BEGIN,Inp_Alligator_jaw_period-1);

   PlotIndexSetInteger(4,PLOT_DRAW_BEGIN,Inp_Alligator_jaw_period-1);

//--- name for DataWindow

   PlotIndexSetString(0,PLOT_LABEL,"Jaws("+string(Inp_Alligator_jaw_period)+")");

   PlotIndexSetString(1,PLOT_LABEL,"Teeth("+string(Inp_Alligator_teeth_period)+")");

   PlotIndexSetString(2,PLOT_LABEL,"Lips("+string(Inp_Alligator_lips_period)+")");

//--- setting a code from the Wingdings charset as the property of PLOT_ARROW

   PlotIndexSetInteger(3,PLOT_ARROW,241);

   PlotIndexSetInteger(4,PLOT_ARROW,242);

//--- Set the vertical shift of arrows in pixels

   PlotIndexSetInteger(3,PLOT_ARROW_SHIFT,9);

   PlotIndexSetInteger(4,PLOT_ARROW_SHIFT,-9);

//--- Set as an empty value 0.0

   PlotIndexSetDouble(3,PLOT_EMPTY_VALUE,0.0);

   PlotIndexSetDouble(4,PLOT_EMPTY_VALUE,0.0);

//--- create handle of the indicator iMA 'Jaws'

   handle_iMA_Jaws=iMA(Symbol(),Period(),Inp_Alligator_jaw_period,0,Inp_Alligator_ma_method,Inp_Alligator_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 'Teeth'

   handle_iMA_Teeth=iMA(Symbol(),Period(),Inp_Alligator_teeth_period,0,Inp_Alligator_ma_method,Inp_Alligator_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 'Lips'

   handle_iMA_Lips=iMA(Symbol(),Period(),Inp_Alligator_lips_period,0,Inp_Alligator_ma_method,Inp_Alligator_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);

     }

//--- bars minimum for calculation

   m_bars_minimum=Inp_Alligator_jaw_period+Inp_Alligator_jaw_shift;

   if(m_bars_minimum<(Inp_Alligator_teeth_period+Inp_Alligator_teeth_shift))

      m_bars_minimum=Inp_Alligator_teeth_period+Inp_Alligator_teeth_shift;

   if(m_bars_minimum<(Inp_Alligator_lips_period+Inp_Alligator_lips_period))

      m_bars_minimum=Inp_Alligator_lips_period+Inp_Alligator_lips_period;

//---

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

  {

//--- check for rates total

   if(rates_total<m_bars_minimum)

      return(0); // not enough bars for calculation

//--- not all data may be calculated

   int calculated=BarsCalculated(handle_iMA_Jaws);

   if(calculated<rates_total)

     {

      Print("Not all data of handle_iMA_Jaws is calculated (",calculated,"bars ). Error",GetLastError());

      return(0);

     }

   calculated=BarsCalculated(handle_iMA_Teeth);

   if(calculated<rates_total)

     {

      Print("Not all data of handle_iMA_Teeth is calculated (",calculated,"bars ). Error",GetLastError());

      return(0);

     }

   calculated=BarsCalculated(handle_iMA_Lips);

   if(calculated<rates_total)

     {

      Print("Not all data of handle_iMA_Lips is calculated (",calculated,"bars ). Error",GetLastError());

      return(0);

     }

//--- we can copy not all data

   int to_copy;

   if(prev_calculated>rates_total || prev_calculated<0)

      to_copy=rates_total;

   else

     {

      to_copy=rates_total-prev_calculated;

      if(prev_calculated>0)

         to_copy++;

     }

//---- get ma buffers

   if(IsStopped())

      return(0); //Checking for stop flag

   if(CopyBuffer(handle_iMA_Jaws,0,0,to_copy,JawsBuffer)<=0)

     {

      Print("getting handle_iMA_Jaws is failed! Error",GetLastError());

      return(0);

     }

   if(IsStopped())

      return(0); //Checking for stop flag

   if(CopyBuffer(handle_iMA_Teeth,0,0,to_copy,TeethBuffer)<=0)

     {

      Print("getting handle_iMA_Teeth is failed! Error",GetLastError());

      return(0);

     }

   if(IsStopped())

      return(0); //Checking for stop flag

   if(CopyBuffer(handle_iMA_Lips,0,0,to_copy,LipsBuffer)<=0)

     {

      Print("getting handle_iMA_Lips is failed! Error",GetLastError());

      return(0);

     }

//--- main loop

   int limit=prev_calculated;

   if(prev_calculated==0)

     {

      UPBuffer[0]=0.0;

      UPBuffer[1]=0.0;

      DOWNBuffer[0]=0.0;

      DOWNBuffer[1]=0.0;

      limit=2;

     }

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

     {

      UPBuffer[i]=0.0;

      DOWNBuffer[i]=0.0;

      //--- UP

      if(

         (high[i-2]>JawsBuffer[i-2] && low[i-2]>JawsBuffer[i-2]) &&

         (high[i-1]>JawsBuffer[i-1] && low[i-1]>JawsBuffer[i-1]) &&

         (open[i-2]>close[i-2] && open[i-1]>close[i-1]))

        {

         if(prev_calculated>0)

            Alert(Symbol(),",",StringSubstr(EnumToString(Period()),7)," ",time[i]," UP");

         UPBuffer[i-2]=low[i-2];

         UPBuffer[i-1]=low[i-1];

         DOWNBuffer[i-2]=0.0;

         DOWNBuffer[i-1]=0.0;

        }

      //--- DOWN

      if(

         (high[i-2]<JawsBuffer[i-2] && low[i-2]<JawsBuffer[i-2]) &&

         (high[i-1]<JawsBuffer[i-1] && low[i-1]<JawsBuffer[i-1]) &&

         (open[i-2]<close[i-2] && open[i-1]<close[i-1]))

        {

         if(prev_calculated>0)

            Alert(Symbol(),",",StringSubstr(EnumToString(Period()),7)," ",time[i]," DOWN");

         UPBuffer[i-2]=0.0;

         UPBuffer[i-1]=0.0;

         DOWNBuffer[i-2]=high[i-2];

         DOWNBuffer[i-1]=high[i-1];

        }

     }

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

   return(rates_total);

  }

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

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