Obviously, the first one is the quickest as it is working with an local variable. Doing multiple calls to the iCustom function (which in turn needs needs to access the indicator code) on every line, is much more costly.
But if you want it even faster, only update the MACD variable with iCustom call, once per bar and not on every tick.
Hmm ok. That is how i view things too - it's just that i had heard (or read) somewhere that is didn't make a difference. Note this is not iCustom, but iMACD, which is native to MQL4 terminal. Would that make a difference?
as for doing it once per bar: how would you do that? i have OnTick() and OnTimer(), not OnBarOpen() ... which make me thirsty now that i think of that ;-)
iCustom or iMACD — they are still function calls with underlying functionality that needs to be processed on every call. A local variable will always be faster.
Below is an example of detecting a new bar. Sample untested, uncompiled code to serve as an example of possible implementation in an Expert Advisor.
// Tick Event Handler void OnTick(void) { // Detect new bar condition static datetime dtBarCurrent = WRONG_VALUE; datetime dtBarPrevious = dtBarCurrent; dtBarCurrent = (datetime) SeriesInfoInteger( _Symbol, _Period, SERIES_LASTBAR_DATE ); bool bBarNew = ( dtBarCurrent != dtBarPrevious ); // Execute only on a new bar condition if( bBarNew ) { // Do something here }; };
iCustom or iMACD — they are still function calls with underlying functionality that needs to be processed on every call. A local variable will always be faster.
Below is an example of detecting a new bar. Sample untested, uncompiled code to serve as an example of possible implementation in an Expert Advisor.
Ah i see, thanks for that Fernando - this code does make it chart (ie timeframe) dependent, not ideal for me. I would probably go for something like:
OnTick() { static int hr_prev; int hr=TimeHour(TimeLocal()); if(hr>hr_prev) { //do something } else { hr_prev=hr; } }
this would allow for doing something every day, hour, minute etc.
this would allow for doing something every day, hour, minute etc.
Your code has a few logic bugs in my opinion! Have a look at the following and see if you can understand why that is.
OnTick() { static int hr_prev = WRONG_VALUE; int hr = TimeHour( TimeCurrent() ); if( hr != hr_prev ) { hr_prev = hr; //do something }; };
Your code has a few logic bugs in my opinion! Have a look at the following and see if you can understand why that is.
Yeah the ">" is an issue for when the day moves into the next (24 --> 1)
and while i'm not familiar with
WRONG_VALUE
other than that i know it equals '-1', i do like your
if(hr!=hr_prev)
as that saves having to do an 'else' statement.
Unless you want to prevent the code to run when market is closed, i'm not sure why TimeCurrent() needs to replace TimeLocal().
While i'm not familiar with
other than that i know it equals '-1', i do like your
as that saves having to do an 'else' statement.
Unless you want to prevent the code to run when market is closed, i'm not sure why TimeCurrent() needs to replace TimeLocal().
Then again, both work i think ?
- You should always initialise the variable to some value that you know will never be of a valid content. Since hours are between 0 and 23, then -1 is an obvious choice and using "Wrong Value" is self documented. If you don't initialise the variable it could potentially have "valid" content that could cause logic problems.
- " != " instead of ">" because when the hour goes beyond 23 and back to 0, then your condition will no longer be valid and no longer triggered.
- The OnTick event handler, is only called when a new tick arrives. It will never be called if the market is closed. However, local time is on a different zone with different daylight savings time changes at different dates or the user could simply make corrections to the local time and then your code would fail. Stick to the trade server time, as that is your data reference, not your local time.
Thanks - fully agree on 1. and 2.
As for LocalTime()... i use it a lot w/o incident or hickup. i find it easier to execute trades at market open when using my 'own' time. I guess with Server time, combined with GMT and Daylight Saving corrections the same can be achieved..
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Quick question: What takes less milliseconds to process?
or
I'm currently assuming the first one processes quicker, but since a variable needs to accessed for every Print() line in both cases (either the macd_signal or the iMACD(bla, bla) variable) it may make no difference at all...
can anyone put this to bed for me once and for all? ;-)