How to close all orders before weekend.

[Deleted]  

The function DayOfWeek() returns the current zero-based day of the week (0-Sunday,1,2,3,4,5,6) of the last known server time.

My broker's (FXopen) server time is GMT. The server opens a new daily bar at GMT 00:00.

Eastern Time (New York) is GMT-4. If the forex market officially closes at 5:00 PM on Friday in New York, my server time should be 9:00 PM.

If I want to close all orders at 8:00 PM GST (4:00 PM New York), would the following be the correct code to use?

void CheckForClose()
{
if( OrdersTotal() > 0 ){
if( (DayOfWeek()==5 && Hour()==20) ){

int ordticket;
for( int i = 0 ; i < OrdersTotal() ; i++ ){
if( OrderSelect( i, SELECT_BY_POS, MODE_TRADES ) == false ){
break; // or should I use "continue" here?
}
if( OrderMagicNumber() != EA_magic ){
continue;
}
if( OrderSymbol() != Symbol() ){
continue;
}
ordticket = OrderTicket();
OrderSelect(ordticket, SELECT_BY_TICKET, MODE_TRADES);
OrderClose(ordticket,OrderLots(),Bid,slippage+MarketInfo(Symbol(),MODE_SPREAD),White);

}

}

return(0);

}

Thank you looking into this, Helmut

[Deleted]  

Gordon's answer in "Help needed" - "You should loop down and not up in your order closing loop" also applies here.

I shall change to... for( int i = OrdersTotal(); i>0; i-- ){

Thank you, Gordon

[Deleted]  
Should I squeeze in "RefreshRates();" before "ordticket..."?
 
engcomp:

Gordon's answer in "Help needed" - "You should loop down and not up in your order closing loop" also applies here.

I shall change to... for( int i = OrdersTotal(); i>0; i-- ){

Trade/history pool indexing starts at zero, so It should be:

for( int i=OrdersTotal()-1; i>=0; i-- ) {   


You have already been asked to use the SRC button. Please do so. It's really annoying to read unindented code.


 
engcomp:
Should I squeeze in "RefreshRates();" before "ordticket..."?
Yes. And u should also check OrderSelect()'s return value.
[Deleted]  

Thank you, thank you, Gordon

The code now reads...

void CloseAll()
{
      int ordticket;
    
      for( int i = OrdersTotal()-1; i>=0 ; i-- ){
        if( OrderSelect( i, SELECT_BY_POS, MODE_TRADES ) == false ){
            break; // Gordon, should this be "continue"?
        }
        if( OrderMagicNumber() != EA_magic ){
            continue;
        }
        if( OrderSymbol() != Symbol() ){
            continue;
        }
        RefreshRates();
        ordticket = OrderTicket();
        if(OrderSelect(ordticket,SELECT_BY_TICKET,MODE_TRADES))
        {
           OrderClose(ordticket,OrderLots(),Bid,slippage+MarketInfo(Symbol(),MODE_SPREAD),White);
           return(0);
        }
        else
        {
           Print("CloseAll() returned an error: ",GetLastError());
           return(-1);
        }
      }
   return(0);
}

The test...

if( (DayOfWeek()==5 && Hour()==20) CloseAll();

is performed elsewhere. Is this correct, or should it be "Hour()>=20"?

This is a wonderful forum, thank you again, Helmut

 
void CloseAll()
{
      int ordticket;
    
      for( int i = OrdersTotal()-1; i>=0 ; i-- ){
        if( OrderSelect( i, SELECT_BY_POS, MODE_TRADES ) == false ){
            break; // Gordon, should this be "continue"?
//yes, break will stop the for loop 
        }
        if( OrderMagicNumber() != EA_magic ){
            continue;
        }
        if( OrderSymbol() != Symbol() ){
            continue;
        }
        RefreshRates();
        ordticket = OrderTicket();
        if(OrderSelect(ordticket,SELECT_BY_TICKET,MODE_TRADES))
        {
           OrderClose(ordticket,OrderLots(),Bid,slippage+MarketInfo(Symbol(),MODE_SPREAD),White);
           return(0);//if you return here the for loop is stopped, some orders could be left open
        }
        else
        {
           Print("CloseAll() returned an error: ",GetLastError());
           return(-1);//if you return here the for loop is stopped, some orders could be left open
        }
      }
   return(0);
}

Hour()>=20 would be better


hth

[Deleted]  

Thank you, Russell

The point you make is perfectly clear. I feel like a fool and appreciate the help.

Best regards, Helmut

 
if you don't make mistakes, you're learning nothing.
[Deleted]  

There are sometimes reasons in an EA when you want to close only the shorts or the longs.

Here is the code that resulted from our discussion...

void CloseAllShorts()
{
    int ordticket;
    
    for( int i = OrdersTotal()-1; i>=0; i-- ){
      if( OrderSelect( i, SELECT_BY_POS, MODE_TRADES ) == false ){
         continue;
      }
      if( OrderMagicNumber() != EA_magic ){
          continue;
      }
      if( OrderSymbol() != Symbol() ){
          continue;
      }
      if( OrderType() != OP_SELL ){
          continue;
      }
      RefreshRates();
      ordticket = OrderTicket();
      if( OrderSelect(ordticket, SELECT_BY_TICKET, MODE_TRADES) == true ){
         OrderClose(ordticket,OrderLots(),Ask,slippage,White);
      } else {
         Print("CloseAllShorts() returned an error: ",GetLastError());
      }
    }
    return(0);
}

void CloseAllLongs()
{
    int ordticket;
    
    for( int i = OrdersTotal()-1; i>=0; i-- ){
      if( OrderSelect( i, SELECT_BY_POS, MODE_TRADES ) == false ){
         continue;
      }
      if( OrderMagicNumber() != EA_magic ){
          continue;
      }
      if( OrderSymbol() != Symbol() ){
          continue;
      }
      if( OrderType() != OP_BUY ){
          continue;
      }
      RefreshRates();
      ordticket = OrderTicket();
      if( OrderSelect(ordticket, SELECT_BY_TICKET, MODE_TRADES) == true ){
         OrderClose(ordticket,OrderLots(),Bid,slippage,White);
      } else {
         Print("CloseAllLongs() returned an error: ",GetLastError());
      }
    }
    return(0);
}
What a service you provide to this forum, thank you, Helmut
 

looks great! you can combine these 2 into 1 function. Makes your code a bit more compact


void CloseAllType(int _iType)
{
    int ordticket;
    
    for( int i = OrdersTotal()-1; i>=0; i-- ){
      if( OrderSelect( i, SELECT_BY_POS, MODE_TRADES ) == false ){
         continue;
      }
      if( OrderMagicNumber() != EA_magic ){
          continue;
      }
      if( OrderSymbol() != Symbol() ){
          continue;
      }
      if( OrderType() != _iType ){
          continue;
      }
      RefreshRates();
      ordticket = OrderTicket();
      double ldPrice = 0;
      if (OrderType() == OP_BUY){
          ldPrice = Bid;
      }      
      if (OrderType() == OP_SELL){
           ldPrice = Ask;          
      }
      if( OrderSelect(ordticket, SELECT_BY_TICKET, MODE_TRADES) == true ){
         OrderClose(ordticket,OrderLots(),ldPrice ,slippage,White);
      } else {
         Print("CloseAllType() returned an error: ",GetLastError());
      }
    }
    return(0);
}