How to count bar after opening order

 

I need help on ea, how to count bar after opening position (buy or sell). That is when ea open/execute order then count will start at next bar and close order when number of bar is equal to 10. This code only count bar from symbol history but does not start count when ea execute order. I want the ea to count 10 bars after execute trade and close when trade is equal to 10.

//--- should it be closed?

      for (int i = 0; i>=Tbar ; i++)

       {

      if(OrderType()==OP_BUY)

                if ( Bars == Tbar )

           {

               //--- close order and exit

               if(!OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet))

                  Print("OrderClose error ",GetLastError());

                     return;

           }

      }

    }

    else // go to short position

           {

            //--- should it be closed?

            if(OrderType()==OP_SELL)

                 if ( Bars == Tbar )

              {

               //--- close order and exit

               if(!OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet))

                  Print("OrderClose error ",GetLastError());

               return;

              }

         }

     } 
 

Forum on trading, automated trading systems and testing trading strategies


Hello,

Please use the SRC button when you post code. Thank you.


This time, I edited it for you.


 
ecoword:

I want the ea to count 10 bars after execute trade and close when trade is equal to 10.

if(OrderType() < 2)
        if(iBarShift(_Symbol, PERIOD_CURRENT, OrderOpenTime()) > 10) {
                if(!OrderClose(OrderTicket(), OrderLots(), OrderType() == OP_BUY ? Bid : Ask, 3, Violet))
                        Print("OrderClose error ",GetLastError());
}
Reason: