Multiple Pairs messing up my for loop again! - page 2

 
DomGilberto:

I don't think I am following you?

... For the sake of making sure this now works and squashing it completely, if I gave each EA its individual magic number that corresponds to just one market, will it resolve this problem with multiple positions?

No . . . look at the functions above, assume you have 5 orders for different Symbols . . . what will be the last OrderSelected() when each of the functions is complete ? is it the same Order you selected in this code where marked . . .

double H1_Close = iClose(NULL, PERIOD_H1, 1);
double MA_sixty = iMA(NULL,60,60,0,1,0,1);

double MinLots = MarketInfo( Symbol(), MODE_MINLOT );

for(PositionIndex = TotalNumberOfOrders - 1; PositionIndex >= 0 ; PositionIndex --) 
  {  
   if( ! OrderSelect(PositionIndex, SELECT_BY_POS, MODE_TRADES) ) continue;   //  <----  what Order is selected here ? ? ? ?
    if( OrderMagicNumber() == MagicNumber  
      && OrderSymbol() == Symbol() )                
         {
            if(OrderType()==OP_SELL )
               {
               MA_Trail();
               CloseHalfOrder1(); 
               double Points_To_Deduct = (( OrderOpenPrice()-OrderTakeProfit()) / RewardRatio);
               FirstTarget_Sell = OrderOpenPrice()-Points_To_Deduct;
               double Points_To_Deduct2 = (( OrderOpenPrice()-OrderTakeProfit()) / ( RewardRatio / 2 ));
               TwoRatio_Sell_Target = OrderOpenPrice()-Points_To_Deduct2;
 
I thought OrderSelect would select any order (count down?), and then if the OrderMagicNumber() == MagicNumber and OrderSymbol()==Symbol() would filter it to get the right order?.... I guess not? (I'm sorry, I'm obviously not understanding the whole for loop then...)
 

https://www.mql5.com/en/forum/139654 - I've given your thread another read but I'm still not getting it.

 
DomGilberto:

https://www.mql5.com/en/forum/139654 - I've given your thread another read but I'm still not getting it.

OK, consider the following Orders . . .

Pos Symbol Magic
0GBPUSD101
1USDJPY212
2EURUSD312
3CHFJPY864
4EURCAD999

Your EA is on a GBPUSD chart, when your code above executes it finds the GBPUSD order and the symbol and magic number match . . . so then we are at this stage in the code . . .

if( OrderMagicNumber() == MagicNumber  
      && OrderSymbol() == Symbol() )                
         {
            if(OrderType()==OP_SELL )
               {
               

. . . the order is a Sell order so next this happens . . .

               {
               MA_Trail();
               CloseHalfOrder1(); 
               double Points_To_Deduct = (( OrderOpenPrice()-OrderTakeProfit()) / RewardRatio);
               FirstTarget_Sell = OrderOpenPrice()-Points_To_Deduct;
               double Points_To_Deduct2 = (( OrderOpenPrice()-OrderTakeProfit()) / ( RewardRatio / 2 ));
               TwoRatio_Sell_Target = OrderOpenPrice()-Points_To_Deduct2;

MA_Trail is called, followed by CloseHalfOrder1() both these functions have a loop that goes through all the Orders, selecting each in turn and seeing if it matches the symbol and magic number . . . so each will will get a match on the GBPUSD order and do their stuff, then they select the USDJPY order and get no match, then select the EURUSD order and get no match, the the CHFJPY order and get no match and finally the EURCAD order and get no match . . . . BUT, they both seleect the EURCAD order last . . . so when these function exit they both have selected the EURCAD order . . . when these functions return they return to here . . .

               MA_Trail();
               CloseHalfOrder1(); 

               double Points_To_Deduct = (( OrderOpenPrice()-OrderTakeProfit()) / RewardRatio);  //   <------  next code to run
               FirstTarget_Sell = OrderOpenPrice()-Points_To_Deduct;
               double Points_To_Deduct2 = (( OrderOpenPrice()-OrderTakeProfit()) / ( RewardRatio / 2 ));
               TwoRatio_Sell_Target = OrderOpenPrice()-Points_To_Deduct2;

. . . and you have a EURCAD order selected when you actually want a GBPUSD order selected . . . so OrderOpenPrice() and OrderTakeProfit() are for the EURCAD order and not for the Order you wanted . . . do you follow ?

If you work through your code in a logical way step by step you can find the issue . . . even without a debugger or variable watch or breakpoints . . .

 
DomGilberto:
I thought OrderSelect would select any order (count down?), and then if the OrderMagicNumber() == MagicNumber and OrderSymbol()==Symbol() would filter it to get the right order?.... I guess not? (I'm sorry, I'm obviously not understanding the whole for loop then...)

Unless your loop includes break after selecting the order that it is looking for, the loop will be completed and then if the last order selected is not the relevant one, your code will not do as you expect
 

Ah! Thank you, I've got it now :) Appreciate you explaining that! I forgot that it will call MA_Trail() and then CloseHalfOrder1() first, then return to do the other code, as you've highlighted. At which point, it is working on the wrong symbol...

This may irritate you a little, but I am not 100% certain how I am utilizing the break operator within this code... I understand that it terminates the execution of the nearest for operator, but I don't understand how I properly utilize the break function to do this... Am I using the break operator in the corresponding MA_Trail() and CloseHalfOrder1() voids? (that have for loops of their own?).

Thank you!

 

I assume it's as simple as: // Once this is complete, it will then termine this loop and return control to where the control was given from... right?

//Moving Average Trailing Stop Function
//+----------------------------------------------------------------------------------------------------------------------------------------+   
void MA_Trail()

{

   double ATR = iATR(NULL,60,14,1);
   double MA = iMA(NULL,60,MA_Period,0,1,0,1);
   
   double BuyStopPriceMath = MA - ATR;
   double SellStopPriceMath = MA + ATR;
   
   double BuyStopPrice = NormalizeDouble(BuyStopPriceMath,5);
   double SellStopPrice = NormalizeDouble(SellStopPriceMath,5);

   for(int b=OrdersTotal()-1; b>=0; b--)
     {
      if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
         if(OrderMagicNumber()==MagicNumber)
            if(OrderSymbol() == Symbol())
               {

               if(OrderType()==OP_BUY)
                  {
                  if(OrderStopLoss() - BuyStopPrice > Point / 2.)continue;
                  if(BuyStopPrice - OrderStopLoss() > Point / 2.)
                     bool BuyModify = OrderModify(OrderTicket(),OrderOpenPrice(),BuyStopPrice,OrderTakeProfit(),0,CLR_NONE);
                     break; // Once this is complete, it will then termine this loop and return control to where the control was given from... right?

                   }     

    
               if(OrderType()==OP_SELL)
                  {
                  if(SellStopPrice - OrderStopLoss() > Point / 2. )continue;
                  if(OrderStopLoss() - SellStopPrice > Point / 2. )
                     bool SellModify = OrderModify(OrderTicket(),OrderOpenPrice(),SellStopPrice,OrderTakeProfit(),0,CLR_NONE);
                     break; // Once this is complete, it will then termine this loop and return control to where the control was given from... right?
                  }
               }   
     }
}  
 
DomGilberto:

I assume it's as simple as: // Once this is complete, it will then termine this loop and return control to where the control was given from... right?

No . . . I don't think it is . . . come on, it's your code, you know what it does and doesn't do and what you need it to do, you should know it better than I do.


Take the scenario where you have 2 Orders for the symbol the EA is on, your main code ( previous page ) works on the first Order, the MA_Trail() and CloseHalfOrder1() will now find this first Order, do what they need to do and break leaving the correct order selected. What happens when you main code loop comes to the 2nd Order ? will MA_Trail() and CloseHalfOrder1() select the first Order again and break leaving the wrong Order selected ?

Answer me this . . . do you need MA_Trail() and CloseHalfOrder1() to ever work on any Order other than the one you currently have selected ? if the answer is no why don't you simply pass the ticket number to these functions, they can then process the one correct Order and life becomes simple . . .

 


Answer me this . . . do you need MA_Trail() and CloseHalfOrder1() to ever work on any Order other than the one you currently have selected ? if the answer is no why don't you simply pass the ticket number to these functions, they can then process the one correct Order and life becomes simple . . .

--There is only ever one order / open trade per market at any one time... So assuming you mean "work on any order other than the one currently selected on THAT specific OrderSymbol() == Symbol()... then the answer is no....

Likewise, why could I not simply use break; like I have written there? Surely that would just do the job and return the right symbol() back to where it was called from and continue on with the rest of the code...? I'm sorry if I am being a little slow, I am just trying to make sure I understand how the break operator works in the logic.

Once it "breaks" from the loop; if that particular void, say the MA_trail gets called again, will it start by counting down, and skipping the 5th position (e.g.)? Otherwise, if it counts down ALL open orders, will it not just pick up the one I want again, do what is needed to be done, then break to go back to the beginning?)

 
DomGilberto:


Answer me this . . . do you need MA_Trail() and CloseHalfOrder1() to ever work on any Order other than the one you currently have selected ? if the answer is no why don't you simply pass the ticket number to these functions, they can then process the one correct Order and life becomes simple . . .

--There is only ever one order / open trade per market at any one time... So assuming you mean "work on any order other than the one currently selected on THAT specific OrderSymbol() == Symbol()... then the answer is no....

Likewise, why could I not simply use break; like I have written there? Surely that would just do the job and return the right symbol() back to where it was called from and continue on with the rest of the code...? I'm sorry if I am being a little slow, I am just trying to make sure I understand how the break operator works in the logic.

Once it "breaks" from the loop; if that particular void, say the MA_trail gets called again, will it start by counting down, and skipping the 5th position (e.g.)? Otherwise, if it counts down ALL open orders, will it not just pick up the one I want again, do what is needed to be done, then break to go back to the beginning?)

RaptorUK:


Take the scenario where you have 2 Orders for the symbol the EA is on, your main code ( previous page ) works on the first Order, the MA_Trail() and CloseHalfOrder1() will now find this first Order, do what they need to do and break leaving the correct order selected. What happens when you main code loop comes to the 2nd Order ? will MA_Trail() and CloseHalfOrder1() select the first Order again and break leaving the wrong Order selected ?

. . . for the reason I stated here ^ ^ ^ ^ ^ ^ ^ ^

You know your code better than I do, perhaps the scenario I described won't happen . . .
Reason: