/*[[
Name := UserFunction: fnBarExit
Author :=
Copyright © 2005, iExpertAdvisor Corp.
Link := http://www.iExpertAdvisor.com
Notes := Free MetaTrader UserFunction, Expert Advisor.  Exit the trade after N bars. Used for testing strategies.
]]*/

inputs:  numBars(4);
vars:     cnt(0);


 for cnt=1 to TotalTrades
 begin
   If( Symbol == Ord(cnt,VAL_SYMBOL) ) then
   {
      If( CurTime() > (LastTradeTime()+ (Period()*numBars*Period))) then
      {
            //close the order
            If (Ord(cnt, VAL_TYPE ) == OP_BUY ) then
            {
               CloseOrder( Ord(cnt,VAL_TICKET), Ord(cnt,VAL_LOTS), Bid, 3, RED ); 
               print("Buy Close Time:", TimeToStr(CurTime));
            }
            else
            {
               CloseOrder( Ord(cnt,VAL_TICKET), Ord(cnt,VAL_LOTS), Ask, 3, RED ); 
               print("Sell Close Time:", TimeToStr(CurTime));
            }
      }
   return(0); 

   }; // end if symbol
end;

return(0);