Download MetaTrader 5

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

To add comments, please log in or register
Proximus
760
Proximus  
RaptorUK:

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

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.

I dont have other solutions.If i'd have then i would do those.


SDC:

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

Yea, sometimes a bar opens below or above the previous candle's close and sometimes right exactly at that level.But sometimes there are these gaps that ruin everything, thats why i would have liked a better solution, to avoid those, but now i see that its not possible.
Proximus
760
Proximus  

Problem, big problem:

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

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

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

}}

So i used this code to close that nasty trade at the closest of the end of the candle, but the problem is that in backtesting it works like magic, but when i tried it on a live demo account, it was very silly.It started to close the positions right after it opened them.I dont understand it why, since Time[0] should be the open time of the current bar, and how can that be bigger than the current time when the trade was put after the open of the candle? It's just nonsense.

But still it is that, so to rule out any error i used only:

if( Time[0]>OrderOpenTime()  )

This in the if, to rule out other interference, but yes, the problem is with this test:

Time[0]>OrderOpenTime() 

How can i bypass it, or can some1 please find another equivalent testing to this that works on live account too,please help!

Simon Gniadkowski
Moderator
18024
Simon Gniadkowski  
Proximus:

Problem, big problem:

So i used this code to close that nasty trade at the closest of the end of the candle, but the problem is that in backtesting it works like magic, but when i tried it on a live demo account, it was very silly.It started to close the positions right after it opened them.I dont understand it why, since Time[0] should be the open time of the current bar, and how can that be bigger than the current time when the trade was put after the open of the candle? It's just nonsense.

It has happened, so find the reason . . . for example, if your OrderSelect() fails ( do you check if it worked or failed ? ) then OrderType() will return the wrong value or the right one just by chance, OrederOpenTime() will return the wrong value . . . perhaps zero and zero is less than Time[0] . . . did you print the value for OrderOpenTime() when trying to figure out what was going wrong ? why not ?

There are good reasons why I keep posting this link . . . What are Function return values ? How do I use them ? why do you continually ignore good advice ?

Ian Venner
2584
Ian Venner  
Proximus:

I dont have other solutions.If i'd have then i would do those.

Yea, sometimes a bar opens below or above the previous candle's close and sometimes right exactly at that level.But sometimes there are these gaps that ruin everything, thats why i would have liked a better solution, to avoid those, but now i see that its not possible.

Does it have to be exactly the close price ? How about trying something like ... time the difference between the arrival of ticks during the formation of the bar .. if they are arriving fast like 1 per second take the price if it arrives less than 5 seconds from the close time of the bar, if the ticks are ariving slow like 1 per minute take the price if a tick arrives in the last 30 seconds of the bar, if no tick arrives during that time take the open price of the subsequent bar.

Proximus
760
Proximus  
SDC:

Does it have to be exactly the close price ? How about trying something like ... time the difference between the arrival of ticks during the formation of the bar .. if they are arriving fast like 1 per second take the price if it arrives less than 5 seconds from the close time of the bar, if the ticks are ariving slow like 1 per minute take the price if a tick arrives in the last 30 seconds of the bar, if no tick arrives during that time take the open price of the subsequent bar.


It doesnt have to be exactly the Close[1] since in the previous posts we ruled out that that is impossible to exactly hit that level.But the second best / nearest thing to that will do it also.Can you please write your idea in a code, to see what is it.
Mehmet Bastem
242
Mehmet Bastem  

Volume[0]==1 ;// new bar = closed last bar.

or.

bool NewBar()

{

static datetime lastbar;

datetime curbar = Time[0];

if(lastbar!=curbar)

{

lastbar=curbar;

return (true);

}

else

{

return(false);

}

}


if (Signal=="BUY" && NewBar())

Proximus
760
Proximus  
Ty for help i resolved the problem, it was actually that i`ve had many charts open at once but and all of them had the same magic nr, now they have randomly generated ones so its ok.
jonjonfx
10
jonjonfx  

Hi,

Is there a way to close open position 5min before the bar ends on H4 Time frame? thanks

Boeing747
241
Boeing747  
jonjon:

Hi,

Is there a way to close open position 5min before the bar ends on H4 Time frame? thanks



 yes, there is
whroeder1
15576
whroeder1  
Mehmet: Volume[0]==1 ;// new bar = closed last bar.
  1. If you miss the very first tick of a bar, your code fails and misses the entire bar. ALWAYS use time.
  2. Don't paste code
    Play video
    Please edit your post.
    For large amounts of code, attach it.
123
To add comments, please log in or register