Closing orders after a specified number of bars

In a comment to one of the latest entries Greg asked for an EA code, which will close the specified order after N bars.

You will find such code in the next part of this entry. For now, lets explain the parameters:

  • ticket – order ticket (its ID, number)
  • closeAfter – number of bars, counted according to the timeframe this EA is working on, after which the order with the specified ticket will be closed

And here is the code:

extern int ticket;
extern int closeAfter=2;

void start() {

 static int prevTime;
 static int bars;

 if(Time[0]!=prevTime) {
  bars++;
  prevTime=Time[0];
 }

 if(bars>closeAfter) {
  if(OrderSelect(ticket,SELECT_BY_TICKET)) {
   if(OrderType()==OP_SELL) {
    OrderClose(ticket,OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),0,CLR_NONE);
   }
   if(OrderType()==OP_BUY) {
    OrderClose(ticket,OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),0,CLR_NONE);
   }
  }
 }

}

Share with your friends:

Send us your comments

Name (required)

Email (required)

Message

  • Facebook