Gator Arrow

Author: Copyright © 2021, Vladimir Karputov
Indicators Used
Gator oscillator
1 Views
0 Downloads
0 Favorites
Gator Arrow
ÿþ//+------------------------------------------------------------------+

//|                                                  Gator Arrow.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 8

#property indicator_plots   4

//--- plot Dream

#property indicator_label1  "Dream"

#property indicator_type1   DRAW_ARROW

#property indicator_color1  clrLightSeaGreen

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- plot Food

#property indicator_label2  "Food"

#property indicator_type2   DRAW_ARROW

#property indicator_color2  clrDeepSkyBlue

#property indicator_style2  STYLE_SOLID

#property indicator_width2  1

//--- plot StopBuy

#property indicator_label3  "StopBuy"

#property indicator_type3   DRAW_ARROW

#property indicator_color3  clrRed

#property indicator_style3  STYLE_SOLID

#property indicator_width3  1

//--- plot StopSell

#property indicator_label4  "StopSell"

#property indicator_type4   DRAW_ARROW

#property indicator_color4  clrBlue

#property indicator_style4  STYLE_SOLID

#property indicator_width4  1

//--- input parameters

input group             "Gator"

input int                  Inp_Gator_jaw_period          = 13;             // Gator: period for the calculation of jaws

input int                  Inp_Gator_jaw_shift           = 8;              // Gator: horizontal shift of jaws

input int                  Inp_Gator_teeth_period        = 8;              // Gator: period for the calculation of teeth

input int                  Inp_Gator_teeth_shift         = 5;              // Gator: horizontal shift of teeth

input int                  Inp_Gator_lips_period         = 5;              // Gator: period for the calculation of lips

input int                  Inp_Gator_lips_shift          = 3;              // Gator: horizontal shift of lips

input ENUM_MA_METHOD       Inp_Gator_ma_method           = MODE_SMMA;      // Gator: type of smoothing

input ENUM_APPLIED_PRICE   Inp_Gator_applied_price       = PRICE_MEDIAN;   // Gator: type of price

//--- indicator buffers

double   DreamBuffer[];

double   FoodBuffer[];

double   StopBuyBuffer[];

double   StopSellBuffer[];

double   GatorUpBuffer[];

double   GatorUpColors[];

double   GatorDownBuffer[];

double   GatorDownColors[];

//---

int      handle_iGator;                         // variable for storing the handle of the iGator indicator

int      bars_calculated   = 0;                 // we will keep the number of values in the Gator Oscillator indicator

int      shift;                                 // shift values for the upper and lower histograms

bool     m_init_error      = false;             // error on InInit

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- indicator buffers mapping

   SetIndexBuffer(0,DreamBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,FoodBuffer,INDICATOR_DATA);

   SetIndexBuffer(2,StopBuyBuffer,INDICATOR_DATA);

   SetIndexBuffer(3,StopSellBuffer,INDICATOR_DATA);

   SetIndexBuffer(4,GatorUpBuffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(5,GatorUpColors,INDICATOR_CALCULATIONS);

   SetIndexBuffer(6,GatorDownBuffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(7,GatorDownColors,INDICATOR_CALCULATIONS);

//--- let's calculate the shift for the upper and lower histograms, that is equal to the difference between the Jaw line and the Teeth line

   shift=MathMin(Inp_Gator_jaw_shift,Inp_Gator_teeth_shift);

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

   PlotIndexSetInteger(0,PLOT_ARROW,76);

   PlotIndexSetInteger(1,PLOT_ARROW,74);

   PlotIndexSetInteger(2,PLOT_ARROW,238);

   PlotIndexSetInteger(3,PLOT_ARROW,236);

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

   PlotIndexSetInteger(0,PLOT_ARROW_SHIFT,-10);

   PlotIndexSetInteger(1,PLOT_ARROW_SHIFT,-10);

   PlotIndexSetInteger(2,PLOT_ARROW_SHIFT,-10);

   PlotIndexSetInteger(3,PLOT_ARROW_SHIFT,-10);

//--- Set as an empty value 0

   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);

   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,0.0);

   PlotIndexSetDouble(2,PLOT_EMPTY_VALUE,0.0);

   PlotIndexSetDouble(3,PLOT_EMPTY_VALUE,0.0);

//--- create handle of the indicator iGator

   handle_iGator=iGator(Symbol(),Period(),

                        Inp_Gator_jaw_period,Inp_Gator_jaw_shift,

                        Inp_Gator_teeth_period,Inp_Gator_teeth_shift,

                        Inp_Gator_lips_period,Inp_Gator_lips_shift,

                        Inp_Gator_ma_method,Inp_Gator_applied_price);

//--- if the handle is not created

   if(handle_iGator==INVALID_HANDLE)

     {

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

      PrintFormat("Failed to create handle of the iGator 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);

     }

//---

   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(m_init_error)

      return(0);

   if(rates_total<=shift)

      return(0);

//--- number of values copied from the iGator indicator

   int values_to_copy;

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

   int calculated=BarsCalculated(handle_iGator);

   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 iGator 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 GatorUpBuffer array is greater than the number of values in the iGator 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 Gator Oscillator indicator

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

   if(!FillArraysFromBuffers(GatorUpBuffer,GatorUpColors,GatorDownBuffer,GatorDownColors,shift,handle_iGator,values_to_copy))

      return(0);

//--- memorize the number of values in the Gator Oscillator indicator

   bars_calculated=calculated;

//--- main loop

   int limit=prev_calculated-1;

   if(prev_calculated==0)

     {

      limit=shift;

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

        {

         DreamBuffer[i]=0.0;

         FoodBuffer[i]=0.0;

         StopBuyBuffer[i]=0.0;

         StopSellBuffer[i]=0.0;

        }

     }

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

     {

      DreamBuffer[i]=0.0;

      FoodBuffer[i]=0.0;

      StopBuyBuffer[i]=0.0;

      StopSellBuffer[i]=0.0;

      if(GatorUpColors[i-shift]==1.0 && GatorDownColors[i-shift]==1.0)

        {

         DreamBuffer[i]=high[i];

         continue;

        }

      if(GatorUpColors[i-shift]==0.0 && GatorDownColors[i-shift]==0.0)

        {

         FoodBuffer[i]=high[i];

         continue;

        }

      if(GatorUpColors[i-shift]==0.0 && GatorDownColors[i-shift]==1.0)

        {

         StopBuyBuffer[i]=high[i];

         continue;

        }

      if(GatorUpColors[i-shift]==1.0 && GatorDownColors[i-shift]==0.0)

        {

         StopSellBuffer[i]=high[i];

         continue;

        }

     }

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

   return(rates_total);

  }

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

//| Filling indicator buffers from the iGator indicator              |

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

bool FillArraysFromBuffers(double &ups_buffer[],         // indicator buffer for the upper histogram

                           double &up_color_buffer[],    // indicator buffer for price indexes of the upper histogram

                           double &downs_buffer[],       // indicator buffer for the lower histogram

                           double &downs_color_buffer[], // indicator buffer for price indexes of the lower histogram

                           int u_shift,                  // shift for the upper and lower histogram

                           int ind_handle,               // handle of the iGator indicator

                           int amount                    // number of copied values

                          )

  {

//--- reset error code

   ResetLastError();

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

   if(CopyBuffer(ind_handle,0,-u_shift,amount,ups_buffer)<0)

     {

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

      PrintFormat("Failed to copy data from the iGator 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 GatorUpColors array with values from the indicator buffer that has index 1

   if(CopyBuffer(ind_handle,1,-u_shift,amount,up_color_buffer)<0)

     {

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

      PrintFormat("Failed to copy data from the iGator 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 GatorDownBuffer array with values from the indicator buffer that has index 2

   if(CopyBuffer(ind_handle,2,-u_shift,amount,downs_buffer)<0)

     {

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

      PrintFormat("Failed to copy data from the iGator 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 GatorDownColors array with values from the indicator buffer that has index 3

   if(CopyBuffer(ind_handle,3,-u_shift,amount,downs_color_buffer)<0)

     {

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

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

      IndicatorRelease(handle_iGator);

  }

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

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