Download MetaTrader 5
To add comments, please log in or register
Sell your applications via the Market. It is secure!
MTcrafter
57
MTcrafter 2014.10.13 21:58 

Hi,  

I'm asking for comment or conceptual comment on the changes in Mt4 (600+) regarding the OnTick() versus Start() command, reaching beyond the limited help file details I've found.  Not expecting specific answers below in every case, but to try to explain the confusion I'm finding if a couple of paragraphs in reply might help clarify things.  Not meaning to rant. :(

Despite having programmed in Mt4 for several years, my experience came from the more forgiving Pascal language and the older pre-600 Mt4 compiler.  I have not tangled with Mt5 previously.  The update to Mt4 600+ has been frustrating.  Many of the help files remain brief and frequently make assumptions to lean examples or no examples at all in some cases.

Is it best to sustain the Start() function in EA's consistent with prior Mt4 design or is the OnTick() function now preferred, if even just in lieu of pending end of life cycle from Mt4 to Mt5?

Is OnTick() an option to be used inside the Start() event for critical process?  It's clear the return functions are different and it appears OnTick() tries to remain on the near stack rather than heap.  Several things might benefit there in performance, I'm sure.

I don't seem to be understanding OnTick() fully as a "replacement" for Start() or in addition to, but that I'm having to declare most variables static under OnTick() if not global to preserver their statefulness.  Increasing exposure to globals wouldn't seem to make sense but the help files I've seen never do say explicitly "do or don't" use OnTick() in a particular manner for EAs, that I've found.  It seems by errors emitted, as if OnTick() is reinforcing rigid persistence to the stack, which makes sense, but in an event driven system it's hard at times to picture the degree of stability on a variable in local scope of the start() function competing for next event.  In the past, I was not bound to using "static" for the errors which now arise under OnTick() and yet variables not declared static seem to lose persistence at end of scan, regardless of next tick arrival.  Or I'm imagining it from runtime assumptions incorrectly.  At least in the tester, static declarations appear under OnTick() to even survive a restart, which really surprised me to see on screen Comment()s show up preserved on restarts.

I've always held to the concept to complete a scan before the next tick by efficiency (if possible), but in conditions like Sleep() events, what happens to the state of the current scan / event with Sleep() delay under OnTick()?  Does the current event survive and delay letting ticks pass by or end up wiped and cycle next event?  I typically have applied a small sleep() period when making calls to the server in the past just as a standard of external events, despite stateful return messaging.  I'm guilty of using RefreshRates() sparingly as needed versus MarketInfo() but even that comes to question if statefulness is being sustained / destroyed per event.

I seem to just be missing something conceptual about OnTick() and possibly reading too much into it for lack of a fuller understanding, (at the risk of being told to go find a relevant programming class) :|

I do understand the difference of OnTick() related to indicators or scripts versus EAs, but any literal clarity from the more experienced crowd regarding EAs with OnTick() would be appreciated.  I'm sure others could benefit as well from a bit more generalized concept than I've found so far at least.

Thanks much for anyone braving some free thinking replies on concept or specifics, greatly appreciated, (end rant).

 MTC 

whroeder1
13610
whroeder1 2014.10.14 13:00  
What's New in MQL4 - MQL4 Documentation
init(), deinit() and start() predefined functions have remained for compatibility, however, OnInit(), OnDeinit(), OnStart(), OnCalculate() and OnTick() ones can now be used instead.
MTcrafter
57
MTcrafter 2014.10.14 19:29  
WHRoeder:
What's New in MQL4 - MQL4 Documentation
init(), deinit() and start() predefined functions have remained for compatibility, however, OnInit(), OnDeinit(), OnStart(), OnCalculate() and OnTick() ones can now be used instead.

Thanks and Yep, that's the exact help file I referred to that doesn't really seem to say much about ...

conceptual comment on the changes in Mt4 (600+) regarding the OnTick() versus Start() command 

Not being sarcastic, I respect you're an accomplished and polished coder, but even with your greater experience, you don't elaborate. 

The OnTick help file says... "can now be used instead." (What is OnTick() used instead of) ?

The Tick event is generated only for Expert Advisors, but this does not mean that Expert Advisors required the OnTick() function, since not only NewTick events are generated for Expert Advisors, but also events of Timer, BookEvent and ChartEvent are generated. It must be declared as the void type, with no parameters: 

So basically, if I just stick with Start(), declare locals static and continue to use RefreshRates() or MarketInfo() strategically, it's about the same thing?  I understood the Start() command was fired with the tick event already and that OnTick() let's any new ticks pass by just like Start() will, if the scan in latent in completion relative to next event.

Is my understanding wrong, or how then does the OnTick() event benefit code or performance, inside the Start() routine otherwise? 

What is the distinction, purpose or benefit of OnTick(), especially forced as void?  Am I missing some other help file that builds on the concepts?

Does it inform me inside Start() that yet another Tick has occurred while the Start() event was in process, (pseudo critical code section-ish)? 

Is it just a matter of equivalency or support for higher order high level code applications, OO, or otherwise?

I tried to emphasize I did the homework to look it up.  I just don't see any real information imparted beyond basic construct.

 

MTC 

UncleSteve
22
UncleSteve 2014.10.14 21:29  

I'd like help on this too, so commenting in the hope that the question gets bumped up the list. I'm afraid that compared to the body of knowledge out there on just about any other language, the documentation and tutorials on MQL4 are just about useless.

I also have found the various references mentioned, and I also have bumped my head on local vs. global variables, so if anyone can make these questions clear I think there will be a lot of pleased new programmers.. 

honest_knave
959
honest_knave 2014.10.14 23:14  

I'm not entirely sure what is being asked, but in a nutshell (and with some gross oversimplifications):

Just replace Start() with OnTick()... they are synonymous. Think of it as a terminology change.

Before Build 600+, you were effectively limited to running code once per tick. If a tick didn't come in, the code didn't run.

Now you can run code on a timer using OnTimer(), or when certain events happen like mouse clicks, pressing keys or moving objects using OnChartEvent().

A variable with local scope can only be accessed from within the procedure where it was declared. So if you declare "double myVar" in OnInit(), you can't access "myVar" in OnTick(). Variables with a local scope do not hold their values once the procedure has run, unless they are declared as static e.g. "static double myVar".

A variable with global scope can be accessed from anywhere within the EA. So if you declare "double myVar" at the top of your EA (outside of any procedures), you can access "myVar" in OnInit(), OnTick() or anywhere else you want. Variables with a global scope will hold their values between procedure calls.

A global variable is something different - it can be accessed between different EAs. They are stored for 4 weeks after their last use. 


whroeder1
13610
whroeder1 2014.10.15 12:09  
MTcrafter:

conceptual comment on the changes in Mt4 (600+) regarding the OnTick() versus Start() command 

The OnTick help file says... "can now be used instead." (What is OnTick() used instead of) ?

Is my understanding wrong, or how then does the OnTick() event benefit code or performance, inside the Start() routine otherwise? 

What is the distinction, purpose or benefit of OnTick(), especially forced as void?  Am I missing some other help file that builds on the concepts?

Does it inform me inside Start() that yet another Tick has occurred while the Start() event was in process, (pseudo critical code section-ish)? 

Is it just a matter of equivalency or support for higher order high level code applications, OO, or otherwise?

  1. There is none.
  2. Instead of EA's Start().
  3. Do one or do the other, there is no both.
  4. None. Mt4 always ignored the return value from Start().
  5. Neither "inform you" Both ignore subsequent ticks while active.
  6. Equivalent as implied by
    remained for compatibility
MTcrafter
57
MTcrafter 2014.10.15 15:10  
WHRoeder:
MTcrafter:

conceptual comment on the changes in Mt4 (600+) regarding the OnTick() versus Start() command 

The OnTick help file says... "can now be used instead." (What is OnTick() used instead of) ?

Is my understanding wrong, or how then does the OnTick() event benefit code or performance, inside the Start() routine otherwise? 

What is the distinction, purpose or benefit of OnTick(), especially forced as void?  Am I missing some other help file that builds on the concepts?

Does it inform me inside Start() that yet another Tick has occurred while the Start() event was in process, (pseudo critical code section-ish)? 

Is it just a matter of equivalency or support for higher order high level code applications, OO, or otherwise?

  1. There is none.
  2. Instead of EA's Start().
  3. Do one or do the other, there is no both.
  4. None. Mt4 always ignored the return value from Start().
  5. Neither "inform you" Both ignore subsequent ticks while active.
  6. Equivalent as implied by
    remained for compatibility

Ok, Thank you.   This confirms a better context to it, for those lacking experience in Mt5 as I am...

Possibly my question should have been, "WHY" is OnTick() used instead of Start(). 

I view your answer to mean the OnTick() handler basically encourages improved Mt5 code structure and performance options in as much as it's requirements on variables, stack, heap, etc. which starts to make some sense in evolution of Mt4 > Mt5 and future code provisions perhaps.   Overall, avoiding long or far calls can have a huge cumulative impact on overall performance and integration stability, something which has always been an issue in the MT4 scripting constructs permitting sloppy code design.

It honestly was not clear to me in the support files that OnTick() might not apply inside the Start() cycle in some instances.  It helps if considering OnTick() as an explicit option of replacement to Start() as likely the safest way to view it.   I didn't test if OnTick() can possibly be permitted inside Start() but it would now seem redundant and prone to several issues if one could even do so, yet there may have been instances beyond obvious for lack of clarification in the help file, leaving assumptions to dangle.

Last, I did not find this, "remained for compatibility" searching help or prior web reference unless you're simply clarifying; all good either way. :)

Since I'm not familiar with Mt5 (which likely doesn't support the Start() command) and my question is here in the Mt4 forum, this also suggests an "Mt4.5" context in compatibility.  I also don't use the newer template provisions which I'm sure would be intending to encourage Mt5 migration and a more rigid learning path.

The bottom line appears to identify Mt5 as supporting stateless design I would think points toward Object Oriented methods.  These cross compatibilities appear to ease differences for Mt4 coders fumbling up to Mt5 methods and greater concepts of more virtual design versus the "stateful" event trap.  The flexibility in Mt5 seems to become nearly endless but perhaps forces a greater reliance on state machine at the designer's hands inside the greater flexibility which might even lend well to UML design and very high-end potential Mt4 has wanted for.

I'm making some reaching assumptions but for those of us grappling with the changes to Mt5 outside of good academic training, making the transition without the benefit of more formal learning is a real pit for wanting to avoid building on poorly written code, if we can understand the newer concepts better.  The Mt4.5 after all has become a "forced" initiative by MQ, so those of us not familiar with Mt5 have our hands full keeping forward code maintenance with learning better design integrity nearly overnight.   This is where any secondary help file system really cannot provide a novice with much better than we have currently, except by greater discussion and if not for the compatibility of crossover options like OnTick() being permitted along side of Start() in Mt4.5 as a "bridge" to Mt5.  It otherwise defines the "leap" to Mt5 which has been thwarting MQ from bringing Mt4 coders forward to Mt5 as they must.  Painful as that may be for some of us, the potential benefits outweigh the struggle, if one intends to stay relevant as methods progress.

Owing to the frustration the build 600+ (Mt4.5) migration has been, the initiative MQ has undertaken is one of the most involved I've personally seen in any compiler overhaul and language upgrade.  The Mt4.5 stage of this represents a huge afterthought with significant engineering thrust by MQ, still working out bugs even now.  I'm sure some if not many might disagree but realistically Mt5 had to mature to a certain point before Mt4 end of life migration could be made a requirement supporting mass transition.  Fuller understanding of this need (and the benefit for those of us confronted by new learning) couldn't be more important to help novices understand and justify the need to "just bite the bullet and go back to the work required to learn it" (and risk asking dumb questions in the open like the OnTick() command you've helped clarify). 

Not so much a rant, but hopefully to encourage others beyond simply "RTFM" and to realize it is a transitional process not always having obvious context in reference, needing further clarification for those learning.

Thanks once more and feel free to correct my assumptions if appropriate or the thread can end.   Simple (or stupid) as it was, you've helped me understand better why the question of OnTick() occurred and why the answers are not always obvious, since the forced migration has literally kept me too busy to learn Mt5 properly, frustrating almost like a paradox.  Yes, I should have followed Mt5 sooner, like many.   Maybe it helps others now stuck learning in similar ways too.  It's a good thing we have those more experienced with patience, willing to help answer the sometimes "less than obvious" dumb questions on the way to Mt5. :)

Regards,

MTC  

rocketman99
122
rocketman99 2014.10.15 22:49  

Not to hijack your thread, but the main reason why nobody cares for MT5 and thus very low adoption was the fact that the language was not backward compatible at all with existing MT4 code. You pretty much had to re-write. Secondly, MT5 enforces the most stupid of rules used in only one country that I am aware of - the no hedging rule. You cannot hedge with MT5. This pretty much broke trading for everybody in the world except those in the US.

And I am not interested in going into a discussion about hedging except to say that I run multiple strategies on the same account. Some longterm, some shortterm. So I must be able to hedge and to control my margin effectively. If I cannot hedge, I need to run multiple accounts and thus require much more capital. Thankfully I am not in the US and will also never use MT5.

Hopefully now that MT5 is being backported to MT4, MT5 will eventually die and they can focus their efforts on a single codebase for a more stable environment.

whroeder1
13610
whroeder1 2014.10.16 12:59  
MTcrafter:Last, I did not find this, "remained for compatibility" searching help or prior web reference unless you're simply clarifying; all good either way. :)
I gave you the link why couldn't you read it?

Changes in MQL4 Language

Added new char, short, long, uchar, ushort, uint, ulong and double data types. This will allow transferring codes from other C++ like languages. Data of various types is processed at different rates. Integer data is the fastest one to be processed. A special co-processor is used to handle the double-precision data. However, due to the complexity of the internal representation of floating-point data, it is processed slower than integer one. Typecasting has also been implemented.

Strings are now presented in Unicode format, though they were in ANSI format (single byte ones) before. That should be considered if the program uses DLLs and passes string variables to them.

Predefined Volume variable is now of long type. The time series for accessing the volumes also consist of long type arrays. It is recommended to use explicit casting of data having this type to the target type in old MQL4 programs to avoid type overflow error.

Structures and classes, object pointers, void type and this key word allowing an object to receive a reference to itself have been added. All object-oriented programming standards are supported:

ООP allows developing programs using classes. This facilitates debugging and development of large applications, as well as provides ability to reuse previously generated code multiple times due to inheritance. However, that does not mean that you cannot generate your MQL4 code in procedure-oriented style as before. You can develop your programs as you did in the past if you don't need the new features.

init(), deinit() and start() predefined functions have remained for compatibility, however, OnInit(), OnDeinit(), OnStart(), OnCalculate() and OnTick() ones can now be used instead. Besides, new predefined OnTimer(), OnChartEvent() and OnTester() handler functions have been implemented. In the previous MQL4, predefined functions could have any parameters and any return type, and they could be called by their names, not signatures. In the new MQL4, all predefined functions should strictly correspond to their signatures. In other words, they should have precisely defined set of parameters and return type.

MTcrafter
57
MTcrafter 2014.10.18 13:06  

WHRoeder 2014.10.16 14:59 #

 
MTcrafter:Last, I did not find this, "remained for compatibility" searching help or prior web reference unless you're simply clarifying; all good either way. :)

I gave you the link why couldn't you read it?

 

Mmmm, apparently because I had already exhausted the local help files in frustration and already moved into the OnTick() help description on that link before I searched for "Compatibility", being over-focused on the single function of interest rather than the overall.  I was then AGAIN returning to the same process on your reference and shot past the first link, to the OnTick() reference redundantly, search fail.  To that extent the adage RTFM applies toward being more thorough, right?  Sorry.  (blush)  I've spent a lot of time hammering help files and Google in recent weeks which has created a negative habit of glancing into drill down, which in this case worked against the goal.   This underscores the extent of frustration of dealing with relatively wide compiler changes amid bringing existing, running code forward in a live setting compounded by the compulsory nature of this transitional Mt4 change.  Reminiscent of past Perl updates fighting with backward compatibility.  Most of what I'm dealing with are stateful issues of real-time tick mode processing which is a tedious issue typically in playback mode as is, when scope issues begin to show up while trying to improve the application along with updating where possible to better structure.

The sun was in my eyes.  I had the flu that day.  Yeah, that's it. :/ 

I'm probably not the only one that will benefit from this incursion through this mud puddle having fairly beat this horse to death [SOLVED], so best I can say for my oversight is "Point taken.", Thank you for bringing it home, RTFM THOROUGHLY. :)

MTC 

/
To add comments, please log in or register