Multiple Pairs messing up my for loop again!

 

I really don't want to be writing another thread about this, but I cannot figure out where I am going wrong?

Am I being an idiot!? But as far as I am concerned, this EA should be working on the CORRECT symbol that is associated with the open position. Therefore, the line at the bottom where I have written notes, should be printing the correct corresponding price!?

int start()
 {
   if(IsNewCandle())
      {
      CheckForMaTrade();//signals, deletions and candle trails only need checked on a new candle.
      }

   
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//<<-- Selecting the correct and corresponding order (EA on Chart) that has been triggered for working on -- >>\\

int PositionIndex; 
int TotalNumberOfOrders;
TotalNumberOfOrders  = OrdersTotal();

static datetime Sell_Targets_Confirmed;
static datetime Buy_Targets_Confirmed;

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;
    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;
                         
               
               if(  OrderOpenTime() != Sell_Targets_Confirmed )
                  {    
                   Print(" First Target: ",DoubleToStr(FirstTarget_Sell,Digits), " On: ", Symbol());  //<< -- This is not corresponding to right Market?
                   Print(" Second Target: ",DoubleToStr(TwoRatio_Sell_Target,Digits), " On: ", Symbol()); //<< -- This is not corresponding to right Market?
                   Sell_Targets_Confirmed = OrderOpenTime();
                  }

...
 

To give you an example of what the prints look like:

2013.11.07 06:03:33     Trend Fishing - V1 - Refined Exits NZDCHF,H1:  Second Target: 159.65150 On: NZDCHF
2013.11.07 06:03:33     Trend Fishing - V1 - Refined Exits NZDCHF,H1:  First Target: 159.23225 On: NZDCHF

2013.11.07 08:01:09     Trend Fishing - V1 - Refined Exits EURNZD,H1:  Second Target: 0.77587 On: EURNZD
2013.11.07 08:01:09     Trend Fishing - V1 - Refined Exits EURNZD,H1:  First Target: 0.77060 On: EURNZD

2013.11.07 08:32:38     Trend Fishing - V1 - Refined Exits GBPJPY,H1:  Second Target: 0.783 On: GBPJPY
2013.11.07 08:32:38     Trend Fishing - V1 - Refined Exits GBPJPY,H1:  First Target: 0.774 On: GBPJPY

They're all using the same EA!?

 
DomGilberto:

I really don't want to be writing another thread about this, but I cannot figure out where I am going wrong?

Am I being an idiot!? But as far as I am concerned, this EA should be working on the CORRECT symbol that is associated with the open position. Therefore, the line at the bottom where I have written notes, should be printing the correct corresponding price!?

Do you perform an OrderSelect() in this function ? CloseHalfOrder1()
 
DomGilberto:

To give you an example of what the prints look like:

They're all using the same EA!?

And the EAs are on different chart Symbols . . . Symbol() is the symbol of the chart the EA is running on . . .

OK, I think I see what you are saying . . . the target prices look wrong for those symbols . . .
 
RaptorUK:
And the EAs are on different chart Symbols . . . Symbol() is the symbol of the chart the EA is running on . . .

OK, I think I see what you are saying . . . the target prices look wrong for those symbols . . .
So you know what to do . . . print all the variables, find out where the problem lies, fix it . . .
 
RaptorUK:
So you know what to do . . . print all the variables, find out where the problem lies, fix it . . .

Start with this . . . if you want the low hanging fruit first . . .

for(PositionIndex = TotalNumberOfOrders - 1; PositionIndex >= 0 ; PositionIndex --) 
  {  
   if( ! OrderSelect(PositionIndex, SELECT_BY_POS, MODE_TRADES) ) continue;
    if( OrderMagicNumber() == MagicNumber  
      && OrderSymbol() == Symbol() )                
         {
            if(OrderType()==OP_SELL )
               {
               Print("Order selected = ", OrderTicket(), " symbol = ", OrderSymbol() );  // <----- add this line

               MA_Trail();
               CloseHalfOrder1(); 

               Print("After other functions, Order selected = ", OrderTicket(), " symbol = ", OrderSymbol() );  // <--- add this line

               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;
                         
               
               if(  OrderOpenTime() != Sell_Targets_Confirmed )
                  {    
                   Print(" First Target: ",DoubleToStr(FirstTarget_Sell,Digits), " On: ", Symbol());  //<< -- This is not corresponding to right Market?
                   Print(" Second Target: ",DoubleToStr(TwoRatio_Sell_Target,Digits), " On: ", Symbol()); //<< -- This is not corresponding to right Market?
                   Sell_Targets_Confirmed = OrderOpenTime();
                  }
 
Ok so here is the prints:

2013.11.07 11:17:08     Trend Fishing - V1 - Refined Exits GBPJPY,H1: After other functions, Order selected = 2154301 symbol = GBPJPY
2013.11.07 11:17:08     Trend Fishing - V1 - Refined Exits GBPJPY,H1: Order selected = 2154301 symbol = GBPJPY

Thing is, right now, I no longer have multiple positions opened. I've restarted the platform and it's just identified the correct targets it needs to be focusing on.

I've just made 15 individual EA's that all contain different sequence of MagicNumbers to make sure they are all working only on the orders that are MagicNumber == MagicNumber and OrderSymbol() == Symbol().... Still cannot figure out why it was doing that though?

Guess I need to wait and see on multiple positions again....

(Thank you very much for your speedy response - really appreciate your help!!)

 
DomGilberto:
Ok so here is the prints:

Thing is, right now, I no longer have multiple positions opened. I've restarted the platform and it's just identified the correct targets it needs to be focusing on.

I've just made 15 individual EA's that all contain different sequence of MagicNumbers to make sure they are all working only on the orders that are MagicNumber == MagicNumber and OrderSymbol() == Symbol().... Still cannot figure out why it was doing that though?

Guess I need to wait and see on multiple positions again....

(Thank you very much for your speedy response - really appreciate your help!!)


RaptorUK:
Do you perform an OrderSelect() in this function ? CloseHalfOrder1()

or in MA_Trail() ?
 
Oh sorry, yes I do in both.

void CloseHalfOrder()
{   
   
   static datetime Closed_FirstTarget;
   datetime        FetchDateOfFirstClose = GlobalVariableGet(" GV_1st ");
   
   static datetime Closed_EMA_Target;
   datetime        FetchDateOfEMAClose = GlobalVariableGet(" GV_EMA ");
   
   static datetime Closed_ThirdTarget;
   datetime        FetchDateOfThirdClose = GlobalVariableGet(" GV_3rd ");
   
   double minLot=MarketInfo(Symbol(),MODE_MINLOT);
   //Print("The minimum lots are: ",DoubleToStr(minLot,Digits));
   double lotStep=MarketInfo(Symbol(),MODE_LOTSTEP);
   //Print("The Lotstep is: ",DoubleToStr(lotStep,Digits));
   
   double EMA_Bar = iClose(NULL, PERIOD_H1, 1);
   double EMA_MA = iMA(Symbol(),60,21,0,1,0,0);
   
   int PositionIndex;    //  <-- this variable is the index used for the loop
   int TotalNumberOfOrders;   //  <-- this variable will hold the number of orders currently in the Trade pool
   TotalNumberOfOrders = OrdersTotal();    // <-- we store the number of Orders in the variable
   
for(PositionIndex = TotalNumberOfOrders - 1; PositionIndex >= 0 ; PositionIndex --) 
    {
     bool CurrentOrder = OrderSelect(PositionIndex,SELECT_BY_POS,MODE_TRADES);
      if( !CurrentOrder ) continue;  
       if(OrderMagicNumber()==MagicNumber)
        if(OrderSymbol() == Symbol())
        { 
         if( CurrentOrder == True)  //<<<<<<<<<-----------  If the OrderSelect returned true, then find the answer to the formula' below?
            {
               double half_1st =MathFloor(OrderLots()/First_Target/lotStep)*lotStep;
               double half_2nd =MathFloor(OrderLots()/EMA_Target/lotStep)*lotStep;
               double Target_2 =MathFloor(OrderLots()/Second_Target/lotStep)*lotStep;

               FirstTarget_Buy =OrderOpenPrice()+(( OrderTakeProfit()-OrderOpenPrice()) / RewardRatio );
                     //if( FirstTarget_Buy == 0.0 )Print(" FirstTarget_Buy = ", FirstTarget_Buy); 
               TwoRatio_Buy = OrderOpenPrice()+(( OrderTakeProfit()-OrderOpenPrice()) / ( RewardRatio / 2 )); 
                     //if( TwoRatio_Buy == 0.0 )Print(" TwoRatio_Buy = ", TwoRatio_Buy);
            }

...

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

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);
                     //if(!BuyModify)Print(" Buy Trailing Stop Failed: ", GetLastError(), " On: ", OrderSymbol());
                   }     

    
               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);
                     //if(!SellModify)Print(" Sell Trailing Stop Failed: ", GetLastError(), " On: ", OrderSymbol());
                  }
               }   
     }
}  
 
DomGilberto:
Oh sorry, yes I do in both.


The loops in these functions loop through all the orders, if the last one they select is not for Symbol() then the order you will then have selected will be wrong for Symbol() . . . the Prints you added will show this when you have multiple orders open on different symbols . .
 

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? (i.e. no two magic numbers are the same, so when it crosses If( OrderMagicNumber() == MagicNumber ) it cannot get muddled up with thinking it is working on different pairs?)

Reason: