Blog Item

How To Test Your Broker's Latency

The LatencyEA measures the latency to your broker's MT4 serverA rise in recent months of the number of EAs which trade so-called impulse spikes (such as Forex Thor) has meant that a lot of questions are now being asked about latency.

Latency is, broadly speaking, a measure of the time taken to send the packets of data from your PC to your broker's server. A poor connection to the broker's server could result in orders failing to execute at the desired price and trading opportunities being missed. What might seem like a very good EA to some users, may easily turn into an absolute nightmare for others!
I say 'broadly speaking' because, to the best of my knowledge, there's absolutely no way of measuring how long it takes to make the one-way connection between the two machines. Instead, we can only measure something that is called round-trip latency.

Round-trip latency is actually a three-part process because it includes not only the two each-way trips from one machine to the other and back again, but it also includes the element of time which is taken by the broker's machine to process the instructions that have been sent and compile a suitable response. For instance, if each trip takes 200 milliseconds, and the broker's server takes 100 milliseconds to execute any instructions and compile its response, then the round-trip latency will be 500 milliseconds; the one-way latency will only be 200 milliseconds and all the important bits and bobs will have been sorted out on your broker's server after 300 milliseconds.


So how can we measure latency to get some sort of an idea whether our connection is any good or not?

Well, firstly, the only MetaQuotes Language functions which involve two-way communication between the user and his broker are the order sending functions. As part of this process, the MT4 client terminal sends an instruction to the broker's MT4 server to either buy or sell a market. The MT4 server executes that instruction and then sends a response back to the MT4 client terminal which either says "Bingo" or "Houston, we got a problem".

So, we simply make a note of the time immediately before sending our order to the broker, and then make another note of the time as soon as we get a response back from the broker to say the order has been executed. Subtract one time from the other and, hey presto, we've got our round-trip latency. It's easy innit? :roll:

Well no, it's actually a bit more complicated than that, because we don't want to be waiting for an EA to be ready to trade in a live environment before we can find out whether our connection is any good or not. To get over this problem, we need to be sending a series of dummy limit or stop orders at a price way away from the current price, so there's no risk whatsoever of the test order actually being filled. Having sent a dummy limit order, we can then delete it straight away. And if we send the dummy orders at regular intervals, we can easily find the maximum, minimum and average latency over a sample of 100's or 1,000's of dummy test orders. Cool, huh?

The good news is that I've written a simple EA which does precisely that, and I've put it onto MellyForex as a FREE download for my readers.

There are no strings attached. You simply attach the Latency EA to the chart window of ANY currency symbol, running on ANY timeframe. Set the frequency in minutes that you want to send the dummy orders, and the EA will do the rest.

The Latency EA will display a comment in the top left of the chart window showing the minimum, maximum and average round-trip latency.

The limit orders themselves are sent at the absolute minimum price and for the minimum contract size allowed for that market so, like I say, there's no real chance of any of the dummy test orders being filled. The EA automatically detects 4- or 5-digit brokers and, for the technically minded, it uses the somewhat obscure magic number 4156434123, so there's unlikely to be any conflict between its trades and those of any other EAs operating on the account. In any event, I've made the EA open source, so you can change or modify the code if you wish.

Please bear in mind that it's an EA, rather than an indicator or a script, so its successful operation is dependent upon a flow of incoming price ticks for it to work. If you notice that it doesn't close an open order immediately, it will simply be because it's waiting for the next incoming tick.

As I've said, you are at liberty to modify the EA if you wish and, throwing a couple of ideas for improvement into the melting pot, you could save the results as a global variable so they could be added to whenever the EA is reloaded or you could write the details of each trade to a CSV file for posterity. You could even run some deviation algorithms on the results if you wish. But that's down to you - please don't ask me to make modifications to the EA for you, as I really don't have the time.

If you like the EA, then all I ask is that you tell your friends about MellyForex and send them to this page to download the EA. Please don't link directly to the download file in case I decide at some future date to either move the file's location or update it with a newer version. And if you do decide to modify the EA, please be sure to leave the original copyright notice intact.

I suppose I should also include a disclaimer to the effect that anybody who uses the Latency EA does so entirely at their own risk. Otherwise, enjoy!

Download the FREE MellyForex Latency EA here

Installation Instructions

There's every chance that the above download link will open the EA as a readable text file in your browser window, rather than presenting it as a file for download. If that's the case, when it opens in your browser window, you just need to hit File > Save As and choose where you want to save it on your PC. You should save the LatencyEA.mq4 file in your MetaTrader Installation Folder > experts folder. The MetaTrader Installation Folder will normally be located at C: > Program Files, although that won't always be the case.

When you restart your MetaTrader 4 client terminal, the MQ4 will automatically compile, and you'll be able to view it as an Expert Advisor in the Navigator panel. From there, you can drag it into a suitable chart window and set the frequency at which you wish the test limit orders to be sent (default is 5 minute intervals). You also need to ensure that Live Trading is enabled, otherwise no orders will be sent.

Discussion (3 comments)

  1. On 05/17/2013 at 19:07, pipscalper said:

    Hey Melly,

    Just gotta say that I really like the look and feel of the new site. :-D

  2. On 10/23/2013 at 01:18, SolarBear said:

    Thanks for sharing~ It's useful

  3. On 04/21/2014 at 22:40, Michal said:

    Exact latency measurement systems are vital for both localhost & VPS trading setups.
    Besides L3 & L4 latencies,
    the most important is the eXecution of Trade Operations [XTO] Latency

    might be interested in TAMARA passive scanner & SONAR active testing of real-time parameters of the Broker Service

    >>> Happy Trading


Add a Comment

;-) :-) :-D :-( :-o :-O B-) :oops: :-[] :-P

To prevent automated Bots form spamming, please enter the text you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.