Closing orders after a specified number of bars

 

Any chance that there's a kind person who can help me with the following pls :-)

Want some code that closes orders after say 4 bars, regardless of whether the trades are profitable or not.

I'm trading hourly bars, so figured if I hadn't hit a profit target or stop by 4 hours, I'd just close the trade. How do I program this?

Gave it a shot, but I just get errors and being a programmer of the novice variety, need to put my hand up and ask.

for(int i=1; i<=OrdersTotal(); i++)
{
if(OrderSelect(i-1,SELECT_BY_POS,MODE_TRADES)==true)
{
if(OrderSymbol()!=Symbol()) continue;
{
int CurrHour=TimeHour(TimeCurrent());
int CloseTradeHour=TimeHour(OrderOpenTime());
if((CurrHour-CloseTradeHour>=5) continue;
{
OrderClose(OrderTicket(),OrderLots(),Bid, PRM_Slippage, MediumSeaGreen);
}
}
}
}

 

Please use this to post code . . . it makes it easier to read.

 

Don't use Bid . . use OrderClosePrice()

 
for(int i=1; i<=OrdersTotal(); i++)
      {
      if(OrderSelect(i-1,SELECT_BY_POS,MODE_TRADES)==true)
         {
         if(OrderSymbol()!=Symbol()) continue;
            {           
            int CurrHour=TimeHour(TimeCurrent());
            int CloseTradeHour=TimeHour(OrderOpenTime());
            if((CurrHour-CloseTradeHour>=5) continue;
               {
                OrderClose(OrderTicket(),OrderLots(),Bid, PRM_Slippage, MediumSeaGreen);
               }
            }
         }
      }

Got it! Ta. M

 

Count down especially when closing orders. Otherwise you'll re-arrange the index and miss orders to close.

for(i=OrdersTotal()-1; i>=0; i--)
 
ubzen:

Count down especially when closing orders. Otherwise you'll re-arrange the index and miss orders to close.


Thanks ubzen. I've just tried it out and I'm getting different results. Still not quite right, but at least you've helped me move a major road block. I'll see if I can work the rest out. M
 
RaptorUK:

Don't use Bid . . use OrderClosePrice()


More of those 101 basics that I'm missing. Will do.

:-)

 
Melymoo:

Got it! Ta. M

for(int i=1; i<=OrdersTotal(); i++)
      {
      if(OrderSelect(i-1,SELECT_BY_POS,MODE_TRADES)==true)
         {
         if(OrderSymbol()!=Symbol()) continue;
            {           
            int CurrHour=TimeHour(TimeCurrent());
            int CloseTradeHour=TimeHour(OrderOpenTime());
            if((CurrHour-CloseTradeHour>=5) continue;
               {
                OrderClose(OrderTicket(),OrderLots(),Bid, PRM_Slippage, MediumSeaGreen);
               }
            }
         }
      }

  1. Using Bid won't work for Sell orders.
  2. If the Order was opened after 20:00 then 4-22 < 5. Your not handling roll over at midnight.
  3. You must count down in the presense of multiple orders (multiple charts)
  4. Not filtering by magic number makes the EA incompatable with any other including manual trading on the same pair (any/other time frames)
  5. Always test return codes.
  6. You wouldn't ever say If( (2+3 == 5) == true ) would you? Then why say if (OrderSelect(...) == true)
for(int iPos=OrdersTotal() - 1; iPos >=0; iPos--) if (
    OrderSelect(iPos,SELECT_BY_POS)
&&  OrderSymbol()      == Symbol()
&&  OrderMagicNumber() == magic.number
){
    int duration = iBarShift(0,NULL, OrderOpenTime());
    if (duration < 5) continue;
    if (!OrderClose(OrderTicket(),OrderLots(), OrderClosePrice(), PRM_Slippage, MediumSeaGreen))
        Alert("OrderClose Failed: ", GetLastError());
}


Also you're closing after 4 bars. Depending on when the order was opened in the bar that's 4-5 hours. If you want exactly 4 hours use:

    int duration = TimeCurrent() - OrderOpenTime();
    if (duration < 5 * 3600) continue;
 

I'm going to use this instead of what I wrote. It also means that I can use it on different times charts because it's based on bars. And big thanks for the other tips too WHRoeder.

:-)

 

   int duration = TimeCurrent() - OrderOpenTime();
    if (duration < 5 * 3600) continue

What happens if the order is placed late on Friday, say 1 hour before markets close?  Will it be closed first thing Sunday when the market opens again?  I assume TimeCurrent() doesn't care if the market is open or closed?

 
n4btb:

   int duration = TimeCurrent() - OrderOpenTime();
    if (duration < 5 * 3600) continue

What happens if the order is placed late on Friday, say 1 hour before markets close?  Will it be closed first thing Sunday when the market opens again?  I assume TimeCurrent() doesn't care if the market is open or closed?

Correct,  use the number of bars and not time . . .  unless you care about time,  then use time.