Free email newsletter with articles on systematic trading

 

 

Newsletter
Article Library
Videos
What's New
About Us
Site Map
Search

 

 

 

 

Adaptrade Software Newsletter Article
 

 

MetaTrader vs. TradeStation: A Language Comparison

by Michael R. Bryant

 

 

Anyone who actively trades forex has probably heard of MetaTrader. They claim to have more than half a million users for the mobile versions of their MT4 and MT5 trading platforms. In response to requests from MetaTrader users, I've been working on adding MetaTrader 4 (MT4) output to Adaptrade Builder. As I finish up the documentation for the new MT4 code output feature in Builder, I thought it would be a good time to discuss some of the ways in which the MT4 programming language, called MQL4, differs from TradeStation's programming language, called EasyLanguage.

 
If you're a systematic trader interested in trading forex and haven't yet chosen a trading platform, or you're thinking of switching from one platform to the other, there are some things you should know about the two scripting languages.
 

Why so Popular?

If you're just learning about MetaTrader,* you might wonder why it's such a popular platform. In my opinion, it's a result of several factors. First, the platform is free. You can download the software and get free forex data as part of the platform at no cost. The platform includes all major forex symbols, and, after you sign up for a free demo account, the data is updated in real time. Also, the MetaTrader 4 scripting language, MQL4, is integral to the platform. You can use MQL4 to write indicators, "scripts" (code to perform specific functions upon request), and "expert advisors (EAs)" (trading strategies).


The MQL4 language, as discussed below, is very versatile and makes MT4, like TradeStation, an extensible platform. There's an active community of MT4 users who contribute indicators and EAs to the MT4 forum. The MT4 platform, like the MQL4 language, is designed specifically for forex. I'll discuss more about how this impacts the MQL4 language below, but the point here is that forex is a big, growing, global market, and MetaTrader has specifically targeted that market. Lastly, unlike TradeStation, which mostly limits users to using its own brokerage services, MetaTrader is compatible with a wide variety of forex brokers.
 

Different Origins

As a long-time TradeStation user, I can recall when TradeStation was only for trading the futures markets. The EasyLanguage programming language was designed by futures traders for futures traders. Over the years, the language has been extended and adapted to work well in other markets, most notably stocks, options, and forex. However, some of its characteristics still reflect its origins in the futures markets.


MQL4 (short for MetaQuotes Language 4) was designed specifically for the forex markets. Oftentimes, instructional material for forex trading focuses on generic trading ideas and concepts, such as technical indicators and types of trading logic, and ignores the details of how the forex markets work and how that makes forex different from other markets, such as stocks and futures. The fact is that forex trading does work somewhat differently from other markets, as anyone transitioning from a different market knows. MQL4 tends to reflect those differences.
 

Key differences
Here are some of the key differences between EasyLanguage and MQL4. Unless otherwise noted, the discussion refers to writing trading strategies and back-testing them on historical data.
 

Code Execution

The premise of EasyLanguage code execution is that all code is executed on the close of each bar of the chart to which the strategy is applied. If the chart consists of daily bars, for example, the code will be executed on the close of each daily bar. If you want the code to execute more frequently, the chart has to be changed to have a smaller bar size. You can, however, force the code to execute certain elements more frequently using the "Look-inside-bar back-testing" feature. This uses price data at a higher resolution than shown on the chart in order to produce more accurate results.


MQL4 code uses a function called start() that executes on each tick. Typically, the main strategy code occurs within the start() function. If you don't want the code to execute on each tick, you have to program this logic into start(). For example, to have the code execute on the open of each bar, you can use the bar volume to detect the open using "Volume[0] <= 1". There's no practical way to detect the close of the bar, so strategies in MT4 typically execute on every tick or on the bar's open.


Because EasyLanguage code executes on the close of the bar, trade order statements are always for execution on the next bar; e.g., "Buy next bar at market". The closest equivalent statement in MQL4 would be to place the order for the current bar at the current bar's open. In this case, the trading logic is always evaluated on the prior bar in MQL4, whereas in EasyLanguage, the logic is evaluated on the current bar.


Unlike EasyLanguage, MQL4 doesn't restrict strategies to the data for the chart on which the strategy has been applied. You can reference any available data series in a MT4 strategy by referring to the symbol and bar size. Bar sizes are limited to 1, 5, 15, 30, 60, and 240 minutes as well as daily, weekly, and monthly. TradeStation has a greater variety of available bar sizes, including bar sizes of any integer number of minutes and tick bars of any number of ticks.
 

Order Execution
EasyLanguage does a commendable job of hiding the complexities of placing and executing trading orders. For example, if you have a short position, and you place a long entry order, if you don't specify the size, the long entry will automatically close out the short trade at the same time it places the long trade. Likewise, if you have multiple pending orders to exit, say, a long trade at market depending on different conditions, if multiple conditions are true at the same time, only one exit order will be placed; the others will be cancelled automatically. Also, trading orders in EasyLanguage persist for just one bar and are automatically cancelled if they're not filled at the close of the next bar.


MQL4 leaves order handling largely up to the programmer. If you have multiple competing orders, you need to manage them yourself, cancelling the ones that don't get executed and making sure that multiple orders don't get executed unintentionally. For example, in MQL4, if you want an entry to reverse an open position, you have to either specify the number of lots to give the desired net result (e.g., sell 2 lots short with 1 lot open long to end up 1 lot short) or track the open position and close it as soon as the new entry is detected.
 

Shares vs. Lots
In EasyLanguage, the size of a trading position is specified in either contracts (e.g., futures) or shares. For forex trading, a standard position size in TradeStation would be 10,000 or 100,000 shares, corresponding to a small or full-size lot. In keeping with its forex orientation, in MetaTrader the trade size is specified in lots, which can be fractional. A full size lot would be a lot size of 1. A mini lot would be a lot size of 0.1.

 

Trading Costs and Fill Prices
Because TradeStation and EasyLanguage were originally oriented towards futures trading, they follow the convention of using slippage to account for the fact that trades are not usually filled at the market price. Slippage is the dollar cost added to the trade to account for this. Typically, you would also enter the commission costs per contract/share or per trade to account for the fees the brokerage charges to execute the trade. All of these costs are treated the same way: they deduct a dollar amount from a profitable trade or add the same amount to a losing trade. The same costs are deducted from all trades, both long and short. At the same time, the trade is assumed to have been filled at the specified price, either the current price for a market order or the specified stop or limit price.


MQL4 uses a somewhat more sophisticated approach to trading costs and fill prices. In MetaTrader, it's important to understand that each price is actually two prices, the bid and the ask. The bid is the lower price, whereas the ask is the higher price. The difference between the bid and the ask is called the bid/ask spread. Buy orders are always filled at the ask, and sell orders are always filled at the bid. A price chart displays the bid price only. This means market buy orders will be filled above the apparent market price (based on the chart), whereas market sell orders will be filled at the price seen on the chart. The bid/ask spread is part of the cost of the trade. This is consistent with the common practice in forex trading of paying for the trade through the spread rather than paying the broker a fixed commission.


MQL4 also uses the bid/ask spread to determine if a pending order is filled. For example, a buy stop order is only filled if the ask price, which is above the chart price (bid), touches the stop price. If, for example, the price bar on the chart just touches the stop price, it may appear that the order should be filled, but MT4 won't show the historical trade as filled unless the ask price reached the buy stop price. Similarly, a buy limit order will not be recorded as filled unless the ask price reaches down to the buy limit price. Sell stops and limits are filled at the bid, so, unlike buy orders, their fill prices directly correspond to the chart prices.


The bid/ask spread is not the only price spread that affects trading orders in MT4/MQL4. If a pending order (stop or limit) is too close to the market at the time it's placed, the order will be rejected. This is based on the idea that there won't be sufficient time to place the order before the market moves through the order price. This minimum distance can be retrieved using the MarketInfo(..) function in MQL4. Similarly, a pending order cannot be modified in MQL4 if the current price for the order is within the so-called "freeze" level. In other words, if the order is so close to the market that it might be filled at any moment, you're not allowed to modify it.


Because of the more sophisticated approach MT4/MQL4 uses to represent order filling, fill prices in MT4 for historical simulations (i.e., back-testing) are likely to be more accurate than in TradeStation.

 

Language Syntax
Both EasyLanguage and MQL4 are C-like languages. That is, they both have similarities to the C programming language, which is a general purpose, procedural programming language developed in the late 1970s. MQL4 is much closer in syntax to C than EasyLanguage. However, while MQL4 looks almost identical to C, there are a few differences, and MQL4 does not implement all of C's language features. The help files in MT4 note the differences.


Indicator Differences
For anyone contemplating converting an EasyLanguage strategy to MQL4 or vice-versa, be aware that not all indicators that are available in both platforms are calculated the same way in each platform. In particular, the following indicators give substantially different values in each platform for the same price data: Momentum, FastD stochastic (main mode of the stochastic indicator in MT4), SlowD stochastic (signal line of the stochastic indicator in MT4), DI-/DI+ (directional movement), ADX, and accumulation/distribution.


It should also be noted that TradeStation includes more built-in indicators than MT4. Through the online forum for MT4, however, it's possible to find a wide variety of indicators that have been provided by other members for free.


Conclusions
Both EasyLanguage and MQL4 are general purpose scripting languages designed for trading the markets. With either language it's possible to develop highly complex and sophisticated trading strategies. In general, my experience, which seems to be supported by others, is that MQL4 is a more challenging language to master than EasyLanguage, though most TradeStation users would probably agree that the name 'EasyLanguage' is a bit of a misnomer.


Much of the complexity of MQL4 comes from the requirements it places on the programmer to manage trading orders, something EasyLanguage handles behind-the-scenes for the most part. However, the extra burden comes with greater control and greater accuracy in estimating fill prices in historical testing. Overall, it's not surprising that MT4 is a popular trading platform for forex and that MQL4 has been high on my list of requests from customers of my Adaptrade Builder software for strategy building.
 

Mike Bryant

Adaptrade Software
 

* There are two current version of MetaTrader: MetaTrader 4 and MetaTrader 5. Both platforms are actively supported but use different scripting languages. MT4 is by far the more popular platform. As a result, this article will focus exclusively on MT4 and its associated scripting language, MQL4.

 

____________

 

This article appeared in the December 2012 issue of the Adaptrade Software newsletter.

 

 

If you'd like to be informed of new developments, news, and special offers from Adaptrade Software, please join our email list. Thank you.

 

Join Our Email List
Email:  
For Email Marketing you can trust

 

 

 

Copyright (c) 2004-2019 Adaptrade Software. All rights reserved.