2 MAs Other TimeFrame

Author: Copyright © 2020, Vladimir Karputov
Indicators Used
Moving average indicator
Miscellaneous
It issuies visual alerts to the screen
0 Views
0 Downloads
0 Favorites
2 MAs Other TimeFrame
ÿþ//+------------------------------------------------------------------+

//|                                        2 MAs Other TimeFrame.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   2

#property indicator_plots     2

//--- the iMA 1 plot

#property indicator_label1  "MA"

#property indicator_type1   DRAW_SECTION

#property indicator_color1  clrMediumPurple

#property indicator_style1  STYLE_SOLID

#property indicator_width1  2

//--- the iMA 2 plot

#property indicator_label2  "MA"

#property indicator_type2   DRAW_SECTION

#property indicator_color2  clrGold

#property indicator_style2  STYLE_SOLID

#property indicator_width2  2

//--- indicator buffers

double   iMABuffer_1[];

double   iMABuffer_2[];

//--- input parameters

input group             "MA 1"

input ENUM_TIMEFRAMES      Inp_MA_period_1         = PERIOD_D1;   // MA 1: timeframe

input int                  Inp_MA_ma_period_1      = 12;          // MA 1: averaging period

input int                  Inp_MA_ma_shift_1       = 0;           // MA 1: horizontal shift

input ENUM_MA_METHOD       Inp_MA_ma_method_1      = MODE_SMA;    // MA 1: smoothing type

input ENUM_APPLIED_PRICE   Inp_MA_applied_price_1  = PRICE_CLOSE; // MA 1: type of price

input group             "MA 2"

input ENUM_TIMEFRAMES      Inp_MA_period_2         = PERIOD_H1;   // MA 2: timeframe

input int                  Inp_MA_ma_period_2      = 12;          // MA 2: averaging period

input int                  Inp_MA_ma_shift_2       = 0;           // MA 2: horizontal shift

input ENUM_MA_METHOD       Inp_MA_ma_method_2      = MODE_SMA;    // MA 2: smoothing type

input ENUM_APPLIED_PRICE   Inp_MA_applied_price_2  = PRICE_CLOSE; // MA 2: type of price

//---

int      handle_iMA_1   = INVALID_HANDLE;    // variable for storing the handle of the iMA indicator

int      handle_iMA_2   = INVALID_HANDLE;    // variable for storing the handle of the iMA indicator

bool     m_init_error   = false;             // error on InInit

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- assignment of array to indicator buffer

   SetIndexBuffer(0,iMABuffer_1,INDICATOR_DATA);

   SetIndexBuffer(1,iMABuffer_2,INDICATOR_DATA);

//--- set shift

   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);

//---

   if(Inp_MA_period_1==PERIOD_CURRENT || Inp_MA_period_1<=Period())

     {

      string err_text=(TerminalInfoString(TERMINAL_LANGUAGE)=="Russian")?

                      "'MA 1: timeframe' =5 <>65B 1KBL <5=LH5 8;8 @02=> ('<=') B5:CI53> B09<D@59<0!":

                      "'MA 1: timeframe' cannot be less or equal ('<=') of the current timeframe!";

      if(MQLInfoInteger(MQL_TESTER)) // when testing, we will only output to the log about incorrect input parameters

         Print(__FILE__," ",__FUNCTION__,", ERROR: ",err_text);

      else // if the Expert Advisor is run on the chart, tell the user about the error

         Alert(__FILE__," ",__FUNCTION__,", ERROR: ",err_text);

      //---

      m_init_error=true;

      return(INIT_SUCCEEDED);

     }

   if(Inp_MA_period_2==PERIOD_CURRENT || Inp_MA_period_2<=Period())

     {

      string err_text=(TerminalInfoString(TERMINAL_LANGUAGE)=="Russian")?

                      "'MA 2: timeframe' =5 <>65B 1KBL <5=LH5 8;8 @02=> ('<=') B5:CI53> B09<D@59<0!":

                      "'MA 2: timeframe' cannot be less or equal ('<=') of the current timeframe!";

      if(MQLInfoInteger(MQL_TESTER)) // when testing, we will only output to the log about incorrect input parameters

         Print(__FILE__," ",__FUNCTION__,", ERROR: ",err_text);

      else // if the Expert Advisor is run on the chart, tell the user about the error

         Alert(__FILE__," ",__FUNCTION__,", ERROR: ",err_text);

      //---

      m_init_error=true;

      return(INIT_SUCCEEDED);

     }

//--- create handle of the indicator iMA

   handle_iMA_1=iMA(Symbol(),Inp_MA_period_1,Inp_MA_ma_period_1,Inp_MA_ma_shift_1,

                    Inp_MA_ma_method_1,Inp_MA_applied_price_1);

//--- if the handle is not created

   if(handle_iMA_1==INVALID_HANDLE)

     {

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

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

                  Symbol(),

                  EnumToString(Inp_MA_period_1),

                  GetLastError());

      //--- the indicator is stopped early

      return(INIT_FAILED);

     }

//--- create handle of the indicator iMA

   handle_iMA_2=iMA(Symbol(),Inp_MA_period_2,Inp_MA_ma_period_2,Inp_MA_ma_shift_2,

                    Inp_MA_ma_method_2,Inp_MA_applied_price_2);

//--- if the handle is not created

   if(handle_iMA_2==INVALID_HANDLE)

     {

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

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

                  Symbol(),

                  EnumToString(Inp_MA_period_2),

                  GetLastError());

      //--- the indicator is stopped early

      return(INIT_FAILED);

     }

//--- show the symbol/timeframe the Moving Average indicator is calculated for

   string  short_name=StringFormat("iMA(%s/%s, %d, %d, %s, %s)",Symbol(),EnumToString(Inp_MA_period_1),

                                   Inp_MA_ma_period_1, Inp_MA_ma_shift_1,EnumToString(Inp_MA_ma_method_1),EnumToString(Inp_MA_applied_price_1));

   PlotIndexSetString(0,PLOT_LABEL,"iMA("+Symbol()+"/"+StringSubstr(EnumToString(Inp_MA_period_1),7,-1)+")");

   short_name=StringFormat("iMA(%s/%s, %d, %d, %s, %s)",Symbol(),EnumToString(Inp_MA_period_2),

                           Inp_MA_ma_period_2, Inp_MA_ma_shift_2,EnumToString(Inp_MA_ma_method_2),EnumToString(Inp_MA_applied_price_2));

   PlotIndexSetString(1,PLOT_LABEL,"iMA("+Symbol()+"/"+StringSubstr(EnumToString(Inp_MA_period_2),7,-1)+")");

//---

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

//--- main loop

   int limit=prev_calculated-1;

   if(prev_calculated==0)

      limit=0;

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

     {

      //--- MA 1

      int i_bar_shift=iBarShift(Symbol(),Inp_MA_period_1,time[i],false);

      if(i_bar_shift<0)

        {

         iMABuffer_1[i]=0.0;

         iMABuffer_2[i]=0.0;

         return(0);

        }

      else

        {

         datetime i_time=iTime(Symbol(),Inp_MA_period_1,i_bar_shift);

         if(i_time==D'1970.01.01 00:00')

           {

            iMABuffer_1[i]=0.0;

            iMABuffer_2[i]=0.0;

            return(0);

           }

         else

           {

            double arr_ma[1];

            int copy_buffer=CopyBuffer(handle_iMA_1,0,i_time,1,arr_ma);

            if(copy_buffer<1)

              {

               iMABuffer_1[i]=0.0;

               iMABuffer_2[i]=0.0;

               return(0);

              }

            else

              {

               iMABuffer_1[i]=arr_ma[0];

              }

           }

        }

      //--- MA 2

      i_bar_shift=iBarShift(Symbol(),Inp_MA_period_2,time[i],false);

      if(i_bar_shift<0)

        {

         iMABuffer_1[i]=0.0;

         iMABuffer_2[i]=0.0;

         return(0);

        }

      else

        {

         datetime i_time=iTime(Symbol(),Inp_MA_period_2,i_bar_shift);

         if(i_time==D'1970.01.01 00:00')

           {

            iMABuffer_1[i]=0.0;

            iMABuffer_2[i]=0.0;

            return(0);

           }

         else

           {

            double arr_ma[1];

            int copy_buffer=CopyBuffer(handle_iMA_2,0,i_time,1,arr_ma);

            if(copy_buffer<1)

              {

               iMABuffer_1[i]=0.0;

               iMABuffer_2[i]=0.0;

               return(0);

              }

            else

              {

               iMABuffer_2[i]=arr_ma[0];

              }

           }

        }

     }

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

   return(rates_total);

  }

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

//| Indicator deinitialization function                              |

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

void OnDeinit(const int reason)

  {

   if(handle_iMA_1!=INVALID_HANDLE)

      IndicatorRelease(handle_iMA_1);

  }

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

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