Sum of 2 Orders are not Correct Why

 

i dont know why SUM of 2 Orders are not correct , i am trying to close Last 2 orders with Minimum 1 usd profit. but in stratgy tester it,s closing no logic some time 0.50 cents and second trade at 0 , some time first order profit 1 usd and second order close at 17 usd loss. i cant understand what is the matter. any thing i am doing wrong


i also have function for LAST AND SECOND LAST OrderOpenTime.



trying hard but dont know what is the matter


please help me







for(i=0;i<=OrdersTotal();i++)
 {

  
  double orderprofit = 0.0;
  double lastorderprofit = 0.0;
  

   if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && OrderType()==OP_BUY && OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber
   )
   {
      
     if(OrderOpenTime()== LastBuy1())
     {
       orderprofit = orderprofit + OrderProfit();
       lastorderprofit = LastOrderProfitBuy();
      if(  orderprofit - lastorderprofit >= Min_Profit &&orderprofit <0 )
      {
     
      
      OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,clrYellow);
      
      OrderClose(OrderTicketNumberBuy(),OrderLots(),OrderClosePrice(),Slippage,clrYellow);
      
      
      }
 }
 
 }
   
  }
 

you reset the value of orderprofit on every new cycle.

//+------------------------------------------------------------------+
double orderprofit=0.0;
double lastorderprofit=0.0;

for(i=0;i<=OrdersTotal();i++)
  {
   if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && OrderType()==OP_BUY && OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
     {
      if(OrderOpenTime()==LastBuy1())
        {
         orderprofit=orderprofit+OrderProfit();
         lastorderprofit=LastOrderProfitBuy();
         if(orderprofit-lastorderprofit>=Min_Profit && orderprofit<0)
           {
            OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,clrYellow);
            OrderClose(OrderTicketNumberBuy(),OrderLots(),OrderClosePrice(),Slippage,clrYellow);
           }
        }
     }
  }
//+------------------------------------------------------------------+

it needs to be declared outside of the loop.

Also please use the styler (Ctrl+,)

And may want to check the return value of OrderClose() function.

 
Marco vd Heijden:

you reset the value of orderprofit on every new cycle.

it needs to be declared outside of the loop.

Also please use the styler (Ctrl+,)

And may want to check the return value of OrderClose() function.



i tried but no luck can you fix it



/////////////Order Close At Average////////////////
 double orderprofit = 0.0;
  double lastorderprofit = 0.0;
  
 for(i=0;i<=OrdersTotal();i++)
 {

  
  

   if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && OrderType()==OP_BUY && OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber
   )
   {
      
     if(OrderOpenTime()== LastBuy1())
     {
       orderprofit =OrderProfit();
       lastorderprofit = LastOrderProfitBuy();
      if(  orderprofit + lastorderprofit >= Min_Profit )
      {
     
      
      OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,clrYellow);
      
      OrderClose(OrderTicketNumberBuy(),OrderLots(),OrderClosePrice(),Slippage,clrYellow);
      
      
      }
      
      
      
 }
 
 }
 else 
      
       orderprofit = 0.0;
       lastorderprofit = 0.0;
   
  }
 
It's not complete.
 
Marco vd Heijden:

It's not complete.






 if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && OrderType()==OP_BUY && OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber
   )
   {
    
     if(OrderOpenTime()== LastBuy1())
     {
     
     
     
       orderprofit =OrderProfit();
       lastorderprofit = LastOrderProfitBuy();
      if(  (orderprofit + lastorderprofit) >= Min_Profit )
      {
      orderprofit = 0.0;
      lastorderprofit = 0.0;
      
      OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,clrYellow);
      
      OrderClose(OrderTicketNumberBuy(),OrderLots(),OrderClosePrice(),Slippage,clrYellow);
      
      
      }
      
      
      
 }
 
 }
 else 
      
       orderprofit = 0.0;
       lastorderprofit = 0.0;
   
  }
  
  

even i tried with this but no luck. i will be very thankful to you if you do some help to fix this. i really stuck on this from 3 days

 
It's not complete.
 
Marco vd Heijden:
It's not complete.



can you help me to complete it please

 
Muhammad Mudasir:



can you help me to complete it please

You can by adding

LastBuy1()

LastOrderProfitBuy()

OrderTicketNumberBuy()
 
Marco vd Heijden:

You can by adding



should i reset all them to 0 on every cycle

 
Muhammad Mudasir:



should i reset all them to 0 on every cycle

No that is the problem you can not reset them if you want to add them together.

I meant adding the code to your post here.

Your code is incomplete, functions are missing.

 
Marco vd Heijden:

No that is the problem you can not reset them if you want to add them together.

I meant adding the code to your post here.

Your code is incomplete, functions are missing.


datetime LastSell0()
 {
 
 
  datetime orderopentime[];                 //--- declare array                     
   int orderstotal=OrdersTotal();     //--- declare orderstotal
   ArrayResize(orderopentime,orderstotal,0); //--- resize array
   for(int i=0;i<orderstotal;i++)            //--- loop
     {
      //---- check selection result 
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)     //--- check result
        {
         Print("Access to history failed with error (",GetLastError(),")");
         break;
        }
      else if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true && OrderSymbol() == Symbol() && OrderType()==OP_SELL ) //--- check result 
        {
         //Print("Symbol: ",OrderSymbol(),
         //      " Ticket: ",OrderTicket(),
         //      " Open Time: ",OrderOpenTime(),
         //      " Open Price: ",OrderOpenPrice(),
         //      " Close Time: ",OrderCloseTime(),
         //      " Close Price: ",OrderClosePrice());
            
         //--- fill array      
          orderopentime[i]=OrderOpenTime(); //--- fill array     
        } 
     }
  int index;                                                        //--- declare index
  index=ArrayMaximum(orderopentime,WHOLE_ARRAY,0);                  //--- find oldest order
  Print(" Last order: ",TimeToString(orderopentime[index]));        //--- print result
  orderopentime[index]=0;                                           //--- reset value so it will not be found again
  index=ArrayMaximum(orderopentime,WHOLE_ARRAY,0);                  //--- find second oldest order
  Print(" Second Last order: ",TimeToString(orderopentime[index])); //--- print result 
  orderopentime[index]=0;                                           //--- reset value so it will not be found again
  index=ArrayMaximum(orderopentime,WHOLE_ARRAY,0);                  //--- find third oldest order
  Print(TimeToString(orderopentime[index]));  //--- print result
    return(OrderOpenTime());
 }
 
 datetime LastBuy0()
 {
 
 
  datetime orderopentime[];                 //--- declare array                     
   int orderstotal=OrdersTotal();     //--- declare orderstotal
   ArrayResize(orderopentime,orderstotal,0); //--- resize array
   for(int i=0;i<orderstotal;i++)            //--- loop
     {
      //---- check selection result 
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)     //--- check result
        {
         Print("Access to history failed with error (",GetLastError(),")");
         break;
        }
      else if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true && OrderSymbol() == Symbol() && OrderType()==OP_BUY ) //--- check result 
        {
         //Print("Symbol: ",OrderSymbol(),
         //      " Ticket: ",OrderTicket(),
         //      " Open Time: ",OrderOpenTime(),
         //      " Open Price: ",OrderOpenPrice(),
         //      " Close Time: ",OrderCloseTime(),
         //      " Close Price: ",OrderClosePrice());
            
         //--- fill array      
          orderopentime[i]=OrderOpenTime(); //--- fill array     
        } 
     }
  int index;                                                        //--- declare index
  index=ArrayMaximum(orderopentime,WHOLE_ARRAY,0);                  //--- find oldest order
  Print(" Last order: ",TimeToString(orderopentime[index]));        //--- print result
  orderopentime[index]=0;                                           //--- reset value so it will not be found again
  index=ArrayMaximum(orderopentime,WHOLE_ARRAY,0);                  //--- find second oldest order
  Print(" Second Last order: ",TimeToString(orderopentime[index])); //--- print result 
  orderopentime[index]=0;                                           //--- reset value so it will not be found again
  index=ArrayMaximum(orderopentime,WHOLE_ARRAY,0);                  //--- find third oldest order
  Print(TimeToString(orderopentime[index]));  //--- print result
    return(OrderOpenTime());
 }
datetime LastSell1()
 {
 
 
  datetime orderopentime[];                 //--- declare array                     
   int orderstotal=OrdersTotal();     //--- declare orderstotal
   ArrayResize(orderopentime,orderstotal,0); //--- resize array
   for(int i=0;i<orderstotal;i++)            //--- loop
     {
      //---- check selection result 
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)     //--- check result
        {
         Print("Access to history failed with error (",GetLastError(),")");
         break;
        }
      else if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true && OrderSymbol() == Symbol() && OrderType()==OP_SELL ) //--- check result 
        {
         //Print("Symbol: ",OrderSymbol(),
         //      " Ticket: ",OrderTicket(),
         //      " Open Time: ",OrderOpenTime(),
         //      " Open Price: ",OrderOpenPrice(),
         //      " Close Time: ",OrderCloseTime(),
         //      " Close Price: ",OrderClosePrice());
            
         //--- fill array      
          orderopentime[i]=OrderOpenTime(); //--- fill array     
        } 
     }
  int index;                                                        //--- declare index
  index=ArrayMaximum(orderopentime,WHOLE_ARRAY,0);                  //--- find oldest order
  Print(" Last order: ",TimeToString(orderopentime[index]));        //--- print result
  orderopentime[index]=0;                                           //--- reset value so it will not be found again
  index=ArrayMaximum(orderopentime,WHOLE_ARRAY,0);                  //--- find second oldest order
  Print(" Second Last order: ",TimeToString(orderopentime[index]));
  return(OrderOpenTime()); //--- print result 
  orderopentime[index]=0;                                           //--- reset value so it will not be found again
  index=ArrayMaximum(orderopentime,WHOLE_ARRAY,0);                  //--- find third oldest order
  Print(TimeToString(orderopentime[index]));  //--- print result
    
 }
 datetime LastBuy1()
 {
 
 
  datetime orderopentime[];                 //--- declare array                     
   int orderstotal=OrdersTotal();     //--- declare orderstotal
   ArrayResize(orderopentime,orderstotal,0); //--- resize array
   for(int i=0;i<orderstotal;i++)            //--- loop
     {
      //---- check selection result 
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)     //--- check result
        {
         Print("Access to history failed with error (",GetLastError(),")");
         break;
        }
      else if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true && OrderSymbol() == Symbol() && OrderType()==OP_SELL ) //--- check result 
        {
         //Print("Symbol: ",OrderSymbol(),
         //      " Ticket: ",OrderTicket(),
         //      " Open Time: ",OrderOpenTime(),
         //      " Open Price: ",OrderOpenPrice(),
         //      " Close Time: ",OrderCloseTime(),
         //      " Close Price: ",OrderClosePrice());
            
         //--- fill array      
          orderopentime[i]=OrderOpenTime(); //--- fill array     
        } 
     }
  int index;                                                        //--- declare index
  index=ArrayMaximum(orderopentime,WHOLE_ARRAY,0);                  //--- find oldest order
  Print(" Last order: ",TimeToString(orderopentime[index]));        //--- print result
  orderopentime[index]=0;                                           //--- reset value so it will not be found again
  index=ArrayMaximum(orderopentime,WHOLE_ARRAY,0);                  //--- find second oldest order
  Print(" Second Last order: ",TimeToString(orderopentime[index]));
  return(OrderOpenTime()); //--- print result 
  orderopentime[index]=0;                                           //--- reset value so it will not be found again
  index=ArrayMaximum(orderopentime,WHOLE_ARRAY,0);                  //--- find third oldest order
  Print(TimeToString(orderopentime[index]));  //--- print result
    
 }
 
 int OrderTicketsBuy ()
{
 for(int i = 0 ; i <= OrdersTotal() ; i++)
    
   {
    
   bool Orderinfo  =  OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
   
   
   if(Orderinfo == True && OrderType()==OP_BUY)
   {
   
   int OrderTicketBuy=OrderTicket(); 
   
   }
   
   
return(OrderTicketBuy);
}
}
int OrderTicketsSell ()
{
 for(int i = 0 ; i <= OrdersTotal() ; i++)
    
   {
    
   bool Orderinfo  =  OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
   
   
   if(Orderinfo == True && OrderType()==OP_SELL)
   {
   
   int OrderTicketSell=OrderTicket(); 
   
   }
   
   
return(OrderTicketSell);
}
}

 


   these are functions