Expert Placing one pair stoploss on other pair.

 

Hello everyone,

i've been having an issue with this function: what i'm basically trying to do is placing a stop loss on the opposite bollinger band when the OnTimer event occurs. The function works perfectly, except for the fact that the stop loss is placed on the wrong instrument when multiple positions are open. Can someone help? Here's the code of the function:

void MidBounceSellSL()
  {


   double BBHi;
   double BBMid;
   double BBLow;






   BBHi=iBands(Symbol(),0,BPeriod,2,0,PRICE_CLOSE,MODE_UPPER,0);
   BBLow=iBands(Symbol(),0,BPeriod,2,0,PRICE_CLOSE,MODE_LOWER,0);
   BBMid=iBands(Symbol(),0,BPeriod,2,0,PRICE_CLOSE,MODE_MAIN,0);



   for(int i=0; i<CalculateCurrentNonUserOrders(Symbol()); i++)
     {

      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) == false)
         continue;
      if(OrderMagicNumber() != MIDBMAGICMA)
         continue;
      if(OrderSymbol() != Symbol()) return;
      else
        {




         //================// SELL MODIFYING CONDITIONS \\================\\ 
         //+++++++++++++++||++++++++++++++++++++++++++++||++++++++++++++++||
         //================\\        STOP  LOSS        //================//
         if(OrderType() == OP_SELL)
           {
            if(TrailingStopAdvanceSell() == false)
              {

               if(OrderStopLoss() != BBHi)



                 {
                  if(!OrderModify(OrderTicket(),Ask,BBHi,OrderTakeProfit(),0,clrGreen))
                    {
                     Print("************************************************Non ho potuto modificare l'ordine #",OrderTicket()," a causa dell'errore #", GetLastError(),".");
                     return;
                    }


                  else
                     Print("Ordine #",OrderTicket()," modificato con successo.");
                  Print(Symbol());

                 }
              }
           }
        }



     }

  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void MidBounceBuySL()
  {


   double BBHi;
   double BBMid;
   double BBLow;






   BBHi=iBands(Symbol(),0,BPeriod,2,0,PRICE_CLOSE,MODE_UPPER,0);
   BBLow=iBands(Symbol(),0,BPeriod,2,0,PRICE_CLOSE,MODE_LOWER,0);
   BBMid=iBands(Symbol(),0,BPeriod,2,0,PRICE_CLOSE,MODE_MAIN,0);




   for(int i=0; i<OrdersTotal(); i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)
         continue;
      if(OrderMagicNumber()!=MIDBMAGICMA)
         continue;
      if(OrderSymbol()!=Symbol())
         continue;




      //================// BUY MODIFYING CONDITIONS \\================\\ 
      //+++++++++++++++||++++++++++++++++++++++++++++||++++++++++++++++||
      //================\\        STOP  LOSS        //================//
      if(OrderType() == OP_BUY)
        {
         if(TrailingStopAdvanceBuy() == false)
           {
              {
               if(OrderStopLoss() != BBLow)
                 {
                  if(!OrderModify(OrderTicket(),Bid,BBLow,OrderTakeProfit(),0,clrGreen))
                    {
                     Print("Non ho potuto modificare l'ordine #",OrderTicket()," a causa dell'errore #", GetLastError(),".");
                     return;
                    }
                  else
                     Print("Ordine #",OrderTicket()," modificato con successo.");
                  Print(Symbol());

                 }
              }
           }
        }
     }


  }

I have no idea why this happens and i've even tried reaching out MQL5 for possible assistance. Any suggestions? 

 
Antonio Bartolucci:

Hello everyone,

i've been having an issue with this function: what i'm basically trying to do is placing a stop loss on the opposite bollinger band when the OnTimer event occurs. The function works perfectly, except for the fact that the stop loss is placed on the wrong instrument when multiple positions are open. Can someone help? Here's the code of the function:

I have no idea why this happens and i've even tried reaching out MQL5 for possible assistance. Any suggestions? 

This line here:

if(!OrderModify(OrderTicket(),Bid,BBLow,OrderTakeProfit(),0,clrGreen))

On your OnTimer event, every required ticket should have its SL modified in this code segment?

So do a print of your parameters of OrderModify(), for each ticket. That should give you a list of OrderTicket()s, SL etc and then work backwards from there if they are not correct.

 
andrew:

This line here:

On your OnTimer event, every required ticket should have its SL modified in this code segment?

So do a print of your parameters of OrderModify(), for each ticket. That should give you a list of OrderTicket()s, SL etc and then work backwards from there if they are not correct.

I did try, and by doing the debugging, it returns all parameters correctly, then it does this  .

It basically places the stops on the other change... I'm desperate, can't figure this out... 


 

I tried also to make it print the TICKET of the selected order, and it's correct. I have no idea why this is happening.

Seems like the expert plugged to a chart modifies the order of another chart. How's this even possible if everything is called by Symbol????



 
  1.         if(OrderType() == OP_SELL)
    ⋮
                      if(!OrderModify(OrderTicket(),Ask,BBHi,OrderTakeProfit(),0,clrGreen))

    You have an open order. You can't modify the open price after the fact.

  2.                if(OrderStopLoss() != BBLow)
    
    Doubles are rarely equal. Understand the links in:
              The == operand. - MQL4 programming forum
 
William Roeder:
  1. You have an open order. You can't modify the open price after the fact.

  2. Doubles are rarely equal. Understand the links in:
              The == operand. - MQL4 programming forum

Ok so, the Ask paramether in the Price section of OrderModify() is an error and in order to fix the doubles error i might want to put a range around the doubles i need. Yet, this does not explain why one instance of the Expert which selects the order by Symbol and Magicnumber, edits orders belonging to another symbol :\

 
Antonio Bartolucci:

Ok so, the Ask paramether in the Price section of OrderModify() is an error and in order to fix the doubles error i might want to put a range around the doubles i need. Yet, this does not explain why one instance of the Expert which selects the order by Symbol and Magicnumber, edits orders belonging to another symbol :\

Probably we don't know enough about your code logic to solve, but what might benefit is some rewriting of the code to simplify and help identify the bug: working out how you might solve the problem.....

It looks like you're doing a similar thing for both buy and sell, so why not have one function?

for(int i=0; i<OrdersTotal(); i++){
 if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES))      continue;
 if(OrderSymbol()!=_Symbol||OrderMagicNumber()!=...)continue;
   
 if(OrderType()==OP_SELL&&OrderStopLoss()>BBHi)
  // OrderModify...
  

if(OrderType()==OP_BUY&&OrderStopLoss()<BBLow)
  // OrderModify...
}

But for debugging, instead of OrderModify inside the code fragment, I might save the modify parameters into a separate array of orders, and read back the values, outside the loop.....

===

loop orders
  select the orders for modification
  write ticket, SL etc into array of orders
 end loop
read array of orders

===

Documentation on MQL5: Trade Functions / OrderSelect
Documentation on MQL5: Trade Functions / OrderSelect
  • www.mql5.com
Selects an order to work with. Returns true if the function has been successfully completed. Returns false if the function completion has failed. For more information about an error call GetLastError(). Do not confuse current pending orders with positions, which are also displayed on the "Trade" tab of the "Toolbox" of the client terminal...
 
andrew:

Probably we don't know enough about your code logic to solve, but what might benefit is some rewriting of the code to simplify and help identify the bug: working out how you might solve the problem.....

It looks like you're doing a similar thing for both buy and sell, so why not have one function?

But for debugging, instead of OrderModify inside the code fragment, I might save the modify parameters into a separate array of orders, and read back the values, outside the loop.....

===

loop orders
  select the orders for modification
  write ticket, SL etc into array of orders
 end loop
read array of orders

===

Tried, but always getting the same bug. 
At this point I’m convinced it’s a source error, because I tried to open a random order ( manually ) and another pair will place the stop loss on that order EVEN THOUGH THERE’S NO MAGIC NUMBER HERE. So at this point it’s clear it’s a source error. 
 
Antonio Bartolucci:
Tried, but always getting the same bug. 
At this point I’m convinced it’s a source error, because I tried to open a random order ( manually ) and another pair will place the stop loss on that order EVEN THOUGH THERE’S NO MAGIC NUMBER HERE. So at this point it’s clear it’s a source error. 

try this one - not tested!

for(int i=0; i<OrdersTotal(); i++)
     {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         break;
      if(OrderSymbol()!=_Symbol && OrderMagicNumber()!=...)
         continue;
      if(OrderSymbol()==_Symbol && OrderMagicNumber()==...)
        {
         if(OrderType()==OP_SELL&&OrderStopLoss()>BBHi)
           {
            if(!OrderModify...))
              {
               Print("Error modifying");
               return;
              }
           }

         if(OrderType()==OP_BUY&&OrderStopLoss()<BBLow)
           {
            if(!OrderModify...))
              {
               Print("Error modifying");
               return;
              }
           }
        }
     }
 
Kenneth Parling:

try this one - not tested!

I will test this and give you response, thank you very much. 
 
Antonio Bartolucci:
I will test this and give you response

don't forget to fill in the OrderModify with details...as for now they are empty

 
Kenneth Parling:

don't forget to fill in the OrderModify with details...as for now they are empty

Of course yes, I already did. 
Reason: