[MQL4 Code Help] Converting If else condition to function for more indicator & Time-frame flexibility

shanmugapradeep

Active Trader
Dec 18, 2020
128
7
34
39
I am redesigning EA, so it can have more accuracy and flexibility.

Original code :

MQL4:
    if(
                                  iRSI(gRandomSymbol,PERIOD_M1,RSI_Period,RSI_AppliedPrice,RSI_shift) > RSI_UpLevel &&
                                  iRSI(gRandomSymbol,PERIOD_M5,RSI_Period,RSI_AppliedPrice,RSI_shift) > RSI_UpLevel &&
                                  iRSI(gRandomSymbol,PERIOD_M15,RSI_Period,RSI_AppliedPrice,RSI_shift) > RSI_UpLevel &&
                                  iRSI(gRandomSymbol,PERIOD_M30,RSI_Period,RSI_AppliedPrice,RSI_shift) > RSI_UpLevel &&
                                  iRSI(gRandomSymbol,PERIOD_H1,RSI_Period,RSI_AppliedPrice,RSI_shift) > RSI_UpLevel &&
                                  iRSI(gRandomSymbol,PERIOD_H4,RSI_Period,RSI_AppliedPrice,RSI_shift) > RSI_UpLevel &&
 
                                  (iStochastic(gRandomSymbol,PERIOD_M1,Stochastic_Kperiod,Stochastic_Dperiod,Stochastic_Slowing,Stochastic_Method,Stochastic_PriceField,MODE_MAIN,Stochastic_Shift) > Stochastic_UpLevel && iStochastic(gRandomSymbol,PERIOD_M1,Stochastic_Kperiod,Stochastic_Dperiod,Stochastic_Slowing,Stochastic_Method,Stochastic_PriceField,MODE_SIGNAL,Stochastic_Shift) > Stochastic_UpLevel) &&
                                  (iStochastic(gRandomSymbol,PERIOD_M5,Stochastic_Kperiod,Stochastic_Dperiod,Stochastic_Slowing,Stochastic_Method,Stochastic_PriceField,MODE_MAIN,Stochastic_Shift) > Stochastic_UpLevel && iStochastic(gRandomSymbol,PERIOD_M5,Stochastic_Kperiod,Stochastic_Dperiod,Stochastic_Slowing,Stochastic_Method,Stochastic_PriceField,MODE_SIGNAL,Stochastic_Shift) > Stochastic_UpLevel) &&
                                  (iStochastic(gRandomSymbol,PERIOD_M15,Stochastic_Kperiod,Stochastic_Dperiod,Stochastic_Slowing,Stochastic_Method,Stochastic_PriceField,MODE_MAIN,Stochastic_Shift) > Stochastic_UpLevel && iStochastic(gRandomSymbol,PERIOD_M15,Stochastic_Kperiod,Stochastic_Dperiod,Stochastic_Slowing,Stochastic_Method,Stochastic_PriceField,MODE_SIGNAL,Stochastic_Shift) > Stochastic_UpLevel) &&
                                  (iStochastic(gRandomSymbol,PERIOD_M30,Stochastic_Kperiod,Stochastic_Dperiod,Stochastic_Slowing,Stochastic_Method,Stochastic_PriceField,MODE_MAIN,Stochastic_Shift) > Stochastic_UpLevel && iStochastic(gRandomSymbol,PERIOD_M30,Stochastic_Kperiod,Stochastic_Dperiod,Stochastic_Slowing,Stochastic_Method,Stochastic_PriceField,MODE_SIGNAL,Stochastic_Shift) > Stochastic_UpLevel) &&
                                  (iStochastic(gRandomSymbol,PERIOD_H1,Stochastic_Kperiod,Stochastic_Dperiod,Stochastic_Slowing,Stochastic_Method,Stochastic_PriceField,MODE_MAIN,Stochastic_Shift) > Stochastic_UpLevel && iStochastic(gRandomSymbol,PERIOD_H1,Stochastic_Kperiod,Stochastic_Dperiod,Stochastic_Slowing,Stochastic_Method,Stochastic_PriceField,MODE_SIGNAL,Stochastic_Shift) > Stochastic_UpLevel) &&
                                  (iStochastic(gRandomSymbol,PERIOD_H4,Stochastic_Kperiod,Stochastic_Dperiod,Stochastic_Slowing,Stochastic_Method,Stochastic_PriceField,MODE_MAIN,Stochastic_Shift) > Stochastic_UpLevel && iStochastic(gRandomSymbol,PERIOD_H4,Stochastic_Kperiod,Stochastic_Dperiod,Stochastic_Slowing,Stochastic_Method,Stochastic_PriceField,MODE_SIGNAL,Stochastic_Shift) > Stochastic_UpLevel) &&
 
                                  MarketInfo(gRandomSymbol, MODE_BID) > iBands(gRandomSymbol,PERIOD_M1,BollingerBands_Period,BollingerBands_deviation,BollingerBands_BandShift,BollingerBands_AppliedPrice,MODE_UPPER,BollingerBands_Shift) &&
                                  MarketInfo(gRandomSymbol, MODE_BID) > iBands(gRandomSymbol,PERIOD_M5,BollingerBands_Period,BollingerBands_deviation,BollingerBands_BandShift,BollingerBands_AppliedPrice,MODE_UPPER,BollingerBands_Shift) &&
                                  MarketInfo(gRandomSymbol, MODE_BID) > iBands(gRandomSymbol,PERIOD_M15,BollingerBands_Period,BollingerBands_deviation,BollingerBands_BandShift,BollingerBands_AppliedPrice,MODE_UPPER,BollingerBands_Shift) &&
                                  MarketInfo(gRandomSymbol, MODE_BID) > iBands(gRandomSymbol,PERIOD_M30,BollingerBands_Period,BollingerBands_deviation,BollingerBands_BandShift,BollingerBands_AppliedPrice,MODE_UPPER,BollingerBands_Shift) &&
                                  MarketInfo(gRandomSymbol, MODE_BID) > iBands(gRandomSymbol,PERIOD_H1,BollingerBands_Period,BollingerBands_deviation,BollingerBands_BandShift,BollingerBands_AppliedPrice,MODE_UPPER,BollingerBands_Shift) &&
                                  MarketInfo(gRandomSymbol, MODE_BID) > iBands(gRandomSymbol,PERIOD_H4,BollingerBands_Period,BollingerBands_deviation,BollingerBands_BandShift,BollingerBands_AppliedPrice,MODE_UPPER,BollingerBands_Shift)
                               )
                                  gSignalStatus = "RSB_UP";
 
                               if(
                                  iRSI(gRandomSymbol,PERIOD_M1,RSI_Period,RSI_AppliedPrice,RSI_shift) < RSI_BelowLevel &&
                                  iRSI(gRandomSymbol,PERIOD_M5,RSI_Period,RSI_AppliedPrice,RSI_shift) < RSI_BelowLevel &&
                                  iRSI(gRandomSymbol,PERIOD_M15,RSI_Period,RSI_AppliedPrice,RSI_shift) < RSI_BelowLevel &&
                                  iRSI(gRandomSymbol,PERIOD_M30,RSI_Period,RSI_AppliedPrice,RSI_shift) < RSI_BelowLevel &&
                                  iRSI(gRandomSymbol,PERIOD_H1,RSI_Period,RSI_AppliedPrice,RSI_shift) < RSI_BelowLevel &&
                                  iRSI(gRandomSymbol,PERIOD_H4,RSI_Period,RSI_AppliedPrice,RSI_shift) < RSI_BelowLevel &&
 
                                  (iStochastic(gRandomSymbol,PERIOD_M1,Stochastic_Kperiod,Stochastic_Dperiod,Stochastic_Slowing,Stochastic_Method,Stochastic_PriceField,MODE_MAIN,Stochastic_Shift) < Stochastic_BelowLevel && iStochastic(gRandomSymbol,PERIOD_M1,Stochastic_Kperiod,Stochastic_Dperiod,Stochastic_Slowing,Stochastic_Method,Stochastic_PriceField,MODE_SIGNAL,Stochastic_Shift) < Stochastic_BelowLevel) &&
                                  (iStochastic(gRandomSymbol,PERIOD_M5,Stochastic_Kperiod,Stochastic_Dperiod,Stochastic_Slowing,Stochastic_Method,Stochastic_PriceField,MODE_MAIN,Stochastic_Shift) < Stochastic_BelowLevel && iStochastic(gRandomSymbol,PERIOD_M5,Stochastic_Kperiod,Stochastic_Dperiod,Stochastic_Slowing,Stochastic_Method,Stochastic_PriceField,MODE_SIGNAL,Stochastic_Shift) < Stochastic_BelowLevel) &&
                                  (iStochastic(gRandomSymbol,PERIOD_M15,Stochastic_Kperiod,Stochastic_Dperiod,Stochastic_Slowing,Stochastic_Method,Stochastic_PriceField,MODE_MAIN,Stochastic_Shift) < Stochastic_BelowLevel && iStochastic(gRandomSymbol,PERIOD_M15,Stochastic_Kperiod,Stochastic_Dperiod,Stochastic_Slowing,Stochastic_Method,Stochastic_PriceField,MODE_SIGNAL,Stochastic_Shift) < Stochastic_BelowLevel) &&
                                  (iStochastic(gRandomSymbol,PERIOD_M30,Stochastic_Kperiod,Stochastic_Dperiod,Stochastic_Slowing,Stochastic_Method,Stochastic_PriceField,MODE_MAIN,Stochastic_Shift) < Stochastic_BelowLevel && iStochastic(gRandomSymbol,PERIOD_M30,Stochastic_Kperiod,Stochastic_Dperiod,Stochastic_Slowing,Stochastic_Method,Stochastic_PriceField,MODE_SIGNAL,Stochastic_Shift) < Stochastic_BelowLevel) &&
                                  (iStochastic(gRandomSymbol,PERIOD_H1,Stochastic_Kperiod,Stochastic_Dperiod,Stochastic_Slowing,Stochastic_Method,Stochastic_PriceField,MODE_MAIN,Stochastic_Shift) < Stochastic_BelowLevel && iStochastic(gRandomSymbol,PERIOD_H1,Stochastic_Kperiod,Stochastic_Dperiod,Stochastic_Slowing,Stochastic_Method,Stochastic_PriceField,MODE_SIGNAL,Stochastic_Shift) < Stochastic_BelowLevel) &&
                                  (iStochastic(gRandomSymbol,PERIOD_H4,Stochastic_Kperiod,Stochastic_Dperiod,Stochastic_Slowing,Stochastic_Method,Stochastic_PriceField,MODE_MAIN,Stochastic_Shift) < Stochastic_BelowLevel && iStochastic(gRandomSymbol,PERIOD_H4,Stochastic_Kperiod,Stochastic_Dperiod,Stochastic_Slowing,Stochastic_Method,Stochastic_PriceField,MODE_SIGNAL,Stochastic_Shift) < Stochastic_BelowLevel) &&
 
                                  MarketInfo(gRandomSymbol, MODE_ASK) < iBands(gRandomSymbol,PERIOD_M1,BollingerBands_Period,BollingerBands_deviation,BollingerBands_BandShift,BollingerBands_AppliedPrice,MODE_LOWER,BollingerBands_Shift) &&
                                  MarketInfo(gRandomSymbol, MODE_ASK) < iBands(gRandomSymbol,PERIOD_M5,BollingerBands_Period,BollingerBands_deviation,BollingerBands_BandShift,BollingerBands_AppliedPrice,MODE_LOWER,BollingerBands_Shift) &&
                                  MarketInfo(gRandomSymbol, MODE_ASK) < iBands(gRandomSymbol,PERIOD_M15,BollingerBands_Period,BollingerBands_deviation,BollingerBands_BandShift,BollingerBands_AppliedPrice,MODE_LOWER,BollingerBands_Shift) &&
                                  MarketInfo(gRandomSymbol, MODE_ASK) < iBands(gRandomSymbol,PERIOD_M30,BollingerBands_Period,BollingerBands_deviation,BollingerBands_BandShift,BollingerBands_AppliedPrice,MODE_LOWER,BollingerBands_Shift) &&
                                  MarketInfo(gRandomSymbol, MODE_ASK) < iBands(gRandomSymbol,PERIOD_H1,BollingerBands_Period,BollingerBands_deviation,BollingerBands_BandShift,BollingerBands_AppliedPrice,MODE_LOWER,BollingerBands_Shift) &&
                                  MarketInfo(gRandomSymbol, MODE_ASK) < iBands(gRandomSymbol,PERIOD_H4,BollingerBands_Period,BollingerBands_deviation,BollingerBands_BandShift,BollingerBands_AppliedPrice,MODE_LOWER,BollingerBands_Shift)
                               )
                                  gSignalStatus = "RSB_BELOW";

This check RSI, Stochastic and BollingerBands together for all time frame but have no flexibility if i want to check for one timeframe or combination of other possibility indicator.

So, with the help of Ai, i have changed the code to

MQL4:
extern string __TradingIndicator__ = "***Trading Indicator***";
    extern bool RSI = true;
    extern bool Stochastic = true;
    extern bool BollingerBands = true;
    extern string __TimeFrame__ = "***TimeFrame Settings***";
    extern bool PeriodM1 = true;
    extern bool PeriodM5 = true;
    extern bool PeriodM15 = true;
    extern bool PeriodM30 = true;
    extern bool PeriodH1 = true;
    extern bool PeriodH4 = true;
    extern bool PeriodD1 = false;
//+------------------------------------------------------------------+
    //|  Function to check RSI conditions                                |
    //+------------------------------------------------------------------+
    void CheckRSIConditions()
      {
       gRSISignal = "NoSignal";
       bool conditionsUp[7], conditionsDown[7];
       ENUM_TIMEFRAMES periods[7];
       int count = 0;
 
       if(PeriodM1)
          periods[count++] = PERIOD_M1;
       if(PeriodM5)
          periods[count++] = PERIOD_M5;
       if(PeriodM15)
          periods[count++] = PERIOD_M15;
       if(PeriodM30)
          periods[count++] = PERIOD_M30;
       if(PeriodH1)
          periods[count++] = PERIOD_H1;
       if(PeriodH4)
          periods[count++] = PERIOD_H4;
       if(PeriodD1)
          periods[count++] = PERIOD_D1;
 
       if(count == 0)
          return;  // Exit if no timeframes are selected
 
       bool allTrueUp = true, allTrueDown = true;
 
       for(int i = 0; i < count; i++)
         {
          double rsiValue = iRSI(gRandomSymbol, periods[i], RSI_Period, RSI_AppliedPrice, RSI_shift);
          conditionsUp[i] = (rsiValue > RSI_UpLevel);
          conditionsDown[i] = (rsiValue < RSI_BelowLevel);
 
          allTrueUp   &= conditionsUp[i];   // If any is false, allTrueUp becomes false
          allTrueDown &= conditionsDown[i]; // If any is false, allTrueDown becomes false
         }
 
       if(allTrueUp)
          gRSISignal = "UP";
       if(allTrueDown)
          gRSISignal = "BELOW";
      }
 
    //+------------------------------------------------------------------+
    //| Function to check Stochastic conditions                          |
    //+------------------------------------------------------------------+
    void CheckStochasticConditions()
      {
       gStochasticSignal = "NoSignal";
       bool conditionsUp[7], conditionsDown[7];
       ENUM_TIMEFRAMES periods[7];
       int count = 0;
 
       if(PeriodM1)
          periods[count++] = PERIOD_M1;
       if(PeriodM5)
          periods[count++] = PERIOD_M5;
       if(PeriodM15)
          periods[count++] = PERIOD_M15;
       if(PeriodM30)
          periods[count++] = PERIOD_M30;
       if(PeriodH1)
          periods[count++] = PERIOD_H1;
       if(PeriodH4)
          periods[count++] = PERIOD_H4;
       if(PeriodD1)
          periods[count++] = PERIOD_D1;
 
       if(count == 0)
          return;  // Exit if no timeframes are selected
 
       bool allTrueUp = true, allTrueDown = true;
 
       for(int i = 0; i < count; i++)
         {
          double mainValue   = iStochastic(gRandomSymbol, periods[i], Stochastic_Kperiod, Stochastic_Dperiod, Stochastic_Slowing, Stochastic_Method, Stochastic_PriceField, MODE_MAIN, Stochastic_Shift);
          double signalValue = iStochastic(gRandomSymbol, periods[i], Stochastic_Kperiod, Stochastic_Dperiod, Stochastic_Slowing, Stochastic_Method, Stochastic_PriceField, MODE_SIGNAL, Stochastic_Shift);
 
          conditionsUp[i]   = (mainValue > Stochastic_UpLevel && signalValue > Stochastic_UpLevel);
          conditionsDown[i] = (mainValue < Stochastic_BelowLevel && signalValue < Stochastic_BelowLevel);
 
          allTrueUp   &= conditionsUp[i];   // If any condition is false, allTrueUp becomes false
          allTrueDown &= conditionsDown[i]; // If any condition is false, allTrueDown becomes false
         }
 
       if(allTrueUp)
          gStochasticSignal = "UP";
       if(allTrueDown)
          gStochasticSignal = "BELOW";
      }
 
    //+------------------------------------------------------------------+
    //|   Function to check Bollinger Band conditions                    |
    //+------------------------------------------------------------------+
    void CheckBollingerBandsConditions()
      {
       gBollingerBandsSignal = "NoSignal";
       bool conditionsUp[7], conditionsDown[7];
       ENUM_TIMEFRAMES periods[7];
       int count = 0;
 
       if(PeriodM1)
          periods[count++] = PERIOD_M1;
       if(PeriodM5)
          periods[count++] = PERIOD_M5;
       if(PeriodM15)
          periods[count++] = PERIOD_M15;
       if(PeriodM30)
          periods[count++] = PERIOD_M30;
       if(PeriodH1)
          periods[count++] = PERIOD_H1;
       if(PeriodH4)
          periods[count++] = PERIOD_H4;
       if(PeriodD1)
          periods[count++] = PERIOD_D1;
 
       if(count == 0)
          return;  // Exit if no timeframes are selected
 
       bool allTrueUp = true, allTrueDown = true;
       double bidPrice = MarketInfo(gRandomSymbol, MODE_BID);
       double askPrice = MarketInfo(gRandomSymbol, MODE_ASK);
 
       for(int i = 0; i < count; i++)
         {
          double upperBand = iBands(gRandomSymbol, periods[i], BollingerBands_Period, BollingerBands_deviation, BollingerBands_BandShift, BollingerBands_AppliedPrice, MODE_UPPER, BollingerBands_Shift);
          double lowerBand = iBands(gRandomSymbol, periods[i], BollingerBands_Period, BollingerBands_deviation, BollingerBands_BandShift, BollingerBands_AppliedPrice, MODE_LOWER, BollingerBands_Shift);
 
          conditionsUp[i]   = (bidPrice > upperBand);
          conditionsDown[i] = (askPrice < lowerBand);
 
          allTrueUp   &= conditionsUp[i];   // If any condition is false, allTrueUp becomes false
          allTrueDown &= conditionsDown[i]; // If any condition is false, allTrueDown becomes false
         }
 
       if(allTrueUp)
          gBollingerBandsSignal = "UP";
       if(allTrueDown)
          gBollingerBandsSignal = "BELOW";
      }
 
    //+------------------------------------------------------------------+
    //|    Indicator Possibilities                                       |
    //+------------------------------------------------------------------+
    void MultiTF_IndicatorPossibilities()
      {
       gMultiTimeFrameSignalStatus = "NoSignal";
       string signals[3]; // Max size for RSI, Stochastic, and BollingerBands
       int count = 0;
 
       if(RSI)
          signals[count++] = gRSISignal;
       if(Stochastic)
          signals[count++] = gStochasticSignal;
       if(BollingerBands)
          signals[count++] = gBollingerBandsSignal;
 
    // Early exit if no indicators are enabled
       if(count == 0)
          return;
 
       bool allUP = true, allBELOW = true;
 
       for(int i = 0; i < count; i++)
         {
          if(signals[i] != "UP")
             allUP = false;
          if(signals[i] != "BELOW")
             allBELOW = false;
         }
 
       if(allUP)
          gMultiTimeFrameSignalStatus = "UP";
       if(allBELOW)
          gMultiTimeFrameSignalStatus = "BELOW";
      }

I have also tried my best to debug and make the code working. i guess something missing which i am unable to find out. I request you to please review and let me know what changes should i make to make my code working.
 

shanmugapradeep

Active Trader
Dec 18, 2020
128
7
34
39
The code looks fine except for that you didn't define RSI_UpLevel, RSI_BelowLevel, Stochastic_UpLevel, Stochastic_BelowLevel, and those global string signal variables.
Thanks for verifying. I tested again and it look working.

Code 1 :

MQL4:
void CheckMovingAverageConditions()
  {
   gMovingAverageSignal = "NoSignal";
   bool conditionsUp[7], conditionsDown[7];
   ENUM_TIMEFRAMES periods[7];
   int count = 0;
 
   if(PeriodM1)
      periods[count++] = PERIOD_M1;
   if(PeriodM5)
      periods[count++] = PERIOD_M5;
   if(PeriodM15)
      periods[count++] = PERIOD_M15;
   if(PeriodM30)
      periods[count++] = PERIOD_M30;
   if(PeriodH1)
      periods[count++] = PERIOD_H1;
   if(PeriodH4)
      periods[count++] = PERIOD_H4;
   if(PeriodD1)
      periods[count++] = PERIOD_D1;
 
   if(count == 0)
      return;  // Exit if no timeframes are selected
 
   bool allTrueUp = true, allTrueDown = true;
   double bidPrice = MarketInfo(gRandomSymbol, MODE_BID);
   double askPrice = MarketInfo(gRandomSymbol, MODE_ASK);
 
   for(int i = 0; i < count; i++)
     {
      double MA_Value = iMA(gRandomSymbol, periods[i], MovingAverage_Period, MovingAverage_MAShift, MovingAverage_Method, MovingAverage_AppliedPrice, MovingAverage_Shift);
 
      conditionsUp[i]   = (bidPrice > MA_Value);
      conditionsDown[i] = (askPrice < MA_Value);
 
      allTrueUp   &= conditionsUp[i];   // If any condition is false, allTrueUp becomes false
      allTrueDown &= conditionsDown[i]; // If any condition is false, allTrueDown becomes false
     }
 
   if(allTrueUp)
      gMovingAverageSignal = "ABOVE";
   if(allTrueDown)
      gMovingAverageSignal = "BELOW";
  }

Code 2 :

MQL4:
void CheckMovingAverageConditions()
  {
   gMovingAverageSignal = "NoSignal";
   ENUM_TIMEFRAMES periods[7];
   int count = 0;
 
   if(PeriodM1)  periods[count++] = PERIOD_M1;
   if(PeriodM5)  periods[count++] = PERIOD_M5;
   if(PeriodM15) periods[count++] = PERIOD_M15;
   if(PeriodM30) periods[count++] = PERIOD_M30;
   if(PeriodH1)  periods[count++] = PERIOD_H1;
   if(PeriodH4)  periods[count++] = PERIOD_H4;
   if(PeriodD1)  periods[count++] = PERIOD_D1;
 
   if(count == 0)
      return;  // Exit if no timeframes are selected
 
   bool allTrueUp = true, allTrueDown = true;
   double bidPrice = MarketInfo(gRandomSymbol, MODE_BID);
   double askPrice = MarketInfo(gRandomSymbol, MODE_ASK);
 
   for(int i = 0; i < count; i++)
     {
      double MA_Value = iMA(gRandomSymbol, periods[i], MovingAverage_Period, MovingAverage_MAShift, MovingAverage_Method, MovingAverage_AppliedPrice, MovingAverage_Shift);
 
      if(bidPrice <= MA_Value)
         allTrueUp = false;   // If any fails, set allTrueUp to false
      if(askPrice >= MA_Value)
         allTrueDown = false; // If any fails, set allTrueDown to false
     }
 
   if(allTrueUp)
      gMovingAverageSignal = "ABOVE";
   else if(allTrueDown)
      gMovingAverageSignal = "BELOW";
  }

Is code 1 and code 2 do exactly same operation? Checking indicator according different timeframe together? If, both do same function, which one should i use for better performance, speed execution and accuracy?
 

Enivid

Administrator
Staff member
Nov 30, 2008
19,495
1,594
144
Odesa
www.earnforex.com
Thanks for verifying. I tested again and it look working.

Code 1 :

MQL4:
void CheckMovingAverageConditions()
  {
   gMovingAverageSignal = "NoSignal";
   bool conditionsUp[7], conditionsDown[7];
   ENUM_TIMEFRAMES periods[7];
   int count = 0;
 
   if(PeriodM1)
      periods[count++] = PERIOD_M1;
   if(PeriodM5)
      periods[count++] = PERIOD_M5;
   if(PeriodM15)
      periods[count++] = PERIOD_M15;
   if(PeriodM30)
      periods[count++] = PERIOD_M30;
   if(PeriodH1)
      periods[count++] = PERIOD_H1;
   if(PeriodH4)
      periods[count++] = PERIOD_H4;
   if(PeriodD1)
      periods[count++] = PERIOD_D1;
 
   if(count == 0)
      return;  // Exit if no timeframes are selected
 
   bool allTrueUp = true, allTrueDown = true;
   double bidPrice = MarketInfo(gRandomSymbol, MODE_BID);
   double askPrice = MarketInfo(gRandomSymbol, MODE_ASK);
 
   for(int i = 0; i < count; i++)
     {
      double MA_Value = iMA(gRandomSymbol, periods[i], MovingAverage_Period, MovingAverage_MAShift, MovingAverage_Method, MovingAverage_AppliedPrice, MovingAverage_Shift);
 
      conditionsUp[i]   = (bidPrice > MA_Value);
      conditionsDown[i] = (askPrice < MA_Value);
 
      allTrueUp   &= conditionsUp[i];   // If any condition is false, allTrueUp becomes false
      allTrueDown &= conditionsDown[i]; // If any condition is false, allTrueDown becomes false
     }
 
   if(allTrueUp)
      gMovingAverageSignal = "ABOVE";
   if(allTrueDown)
      gMovingAverageSignal = "BELOW";
  }

Code 2 :

MQL4:
void CheckMovingAverageConditions()
  {
   gMovingAverageSignal = "NoSignal";
   ENUM_TIMEFRAMES periods[7];
   int count = 0;
 
   if(PeriodM1)  periods[count++] = PERIOD_M1;
   if(PeriodM5)  periods[count++] = PERIOD_M5;
   if(PeriodM15) periods[count++] = PERIOD_M15;
   if(PeriodM30) periods[count++] = PERIOD_M30;
   if(PeriodH1)  periods[count++] = PERIOD_H1;
   if(PeriodH4)  periods[count++] = PERIOD_H4;
   if(PeriodD1)  periods[count++] = PERIOD_D1;
 
   if(count == 0)
      return;  // Exit if no timeframes are selected
 
   bool allTrueUp = true, allTrueDown = true;
   double bidPrice = MarketInfo(gRandomSymbol, MODE_BID);
   double askPrice = MarketInfo(gRandomSymbol, MODE_ASK);
 
   for(int i = 0; i < count; i++)
     {
      double MA_Value = iMA(gRandomSymbol, periods[i], MovingAverage_Period, MovingAverage_MAShift, MovingAverage_Method, MovingAverage_AppliedPrice, MovingAverage_Shift);
 
      if(bidPrice <= MA_Value)
         allTrueUp = false;   // If any fails, set allTrueUp to false
      if(askPrice >= MA_Value)
         allTrueDown = false; // If any fails, set allTrueDown to false
     }
 
   if(allTrueUp)
      gMovingAverageSignal = "ABOVE";
   else if(allTrueDown)
      gMovingAverageSignal = "BELOW";
  }

Is code 1 and code 2 do exactly same operation? Checking indicator according different timeframe together? If, both do same function, which one should i use for better performance, speed execution and accuracy?
Both do the same thing. I'd use the second one unless you need the array of conditions for each timeframe separately to do some other stuff.
 

shanmugapradeep

Active Trader
Dec 18, 2020
128
7
34
39
Both do the same thing. I'd use the second one unless you need the array of conditions for each timeframe separately to do some other stuff.
I am not using the array anywhere. So, here is my final code as per your recommendation. Please check and confirm everything is correct.

Logic is very simple,

Example 1 :

RSI and BB is true
M1, M5 and M15 is true

means RSI and BB should be above/Below for All M1, M5, M15. if both indicator Above for all 3 timeframe => Take trade

Example 2 :

if all 4 indicator is true and M1 to H4 is true means, All indicator for all timeframe must be above or below, so it can take trade


MQL4:
extern string __TradingIndicator__ = "***Trading Indicator***";
extern bool RSI = true;
extern bool Stochastic = true;
extern bool BollingerBands = true;
extern bool MovingAverage = true;
 
extern string __TimeFrame__ = "***TimeFrame Settings***";
extern bool PeriodM1 = true;
extern bool PeriodM5 = true;
extern bool PeriodM15 = true;
extern bool PeriodM30 = true;
extern bool PeriodH1 = true;
extern bool PeriodH4 = true;
extern bool PeriodD1 = false;
 
extern string __TradingStrategy__ = "***Trading Strategy***";
enum TradeDirection_enum {Trend, CounterTrend};
extern TradeDirection_enum TradeDirection = CounterTrend;
 
string gRSISignal, gStochasticSignal, gBollingerBandsSignal, gMovingAverageSignal, gMultiTimeFrameSignalStatus;
 
void OnTick()
  {
      if(RSI)
         CheckRSIConditions();
      if(Stochastic)
         CheckStochasticConditions();
      if(MovingAverage)
         CheckMovingAverageConditions();
      if(BollingerBands)
         CheckBollingerBandsConditions();
      MultiTF_IndicatorPossibilities();
  }
 
  //+------------------------------------------------------------------+
//|  Function to check RSI conditions                                |
//+------------------------------------------------------------------+
void CheckRSIConditions()
  {
   gRSISignal = "NoSignal";
   ENUM_TIMEFRAMES periods[7];
   int count = 0;
 
   if(PeriodM1)
      periods[count++] = PERIOD_M1;
   if(PeriodM5)
      periods[count++] = PERIOD_M5;
   if(PeriodM15)
      periods[count++] = PERIOD_M15;
   if(PeriodM30)
      periods[count++] = PERIOD_M30;
   if(PeriodH1)
      periods[count++] = PERIOD_H1;
   if(PeriodH4)
      periods[count++] = PERIOD_H4;
   if(PeriodD1)
      periods[count++] = PERIOD_D1;
 
   if(count == 0)
      return;  // Exit if no timeframes are selected
 
   bool allTrueUp = true, allTrueDown = true;
 
   for(int i = 0; i < count; i++)
     {
      double rsiValue = iRSI(gRandomSymbol, periods[i], RSI_Period, RSI_AppliedPrice, RSI_shift);
 
      if(rsiValue <= RSI_UpLevel)
         allTrueUp = false;
      if(rsiValue >= RSI_BelowLevel)
         allTrueDown = false;
     }
 
   if(allTrueUp)
      gRSISignal = "ABOVE";
   else
      if(allTrueDown)
         gRSISignal = "BELOW";
  }
 
//+------------------------------------------------------------------+
//| Function to check Stochastic conditions                          |
//+------------------------------------------------------------------+
void CheckStochasticConditions()
  {
   gStochasticSignal = "NoSignal";
   ENUM_TIMEFRAMES periods[7];
   int count = 0;
 
   if(PeriodM1)
      periods[count++] = PERIOD_M1;
   if(PeriodM5)
      periods[count++] = PERIOD_M5;
   if(PeriodM15)
      periods[count++] = PERIOD_M15;
   if(PeriodM30)
      periods[count++] = PERIOD_M30;
   if(PeriodH1)
      periods[count++] = PERIOD_H1;
   if(PeriodH4)
      periods[count++] = PERIOD_H4;
   if(PeriodD1)
      periods[count++] = PERIOD_D1;
 
   if(count == 0)
      return;  // Exit if no timeframes are selected
 
   bool allTrueUp = true, allTrueDown = true;
 
   for(int i = 0; i < count; i++)
     {
      double mainValue   = iStochastic(gRandomSymbol, periods[i], Stochastic_Kperiod, Stochastic_Dperiod,
                                       Stochastic_Slowing, Stochastic_Method, Stochastic_PriceField, MODE_MAIN, Stochastic_Shift);
      double signalValue = iStochastic(gRandomSymbol, periods[i], Stochastic_Kperiod, Stochastic_Dperiod,
                                       Stochastic_Slowing, Stochastic_Method, Stochastic_PriceField, MODE_SIGNAL, Stochastic_Shift);
 
      if(mainValue <= Stochastic_UpLevel || signalValue <= Stochastic_UpLevel)
         allTrueUp = false;
      if(mainValue >= Stochastic_BelowLevel || signalValue >= Stochastic_BelowLevel)
         allTrueDown = false;
     }
 
   if(allTrueUp)
      gStochasticSignal = "ABOVE";
   else
      if(allTrueDown)
         gStochasticSignal = "BELOW";
  }
 
//+------------------------------------------------------------------+
//|   Function to check Bollinger Band conditions                    |
//+------------------------------------------------------------------+
void CheckBollingerBandsConditions()
  {
   gBollingerBandsSignal = "NoSignal";
   ENUM_TIMEFRAMES periods[7];
   int count = 0;
 
   if(PeriodM1)
      periods[count++] = PERIOD_M1;
   if(PeriodM5)
      periods[count++] = PERIOD_M5;
   if(PeriodM15)
      periods[count++] = PERIOD_M15;
   if(PeriodM30)
      periods[count++] = PERIOD_M30;
   if(PeriodH1)
      periods[count++] = PERIOD_H1;
   if(PeriodH4)
      periods[count++] = PERIOD_H4;
   if(PeriodD1)
      periods[count++] = PERIOD_D1;
 
   if(count == 0)
      return;  // Exit if no timeframes are selected
 
   bool allTrueUp = true, allTrueDown = true;
   double bidPrice = MarketInfo(gRandomSymbol, MODE_BID);
   double askPrice = MarketInfo(gRandomSymbol, MODE_ASK);
 
   for(int i = 0; i < count; i++)
     {
      double upperBand = iBands(gRandomSymbol, periods[i], BollingerBands_Period, BollingerBands_deviation,
                                BollingerBands_BandShift, BollingerBands_AppliedPrice, MODE_UPPER, BollingerBands_Shift);
      double lowerBand = iBands(gRandomSymbol, periods[i], BollingerBands_Period, BollingerBands_deviation,
                                BollingerBands_BandShift, BollingerBands_AppliedPrice, MODE_LOWER, BollingerBands_Shift);
 
      if(bidPrice <= upperBand)
         allTrueUp = false;
      if(askPrice >= lowerBand)
         allTrueDown = false;
     }
 
   if(allTrueUp)
      gBollingerBandsSignal = "ABOVE";
   else
      if(allTrueDown)
         gBollingerBandsSignal = "BELOW";
  }
 
//+------------------------------------------------------------------+
//|   Function to check Moving Average conditions                    |
//+------------------------------------------------------------------+
void CheckMovingAverageConditions()
  {
   gMovingAverageSignal = "NoSignal";
   ENUM_TIMEFRAMES periods[7];
   int count = 0;
 
   if(PeriodM1)
      periods[count++] = PERIOD_M1;
   if(PeriodM5)
      periods[count++] = PERIOD_M5;
   if(PeriodM15)
      periods[count++] = PERIOD_M15;
   if(PeriodM30)
      periods[count++] = PERIOD_M30;
   if(PeriodH1)
      periods[count++] = PERIOD_H1;
   if(PeriodH4)
      periods[count++] = PERIOD_H4;
   if(PeriodD1)
      periods[count++] = PERIOD_D1;
 
   if(count == 0)
      return;  // Exit if no timeframes are selected
 
   bool allTrueUp = true, allTrueDown = true;
   double bidPrice = MarketInfo(gRandomSymbol, MODE_BID);
   double askPrice = MarketInfo(gRandomSymbol, MODE_ASK);
 
   for(int i = 0; i < count; i++)
     {
      double MA_Value = iMA(gRandomSymbol, periods[i], MovingAverage_Period, MovingAverage_MAShift, MovingAverage_Method, MovingAverage_AppliedPrice, MovingAverage_Shift);
 
      if(bidPrice <= MA_Value)
         allTrueUp = false;   // If any fails, set allTrueUp to false
      if(askPrice >= MA_Value)
         allTrueDown = false; // If any fails, set allTrueDown to false
     }
   if(TradeDirection == Trend)
     {
      if(allTrueUp)
         gMovingAverageSignal = "ABOVE";
      if(allTrueDown)
         gMovingAverageSignal = "BELOW";
     }
   else
      if(TradeDirection == CounterTrend)
        {
         if(allTrueUp)
            gMovingAverageSignal = "BELOW";
         if(allTrueDown)
            gMovingAverageSignal = "ABOVE";
        }
  }
 
//+------------------------------------------------------------------+
//|    Indicator Possibilities                                       |
//+------------------------------------------------------------------+
void MultiTF_IndicatorPossibilities()
  {
   gMultiTimeFrameSignalStatus = "NoSignal";
   string signals[4];
   int count = 0;
 
   if(RSI)
      signals[count++] = gRSISignal;
   if(Stochastic)
      signals[count++] = gStochasticSignal;
   if(BollingerBands)
      signals[count++] = gBollingerBandsSignal;
   if(MovingAverage)
      signals[count++] = gMovingAverageSignal;
 
   if(count == 0)
      return;  // Exit early if no indicators are enabled
 
   bool allABOVE = true, allBELOW = true;
 
   for(int i = 0; i < count && (allABOVE || allBELOW); i++)
     {
      if(signals[i] != "ABOVE")
         allABOVE = false;
      if(signals[i] != "BELOW")
         allBELOW = false;
     }
 
   if(allABOVE)
      gMultiTimeFrameSignalStatus = "ABOVE";
   else
      if(allBELOW)
         gMultiTimeFrameSignalStatus = "BELOW";
  }

Please check ones and confirm me. Thanks again
 

Enivid

Administrator
Staff member
Nov 30, 2008
19,495
1,594
144
Odesa
www.earnforex.com
I am not using the array anywhere. So, here is my final code as per your recommendation. Please check and confirm everything is correct.

Logic is very simple,

Example 1 :

RSI and BB is true
M1, M5 and M15 is true

means RSI and BB should be above/Below for All M1, M5, M15. if both indicator Above for all 3 timeframe => Take trade

Example 2 :

if all 4 indicator is true and M1 to H4 is true means, All indicator for all timeframe must be above or below, so it can take trade


MQL4:
extern string __TradingIndicator__ = "***Trading Indicator***";
extern bool RSI = true;
extern bool Stochastic = true;
extern bool BollingerBands = true;
extern bool MovingAverage = true;
 
extern string __TimeFrame__ = "***TimeFrame Settings***";
extern bool PeriodM1 = true;
extern bool PeriodM5 = true;
extern bool PeriodM15 = true;
extern bool PeriodM30 = true;
extern bool PeriodH1 = true;
extern bool PeriodH4 = true;
extern bool PeriodD1 = false;
 
extern string __TradingStrategy__ = "***Trading Strategy***";
enum TradeDirection_enum {Trend, CounterTrend};
extern TradeDirection_enum TradeDirection = CounterTrend;
 
string gRSISignal, gStochasticSignal, gBollingerBandsSignal, gMovingAverageSignal, gMultiTimeFrameSignalStatus;
 
void OnTick()
  {
      if(RSI)
         CheckRSIConditions();
      if(Stochastic)
         CheckStochasticConditions();
      if(MovingAverage)
         CheckMovingAverageConditions();
      if(BollingerBands)
         CheckBollingerBandsConditions();
      MultiTF_IndicatorPossibilities();
  }
 
  //+------------------------------------------------------------------+
//|  Function to check RSI conditions                                |
//+------------------------------------------------------------------+
void CheckRSIConditions()
  {
   gRSISignal = "NoSignal";
   ENUM_TIMEFRAMES periods[7];
   int count = 0;
 
   if(PeriodM1)
      periods[count++] = PERIOD_M1;
   if(PeriodM5)
      periods[count++] = PERIOD_M5;
   if(PeriodM15)
      periods[count++] = PERIOD_M15;
   if(PeriodM30)
      periods[count++] = PERIOD_M30;
   if(PeriodH1)
      periods[count++] = PERIOD_H1;
   if(PeriodH4)
      periods[count++] = PERIOD_H4;
   if(PeriodD1)
      periods[count++] = PERIOD_D1;
 
   if(count == 0)
      return;  // Exit if no timeframes are selected
 
   bool allTrueUp = true, allTrueDown = true;
 
   for(int i = 0; i < count; i++)
     {
      double rsiValue = iRSI(gRandomSymbol, periods[i], RSI_Period, RSI_AppliedPrice, RSI_shift);
 
      if(rsiValue <= RSI_UpLevel)
         allTrueUp = false;
      if(rsiValue >= RSI_BelowLevel)
         allTrueDown = false;
     }
 
   if(allTrueUp)
      gRSISignal = "ABOVE";
   else
      if(allTrueDown)
         gRSISignal = "BELOW";
  }
 
//+------------------------------------------------------------------+
//| Function to check Stochastic conditions                          |
//+------------------------------------------------------------------+
void CheckStochasticConditions()
  {
   gStochasticSignal = "NoSignal";
   ENUM_TIMEFRAMES periods[7];
   int count = 0;
 
   if(PeriodM1)
      periods[count++] = PERIOD_M1;
   if(PeriodM5)
      periods[count++] = PERIOD_M5;
   if(PeriodM15)
      periods[count++] = PERIOD_M15;
   if(PeriodM30)
      periods[count++] = PERIOD_M30;
   if(PeriodH1)
      periods[count++] = PERIOD_H1;
   if(PeriodH4)
      periods[count++] = PERIOD_H4;
   if(PeriodD1)
      periods[count++] = PERIOD_D1;
 
   if(count == 0)
      return;  // Exit if no timeframes are selected
 
   bool allTrueUp = true, allTrueDown = true;
 
   for(int i = 0; i < count; i++)
     {
      double mainValue   = iStochastic(gRandomSymbol, periods[i], Stochastic_Kperiod, Stochastic_Dperiod,
                                       Stochastic_Slowing, Stochastic_Method, Stochastic_PriceField, MODE_MAIN, Stochastic_Shift);
      double signalValue = iStochastic(gRandomSymbol, periods[i], Stochastic_Kperiod, Stochastic_Dperiod,
                                       Stochastic_Slowing, Stochastic_Method, Stochastic_PriceField, MODE_SIGNAL, Stochastic_Shift);
 
      if(mainValue <= Stochastic_UpLevel || signalValue <= Stochastic_UpLevel)
         allTrueUp = false;
      if(mainValue >= Stochastic_BelowLevel || signalValue >= Stochastic_BelowLevel)
         allTrueDown = false;
     }
 
   if(allTrueUp)
      gStochasticSignal = "ABOVE";
   else
      if(allTrueDown)
         gStochasticSignal = "BELOW";
  }
 
//+------------------------------------------------------------------+
//|   Function to check Bollinger Band conditions                    |
//+------------------------------------------------------------------+
void CheckBollingerBandsConditions()
  {
   gBollingerBandsSignal = "NoSignal";
   ENUM_TIMEFRAMES periods[7];
   int count = 0;
 
   if(PeriodM1)
      periods[count++] = PERIOD_M1;
   if(PeriodM5)
      periods[count++] = PERIOD_M5;
   if(PeriodM15)
      periods[count++] = PERIOD_M15;
   if(PeriodM30)
      periods[count++] = PERIOD_M30;
   if(PeriodH1)
      periods[count++] = PERIOD_H1;
   if(PeriodH4)
      periods[count++] = PERIOD_H4;
   if(PeriodD1)
      periods[count++] = PERIOD_D1;
 
   if(count == 0)
      return;  // Exit if no timeframes are selected
 
   bool allTrueUp = true, allTrueDown = true;
   double bidPrice = MarketInfo(gRandomSymbol, MODE_BID);
   double askPrice = MarketInfo(gRandomSymbol, MODE_ASK);
 
   for(int i = 0; i < count; i++)
     {
      double upperBand = iBands(gRandomSymbol, periods[i], BollingerBands_Period, BollingerBands_deviation,
                                BollingerBands_BandShift, BollingerBands_AppliedPrice, MODE_UPPER, BollingerBands_Shift);
      double lowerBand = iBands(gRandomSymbol, periods[i], BollingerBands_Period, BollingerBands_deviation,
                                BollingerBands_BandShift, BollingerBands_AppliedPrice, MODE_LOWER, BollingerBands_Shift);
 
      if(bidPrice <= upperBand)
         allTrueUp = false;
      if(askPrice >= lowerBand)
         allTrueDown = false;
     }
 
   if(allTrueUp)
      gBollingerBandsSignal = "ABOVE";
   else
      if(allTrueDown)
         gBollingerBandsSignal = "BELOW";
  }
 
//+------------------------------------------------------------------+
//|   Function to check Moving Average conditions                    |
//+------------------------------------------------------------------+
void CheckMovingAverageConditions()
  {
   gMovingAverageSignal = "NoSignal";
   ENUM_TIMEFRAMES periods[7];
   int count = 0;
 
   if(PeriodM1)
      periods[count++] = PERIOD_M1;
   if(PeriodM5)
      periods[count++] = PERIOD_M5;
   if(PeriodM15)
      periods[count++] = PERIOD_M15;
   if(PeriodM30)
      periods[count++] = PERIOD_M30;
   if(PeriodH1)
      periods[count++] = PERIOD_H1;
   if(PeriodH4)
      periods[count++] = PERIOD_H4;
   if(PeriodD1)
      periods[count++] = PERIOD_D1;
 
   if(count == 0)
      return;  // Exit if no timeframes are selected
 
   bool allTrueUp = true, allTrueDown = true;
   double bidPrice = MarketInfo(gRandomSymbol, MODE_BID);
   double askPrice = MarketInfo(gRandomSymbol, MODE_ASK);
 
   for(int i = 0; i < count; i++)
     {
      double MA_Value = iMA(gRandomSymbol, periods[i], MovingAverage_Period, MovingAverage_MAShift, MovingAverage_Method, MovingAverage_AppliedPrice, MovingAverage_Shift);
 
      if(bidPrice <= MA_Value)
         allTrueUp = false;   // If any fails, set allTrueUp to false
      if(askPrice >= MA_Value)
         allTrueDown = false; // If any fails, set allTrueDown to false
     }
   if(TradeDirection == Trend)
     {
      if(allTrueUp)
         gMovingAverageSignal = "ABOVE";
      if(allTrueDown)
         gMovingAverageSignal = "BELOW";
     }
   else
      if(TradeDirection == CounterTrend)
        {
         if(allTrueUp)
            gMovingAverageSignal = "BELOW";
         if(allTrueDown)
            gMovingAverageSignal = "ABOVE";
        }
  }
 
//+------------------------------------------------------------------+
//|    Indicator Possibilities                                       |
//+------------------------------------------------------------------+
void MultiTF_IndicatorPossibilities()
  {
   gMultiTimeFrameSignalStatus = "NoSignal";
   string signals[4];
   int count = 0;
 
   if(RSI)
      signals[count++] = gRSISignal;
   if(Stochastic)
      signals[count++] = gStochasticSignal;
   if(BollingerBands)
      signals[count++] = gBollingerBandsSignal;
   if(MovingAverage)
      signals[count++] = gMovingAverageSignal;
 
   if(count == 0)
      return;  // Exit early if no indicators are enabled
 
   bool allABOVE = true, allBELOW = true;
 
   for(int i = 0; i < count && (allABOVE || allBELOW); i++)
     {
      if(signals[i] != "ABOVE")
         allABOVE = false;
      if(signals[i] != "BELOW")
         allBELOW = false;
     }
 
   if(allABOVE)
      gMultiTimeFrameSignalStatus = "ABOVE";
   else
      if(allBELOW)
         gMultiTimeFrameSignalStatus = "BELOW";
  }

Please check ones and confirm me. Thanks again
Looks fine to me.