Problem with stop loss code

 

Hi, I am having trouble with my code which I use as an extra insurance to close any trades that are too far in the red (more than 7 pips). It seems like it closes trades almost immediately some times. It seems very unstable. I am wondering if a switch operator cannot handle more than one stipulation. Any help would be appreciated. By the way this is for MQL4 - is there a place for MQL4 or is it all mixed with 5 now? Thanks


 

void CheckAllOrders()
        {
        int mTotal = OrdersTotal();
        for ( int i = mTotal - 1; i >= 0; i-- )
         {
         while ( !IsTradeAllowed() ) Sleep(100);
         
         if (OrderSelect ( i, SELECT_BY_POS )== true)
           {
         int mType = OrderType();
         bool mResult = false;
         if ( mType == 0 &&  MarketInfo(Symbol(),MODE_BID) + 0.0007 > OrderOpenPrice() ) continue;
         if ( mType == 1 &&  MarketInfo(Symbol(),MODE_ASK) - 0.0007 < OrderOpenPrice() ) continue;
         if ( mType == 2 ) continue;
         if ( mType == 3 ) continue;
         if ( mType == 4 ) continue;
         if ( mType == 5 ) continue;
           
         switch ( mType ) 
               {
               case OP_BUY:
                  mResult = OrderClose (OrderTicket(), OrderLots(), Bid, 0, Red);
                  if(mResult == true)
                    {
                    break;
                    }
                  
               case OP_SELL:
                  mResult = OrderClose (OrderTicket(), OrderLots(), Ask, 0, Red);
                  if(mResult == true)
                    {
                    break;
                    }
               }
               
               if (mResult = false)
                  {
                  Alert("Order",OrderTicket(),"failed to close. Error",GetLastError());
               
                  }
         
           }       
         }       
         return;
      }   
 
Mark Boc:

Hi, I am having trouble with my code which I use as an extra insurance to close any trades that are too far in the red (more than 7 pips). It seems like it closes trades almost immediately some times. It seems very unstable. I am wondering if a switch operator cannot handle more than one stipulation. Any help would be appreciated. By the way this is for MQL4 - is there a place for MQL4 or is it all mixed with 5 now? Thank

EDIT: see post#8

Of course your trades close immediately because your condition is met immediately. At least you have to change these two conditions:

BTW using hard coded number isn't good idea.

 
Petr Nosek:

Of course your trades close immediately because your condition is met immediately. At least you have to change these two conditions:

BTW using hard coded number isn't good idea.

It could also have something to do with the perpetual loop he enters into directly following the first for loop iteration.
 
nicholishen:
It could also have something to do with the perpetual loop he enters into directly following the first for loop iteration.

Do you mean: while ( !IsTradeAllowed() ) Sleep(100); ?

I don't think  that has something to do with immediately closing orders.
There are inverted signs +/- in his conditions. If the spread is less than 0.0007 then orders close immediately. I've fixed that in my example.

 
Petr Nosek:

Do you mean: while ( !IsTradeAllowed() ) Sleep(100); ?

I don't think  that has something to do with immediately closing orders.
There are inverted signs +/- in his conditions. If the spread is less than 0.0007 then orders close immediately. I've fixed that in my example.

For some reason I read that as !IsStopped(). 

 
Mark Boc: By the way this is for MQL4 - is there a place for MQL4 or is it all mixed with 5 now?
MQL4 section, (bottom of the Root page?)
          General rules and best pratices of the Forum. - General - MQL5 programming forum
Next time post in the correct place. The moderators will likely move has moved this thread there soon.
 
Petr Nosek:

Of course your trades close immediately because your condition is met immediately. At least you have to change these two conditions:

BTW using hard coded number isn't good idea.

Hi Petr,

I respectfully disagree with your code. If you look at it again you will see that it should only go to the switch operator if it is further than 7 pips in the red and then should be automatically closed. It seems to work properly if it is on only one currency, but not if there are multiple currencies but I cannot see why that is the case. Also you said that you should not use a hard coded number - is that because it makes the code too unstable or because it is too easy to make a mistake? I am new to programming and am disappointed as my code seems to have many examples like above where it should work but doesn't or only sometimes. Is there any article you can suggest which shows how to make your code more stable? Thanks

 

For anyone who is interested - by adding 




if(OrderSymbol()!= Symbol())continue;

seems to make code work properly. I have no idea why!!!

 
Mark Boc:

Hi Petr,

I respectfully disagree with your code. If you look at it again you will see that it should only go to the switch operator if it is further than 7 pips in the red and then should be automatically closed. It seems to work properly if it is on only one currency, but not if there are multiple currencies but I cannot see why that is the case. Also you said that you should not use a hard coded number - is that because it makes the code too unstable or because it is too easy to make a mistake? I am new to programming and am disappointed as my code seems to have many examples like above where it should work but doesn't or only sometimes. Is there any article you can suggest which shows how to make your code more stable? Thanks

I'm sorry you are absolutely right. I'm used to on my style coding where if a condition is met then code do something. But your style is opposite. In this case your problem is in using Symbol(), Ask and Bid. Here is your code very fast updated (I hope I didn't make any mistakes) by me:

void CheckAllOrders()
  {
   int mTotal=OrdersTotal();
   for(int i=mTotal-1;i>=0;i--)
     {
      while(!IsTradeAllowed()) Sleep(100);
      if(OrderSelect(i,SELECT_BY_POS))
        {
         int mType=OrderType();
         bool mResult=true;
         double mSL=70*SymbolInfoDouble(OrderSymbol(),SYMBOL_POINT);
         if(mType==OP_BUY && OrderOpenPrice()-SymbolInfoDouble(OrderSymbol(),SYMBOL_BID)>mSL)
            mResult=OrderClose(OrderTicket(),OrderLots(),SymbolInfoDouble(OrderSymbol(),SYMBOL_BID),0,Red);
         else if(mType==OP_SELL && SymbolInfoDouble(OrderSymbol(),SYMBOL_ASK)-OrderOpenPrice()>mSL)
            mResult=OrderClose(OrderTicket(),OrderLots(),SymbolInfoDouble(OrderSymbol(),SYMBOL_ASK),0,Red);
         if(!mResult) Alert("Order",OrderTicket(),"failed to close. Error",GetLastError());
        }
     }
  }

But still there is a problem with mSL. This code works for 5 digit brokers for Forex well but... Better way is using maximal loss (in currency) than 7 PIPs (70 Points). But this is a more complex problem.

 
Mark Boc:

For anyone who is interested - by adding 

seems to make code work properly. I have no idea why!!!

Your adding can't work properly if you open orders on more than one Symbol.

Reason: