Download MetaTrader 5

Help me close order at the bar's end! - page 2

To add comments, please log in or register
Proximus
767
Proximus  

Hmm so this would be good?

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

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

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

}}

Actually i changed Time[1] to Time[0] because Time[1] was skipping 1 more bar, so Time[0] is actually representing the Open[0].If anyone knows anything better/smoother than this please tell!

Tjipke de Vries
6433
Tjipke de Vries  
Proximus:

Hmm so this would be good?

Actually i changed Time[1] to Time[0] because Time[1] was skipping 1 more bar, so Time[0] is actually representing the Open[0].If anyone knows anything better/smoother than this please tell!

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

and check also if orderclose succeed

Proximus
767
Proximus  
deVries:

and check also if orderclose succeed

Well if not succeded then when the start() function repeats itself it will test again that if() before the orderclose() and since the conditions will still be true to proceed, it wil try to close it again.I cant try to close it 2 times in the same loop of the start(),must wait another tick no?
Simon Gniadkowski
Moderator
18056
Simon Gniadkowski  
Proximus:
Well if not succeded then when the start() function repeats itself it will test again that if() before the orderclose() and since the conditions will still be true to proceed, it wil try to close it again.I cant try to close it 2 times in the same loop of the start(),must wait another tick no?
Don't you want to know that it failed ? and if it failed why it failed ? and what the value of the relevant variables were at the time that could have made it fail ? if you know all this you can fix it so it doesn't fail next time . . .
Simon Gniadkowski
Moderator
18056
Simon Gniadkowski  
Proximus:

Hmm so this would be good?

Actually i changed Time[1] to Time[0] because Time[1] was skipping 1 more bar, so Time[0] is actually representing the Open[0].If anyone knows anything better/smoother than this please tell!

No, this will always close regardless of the tick being the tick of the next bar or not, for any bar following the bar when the trade was opened and any time during that bar Time[0] will always be greater than OrderOpenTime().

If you want to close as close to the close of the bar then you have to determine if the current tick is the first tick of the new bar . . . . then you also have to make sure you close works because you will only get one chance par bar.
Tjipke de Vries
6433
Tjipke de Vries  
Proximus:
Well if not succeded then when the start() function repeats itself it will test again that if() before the orderclose() and since the conditions will still be true to proceed, it wil try to close it again.I cant try to close it 2 times in the same loop of the start(),must wait another tick no?


you started that it has to close last tick of bar

we tried you to understand not possible to know when last tick bar is coming

now it doesn't matter if closing fails we try again, again,again on following ticks

did you also miss other changes i did suggest

what sense does it make refreshrates after orderclose ??

and do you know why slippage instead of TAKEPROFITPIPS

it depends on the errorreturn you can try to close it 2 times in same loop

if tradecontext too busy and you have more trades inside the loop to close the n great possibillity they fail also

Proximus
767
Proximus  
RaptorUK:
No, this will always close regardless of the tick being the tick of the next bar or not, for any bar following the bar when the trade was opened and any time during that bar Time[0] will always be greater than OrderOpenTime().

If you want to close as close to the close of the bar then you have to determine if the current tick is the first tick of the new bar . . . . then you also have to make sure you close works because you will only get one chance par bar.

Isnt the start() repeat itself after each tick? Am I wrong?

Anyway imagine like this:

OrderOpenTime() = 12:12:01 (12 H 12 MIN 1 SEC)

The order gets opened,the Orderclose() function packet is after my OrderSend() one in the main code so, right after opening the order my OrderClose() packet will test that the conditions did met to close the order or not.Let's say it did now its time to close the order:

Time[0] is the open price of this bar, so by common logic it must be smaller than the time the order was open, because the order cant be opened before the same bar opened.It's: 12:12:00

So by all means :

if(Time[0]>OrderOpenTime())

Will return true value so the orderclose will be triggered.If it cant close it, for any reason, like slippage or so, no problem, the start() repeats itself

And by the next repeat the Time[0] will 100% be bigger than the open time of the order,because its logical, so if the first close fails the other OrderClose() will happen every time afterwards when the start() will repeat itself, and since i think that start() repeats itself by every tick, then i dont think there is a better way to close the order quicker, i hope i explained it clearly :)

Proximus
767
Proximus  
deVries:


you started that it has to close last tick of bar

we tried you to understand not possible to know when last tick bar is coming

now it doesn't matter if closing fails we try again, again,again on following ticks

did you also miss other changes i did suggest

what sense does it make refreshrates after orderclose ??

and do you know why slippage instead of TAKEPROFITPIPS

it depends on the errorreturn you can try to close it 2 times in same loop

if tradecontext too busy and you have more trades inside the loop to close the n great possibillity they fail also

I use TAKEPROFITPIPS because that is my variable for TAKEPROFIT, and since i allow maximum slippage as much as my TP level, because its logical that you should not have more slippage than your TP if you close orders with OrderClose(), because if you do then the TAKEPROFIT will close it instead of you more efficiently than the OrderClose().
Simon Gniadkowski
Moderator
18056
Simon Gniadkowski  
Proximus:

Isnt the start() repeat itself after each tick? Am I wrong?

Yes, start() is called for each tick unless it is still executing . . .

Proximus:

Anyway imagine like this:

OrderOpenTime() = 12:12:01 (12 H 12 MIN 1 SEC)

The order gets opened,the Orderclose() function packet is after my OrderSend() one in the main code so, right after opening the order my OrderClose() packet will test that the conditions did met to close the order or not.Let's say it did now its time to close the order:

Time[0] is the open price of this bar, so by common logic it must be smaller than the time the order was open, because the order cant be opened before the same bar opened.It's: 12:12:00

So by all means :

Will return true value so the orderclose will be triggered.If it cant close it, for any reason, like slippage or so, no problem, the start() repeats itself

And by the next repeat the Time[0] will 100% be bigger than the open time of the order,because its logical, so if the first close fails the other OrderClose() will happen every time afterwards when the start() will repeat itself, and since i think that start() repeats itself by every tick, then i dont think there is a better way to close the order quicker, i hope i explained it clearly :)

Yes, but that isn't what you asked for in your first post in this thread . . . now you are saying it's OK for the close to be delayed by Period and then another Period, etc. If you want to Close as close to the bar finish then you have to do it at the first tick of the next bar, and you must check if the Close failed and retry in the correct manner so that you succeed in Closing it.
Ian Venner
2584
Ian Venner  

It is quite rare for the close price of a bar not to be repeated on the susequent bar, not neccessarily its open price ...

To add comments, please log in or register