简体   繁体   中英

MQL4: Only half of my if/else if loop leads to current execution

I have this little function that is giving me trouble, only part of the function will follow my conditions at one time.

bool trend()
   {
     //is there a trend?
     close1 = iClose(NULL,0,1); //vars
     close2 = iClose(NULL,0,2);
     close3 = iClose(NULL,0,3);

     open1 = iOpen(NULL,0,1);
     open2 = iOpen(NULL,0,2);
     open3 = iOpen(NULL,0,3);

     if(close3 > open3 && close2 > open2 && close1 > open1)
     {
        return(true);           //uptrend
     }

     else if(close3 < open3 && close2 < open2 && close1 < open1)
     {
        return(false);         //downtrend
     }

     else return(EMPTY_VALUE);
  }

This is how the function gets called, under int start()

  trending = trend();

  if (trending == true) Order = SIGNAL_BUY; // Rule to ENTER a Long trade

  if (trending == false) Order = SIGNAL_SELL; // Rule to ENTER a Short trade

As written above, my sell signal will work following the conditions, but the buy signals don't follow the conditions, and I can't figure out how they are triggering.

If I remove the "else return(EMPTY_VALUE);" then the buy orders start following the condition but the sell orders no longer follow the conditions. The broken sell order seems to behave like the broken buy order was.

Any ideas why my function is behaving like this? Thanks!

Your function is declared as bool trend() , which means that it can return either true or false . In the line else return(EMPTY_VALUE) , the constant EMPTY_VALUE (which has the value 0x7FFFFFFF according to the MQL documentation) is implicitly converted to true . This means that your function will return true (which emits your buy signal) if there is no uptrend and no downtrend.

If you leave out the last line else return(EMPTY_VALUE) you have a missing return statement. This leads to undefined behaviour if you try to access the return value of the function, which you do in the line trending = trend() .

To sum it up: Your problem is that the trend function can only return one of two values, true or false . But what you need is a function that returns one of three values uptrend , downtrend , no_trend . You could declare an enum with those three values and change the return type accordingly:

enum Trend {
    UPTREND,
    DOWNTREND,
    NONE
}

Trend trend() {
    // check if there is a trend
    // [...]
    if (close3 > open3 && close2 > open2 && close1 > open1) {
        return UPTREND;
    }
    else if (close3 < open3 && close2 < open2 && close1 < open1) {
        return DOWNTREND;
    }
    else {
        return NONE;
    }
}

and then later

Trend trending = trend();
if (trending == UPTREND) Order = SIGNAL_BUY;
if (trending == DOWNTREND) Order = SIGNAL_SELL;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM