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


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.