Help me close order at the bar's end!

 

I need some help to formulate a code which will close the order exactly at the bar's end.It is for a special scalper EA, so it needs pinpoint precision.Let me illustrate what i mean:



Here you can see for example an M15 EUR/USD chart.Now imagine if i had any indicator that took me into a long trade right at the open of the candle marked X, so it went up then a bearish candle followed, with a low very low, but that doesnt matter, what matters it that based on my calculations, the optimal exit here would be the close point of the bar marked 1.The tricky part is that i dont know how to close the order at the close price of bar nr 1, and here i need help!

The best solution that i could think of for now is this (in case of this imaginary BUY trade):


/////////////////OrderSelect() and other stuff

if( OrderType() == OP_BUY ){
    
if( /* blablabla condition && */ Time[0]>OrderOpenTime()  ){

OrderClose( OrderTicket(), OrderLots(),OrderClosePrice() ,TAKEPROFITPIPS,CLR_NONE);             
RefreshRates();    

}}

This code closes the buy trade exactly at the Open[0], but not the Close[1].You see, because if you look on the picture and check the bar marked 2, you can see that the bar 2 did not opened at that price as the previous one closed it opened at a slightly different price.And sometimes a huge gap happens like here:



So experience tells that:

Close[1] != Open[0]

In very rare situations it is.So this is why i need the order to be closed right at Close[1].

The one feasable solution i though is that by counting the time, for example this is M1 and M15 chart, count the number of seconds either 60 for M1 or 900 for M15 in either case and close the order at seconds 59 or 599.The problems with this is that ticks are 6 seconds i guess (correct me if i`m wrong) and you cant precisely hit that because because of this time problem and additional slippage problem you might close the trade way after the bar closes.

So let's recap my question is this possible to count the time just like i described above, if yes then how to evade the slippage and tick lagging problem?

Or if you come up with other solutions how to close the order right at Close[1] and not Open[0] then i`m open to it.Please help me with this, this is a very important problem of my EA!

 
Proximus:

I need some help to formulate a code which will close the order exactly at the bar's end.

there isn't a situation you can tell last tick this moment is one tick before last tick of current bar
 

Modulus, check to see when there is no remainder. good luck

if( !MathMod( TimeCurrent(), PERIOD_M5 * 60 ) ) 

      OrderClose( OrderTicket(), OrderLots(), Bid , slippage );
 
trevone:

Modulus, check to see when there is no remainder. good luck

That won't work . . . it will either be before the end of the bar or after it . . . never at the end of the bar. As deVries said, it's not possible to detect the end of the current bar, but we can detect the start of the next one . . .
 
RaptorUK:
That won't work . . . it will either be before the end of the bar or after it . . . never at the end of the bar. As deVries said, it's not possible to detect the end of the current bar, but we can detect the start of the next one . . .


Before it is not that bad, just after it should not be.If it could be everytime before the close, but as close as possible to the close, then i would accept that solution too, but if its only 1 time after the close then it's no better than the one i thought of and therefore not acceptable.But why exactly does it close after the current tick?


And what about the other solution, that taking the 1 second before the close of the bar?

Like this in the case of M1

if( Time[0]>=OrderOpenTime()+59  ) 

      OrderClose( OrderTicket(), OrderLots(), Bid , slippage );

In this case how to resolve the tick problem if there is any?

 
Proximus:

Before it is not that bad, just after it should not be.If it could be everytime before the close, but as close as possible to the close, then i would accept that solution too, but if its only 1 time after the close then it's no better than the one i thought of and therefore not acceptable.But why exactly does it close after the current tick?


And what about the other solution, that taking the 1 second before the close of the bar?

Like this in the case of M1

In this case how to resolve the tick problem if there is any?




if not then this makes you wait a whole minute more and also in that case if there is any....
 
Proximus:

Before it is not that bad, just after it should not be.If it could be everytime before the close, but as close as possible to the close, then i would accept that solution too, but if its only 1 time after the close then it's no better than the one i thought of and therefore not acceptable.But why exactly does it close after the current tick?

Things only happen in an EA ( and Indicator ) when there is a tick, you have no way of knowing when the last tick of a bar is until you have the first tick on the next bar . . . even if the tick is at 59 mins and 59 seconds past the hour for a H1 bar, there could still be another tick during that second . . .

Proximus:

And what about the other solution, that taking the 1 second before the close of the bar?

Like this in the case of M1

In this case how to resolve the tick problem if there is any?

. . . and you can't wait for the last second of the bar because there may be no ticks for the last 5 seconds of the bar so in that case you will never "see" the last second of the bar.

 
RaptorUK:

Things only happen in an EA ( and Indicator ) when there is a tick, you have no way of knowing when the last tick of a bar is until you have the first tick on the next bar . . . even if the tick is at 59 mins and 59 seconds past the hour for a H1 bar, there could still be another tick during that second . . .

. . . and you can't wait for the last second of the bar because there may be no ticks for the last 5 seconds of the bar so in that case you will never "see" the last second of the bar.

So i should understand that if the interbank exchange rates doesn't change in a given period of time, the broker doesn't calls down the new price data?

I thought that a tick in MT4 meant that it always refreshes the price after X periods of seconds no matter what the price or volume is.So lets say the refresh rate would be 3 seconds then you'd know that on the M1 you hade to close that trade right at 57 seconds or before.


Given this situation, couldn't i use the:

RefreshRates();   

To call down the last tick at 58 seconds and exit at 59?

 
Proximus:

So i should understand that if the interbank exchange rates doesn't change in a given period of time, the broker doesn't calls down the new price data?

I thought that a tick in MT4 meant that it always refreshes the price after X periods of seconds no matter what the price or volume is.So lets say the refresh rate would be 3 seconds then you'd know that on the M1 you hade to close that trade right at 57 seconds or before.

If the Broker's prices don't change, Bid and Ask, there is no new tick . . . around Midnight GMT on some of the lesser traded pairs there can be no ticks for a minute or more . . . a new tick is not generated after a preset time, it happens when there is a change in price, Bid or Ask ( and maybe in some other circumstances, change in other symbol parameters ) . . .

Proximus:

Given this situation, couldn't i use the:

To call down the last tick at 58 seconds and exit at 59?

How will RefreshRates() get called if there is no tick ?
 

Thanks for clarification of how the ticks work in MT4.

RaptorUK:

If the Broker's prices don't change, Bid and Ask, there is no new tick . . . around Midnight GMT on some of the lesser traded pairs there can be no ticks for a minute or more . . . a new tick is not generated after a preset time, it happens when there is a change in price, Bid or Ask ( and maybe in some other circumstances, change in other symbol parameters ) . . .

How will RefreshRates() get called if there is no tick ?

Well i thought the RefreshRates() will force the broker to call down a new set of feed from his liquidity provider, but i guess it is not.


Anyway, if can anyone find a better solution to my problem than this, then please share it with me, its very important!Thanks ahead!

/////////////////OrderSelect() and other stuff

if( OrderType() == OP_BUY ){
    
if( /* blablabla condition && */ Time[0]>OrderOpenTime()  ){

OrderClose( OrderTicket(), OrderLots(),OrderClosePrice() ,TAKEPROFITPIPS,CLR_NONE);             
RefreshRates();    

}}
 
Proximus:

Thanks for clarification of how the ticks work in MT4.

RaptorUK:

If the Broker's prices don't change, Bid and Ask, there is no new tick . . . around Midnight GMT on some of the lesser traded pairs there can be no ticks for a minute or more . . . a new tick is not generated after a preset time, it happens when there is a change in price, Bid or Ask ( and maybe in some other circumstances, change in other symbol parameters ) . . .

How will RefreshRates() get called if there is no tick ?

Well i thought the RefreshRates() will force the broker to call down a new set of feed from his liquidity provider, but i guess it is not.


Anyway, if can anyone find a better solution to my problem than this, then please share it with me, its very important!Thanks ahead!


The very best you can do is use the next tick after the last tick of the Bar, in other words the first tick of the next Bar.
Reason: