Expert Placing one pair stoploss on other pair. - page 2

 
Kenneth Parling:

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

Same error. Keeps importing values from other pairs. Even for the manually opened order. 
 
Antonio Bartolucci:
Same error. Keeps importing values from other pairs. Even for the manually opened order. 

there's got to be some other part of your code that's doing this...

 
Antonio Bartolucci:
Same error. Keeps importing values from other pairs. Even for the manually opened order. 

When you say manually opened order, you mean a snippet of your code put into a new testing EA? 

And do you run the original EA on a VPS? Make sure that is not running....

I think I would remove all charts except one, remove all indicators and even global variables on terminal, and then put your testing EA on that chart. So your test EA is the only one operating. Make your testing code as simple as possible, get it working and then expand it to include your other variables....you're looking for an "ah.." moment I think...

 
andrew:

When you say manually opened order, you mean a snippet of your code put into a new testing EA? 

And do you run the original EA on a VPS? Make sure that is not running....

I think I would remove all charts except one, remove all indicators and even global variables on terminal, and then put your testing EA on that chart. So your test EA is the only one operating. Make your testing code as simple as possible, get it working and then expand it to include your other variables....you're looking for an "ah.." moment I think...

I mean I hit the sell or buy button on the top left: that should mean that the Magic Number should be 0. So why would it even select it? If I have, after the OrderSelect 
If(OrderMagicNumber() != MIDBMAGICMA) continue; 
?

EA is running on local client, no VPS.

 
Antonio Bartolucci:
I mean I hit the sell or buy button on the top left: that should mean that the Magic Number should be 0. So why would it even select it? If I have, after the OrderSelect 
If(OrderMagicNumber() != MIDBMAGICMA) continue; 
?

EA is running on local client, no VPS.

I'd apply a "scorched earth" approach here, rather than try to figure it out. Which is something I'd had to do many times, before an "of course!" moment...

Have a single chart open, remove everything. Place a manual order, even modify it manually. That must work ok?

Then, code an EA to modify the single order you've placed; this should only be a few lines of code. But hard code the values you want; magicnumber, symbol, period etc . don't rely on Symbol(), MIDMAGICMA etc. 

If there's no errors then expand your code using your variables....then, add another order

 
Kenneth Parling:

try this one - not tested!

Nothing seems to fix this error unfortunately. I keep getting the same bug and it's frustrating. At this point this must be a source code bug. I tried everything

 
  1. for(int i=0; i<OrdersTotal(); i++)
         {
          if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
             break;
          if(OrderSymbol()!=_Symbol && OrderMagicNumber()!=...)
             continue;

    This selects all symbols with the proper magic number and all orders of the current chart symbol regardless of MN.

    Symbol
    MN
    OrderSymbol OrderMagicNumber OS != Sym
    OMN != MN
    OS != Sym && OMN != Mn
    A
    1
    A
    1
    false
    false
    false: A/MN OK
    A
    1
    A
    2
    false
    true
    false: All A OK
    A
    1
    B
    1
    true
    false
    false: All MN OK
    A
    1
    B
    2
    true
    true
    true: reject non-Sym && non-MN

    Don't use negative logic. Simplify your code.

    for(int i=OrdersTotal()-1; i>=0; --i) if(
       OrderSelect(i,SELECT_BY_POS,MODE_TRADES)
    && OrderMagicNumber() == …
    && OrderSymbol()      == _Symbol
    ){
      // Process order A/MN
  2. In the presence of multiple orders (one EA multiple charts, multiple EAs, manual trading,) while you are waiting for the current operation (closing, deleting, modifying) to complete, any number of other operations on other orders could have concurrently happened and changed the position indexing:
    1. For non-FIFO (non-US brokers), (or the EA only opens one order per symbol,) you can simply count down, in a position loop, and you won't miss orders. Get in the habit of always counting down.
                Loops and Closing or Deleting Orders - MQL4 programming forum
      For In First Out (FIFO rules-US brokers,) and you (potentially) process multiple orders per symbol, you must find the earliest order (count up,) close it, and on a successful operation, reprocess all positions.
                CloseOrders by FIFO Rules - Strategy Tester - MQL4 programming forum - Page 2 #16
                MetaTrader 5 platform beta build 2155: MQL5 scope, global Strategy Tester and built-in Virtual Hosting updates - Best Expert Advisors - General - MQL5 programming forum #1 № 11 ACCOUNT_FIFO_CLOSE

    2. and check OrderSelect in case earlier positions were deleted.
                What are Function return values ? How do I use them ? - MQL4 programming forum
                Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
    3. and if you (potentially) process multiple orders, must call RefreshRates() after server calls if you want to use, on the next order / server call, the Predefined Variables (Bid/Ask) or (be direction independent and use) OrderClosePrice().

 
William Roeder:
  1. This selects all symbols with the proper magic number and all orders of the current chart regardless of the magic number. Don't use negative logic.

    _Symbol
    MN
    OrderSymbol OrderMagicNumber OS != Sym
    OMN != MN
    OS != Sym && OMN != Mn
    A
    1
    A
    1
    false
    false
    true: reject
    A
    1
    A
    2
    false
    true
    false: All A OK
    A
    1
    B
    1
    true
    false
    false: All MN 1 OK
    A
    1
    B
    2
    true
    true
    true: reject Sym/MN

  2. Don't use negative logic. Simplify your code.

  3. In the presence of multiple orders (one EA multiple charts, multiple EAs, manual trading,) while you are waiting for the current operation (closing, deleting, modifying) to complete, any number of other operations on other orders could have concurrently happened and changed the position indexing:
    1. For non-FIFO (non-US brokers), (or the EA only opens one order per symbol,) you can simply count down, in a position loop, and you won't miss orders. Get in the habit of always counting down.
                Loops and Closing or Deleting Orders - MQL4 programming forum
      For In First Out (FIFO rules-US brokers,) and you (potentially) process multiple orders per symbol, you must find the earliest order (count up,) close it, and on a successful operation, reprocess all positions.
                CloseOrders by FIFO Rules - Strategy Tester - MQL4 programming forum - Page 2 #16
                MetaTrader 5 platform beta build 2155: MQL5 scope, global Strategy Tester and built-in Virtual Hosting updates - Best Expert Advisors - General - MQL5 programming forum #1 № 11 ACCOUNT_FIFO_CLOSE

    2. and check OrderSelect in case earlier positions were deleted.
                What are Function return values ? How do I use them ? - MQL4 programming forum
                Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
    3. and if you (potentially) process multiple orders, must call RefreshRates() after server calls if you want to use, on the next order / server call, the Predefined Variables (Bid/Ask) or (be direction independent and use) OrderClosePrice().

William Roeder, you might be my savior, so I edited the code in order to make it counting down. It now looks like this! Thanks for your precious help. 

void MidBounceSellSL()
  {


   double BBHi;
   double BBMid;
   double BBLow;

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








      
   for(int i=OrdersTotal()-1; i>=0; --i) 
   if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)&& OrderMagicNumber() == MIDBMAGICMA && OrderSymbol() == _Symbol)
   {
      

   
   


         //================// SELL MODIFYING CONDITIONS \\================\\ 
         //+++++++++++++++||++++++++++++++++++++++++++++||++++++++++++++++||
         //================\\        STOP  LOSS        //================//
         

         {

            if(TrailingStopAdvanceSell() == false)
              {

               if(OrderStopLoss() > BBMid && OrderStopLoss() < BBHi) {Print("Stop loss già piazzato");
                return;
              }
               else
               {



                 {
                  if(!OrderModify(OrderTicket(),Ask,BBMid+(StopLevel*PointValue),OrderTakeProfit(),0,clrGreen))
                    {
                     Print("************************************************SL: ",BBMid,". Non ho potuto modificare l'ordine #",OrderTicket()," a causa dell'errore #", GetLastError(),".");
                     break;
                    }


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


                 }
              }
           
        

            }

     }

  }
}
 
William Roeder:
  1. This selects all symbols with the proper magic number and all orders of the current chart symbol regardless of MN.

    Symbol
    MN
    OrderSymbol OrderMagicNumber OS != Sym
    OMN != MN
    OS != Sym && OMN != Mn
    A
    1
    A
    1
    false
    false
    false: A/MN OK
    A
    1
    A
    2
    false
    true
    false: All A OK
    A
    1
    B
    1
    true
    false
    false: All MN OK
    A
    1
    B
    2
    true
    true
    true: reject non-Sym && non-MN

    Don't use negative logic. Simplify your code.

  2. In the presence of multiple orders (one EA multiple charts, multiple EAs, manual trading,) while you are waiting for the current operation (closing, deleting, modifying) to complete, any number of other operations on other orders could have concurrently happened and changed the position indexing:
    1. For non-FIFO (non-US brokers), (or the EA only opens one order per symbol,) you can simply count down, in a position loop, and you won't miss orders. Get in the habit of always counting down.
                Loops and Closing or Deleting Orders - MQL4 programming forum
      For In First Out (FIFO rules-US brokers,) and you (potentially) process multiple orders per symbol, you must find the earliest order (count up,) close it, and on a successful operation, reprocess all positions.
                CloseOrders by FIFO Rules - Strategy Tester - MQL4 programming forum - Page 2 #16
                MetaTrader 5 platform beta build 2155: MQL5 scope, global Strategy Tester and built-in Virtual Hosting updates - Best Expert Advisors - General - MQL5 programming forum #1 № 11 ACCOUNT_FIFO_CLOSE

    2. and check OrderSelect in case earlier positions were deleted.
                What are Function return values ? How do I use them ? - MQL4 programming forum
                Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
    3. and if you (potentially) process multiple orders, must call RefreshRates() after server calls if you want to use, on the next order / server call, the Predefined Variables (Bid/Ask) or (be direction independent and use) OrderClosePrice().

So my code is this right now but the problem seem persisting. I don't know what else to try. It keeps placing stop loss on another pair. 

void MidBounceBuySL()
  {


   double BBHi;
   double BBMid;
   double BBLow;






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



   for(int i=OrdersTotal()-1; i>=0; --i) 
   if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)&& OrderMagicNumber() == MIDBMAGICMA && OrderSymbol() == _Symbol)
   {



      //================// BUY MODIFYING CONDITIONS \\================\\ 
      //+++++++++++++++||++++++++++++++++++++++++++++||++++++++++++++++||
      //================\\        STOP  LOSS        //================//
      if(OrderType() == OP_BUY)
        {
         if(TrailingStopAdvanceBuy() == false)
           {
              {
               if(OrderStopLoss() <= BBMid && OrderStopLoss() < BBHi) {Print("Stop loss già piazzato"); return; break;}
               else
                 {
                  if(!OrderModify(OrderTicket(),Bid,BBMid-StopLevel*PointValue,OrderTakeProfit(),0,clrGreen))
                    {
                     Print("Non ho potuto modificare l'ordine #",OrderTicket()," a causa dell'errore #", GetLastError(),".");
                     return;
                    }
                  else
                     Print("Ordine #",OrderTicket()," modificato con successo.");
                     break;


                 }
              }
           }
        }
     }


  }
  
 
Antonio Bartolucci: So my code is this right now but the problem seem persisting. I don't know what else to try. It keeps placing stop loss on another pair. 

Print the chart symbol along with the ticket number, and you will find it's being modified somewhere else.

Reason: