Translation please?

[Deleted]  

Hi, after several days of trying to understand what it does I'll ask you guys here what the below mentioned means in human language.

int BarCount;

BarCount = Bars;

if (Bars != BarCount) //etc.

the mql4 reference says Bars stands for the number of bars in the current chart. The EA I'm working on has this Bars check in the start section and is the condition of basically every OrderSend, OrderModify and OrderClose and what not. At first I thought it is checking whether there are enough bars for conditions with bar shifts to be valid. Needless to say that this is nonsense.

WHRoeder kept reminding me that Bars is unreliable and that I should use Time instead. I did that but I want to understand why in order to use it correctly. What does this Bars != Barcount do anyway? Since Barcount = Bars it's basically saying if (Bars != Bars). In human language that means "if the number of bars in a chart does not equal the number of bars in a chart". But when does the number of bars in a chart not equal the number of bars in a chart? I mean the number of bars in a chart always equals the number of bars in a chart. There's no point in checking that.

The only noteworthy thing is that it always seems to be linked with Tickchecks and EachTickMode.

extern bool EachTickMode = True;

bool TickCheck = False;  // before init section

if (EachTickMode && Bars != BarCount) TickCheck = False; // beginning of the main iteration section

if ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount))) {OrderClose //and so on and so forth
if (!EachTickMode) BarCount = Bars; //wth?

if ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount))) {OrderSend //and so on and so forth
if (EachTickMode) TickCheck = True;  //why? What's the point?
if (!EachTickMode) BarCount = Bars;  //???

if (!EachTickMode) BarCount = Bars;  // again?? at the end of the main interation section just before the last return{0}

As I mentioned above WHRoeder suggested to use the following

datetime Time0;
...
if (Time[0] != Time0){ Time0=Time[0]; ...

instead of Bars!=Barcount so I replaced every mention of Bars!=Barcount in the code with Time[0]!=Time0 but I wonder whether this is the correct use of it. Doesn't the array number have to change like Time[1] or something like that?

Explanations in human language are very appreciated.

William Roeder  

Initially Bars will be 100 (in the tester.) On the start of a new bar it will change to 101. if (barCount != Bars) will compare 100 != 101 the first new tick. At the end it sets barCount=Bars so subsequent ticks will be comparing 101 != 101 false not a new bar.

The problem is once you reach Max Bars in Chart the count will no longer change and the code fails.

Using time Once a new bar starts what WAS Time[0] is now Time[1] and Time[0] has incremented by the chart period*60. By comparing Time0 to Time[0] they compare not equal the first new tick.

The Tickchecks and EachTickMode is all part of the code generator you used. I have no idea what YOU had in mind when YOU created them.

[Deleted]  

Hmm....

I understood why the Barcheck code will fail but I do not understand what it is there for / why it is used in the first place. What is it supposed to do / prevent the EA from doing? (Like I don't know, prevent the EA from sending Order commands when Internet connection is down or what?)

Consequently is that check (be it Bars or Time) or the Tickcheck-thing even necessary?

Simon Gniadkowski  
Yojimbo:

Hmm....

I understood why the Barcheck code will fail but I do not understand what it is there for / why it is used in the first place.

It's usually there so that the code is only executed once each time a new bar has completed . . . rather than for each tick.
[Deleted]  
RaptorUK:
It's usually there so that the code is only executed once each time a new bar has completed . . . rather than for each tick.

Thank you very much. Now I get it. I had a hunch as it should be self-explanatory from the code but I was confused and wanted to be sure. Thx.