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) {

 if(bars>closeAfter) {
  if(OrderSelect(ticket,SELECT_BY_TICKET)) {
   if(OrderType()==OP_SELL) {
   if(OrderType()==OP_BUY) {


Share with your friends:

Send us your comments

Name (required)

Email (required)


  • Facebook