Download MetaTrader 5

How to get an EA to calculate an indicator only when the bar is closed

To add comments, please log in or register
Anthony
36
Anthony  

Hi Guys

I'm having a slight problem with getting the backtester and even live trading to get an indicator calculated only after a bar is closed. Say we're looking at an MA or MACD cross system, like the EA's provided with metatrader, with each tick of a bar, the MACD or MA could go from crossing or equaling each other and not, but all i really want to know is what the values are when the bar is closed, ie, the final value, the one which is printed on the chart. I've managed to find a NewBar() function that works out if it's the first tick of a bar but not 100% sure how it works, so i can't modify it only do something on the last tick or close of bar. Does anyone know how to edit this function or of another way to ensure that i'm only calculating indicators when they've been finalised??

The code attached is the MACD sample, just with the NewBar() function added, so it will only make 1 trade if the condition is met till the MACD crosses the opposite way, but also takes the value calculated on the first tick of the bar, which is the problem i'm speaking of. Unfortunately it's in a text file as MetaTrader won't put the mq4 or ex4 files in the correct directory.

Any help would be greatly appreciated,

Anthony

Files:
brewmanz
486
brewmanz  

(not looked at code but ...)

The trick I use to only calc on FINAL Close values ...

.. is to work then out on Open of NEXT bar - only 1 tick late ;-)

Anthony
36
Anthony  
Thanks for that, but how do i know or tell the code only to calculate then, ie, all the indicators are based on closed prices, but that doesn't seem to make a difference, still calculating every tick
Chad Howarth
80
Chad Howarth  
I do something like this for any code that should only operate on closed bars:
datetime Now = 0;
int start()
{
   if( Now != Time( 0 ))
   {
      Now = Time( 0 );

   ...query indicators here, at bar 1 instead of 0
   }
}
brewmanz
486
brewmanz  

I think that it should be

static datetime Now = 0;

Chad Howarth
80
Chad Howarth  

Why the use of static, if I may ask?

In my own code I declare the variable as a global, and initialize it to 0 in the init() function.

alexander supertrade
290
alexander supertrade  
The problem is that you never know which tick will be the last of te bar (though I suppose since you know the timeframe you're using you can time how long a bar has been open &anticipate a given tick as the last, but thats a bit ad-hoc). As brewmanz said, best to just do it at the open of a new bar. the datetime needs to be static because you need to save the value from the last time the program ran. If you don't designate it as static it will be re-initialized to zero (or whatever you initialize it to) every run and your if-statement will always be true.
brewmanz
486
brewmanz  
Anomalous:

Why the use of static, if I may ask?

In my own code I declare the variable as a global, and initialize it to 0 in the init() function.

I apologise - I should have looked more closely!

Yes, your global declaration is correct.

If declared *inside* start() then static is needed - and your way doesn't

Chad Howarth
80
Chad Howarth  

Brewmanz, thanks for keeping me on my toes. I've only studied MQL4 for a few weeks now, and it has been 15 years since my last computer science class. Not hard to get me to question myself!

P.S. Is brewmanz kind of like humanzee? Are you claiming to be some hybrid of man and beer? ;)

Straightjakket
5
Straightjakket  

Hi There, i am having the same issue with an inside bar indicator - i get a notification every hour.

any help would be much appreciated.

Files:
To add comments, please log in or register