Switching to Different Time Frames

 
//PSEUDO CODE
Signals : at least 3 signals to monitor (could add up some later) 
confirmation signals: 2 signals
// I created boolean functions to identify these signals 
// with ENUM_TIMEFRAMES and CandlePosition as input parameters

TimeFrames : HigherTimeframe, MiddleTimeFrame, SmallestTimeFrame 

Monitor HigherTimeFrame for Signals:
if (signal1 or signal2 or signal3 appears)
{
  Switch to Middle TimeFrame:
  Monitor MiddleTimeFrame for signal confirmation: 
  Monitor Signal Validity of HigherTimeFrame: 
  if (SignalType1 on HigherTimeFrame)
  {
    watchout for Confirmation Type1 
    if (signal confirmed in MiddleTimeFrame and HigherTimeFrame signal is still valid)
    {
      Switch to SmallestTimeFrame:
      Monitor validity of MiddleTimeFrame:
      Monitor validity of the HigherTimeFrame:
      Monitor for Entry Signal:
      if (Entry Signal) and (MiddleTimeFrame Signal && HigherTimeFrame Signal are still valid):
        Execute Trade:
    }
  }
  if (SignalType2 on HigherTimeFrame)
  {
    watchout for Confirmation Type2
  }
  if (...) 
  {
    ...
  }
}



// SOME OF THE SIGNAL FUNCTIONS
//+------------------------------------------------------------------+
//| Signal Related Function - CSM (Candle Stick Momentum)            |
//+------------------------------------------------------------------+
bool SignalCSM_SELL(ENUM_TIMEFRAMES _signalTimeFrame, int _candlePeriod )
  {
      double LowerBB  = FuncInd_BBand(ind_BBandPeriod,_signalTimeFrame, MODE_LOWER,_candlePeriod);
      if((iClose(_Symbol, _signalTimeFrame, _candlePeriod) <= LowerBB))  //Candle[1] is Negative  
        {
          if (FuncNewBar(_signalTimeFrame)){
            Print("CSM Signal at LowerBB");
            // ChartAnnotate(ArrowCheck, "CSM@LowerBB"+string(Time[_candlePeriod]), Time[_candlePeriod], Low[_candlePeriod] );
          }
          return true;
        }
    return false;
}

bool SignalCSM_BUY(ENUM_TIMEFRAMES _signalTimeFrame, int _candlePeriod )
  {
      double UpperBB  = FuncInd_BBand(ind_BBandPeriod, _signalTimeFrame, MODE_UPPER,_candlePeriod);
      if((iClose(_Symbol, _signalTimeFrame, _candlePeriod) >= UpperBB))  //Candle[1] is Positive  
        {
          if (FuncNewBar(_signalTimeFrame)){
            Print("CSM Signal at UpperBB");
            // ChartAnnotate(ArrowCheck, "CSM@UpperBB"+string(Time[_candlePeriod]), Time[_candlePeriod], High[_candlePeriod] );
          }
          return true;
        }
    return false;
}

I wish to solicit any thoughts or suggestions on how to handle this case efficiently in OnTick()  function aside from series of "If" statements.

THank you in advanced.


 
  1. Bong Bungalan Jr.: I wish to solicit any thoughts or suggestions on how to handle this case efficiently in OnTick()  function aside from series of "If" statements.

    Why do you think you need a "series of if statements?"

  2. Code your condition as a function and pass in the TF.

  3. FuncNewBar as to have three separate static variables (one per TF) or be three separate functions.

    I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
              Running EA once at the start of each bar - MQL4 programming forum 2011.05.06

    Just test for a new bar on the lowest TF. If it is, then and only then do you need to test the others.
    void OnTick(){
       static datetime curTime=0; datetime preTime = curTime; curTime = iTime(NULL, SmallestTimeFrame,0);
       bool isNewBarSmall  = preTime != curTime;
       if(!isNewBarSmall) return; // You said "efficient."
       bool isNewBarMiddle = curTime % PeriodSeconds(MiddleTimeFrame) == 0;
       bool isNewBarHigher = curTime % PeriodSeconds(HigherTimeframe) == 0;


 

@William Roeder -  Thank you for your thoughts. As to the question of "why series of if statements" - this is my humble (noob) way of solving this problem and I am thinking there must be a better way. 

To make this clearer, here is a snippet of my OnTick() function for you to critique.  I wish to learn how to formulate algorithm to solve problems efficiently since I do not have any CS background and I am just started learning to code.

Also, _comment is my way of checking  if signals have been detected by counting them (wondering too if there is a more efficient way). 

void OnTick()
  {
    double ind_MiddleBB, ind_UpperBB,  ind_LowerBB  ;
    _comment =  "Testing NewBarFunction: \nHigherTF: " + string (HTSignalCounter) + "\n";
    _comment = _comment + "CSM Buy  : " + string(_BuysignalInHigherTF) + "\n";
    _comment = _comment + "CSM Sell : " + string(_SellsignalInHigherTF) + "\n";
    _comment = _comment + "MIddle TF: " + string(MTSignalCounter) + "\n";
    _comment = _comment + "Smallest TF: " + string(STSignalCounter) + "\n";
  
    Comment (_comment);

    // if (FuncNewBar(HigherTF)) HTSignalCounter = HTSignalCounter +1;
    // if (FuncNewBar(MiddleTF)) MTSignalCounter = MTSignalCounter +1;
    // if (FuncNewBar(SmallestTF)) STSignalCounter = STSignalCounter +1;

    if(FuncNewBar(HigherTF))   
      {
        ind_MiddleBB = FuncInd_BBand(ind_BBandPeriod,HigherTF, MODE_MAIN, 0); 
        ind_UpperBB  = FuncInd_BBand(ind_BBandPeriod,HigherTF, MODE_UPPER, 0);
        ind_LowerBB  = FuncInd_BBand(ind_BBandPeriod,HigherTF, MODE_LOWER, 0);   
        if (SignalCSM_SELL(HigherTF,0))
          {
            HTSignalCounter = HTSignalCounter +1;
            Print(" CSM Sell SIgnal: ");
            // Comment (_comment);
            _SellsignalInHigherTF = true;
            _BuysignalInHigherTF = false;
          }  
          else
          if (SignalCSM_BUY(HigherTF,0))
            {
              HTSignalCounter = HTSignalCounter +1;
              Print(" CSM Buy SIgnal: ");
              _BuysignalInHigherTF = true;
              _SellsignalInHigherTF = false;
            }
      }
  
  // Check Confirmation in Middle Time Frames if Signals occured in HigherTF
    if (FuncNewBar(MiddleTF)) 
      {
        if (_BuysignalInHigherTF)
          { 
            //Look for Extreme Buy on H1 (MiddleTF)
            if(SignalExtremeBuy(MiddleTF, 1)) 
              {
                _BuysignalConfirmed = true;
                _SellsignalConfirmed = false;
                MTSignalCounter = MTSignalCounter + 1;
              }
          } 
        else
        if (_SellsignalInHigherTF)
          {
            // Look for Extreme Sell on H1
            if(SignalExtremeSell(MiddleTF, 1)) 
              {
                _SellsignalConfirmed = true;
                _BuysignalConfirmed = false;
                MTSignalCounter = MTSignalCounter + 1;
              }  
          }
      }