Loop issue

 

Hi all,

there is an issue with the below code, as the loop only modifies (TP/SL) the first order, and doesn't loop through all of the existing orders.

Can some one help.


#property copyright "C"
#property link      "h"
#property show_inputs

extern double     TakeProfit        = 1.2550;
extern double     StopLoss          = 0;
extern bool buyorder  = FALSE;
extern bool sellorder = FALSE;

int start()
  {
//----
   int HowDeepIsYourLove = OrdersTotal();
   double TP,SL;
   
   
//BUY
 if(buyorder == TRUE){  
 
   for(int z=0;z<HowDeepIsYourLove;z++) {
         if(OrderSelect(z,SELECT_BY_POS,MODE_TRADES)) {
            if(OrderType()==OP_BUY) {
            if(TakeProfit==0.0) TP=0;
            else TP = TakeProfit; 
            if(StopLoss==0.0) SL=0;
            else SL = StopLoss; 
            if(OrderTakeProfit()!=TP||OrderStopLoss()!=SL)
               OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP,0,CLR_NONE);
            }
         }
   }
   }
//sell
else if(sellorder == TRUE){ 

   for(int i=0;i<HowDeepIsYourLove;i++) {
      if(OrderSelect(z,SELECT_BY_POS,MODE_TRADES)) {
            if(OrderType()==OP_SELL) {
            if(TakeProfit==0.0) TP=0;
            else TP= TakeProfit; 
            if(StopLoss==0.0) SL=0;
            else SL= StopLoss; 
            if(OrderTakeProfit()!=TP||OrderStopLoss()!=SL)
               OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP,0,CLR_NONE);
            }
       }
     }  
    }
//----
   return(0);
  }
//+--------
 
t4fast:


Hi all,

there is an issue with the below code, as the loop only modifies (TP/SL) the first order, and doesn't loop through all of the existing orders.

Can some one help. 

Find out what is happening with your functions:  What are Function return values ? How do I use them ?
 
t4fast:


Hi all,

there is an issue with the below code, as the loop only modifies (TP/SL) the first order, and doesn't loop through all of the existing orders.

Can some one help.


Maybe this is what you need, Loops and Closing or Deleting Orders, and btw, you can compare double like this :

if(OrderTakeProfit() != TP||OrderStopLoss() != SL)

You may also read this Can price != price ?  

both by the same author, actually. Here's another one Some double values invisible during cycle? by different author.

Reason: