How to close a trade one bar after the entry point?

 

Hi, I would like to know how can I close my orders at the next bar of my entry point.

Thanks

 
rodrigosm:

Hi, I would like to know how can I close my orders at the next bar of my entry point.

Thanks

on the beginning of a next Bar:

Time[0]>OrderOpenTime() ... OrderClose

 
EADeveloper:

on the beginning of a next Bar:

Time[0]>OrderOpenTime() ... OrderClose


I did that before post it, but MT4 showed me the following message: Error 138. I know it's an error on close order, but I don't know why it's showing that.

Thanks for help me.

 

show the relevant code regarding OrdoerClose()

 

Did you do refresh rates before the order close ? Error 138 means out of date price, if you were trading manually that probably would have been a requote. In other words you tried to close the order on a price that is no longer the current price. I would imagine many traders execute orders at the ticks of previous bar close and new bar beginning and that may cause prices to change very rapidly at the time of the open price so it may just be slippage causeing the error.

 

Here we go with some code... It's never close a trade... And I did not forget to call CheckForClose on the main.

Thanks

void CheckForClose()
   {
    
   
   for(int cnt=0; cnt < OrdersTotal(); cnt++)
         {

         
         if(OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES)==true) 
            {  int i=0;
                              
               if(OrderType()==OP_BUY &&  OrderSymbol()==Symbol() && Time[0]>OrderOpenTime() ) //  check for opened BUY position // check for symbol // check for the stop Loss condition
               { Print(" Close Buy ok");
                  OrderClose(OrderTicket(),
                           Lots,
                           Ask,
                           3,
                           Red);  
               } 
               
               if(OrderType()==OP_SELL &&  OrderSymbol()==Symbol() && Time[0]>OrderOpenTime() )
               {  Print(" Close Sell ok");
                  OrderClose(OrderTicket(),
                           Lots,
                           Bid,
                           3,
                           Red);
               }
            }
         }
}
 

Errors in Journal? Try for GetLastError() .. see help

More good to count down the orders because otherwise your loop try to access maybe closed orders f.e. for(int cnt=OrdersTotal()-1; cnt>=0; cnt--)

You see the Print Statement from time to time?

Try OrderClosePrice() instead of Ask or Bid, at least in my case is working.

Maybe Slippage of 3 is to small for your broker. 5 Digits brokers need to multiply by 10, in your case Slippage would be 30

If all does not help, you call CheckForClose in a wrong way :-)

 

wrong

if(OrderType()==OP_BUY ......)

OrderClose(OrderTicket(), Lots, Ask, 3, Red);

if(OrderType()==OP_SELL ......)

OrderClose(OrderTicket(), Lots, Bid, 3, Red);

supposed to be reversed

if(OrderType()==OP_BUY ......)

OrderClose(OrderTicket(), Lots, Bid, 3, Red);

if(OrderType()==OP_SELL ......)

OrderClose(OrderTicket(), Lots, Ask, 3, Red);

& BTW u don't need to use Ask & Bid just OrderClosePrice()

 

Thanks EADeveloper and gjol.

gjol, it worked.

 
rodrigosm:

gjol, it worked.


i assume
Reason: