Two MA Other TimeFrame Filling Correct

Author: Copyright © 2021, Vladimir Karputov
Indicators Used
Moving average indicator
Miscellaneous
It issuies visual alerts to the screen
0 Views
0 Downloads
0 Favorites
Two MA Other TimeFrame Filling Correct
ÿþ//+------------------------------------------------------------------+

//|                       Two MA Other TimeFrame Filling Correct.mq5 |

//|                              Copyright © 2021, Vladimir Karputov |

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

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

#property copyright "Copyright © 2021, 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     1

//--- MA Filling Correct

#property indicator_label1  "MA Filling Correct"

#property indicator_type1   DRAW_FILLING

#property indicator_color1  clrMediumPurple,clrLimeGreen

#property indicator_width1  1

//--- indicator buffer

double   iMAFastBuffer[];

double   iMASlowBuffer[];

//--- input parameters

input group             "MAs"

input ENUM_TIMEFRAMES      Inp_MA_period_other        = PERIOD_D1;   // MA: other timeframe

input group             "MA Fast"

input int                  Inp_MA_Fast_ma_period      = 12;          // MA Fast: averaging period

input ENUM_MA_METHOD       Inp_MA_Fast_ma_method      = MODE_SMA;    // MA Fast: smoothing type

input ENUM_APPLIED_PRICE   Inp_MA_Fast_applied_price  = PRICE_CLOSE; // MA Fast: type of price

input group             "MA Slow"

input int                  Inp_MA_Slow_ma_period      = 36;          // MA Slow: averaging period

input ENUM_MA_METHOD       Inp_MA_Slow_ma_method      = MODE_SMA;    // MA Slow: smoothing type

input ENUM_APPLIED_PRICE   Inp_MA_Slow_applied_price  = PRICE_CLOSE; // MA Slow: type of price

//---

datetime m_prev_bars       = 0;                 // "0" -> D'1970.01.01 00:00';

datetime m_prev_bars_other = 0;                 // "0" -> D'1970.01.01 00:00';

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

int      handle_iMA_Slow   = 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,iMAFastBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,iMASlowBuffer,INDICATOR_DATA);

//--- set shift

   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);

//PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,0.0);

//---

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

     {

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

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

                      "'MA: 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_Fast=iMA(Symbol(),Inp_MA_period_other,Inp_MA_Fast_ma_period,0,

                       Inp_MA_Fast_ma_method,Inp_MA_Fast_applied_price);

//--- if the handle is not created

   if(handle_iMA_Fast==INVALID_HANDLE)

     {

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

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

                  Symbol(),

                  EnumToString(Inp_MA_period_other),

                  GetLastError());

      //--- the indicator is stopped early

      return(INIT_FAILED);

     }

//--- create handle of the indicator iMA

   handle_iMA_Slow=iMA(Symbol(),Inp_MA_period_other,Inp_MA_Slow_ma_period,0,

                       Inp_MA_Slow_ma_method,Inp_MA_Slow_applied_price);

//--- if the handle is not created

   if(handle_iMA_Slow==INVALID_HANDLE)

     {

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

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

                  Symbol(),

                  EnumToString(Inp_MA_period_other),

                  GetLastError());

      //--- the indicator is stopped early

      return(INIT_FAILED);

     }

   string period="("+StringSubstr(EnumToString(Inp_MA_period_other),7,-1)+")";

   PlotIndexSetString(0,PLOT_LABEL,"MA Fast "+period+";"+"MA Slow "+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[])

  {

   if(m_init_error)

      return(0);

//--- main loop

   int limit=prev_calculated-2;

   if(prev_calculated==0)

      limit=0;

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

     {

      //--- MA Fast

      double price_other;

      datetime time_other;

      if(!GetPriceOther(time[i],price_other,time_other,handle_iMA_Fast))

        {

         iMAFastBuffer[i]=0.0;

         m_prev_bars_other=0;

         return(0);

        }

      iMAFastBuffer[i]=price_other;

      m_prev_bars_other=time_other;

      for(int j=i; j>=0; j--)

        {

         if(!GetPriceOther(time[j],price_other,time_other,handle_iMA_Fast))

           {

            iMAFastBuffer[j]=0.0;

            m_prev_bars_other=0;

            return(0);

           }

         if(m_prev_bars_other!=time_other)

            break;

         //---

         iMAFastBuffer[j]=price_other;

        }

      //--- MA Slow

      price_other=0.0;

      time_other=0;

      if(!GetPriceOther(time[i],price_other,time_other,handle_iMA_Slow))

        {

         iMASlowBuffer[i]=0.0;

         m_prev_bars_other=0;

         return(0);

        }

      iMASlowBuffer[i]=price_other;

      m_prev_bars_other=time_other;

      for(int j=i; j>=0; j--)

        {

         if(!GetPriceOther(time[j],price_other,time_other,handle_iMA_Slow))

           {

            iMASlowBuffer[j]=0.0;

            m_prev_bars_other=0;

            return(0);

           }

         if(m_prev_bars_other!=time_other)

            break;

         //---

         iMASlowBuffer[j]=price_other;

        }

     }

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

   return(rates_total);

  }

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

//| Indicator deinitialization function                              |

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

void OnDeinit(const int reason)

  {

   if(handle_iMA_Fast!=INVALID_HANDLE)

      IndicatorRelease(handle_iMA_Fast);

   if(handle_iMA_Slow!=INVALID_HANDLE)

      IndicatorRelease(handle_iMA_Slow);

  }

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

//| Get Price Other                                                  |

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

bool GetPriceOther(const datetime time_current,double &price,datetime &time,const int handle)

  {

   int i_bar_shift_other=iBarShift(Symbol(),Inp_MA_period_other,time_current,false);

   if(i_bar_shift_other<0)

     {

      return(false);

     }

   else

     {

      datetime i_time_other=iTime(Symbol(),Inp_MA_period_other,i_bar_shift_other);

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

        {

         return(false);

        }

      else

        {

         if(BarsCalculated(handle)<=0)

            return(false);

         double arr_ma[];

         int copy_buffer=CopyBuffer(handle,0,i_time_other,1,arr_ma);

         if(CopyBuffer(handle,0,i_time_other,1,arr_ma)!=1)

           {

            return(false) ;

           }

         else

           {

            price=arr_ma[0];

            time=i_time_other;

           }

        }

     }

//---

   return(true);

  }

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

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