RSIOnAlligator

Author: Copyright © 2020, Vladimir Karputov
Price Data Components
Indicators Used
Moving average indicatorRelative strength index
0 Views
0 Downloads
0 Favorites
RSIOnAlligator
ÿþ//+------------------------------------------------------------------+

//|                                               RSIOnAlligator.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_separate_window

#property indicator_buffers 3

#property indicator_plots   3

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

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

input group             "RSI"

input int                  Inp_RSI_ma_period    = 3;           // RSI: averaging period

input ENUM_APPLIED_PRICE   Inp_RSI_applied_price= PRICE_CLOSE; // RSI: type of price

//--- indicator buffers

double   JawsBuffer[];

double   TeethBuffer[];

double   LipsBuffer[];

//---

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      handle_iRSI_Jaws;                      // variable for storing the handle of the iRSI indicator

int      handle_iRSI_Teeth;                     // variable for storing the handle of the iRSI indicator

int      handle_iRSI_Lips;                      // variable for storing the handle of the iRSI indicator

//---

int      bars_calculated=0;                     // we will keep the number of values in the Relative Strength Index indicator

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- indicator buffers mapping

   SetIndexBuffer(0,JawsBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,TeethBuffer,INDICATOR_DATA);

   SetIndexBuffer(2,LipsBuffer,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);

//--- line shifts when drawing

   PlotIndexSetInteger(0,PLOT_SHIFT,Inp_Alligator_jaw_shift);

   PlotIndexSetInteger(1,PLOT_SHIFT,Inp_Alligator_teeth_shift);

   PlotIndexSetInteger(2,PLOT_SHIFT,Inp_Alligator_lips_shift);

//--- name for DataWindow

   PlotIndexSetString(0,PLOT_LABEL,"RSIOnJaws("+IntegerToString(Inp_Alligator_jaw_period)+")");

   PlotIndexSetString(1,PLOT_LABEL,"RSIOnTeeth("+IntegerToString(Inp_Alligator_teeth_period)+")");

   PlotIndexSetString(2,PLOT_LABEL,"RSIOnLips("+IntegerToString(Inp_Alligator_lips_period)+")");

//--- create handle of the indicator iMA

   handle_iMA_Jaws=iMA(Symbol(),Period(),Inp_Alligator_jaw_period,Inp_Alligator_jaw_shift,

                       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 ('Jaws') indicator 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_Alligator_teeth_period,Inp_Alligator_teeth_shift,

                        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 ('Teeth') indicator 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_Alligator_lips_period,Inp_Alligator_lips_shift,

                       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 ('Lips') indicator 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 iRSI

   handle_iRSI_Jaws=iRSI(Symbol(),Period(),Inp_RSI_ma_period,handle_iMA_Jaws);

//--- if the handle is not created

   if(handle_iRSI_Jaws==INVALID_HANDLE)

     {

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

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

   handle_iRSI_Teeth=iRSI(Symbol(),Period(),Inp_RSI_ma_period,handle_iMA_Teeth);

//--- if the handle is not created

   if(handle_iRSI_Teeth==INVALID_HANDLE)

     {

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

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

   handle_iRSI_Lips=iRSI(Symbol(),Period(),Inp_RSI_ma_period,handle_iMA_Lips);

//--- if the handle is not created

   if(handle_iRSI_Lips==INVALID_HANDLE)

     {

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

      PrintFormat("Failed to create handle of the iRSI ('Lips') 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[])

  {

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

   int values_to_copy;

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

   int calculated_jaw=BarsCalculated(handle_iRSI_Jaws);

   if(calculated_jaw<=0)

     {

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

      return(0);

     }

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

   int calculated_teeth=BarsCalculated(handle_iRSI_Teeth);

   if(calculated_teeth<=0)

     {

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

      return(0);

     }

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

   int calculated_lips=BarsCalculated(handle_iRSI_Lips);

   if(calculated_lips<=0)

     {

      PrintFormat("BarsCalculated(handle_iRSI_teeth) returned %d, error code %d",calculated_lips,GetLastError());

      return(0);

     }

   if(calculated_jaw!=calculated_teeth || calculated_jaw!=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 iRSI 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 iRSIBuffer array is greater than the number of values in the iRSI 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 array with values of the iRSI indicator

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

   if(!FillArrayFromBuffer(JawsBuffer,handle_iRSI_Jaws,values_to_copy))

      return(0);

   if(!FillArrayFromBuffer(TeethBuffer,handle_iRSI_Teeth,values_to_copy))

      return(0);

   if(!FillArrayFromBuffer(LipsBuffer,handle_iRSI_Lips,values_to_copy))

      return(0);

//--- memorize the number of values in the Relative Strength Index indicator

   bars_calculated=calculated;

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

   return(rates_total);

  }

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

//| Filling indicator buffers from the iRSI indicator                |

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

bool FillArrayFromBuffer(double &rsi_buffer[],  // indicator buffer of Relative Strength Index values

                         int ind_handle,        // handle of the iRSI indicator

                         int amount             // number of copied values

                        )

  {

//--- reset error code

   ResetLastError();

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

   if(CopyBuffer(ind_handle,0,0,amount,rsi_buffer)<0)

     {

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

      PrintFormat("Failed to copy data from the iRSI 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_Jaws);

   if(handle_iMA_Lips!=INVALID_HANDLE)

      IndicatorRelease(handle_iMA_Jaws);

   if(handle_iRSI_Jaws!=INVALID_HANDLE)

      IndicatorRelease(handle_iMA_Jaws);

   if(handle_iRSI_Teeth!=INVALID_HANDLE)

      IndicatorRelease(handle_iMA_Jaws);

   if(handle_iRSI_Lips!=INVALID_HANDLE)

      IndicatorRelease(handle_iMA_Jaws);

  }

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

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