If statement and StopLossPips

 

I have an extern double StopLossPips and I would like to make it conditional with an IF statement. Basically I don't want the stop loss to kick in unless some condition exists in the market. In this case price above/below a 5MA. (_Is5MAStopUp)   I have tried to implement it within the code where the StopLossPips is referenced and I have put it before the subopenorder and 1-2 other places but not luck. I cant understand why the IF statement will not take. Any help appreciated.

 

      if (  
         (OpenOnSignalChange  && lastDirection != OP_BUY && curDirection == OP_BUY) 
         || 
         (!OpenOnSignalChange && curDirection  == OP_BUY && TradeCount[OP_BUY] == 0 && lastDirection == -1)
         ) 
         {
         closeTrades(OP_SELL);
        if(_Is5MAStopUp) {
        if (StopLossPips > 0)StopLoss = Ask - StopLossPips * MyPoint; else StopLoss = getZigZagStopLoss(OP_BUY);
            Lots = getLotSize3( (Ask-StopLoss)/MyPoint );
         subOpenOrder(OP_BUY,StopLossPips,TakeProfitPips,Lots);
         lastDirState = 1;
      }} else if ( (OpenOnSignalChange && lastDirection != OP_SELL && curDirection == OP_SELL)
               || (!OpenOnSignalChange && curDirection == OP_SELL && TradeCount[OP_SELL] == 0 && lastDirection == -1)
               ) {
         closeTrades(OP_BUY);
         if(_Is5MAStopDown){
          if (StopLossPips > 0)StopLoss = Bid + StopLossPips * MyPoint; else StopLoss = getZigZagStopLoss(OP_SELL);
         Lots = getLotSize3( (StopLoss-Bid)/MyPoint );
         //to otwiera pierwsze zlecenia
       subOpenOrder(OP_SELL, StopLossPips,TakeProfitPips,Lots);
         //Comment("SUB "+TimeToStr(TimeCurrent()));
         //Print("SUB "+TimeToStr(TimeCurrent()));
         lastDirState = 1;
      }
      }
      else 
      {
         if(lastDirState==1)
         lastDirState = -1;
      }
 
 
Angof1497: I cant understand why the IF statement will not take.
So put Print statements dumping your variables before each IF and a print inside (to know it triggered) so you find out WHY!
 
Angof1497:

I have an extern double StopLossPips and I would like to make it conditional with an IF statement. Basically I don't want the stop loss to kick in unless some condition exists in the market. In this case price above/below a 5MA. (_Is5MAStopUp)   I have tried to implement it within the code where the StopLossPips is referenced and I have put it before the subopenorder and 1-2 other places but not luck. I cant understand why the IF statement will not take. Any help appreciated.


Make your  extern an int  it's not quite as simple as this . . .

 if (StopLossPips > 0)

  . .  to test if a double value is really zero or not,  more information in this thread,  please read it:  Can price != price ? 

 

Thanks for replying, that if (StopLossPips  > 0 ) actually works. I don't know if you mean the other If statement may function if I make the stop loss pips n int.

 
Angof1497:

Thanks for replying, that if (StopLossPips  > 0 ) actually works. I don't know if you mean the other If statement may function if I make the stop loss pips n int.

Make your extern an int  not a double.

And read the thread at the link I gave. 

 
RaptorUK:

Make your  extern an int  it's not quite as simple as this . . .

  . .  to test if a double value is really zero or not,  more information in this thread,  please read it:  Can price != price ? 

Since mq4 doubles are double precision all integers (up to 2 billion) are stored exactly. d = int; if (d == aInt) will work correctly.
 

Is it not true that Ordermodify wont work for changing a stop loss if that stop loss eg 50 pips  is lower than what the market currently is say for example 100 pips? Would the ordermodify close my position or would the Ordermodify fail?

 
Angof1497, there are a lot of conditions in several branches within your if statement. Such intricate logic can contain conflicting possibilities and prevent the statement from being parsed all the way through. However, if we accept the premise that your logic is valid, there is something that can prevent the CALLED subroutine from executing. I know, because I have had this problem. When you try to execute an OrderClose() or OrderOpen() call, you need to use the NormalizeDouble() function for each of the doubles you put in the  call. Failure to normalize the values results in a failed execution of the call. This can appear to be a failure of the if statement, when in fact the if statement may be functioning properly. You are making two calls to other routines, closeTrades() and subOpenOrder(). It might help if you look at those routines and verify whether or not you are normalizing your parameters.
 
RaptorUK, in both the modify and the send (open) funtions, the stoploss parameter is required to be a double. You can't send an int. Plus, the comparison is to find out if the stop loss is GREATER THAN zero. There is no problem with that comparison.
 
CAonMQL4:
RaptorUK, in both the modify and the send (open) funtions, the stoploss parameter is required to be a double. You can't send an int. Plus, the comparison is to find out if the stop loss is GREATER THAN zero. There is no problem with that comparison.

Yes,  you are correct,  until the OP wants to test if the SL is greater than a value other than zero or equal to a value other than 0.

I was talking about the variable  StopLossPips  which,  from the variable name,  I would assume to be the position of the stoploss relative to the entry in pips,  so this can be an interger,  for example 10 pips,  or if you want more precision change it to StopLossPoints  it can stay as an int and the test . . . 

 if (StopLossPoints > 0)

. . .  works reliably.  When using it in an OrderSend() or OrderModify() it is used it to calculate the actual StopLoss . . .

double StopLoss;

StopLoss = Ask - (StopLossPoints * Point);
Reason: