How to make multi time frame and multi currency pair scanner with i custom buffer alert for mt4 indicator

Terence Elive Uzoma Udalor
407

Hello,

I am new to Meta trader 4 programing . I have this moving average indicator that display Blue (buy) and  Red(sell) when certain conditions are met.

At the moment it works on just the current chart time frame.  I have  been difficulty  trying to make it scan multiple currency pairs accross multiple  time frame so as to save time finding pairs that meet condition of the inidcator.

It is to say which code will I need to add to current indicator code  so that ones the indicator is loaded in a single chart it will scan multiple time frames using the  list of currency pairs (major and minor) available in the Market Watch window or using preset currency pair input. Ones the indicator detects a currency pair, it will alert it in a dash board .

Please if you have any idea kindly share. Thank yo

Please code below:

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

//|                             Indicator: Double Ma above_cross.mq4 |

//|                                                                  |

//|                                                                  |

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



#include <stdlib.mqh>

#include <stderror.mqh>



//--- indicator settings

#property indicator_separate_window

#property indicator_buffers 2



#property indicator_type1 DRAW_ARROW

#property indicator_width1 2

#property indicator_color1 Blue

#property indicator_label1 "Buy"



#property indicator_type2 DRAW_ARROW

#property indicator_width2 2

#property indicator_color2 Red

#property indicator_label2 "Sell"



//--- indicator buffers

double Buffer1[];

double Buffer2[];



extern int EMA1 = 5;

extern int EMA2 = 20;

extern int EMA_Bar_Shift_5 = 0;

extern int EMA_Bar_Shift_20 = 0;

double myPoint; //initialized in OnInit



void myAlert(string type, string message)

  {

   if(type == "print")

      Print(message);

   else if(type == "error")

     {

      Print(type+" | Double Ma cross @ "+Symbol()+","+IntegerToString(Period())+" | "+message);

     }

   else if(type == "order")

     {

     }

   else if(type == "modify")

     {

     }

  }



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

//| Custom indicator initialization function                         |

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

int OnInit()

  {   

   IndicatorBuffers(2);

   SetIndexBuffer(0, Buffer1);

   SetIndexEmptyValue(0, 0);

   SetIndexArrow(0, 241);

   SetIndexBuffer(1, Buffer2);

   SetIndexEmptyValue(1, 0);

   SetIndexArrow(1, 242);

   //initialize myPoint

   myPoint = Point();

   if(Digits() == 5 || Digits() == 3)

     {

      myPoint *= 10;

     }

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

  {

   int limit = rates_total - prev_calculated;

   //--- counting from 0 to rates_total

   ArraySetAsSeries(Buffer1, true);

   ArraySetAsSeries(Buffer2, true);

   //--- initial zero

   if(prev_calculated < 1)

     {

      ArrayInitialize(Buffer1, 0);

      ArrayInitialize(Buffer2, 0);

     }

   else

      limit++;

   

   //--- main loop

   for(int i = limit-1; i >= 0; i--)

     {

      if (i >= MathMin(5000-1, rates_total-1-50)) continue; //omit some old rates to prevent "Array out of range" or slow calculation   

      //Indicator Buffer 1

      if((iMA(NULL, PERIOD_CURRENT, EMA1, 0, MODE_EMA, PRICE_CLOSE, EMA_Bar_Shift_5+i) > iMA(NULL, PERIOD_CURRENT, EMA2, 0, MODE_EMA, PRICE_CLOSE, EMA_Bar_Shift_20+i)

      && iMA(NULL, PERIOD_CURRENT, EMA1, 0, MODE_EMA, PRICE_CLOSE, EMA_Bar_Shift_5+i+1) < iMA(NULL, PERIOD_CURRENT, EMA2, 0, MODE_EMA, PRICE_CLOSE, EMA_Bar_Shift_20+i+1) //Moving Average crosses above Moving Average

      )||(iMA(NULL, PERIOD_CURRENT, EMA1, 0, MODE_EMA, PRICE_CLOSE, EMA_Bar_Shift_5+i+1) > iMA(NULL, PERIOD_CURRENT, EMA2, 0, MODE_EMA, PRICE_CLOSE, EMA_Bar_Shift_20+i+1)

      && iMA(NULL, PERIOD_CURRENT, EMA1, 0, MODE_EMA, PRICE_CLOSE, EMA_Bar_Shift_5+i+2) < iMA(NULL, PERIOD_CURRENT, EMA2, 0, MODE_EMA, PRICE_CLOSE, EMA_Bar_Shift_20+i+2) //Moving Average crosses above Moving Average

      )

      )

        {

         Buffer1[i] = Low[i] - iATR(NULL, PERIOD_CURRENT, 14, i); //Set indicator value at Candlestick Low - Average True Range

        }

      else

        {

         Buffer1[i] = 0;

        }

      //Indicator Buffer 2

      if((iMA(NULL, PERIOD_CURRENT, EMA1, 0, MODE_EMA, PRICE_CLOSE, EMA_Bar_Shift_5+i) < iMA(NULL, PERIOD_CURRENT, EMA2, 0, MODE_EMA, PRICE_CLOSE, EMA_Bar_Shift_20+i)

      && iMA(NULL, PERIOD_CURRENT, EMA1, 0, MODE_EMA, PRICE_CLOSE, EMA_Bar_Shift_5+i+1) > iMA(NULL, PERIOD_CURRENT, EMA2, 0, MODE_EMA, PRICE_CLOSE, EMA_Bar_Shift_20+i+1) //Moving Average crosses below Moving Average

      )||(iMA(NULL, PERIOD_CURRENT, EMA1, 0, MODE_EMA, PRICE_CLOSE, EMA_Bar_Shift_5+i+1) < iMA(NULL, PERIOD_CURRENT, EMA2, 0, MODE_EMA, PRICE_CLOSE, EMA_Bar_Shift_20+i+1)

      && iMA(NULL, PERIOD_CURRENT, EMA1, 0, MODE_EMA, PRICE_CLOSE, EMA_Bar_Shift_5+i+2) > iMA(NULL, PERIOD_CURRENT, EMA2, 0, MODE_EMA, PRICE_CLOSE, EMA_Bar_Shift_20+i+2) //Moving Average crosses below Moving Average

      )

      )

        {

         Buffer2[i] = High[i] + iATR(NULL, PERIOD_CURRENT, 14, i); //Set indicator value at Candlestick High + Average True Range

        }

      else

        {

         Buffer2[i] = 0;

        }

     }

   return(rates_total);

  }

//+------------------------------------------------------------------+
William Roeder
25844
William Roeder  

Elterry: At the moment it works on just the current chart time frame.  I have  been difficulty  trying to make it scan multiple currency pairs accross multiple  time

  1. Your indicator is fine.

  2. Have your EA handle 4066 errors and then read the indicator from whatever Symbols/TFs you want.

    On MT4: Unless the current chart is that specific symbol(s)/TF(s) referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
              Download history in MQL4 EA - Forex Calendar - MQL4 programming forum - Page 3 #26 № 4 2019.05.20


  3. If you code your EA to only process the current symbol then and only then can you use any predefined variables. Code it and put it on multiple charts.
    Predefined variables: Ask, Bars, Bid, Close[], High[], Low[], Open[], Point, Time[], Volume[]
              RefreshRates - Timeseries and Indicators Access - MQL4 Reference
              Predefined Variables - MQL4 Reference