In a nutshell: no, and maybe.
No-one was more surprised than me when I plucked a simple strategy out of the air to develop my MQL5 coding skills, and it actually showed some promise. This is the FrAMA Cross EA, which simply uses a moving average cross on a 1H EURUSD chart with the Fractal Moving Average indicator that comes with MT5. The two MA periods are 6 and 12, with a fixed stoploss of 50 pips. Each cross event reverses the direction of the trade. My original blogpost for this EA is here.
In the absence of a strategy tester for MT5 so far, and witnessing the promising equity curve in the forward tests, I decided to code the FrAMA indicator into MT4 and backtest it. The formula is well documented under the Analytics section of the MT5 terminal help file.
So here's an MT4 custom FrAMA indicator, which gave me the satisfaction of behaving exactly the same as its MT5 counterpart.
I wasn't particularly surprised to see that the backtest using the above parameters is rather poor. The equity curve from 1/1/2004 to now is like this:
Maybe there's some tweaking that can be done - there is an extended profitable period from last August - but I'm not going to bother because it tends to reinforce my experience that H1 charts are very difficult to trend-trade.
Not to be deterred, I set about optimising the parameters using the MT4 strategy tester. There are lots of ways to trend-trade Daily charts - is this another one? The answer is, as is usually the case with optimisation of EAs on daily charts, "maybe". Here's an optimised backtest of a FrAMA Cross EA on a Daily EURUSD chart with Fractal MA periods set to 2 and 58, ending at 31/12/2008.
… and here is the out of sample 1/1/2009 to present.
In case anyone is wondering, this is a legitimate use of the open-prices-only model for backtesting, because the EA gets its entry and exit signals from events detected in the previous completed bar. If you want to wait for half an hour, a 90% modelling backtest of every tick will look the same, and an EA of this type will behave almost to the dollar the same in live trading as it does in backtests. But notice that I didn't say that it will necessarily be as profitable - optimisation of daily charts introduces a big fluke factor, caused by the fact that the total number of trades is statistically too small.
So where to from here? Before even considering trading them live, I usually mothball EAs of this type for at least a year, dust them off, and see how they would have performed. With the massive peak in volatility from the GFC well behind us, maybe it's time to get into long term trend trading again. In the meantime, you can watch grass grow (average 1 trade every 3 weeks!) with the VOM version of the EA.
I've been trading foreign exchange for five years now, all on the Metatrader platform, and I enjoy the challenge of searching for strategies and turning them into programs. This blog tracks whatever vague direction I happen to be taking at the time.
Thursday, February 4, 2010
Wednesday, January 27, 2010
Getting there with the VOM
I've made a number of significant changes to the VOM code, and things are looking better.
Here's a shot of six "Random Loser" EAs using the VOM. At the beginning of each 1M candle, they toss a coin and either enter EURUSD long or short, up to a maximum of three open virtual orders. I designed them to be a stringent test of the VOM because it has to handle six virtual order requests at once.
Although 18 virtual orders in total are open, there is only the one position open at sell 0.2 lots, which is the sum of all the virtual orders.
The two real VOM EAs that I mentioned in a recent post have now started trading properly, with screen shots being updates, so that's more of a real-word test.
Here's a shot of six "Random Loser" EAs using the VOM. At the beginning of each 1M candle, they toss a coin and either enter EURUSD long or short, up to a maximum of three open virtual orders. I designed them to be a stringent test of the VOM because it has to handle six virtual order requests at once.
Although 18 virtual orders in total are open, there is only the one position open at sell 0.2 lots, which is the sum of all the virtual orders.
The two real VOM EAs that I mentioned in a recent post have now started trading properly, with screen shots being updates, so that's more of a real-word test.
Saturday, January 16, 2010
VOM update
Well, it turned out to be a bit ambitious to put the VOM V0.2 out there for people to have a look at. I've had hassles with MT5 build 238, which continually crashed (blew up, atomic bomb style - aka U238?). That's now fixed I think with build 239. Also I'm having to look very closely at resource locking, because multiple VOMs are becoming tangled when ticks arrive rapidly. I could have sworn that I tested all of that before, and maybe I did and the new builds are behaving differently.
Instead of trying to do something with real EA strategies, I've written a really simple EA that I call "Random_loser" to test the locking process. 5% of the time it buys; 5% it sells, and the rest it does nothing! Interesting to watch two of them trading together.
Anyhow, with the market closed and no strategy tester yet, I'll be waiting until next week to take another look, which is probably a good thing.
Instead of trying to do something with real EA strategies, I've written a really simple EA that I call "Random_loser" to test the locking process. 5% of the time it buys; 5% it sells, and the rest it does nothing! Interesting to watch two of them trading together.
Anyhow, with the market closed and no strategy tester yet, I'll be waiting until next week to take another look, which is probably a good thing.
Saturday, January 9, 2010
Testing the Virtual Order Manager with hedging orders from two EAs
I have written several times before about writing a Virtual Order Manager to enable hedged trades in MetaTrader 5. Well, it's been "complete" for about three weeks, but what I underestimated was the amount of testing required. Nonetheless, I feel it's getting close to release V0.1 time (even this will contain plenty of bugs, I'm sure), so for those who are interested I am publishing a forward test of two EAs re-written to run on it.
Firstly, a diagram of how an EA interfaces to the VOM
Secondly, a diagram of the VOM modules
... and lastly a screenshot of the VOM tester EA that I have been using to put it through its paces
Here are all the Virtual Order Manager files in beta V0.2.
It's been a very interesting exercise in MQL5 programming and I welcome readers' comments.
- The Support Resistance EA which I wrote about in August 2009 and which I have been forward testing for some time on MT4. You can download the MQL5 VOM code for this here.
- My first MT5 EA, the FrAMA Cross EA, which I wrote about in October 2009. You can download the MQL5 VOM code for this here.
- Live screenshot for FrAMA_Cross_EA_VOM
- Live screenshot for Support_Resistance_EA_VOM
True to my article published on MQL5.com about auto-generating documentation from MQL5 code, here is the draft VOM manual created using Doxygen. Note that you will need to right-click / save this file on your PC (eg in the MetaTrader 5/Help folder) to view it. Here are three pictures from the manual which set the scene - the manual discusses them in some detail:
It's been a very interesting exercise in MQL5 programming and I welcome readers' comments.
Sunday, December 13, 2009
A crude "API" for MetaTrader 4
In response to Ludosm's request to publish this utility, here it is. Using this API, any other software can drive trade functions in MT4 by generating simple one line CSV files in a similar format to the original MQL4 trade commands.
So where you would write the following in an EA ....
OrderSend(symbol, cmd, volume, price, slippage, stoploss, takeprofit, comment, magic, expiration, arrow_color);
.... the CSV file looks like this
OrderSend, symbol, cmd, volume, price, slippage, stoploss, takeprofit, comment, magic, expiration, arrow_color
A price of Bid or Ask is interpreted correctly.
An important difference is that open orders are located using their Comment, meaning that comments should have a numerical sequence.
So OrderClose(ticket); becomes OrderClose,Comment
There are additional comments at the top of the script. Hope it's useful.
So where you would write the following in an EA ....
OrderSend(symbol, cmd, volume, price, slippage, stoploss, takeprofit, comment, magic, expiration, arrow_color);
.... the CSV file looks like this
OrderSend, symbol, cmd, volume, price, slippage, stoploss, takeprofit, comment, magic, expiration, arrow_color
A price of Bid or Ask is interpreted correctly.
An important difference is that open orders are located using their Comment, meaning that comments should have a numerical sequence.
So OrderClose(ticket); becomes OrderClose,Comment
There are additional comments at the top of the script. Hope it's useful.
Tuesday, December 8, 2009
1500 and counting; what's coming up
Very gratifying to see that I've passed the 1500 mark with hits to this blog - thanks everyone for the interest. So that's a very modest average of 10 hits a day, although it started very slow and has risen to about 50 per day recently. As an aside, it's fascinating to be a microscopic part of the Google machine. The blog has so far had 11 ad click-throughs to generate $18.11 revenue in five months!! Keep on clicking, guys - I'll give up my day job soon :)
In the meantime, I have lots of balls in the air with trading and coding ideas (happy as a pig in sh*t, as they say)
In the meantime, I have lots of balls in the air with trading and coding ideas (happy as a pig in sh*t, as they say)
- First priority - finish off an article which expands on my auto-generated documentation for MQL5 post in this blog. The Metaquotes guys have showed an interest in it and it's likely to be published on the official MQL5 site. The last thing to finish off is a comment processor which trawls through the entire MQL5 folder and subfolders and slightly modifies the comments so that they can be read in by Doxygen and appear in the comiled help documentation.
- Finish off the MQL5 Virtual Order Manager so people can try it and comment. I've written the VOM itself, but need to polish up the EA I use to test it so it can be debugged more fully. This EA is a series of buttons in a chart window which allow real and virtual orders to be opened and closed. I'll probably release the whole lot then, but also need to follow up quickly with an indicator which reports open virtual orders in a table.
- The Neural Network EA has been grinding along. I hit a difficult snag with the retrieval of history - other people have also complained that they can't retrieve the full history with Close[xxxxx]. My testing seems to indicate a limit of 1000 bars in the past (or is it 1024?) or only 10 days of 15 minute bars, although some others talk about a gradually increasing value of Bars but they can't work out what triggers it. The EA and associated neural network dll works well functionally, but the neural network can't be trained on a sufficiently long dataset to prove the concept. I can see two ways forward: either write a history reader which reads history directly from the MT4 *.hst files. Or, write the whole damn thing in MQL5, which I have found doesn't have the limitation. The latter will take a lot more time, but I've always had a plan to write a learning neural network in MQL5 in preparation for the trading championships in 2010, so I'm leaning that way. I wish Metaquotes would let us know when the strategy tester will become available.
- Allow me some (OK, a lot of) misty speculation: if an MQL5 neural network EA proves to be profitable (or any MQL5 EA), then it would be quite easy to write a MetaTrader 5 to MetaTrader 4 messaging utility to enable any MQL5 EA on demo to trade live now using MetaTrader 4. Already sitting in my MT4 code archive somewhere is a crude MT4 API EA which accepts trading commands via *.csv files.
Friday, November 20, 2009
Writing a Virtual Order Manager to enable hedging in MetaTrader 5
Since I wrote about a Virtual Order Manager in August, the MetaTrader 5 public beta commenced, and it has become clear that arguably the biggest change in the transition from MetaTrader 4 to MetaTrader 5 is the management of open trades as positions. At any one time there can be one position only open for each currency pair, and the size of this position adjusts up and down each time orders are processed by the broker. Apart from anything else, this aligns with the FIFO rules recently introduced in the US which effectively outlaw hedging.
To work in this position-centric environment, each Expert Advisor written in MQL5 needs additional programming to record which trades it has open, because it cannot find this out in a simple way from the broker. The most startling example of this would be when two EAs running against the same pair, say a scalper and a trend-follower, issue orders in opposite directions. In MT4, the result would be a long and short order with zero margin used. In MT5, no position would be open at all.
An example of the programming challenge is in the following function. Most people would use something similar in their MT4 EAs to ensure that only one order is open at a time. A similar function would not work as intended in MT5.
int OpenOrders(int magic)
{
int OrderCount = 0;
for (int i = OpenOrdersTotal()-1 ; i >= 0 ; i--)
{
OrderSelect(i,SELECT_BY_POS, MODE_TRADES);
if (OrderMagicNumber() == magic
&& (OrderType() == OP_BUY || OrderType() == OP_SELL)
&& OrderSymbol() == Symbol())
{
OrderCount++;
}
}
return(OrderCount);
}
So ... here comes a Virtual Order Manager
To work with this new order management environment I am writing a utility which I call a Virtual Order Manager, or VOM, which maintains a local record at the MetaTrader terminal of the orders issued by each EA.
Here are the attributes of the VOM:
Behaviour
An example of the programming challenge is in the following function. Most people would use something similar in their MT4 EAs to ensure that only one order is open at a time. A similar function would not work as intended in MT5.
int OpenOrders(int magic)
{
int OrderCount = 0;
for (int i = OpenOrdersTotal()-1 ; i >= 0 ; i--)
{
OrderSelect(i,SELECT_BY_POS, MODE_TRADES);
if (OrderMagicNumber() == magic
&& (OrderType() == OP_BUY || OrderType() == OP_SELL)
&& OrderSymbol() == Symbol())
{
OrderCount++;
}
}
return(OrderCount);
}
So ... here comes a Virtual Order Manager
To work with this new order management environment I am writing a utility which I call a Virtual Order Manager, or VOM, which maintains a local record at the MetaTrader terminal of the orders issued by each EA.
Here are the attributes of the VOM:
Behaviour
- Real orders at the broker reflect total position for that pair, but the VOM also maintains a virtual set of orders at the MT5 terminal. As an example one EA could be virtual long EURUSD 0.2 lots and another EA could be virtual short EURUSD 0.3 lots, with the real position showing in the MT5 open orders list as short 0.1 lots.
- Virtual stoplosses, takeprofits, magic numbers and comments are supported. Virtual stoplosses and takeprofits can be modified
- Real broker stoplosses are maintained at a configurable distance away from the virtual stoplosses as disaster protection against PC or internet connection failures.
- Virtual entry prices, stoplosses and takeprofits can optionally be displayed as horizontal lines on the chart.
- [for eventual development] A virtual_statement.htm can be produced similar to the normal statement.htm which lists open orders and closed order history.
Implementation
- The VOM functionality is added with an #include
in each EA. - I am modelling the API for the VOM on MT4 trading functions. These are familiar, proven functions which will make migration from MT4 EAs easier. So instead of writing OrderSend(….), one writes VirtualOrderManager.OrderSend(…..). I have simplified some of the functions so they are not necessarily a plug-in replacement, but I may also implement the original full versions to make MT4 migration even easier.
- The list of open orders is maintained in a file for all VOM-enabled EAs to access. A table listing all open orders will be viewable using a OpenVirtualOrderViewer.mq5 indicator.
Challenges
Some of the more challenging parts of writing the VOM are listed here- The VOM must be able to track when a pending order activates. I am using the OnTrade() event in MT5 for this purpose.
- Since multiple EAs read and write to the one file listing virtual open orders, I have to use a global variable as a file locking mechanism.
- The value of the disaster protection stoploss is not easy to determine because it is obtained from a number of virtual orders.
Release
So when will version 1.0 be released? I would hope within two weeks.
Subscribe to:
Posts (Atom)


