Condition of liquidation

 

If I put it in this way,

Is there any error?





int CurrentTime;
int Past = 2;
int BarsCount = 0;
int i;
datetime PreviousBar;

int start()
  {



if((NewBar()) ){ 
for(cnt=0;cnt<OrdersTotal();cnt++)    
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES); 
      if(OrderMagicNumber()==MagicNo && OrderSymbol() == Symbol())  
        {
         if(OrderType()==OP_BUY)   
           {
            if(/*Condition of liquidation*/)
                {
                 OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); 
                 return(0);
                }
           }
         else 
           {
            if(/*Condition of liquidation*/)
              {
               OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet);
               return(0);
              }
           }
        }
     }

} return(0); }

bool NewBar() { if(PreviousBar<Time[0]) { PreviousBar=Time[0]; return(true); } return(false); }

 
for(cnt=0;cnt<OrdersTotal();cnt++)

Every time you close an order, OrdersTotal() will reduce by 1.

Count down when closing orders.

for(cnt=OrdersTotal()-1;cnt>=0;cnt--)


Don't use Bid and Ask to close an order in a loop without RefreshRates() as new ticks could occur.

or

Use OrderClosePrice() after selecting an order and there is no need to differentiate between Buy and Sell orders.

 

Are you saying this is changing?


However, the results are still not working



if((NewBar()) ){ 


  if(OrdersTotal() >0){
   for(cnt= OrdersTotal()-1 ;cnt>=0;cnt--)    
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);   
      if(OrderMagicNumber()==MagicNo && OrderSymbol() == Symbol()) 
        {
         if(OrderType()==OP_BUY)  
           {
            if(UpTrendP >0 && DownTrend >0) 
                {
                 OrderClose(OrderTicket(),OrderLots(),Bid,3,Blue);
                 return(0);
                }
           }
         else 
           {
            if(UpTrend >0 && DownTrendP >0) 
              {
               OrderClose(OrderTicket(),OrderLots(),Ask,3,Red); /
               return(0);
              }
           }
        }
     }
     }
}     
 
In the presence of multiple orders (one EA multiple charts, multiple EAs, manual trading,) because 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 (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.
  2. For FIFO (US brokers,) and you (potentially) process multiple orders per symbol, you must count up and on a successful operation, reprocess all positions (set index to -1 before continuing.)
  3. and check OrderSelect.
  4. and if you (potentially) process multiple orders, must call RefreshRates() after server calls if you want to use the Predefined Variables (Bid/Ask) or OrderClosePrice() instead, on the next order/server call.
 
whroeder1:
In the presence of multiple orders (one EA multiple charts, multiple EAs, manual trading,) because 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 (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.
  2. For FIFO (US brokers,) and you (potentially) process multiple orders per symbol, you must count up and on a successful operation, reprocess all positions (set index to -1 before continuing.)
  3. and check OrderSelect.
  4. and if you (potentially) process multiple orders, must call RefreshRates() after server calls if you want to use the Predefined Variables (Bid/Ask) or OrderClosePrice() instead, on the next order/server call.

It is too abstract for beginners like me.


Could you please edit the above code for example?

 
cape1354:

It is too abstract for beginners like me.


Could you please edit the above code for example?


Hi,

try this one :)

int totaltrades=OrdersTotal();
for(cnt=0; cnt<totaltrades; cnt++)
{
  //... your code here
}

and also, later you will understand why code above is better than this one:

for(cnt=0;cnt<OrdersTotal();cnt++)    
{
  //...
}
Reason: