1 (edited by sleytus 2018-07-31 05:24:09)

Topic: Using OnTimer versus OnTick with an MT4 Expert Advisor

Recently on another forum I posted a comment / question about the use of OnTimer versus OnTick.  Below is a link.  I'm posting this partly so you can read my description and partly to check out the response.  Make sure there are no children in the room...

The fellow who runs the blog has a love / hate relationship with MT4.  On one hand his life revolves around MT4, but on the other he hates it.  He even changed some of the words in my post -- e.g. changing "MT4 Strategy Tester" to "Empty4Shit Tester".  Very strange...

Anyways -- it reminded me what a pleasure it is to come here, even though sometimes I get frustrated

http://www.stevehopwoodforex.com/phpBB3/viewtopic.php?f=5&t=5498

I've posted before about the use of OnTimer versus OnTick in Portfolio Maker.  And until a couple of weeks ago people had reported conflicting observations.  However, recently I found/solved the problem that makes use of OnTimer more reliable.  Depending on your opening postion, the generated *.mq4 code may perform the following check:
      bool isNewBar=(barTime<dataMarket.BarTime && dataMarket.Volume<5);

However, the condition "dataMarket.Volume<5" usually returns 'false' when OnTimer is used -- hence, the strategy doesn't trade as often as the OnTick version.  Changing the above line to:
      bool isNewBar=(barTime<dataMarket.BarTime);
now results in OnTimer working more reliably.

Actually, a few months ago Popov had pointed-out the above line of code to me -- but I didn't make the connection.

Re: Using OnTimer versus OnTick with an MT4 Expert Advisor

The && dataMarket.Volume < 5 is a naive try to make the expert start trading from the next new bar. It is because when you run the expert, it is most probably at the middle of a bar and the first tick will give "isNewBar = true".

Anyway, it must be an easier way to do it as for an example to set the initial barTime = actual bar time. It must work. I think the original expert may work even without that protection.

By the way, the legacy Forex Strategy Trader used similar technique to request signals. There was no OnTime event at that time and I used a infinite loop with Sleep(100) to imitate it.

Re: Using OnTimer versus OnTick with an MT4 Expert Advisor

Interesting -- thank you, Popov.  And if you don't mind my asking -- why did you choose to simulate OnTimer in the legacy FST?  The start() function used to be the equivalent of today's OnTick() -- is that correct?  Why didn't you simply use start()?

Based on my experience using Portfolio Maker and creating portfolio EAs from FSB-generated strategies, I am now feeling more confident that OnTimer is the way to go.  And this is due to the threading model used by MT4 -- it seems it kills the OnTick thread if it takes too long.  I'm now up to 80 FSB-generated strategies in a single portfolio EA and it seems to be trading fine in a Real account with minimal CPU overhead -- since it only requires one chart and OnTimer runs once every 10 seconds.

Though my experience with the other forum wasn't so pleasant, it was interesting to learn that most of the EAs they are working on also use OnTimer.  I had hoped to learn more about why they chose OnTimer over OnTick, but my comment about MT4 Strategy Tester potentially being useful, didn't go over very well.

Re: Using OnTimer versus OnTick with an MT4 Expert Advisor

The legacy FST (forex Strategy Trader) Expert has no trading logic. It is both server and a client. It send the actual market and position info to FST at every 0.1 sec. FST runs the strategies, listens for request from the expert , calculates the signals and return response. The expert traders according to the response. Despite the fact it looks complicated it worked well.
The benefit was that we have only one copy of the indicators and the trading logic. It was not necessary to have MQL code at all except for the generic server expert. it was actually something like signal copier.

The drawback is that the trader must install FST on his machine (VPS) and a dll bridge. It was problem some times. Onother drawback is that we don't have a single expert advisor. It took me long time to realize that I can make this complete system working in MT. MetaQuotes improved the MQL and added  OOP, so I re-created the FST logic inside the expert.

Offff. I completely forgot that this model still works in the FSB Pro Trader mode. It is very versatile. I thought I was removed the bridge years ago. No one speaks for that now. Anyway, it is an interesting system. You may find info and videos. Of course, the exported experts are better in many cases.

Re: Using OnTimer versus OnTick with an MT4 Expert Advisor

Thank you, Popov.  I enjoy reading and learning more about the history of how your software evolved.  I imagine it's been an interesting and challenging journey. 

When one considers how many people trade forex (both currently and in the past), how much money changes hands and how difficult it is to succeed, it boggles my mind that one person (i.e. yourself) can make such a big impact.

Re: Using OnTimer versus OnTick with an MT4 Expert Advisor

sleytus wrote:

Recently on another forum I posted a comment / question about the use of OnTimer versus OnTick.  Below is a link.  I'm posting this partly so you can read my description and partly to check out the response.  Make sure there are no children in the room...

The fellow who runs the blog has a love / hate relationship with MT4.  On one hand his life revolves around MT4, but on the other he hates it.  He even changed some of the words in my post -- e.g. changing "MT4 Strategy Tester" to "Empty4Shit Tester".  Very strange...

Anyways -- it reminded me what a pleasure it is to come here, even though sometimes I get frustrated

http://www.stevehopwoodforex.com/phpBB3/viewtopic.php?f=5&t=5498

I've posted before about the use of OnTimer versus OnTick in Portfolio Maker.  And until a couple of weeks ago people had reported conflicting observations.  However, recently I found/solved the problem that makes use of OnTimer more reliable.  Depending on your opening postion, the generated *.mq4 code may perform the following check:
      bool isNewBar=(barTime<dataMarket.BarTime && dataMarket.Volume<5);

However, the condition "dataMarket.Volume<5" usually returns 'false' when OnTimer is used -- hence, the strategy doesn't trade as often as the OnTick version.  Changing the above line to:
      bool isNewBar=(barTime<dataMarket.BarTime);
now results in OnTimer working more reliably.

Actually, a few months ago Popov had pointed-out the above line of code to me -- but I didn't make the connection.

Hey Steve,

I just had a look at that appalling exchange on TOF (That Other Forum), and I have to say, though it made me laugh, I'm pretty disgusted. Lol, who tf does TOS (That Other Steve) actually think he is?? Dimwit?? Lmao, who's the freaking dimwit?! I can't believe how restrained and polite you were with such a toxic cretin. Well, good on him in a way, since the planet is groaning under the weight of forex forums, and deciding where to spend our time is a real issue. And here I am, newly registered here, and not there. 555 (as the Thais say)

Your post on TOF came up when I was hunting around for views that people might have on exactly the structural problem you raised, re OnTimer() versus OnTick(). I hadn't come across the possible issue of MT4 killing off our thread if OnTick() takes too long to return, and I suspect my current project will be vulnerable to just such a calamity. Previously, I'd only ever used OnTimer() to generate simulated (pseudo)ticks for me on the weekends, for development and testing purposes. Now I have to consider putting my main workflow in there, which will be an interesting switch, and one that might have taken me untold hours to nut out if you hadn't raised it. So thanks for doing that, and good luck with your trading!

He who laughs last is the last left laughing.