Script to Close Orders That Are Too Old

Sup Babypips,

I’m trying to write a quick script to close orders that are too old.

Here what I have so far:

int oo_5, total_5;
total_5 = OrdersTotal();
for(oo_5=(total_5);oo_5>=0;oo_5–)
{//X1
if
(
OrderSelect(oo_5, SELECT_BY_POS, MODE_TRADES)==true
&&
total_5>0
)
{//X2
if
((TimeCurrent()-OrderOpenTime()) > MaxAge)
{//X3
if(OrderType() == OP_BUY) // long position is opened
{//X4
Print("Closing Old BUY Order: “,OrderOpenTime(),” - ",TimeCurrent());
OrderClose(OrderTicket(), OrderLots(), Bid, 3, Violet); // close buy position
}//X4 SHUT
else
{//X5
Print("Closing Old SELL Order: “,OrderOpenTime(),” - ",TimeCurrent());
OrderClose(OrderTicket(), OrderLots(), Ask, 3, Violet); // close short position
}//X5 SHUT
}//X3 SHUT
}//X2 SHUT
}//X1 SHUT

MaxAge would be defined as the number of seconds. Setting MaxAge = 3600 should close and order that is more than an hour old.

Any idea what I’m doing wrong? I feel like I’m taking crazy pills here.

-DK