Download MetaTrader 5

Bar close rules

To add comments, please log in or register
Follow the news. View our company's official channel!
Dominic Gilbert
1639
Dominic Gilbert 2014.08.06 21:31 

Is it better to use iClose() or Close[1] when comparing two in an "if" statement?

 I have an open position I want to manage through comparing of H1 bars. I don't seem to be getting it right.

Hypothetically, what is the best way of comparing bar [1] with [2], [3] and [4] (and so forth).

 I.e. creating test rules like (see below). Obviously this will never work and the first if statement will never pass as true. So I am wanting to understand how it is you compare two bars and then move onto the next 2 (if that makes any sense!). 

 

if( Open[0] >= Close[1] || Close[1] >= Open[0] )continue;
if( Close[1] > Close[2] )
{
OrderClose();
}                 
GreenMoney
379
GreenMoney 2014.08.06 22:20  

Is it better to use iClose() or Close[1] when comparing two in an "if" statement? 

If you only are working with price data on the current chart, Close[] is probably faster than using a function call.  

 

Hypothetically, what is the best way of comparing bar [1] with [2], [3] and [4] (and so forth). 

You first have to determine how or in what way you want [1], [2], [3], [4], etc, to relate to each other. Thereafter you can figure out the best logic and most appropriate way to code it.

 

I.e. creating test rules like (see below). Obviously this will never work and the first if statement will never pass as true. So I am wanting to understand how it is you compare two bars and then move onto the next 2 (if that makes any sense!). 

if( Open[0] >= Close[1] || Close[1] >= Open[0] )continue;
if( Close[1] > Close[2] )
{
OrderClose();
} 

First, I believe the first IF statement should be true most (or all) of the time.  True || False = True; False || True = True; and True || True = True.  However, False || False = False.  But I don't see where "Open[0] >= Close[1]" and "Close[1] >= Open[0]" could both be false at the same time.  Second, why use the Continue operator in this context?

Dominic Gilbert
1639
Dominic Gilbert 2014.08.06 22:33  

lol, yeah I meant to say true all the time! It was just an example. Basically check out this screenshot here (as I am working with Close[0] at the moment to no avail).

 This screenshot might explain a little easier: http://screencast.com/t/BowCtzFWxr

Tbh, I am getting so confused myself today, I'm finding it hard to even explain what I am after lol. Been looking at code for too long today. No doubt the answer will be staring right at me!! 

Simon Gniadkowski
Moderator
18001
Simon Gniadkowski 2014.08.07 10:36  
Thirteen:

If you only are working with price data on the current chart, Close[] is probably faster than using a function call.  

Unless the predefined variable, Close[] is out of date.  It can be in the same way that Bid and Ask predefined variables can be . . .  it's something to be aware of.
Dominic Gilbert
1639
Dominic Gilbert 2014.08.07 13:50  
Yea I hear you RaptorUK. What are your thought on why I am trying to achieve? Do you think Close[1] is the best? Could this be done (check screencast screenshot) using a for loop?
Simon Gniadkowski
Moderator
18001
Simon Gniadkowski 2014.08.07 16:39  
DomGilberto:
Yea I hear you RaptorUK. What are your thought on why I am trying to achieve? Do you think Close[1] is the best? Could this be done (check screencast screenshot) using a for loop?


Close[0] is Bid and is obviously updated on each tick,  so if you are simply checking each tick there should be no issue with using Close[0],  Close[n] is static so does not change ever . . .  but if you are talking about real closes you can't consider bar 0 as it is only closed when it has become bar 1.  So do a new bar check and when you have a new bar then test bar1 close vs bar2 close . . .

 

Is this what you needed/want to do ?

GreenMoney
379
GreenMoney 2014.08.07 17:11  
RaptorUK:
Unless the predefined variable, Close[] is out of date.  It can be in the same way that Bid and Ask predefined variables can be . . .  it's something to be aware of.


That's a good point.  Nevertheless, I believe (please correct me if I'm wrong) the out-of-date issue mostly affects access to Close[0], since Close[0] == Bid.  There also is a scenario where a incoming tick, while ignored by an EA because it's onTick() is running, would create a new bar.  At that point, (I assume) the EA's local copy of history data would be out of sync with the terminal's history data until the EA's local copy gets updated.  In either of these situations, RefreshRates() should be called to update the EA's local copy of history data.

While I don't look at execution speed very often (especially given today's processor speeds), if I'm going to access the current and last five bar closes and compare them, I do see a possible execution advantage when using Close[], rather than iClose(), even if I have to call RefreshRates(): one function call plus six array accesses versus six function calls.  But I doubt the OP had that in mind when asking whether it's better to use Close[] or iClose(). :)

Simon Gniadkowski
Moderator
18001
Simon Gniadkowski 2014.08.07 22:03  
Thirteen:


That's a good point.  Nevertheless, I believe (please correct me if I'm wrong) the out-of-date issue mostly affects access to Close[0], since Close[0] == Bid.  

SNIP

Yep,  I agree with everything you wrote :-)
Dominic Gilbert
1639
Dominic Gilbert 2014.08.10 17:47  

Right, thanks for both your replies :)

 Thirteen, you're definitely right. I did not consider the differences you've gone into when comparing Close[] and iClose[]. Interesting what you've said though! :)

 I'm not sure how well I am explaining this, but I guess in a nutshell I am finding it hard to get the if statements right. 

http://screencast.com/t/qrSijOwDEStJ < check this screenshot.

I am finding this hard to write in an if statement? 

 


 

To add comments, please log in or register