Saturday, May 15, 2010

An early stopping Neural Network strategy

I've written several times in this blog about a Neural Network idea that I've been working on for some time.  I wrote a NN dll for MQL4, but found that the treatment of history in MT4 makes the collection of sufficient data to train the NN very difficult.  However, in the process I did come up with a manual approach to training the NN which showed promise.  With the much faster speed of MQL5, and the very welcome ability to encapsulate code, it is now fully possible to write everything in MQL5, including coding the manual process, meaning that the whole concept can be backtested and optimised.

The broadest descripion of the idea is early stopping, which is not new in the slightest, and in fact most NN literature mentions it.  Early stopping attempts to address the problem with NNs that they are so good at learning that eventually they start to memorise individual data items, which is the familiar problem found in EA optimisation of curve fitting.  Early stopping terminates the learning process at a point where hopefully the NN has only learnt patterns, rather than data.

So this is the hypothesis: a profitable NN EA can be developed ...
  1. If a NN can be trained using an early stopping technique to recognise forex patterns
  2. If patterns repeat in forex trading, and more importantly, come in clusters
There is no doubt in my mind that item 1 is true, since I have achieved it with manual selection of NNs.  What is somewhat less certain is item 2, although the general technical analysis approach does tend to support it.  Terms such as trending market, or sideways market imply persistence of patterns,  and even a non-profitable EA can often show lengthy periods of profitability.

This last point is another key: rather than discarding all technical analysis in favour of a magical NN black box, why not look first for a basic EA strategy which seems to support item 2 above?  Its equity curve should show extended periods of profitability which could then be selected by a NN filter, in a similar way that the commonly used long term moving average is used to detect a trending market.

The whole idea is already in MQL5 code, with a number of coding errors which I need to find, which will take some time.

Here's the macro view

Thursday, May 13, 2010

At last, the VOM article has been published

See the MQL5.com article section.  It was a big effort for me to get it finished, topped off at the end by their somewhat tricky article editing software.  But the editor, Eugene, was very helpful, and it's now up there.  A nice WebMoney payment, and an MQL5.com rating that has jumped to 588.

What now?  It's been hanging over my head for so long.  I get the feeling that it will be rather overwhelming for most readers, although I hope it is taken up by advanced coders and cleaned up in the process.  The fact remains that it is really the only way to manage multiple orders on the one symbol in MT5.  I'm very prepared to spend time responding to questions and fixing bugs.

But what I really want to do now is to finish off my Neural Network EA, and discover whether the approach that I've been thinking about for so long is going to work.  It's coded, but something's wrong with the learning process, and will probably require many hours to find out why. 

Monday, April 19, 2010

Eight months of virtual hosting

A lot has happened since my first blog entry on virtual hosting, which was on 28th August last year.
My original post described what I did to set up a virtual host. In summary
  • A simple request to Commercial Network Services that enabled me to have Metatrader 4 up and running in two hours
  • Situated in New York to minimise ping times to FXDD.
  • $US30 per month
What has happened since then
  • I’ve had the most relaxing eight months I can recall in terms of MT4 reliability. The virtual hosting service isolated my MT4 live account from the implications of a 36 hour phone line fault, a 12 hour power outage in my street and two multi-day visits to hospital. Conversely, I experienced one issue with a failed restart command that I requested of the virtual host, which CNS responded to within a couple of hours.
  • I started the virtual host with 384MB of memory, but with up to three MT4 clients and two beta MT5 clients running I upgraded to 640MB at the additional cost of $US5.12 per month. The amount of container memory may still seem very low, but it’s the right amount for the purpose now, and can be adjusted immediately any time, in contrast with the purchase of a general purpose PC with multi-GB of memory to run large concurrent applications and to cater for future requirements.
  • For some time CNS has been encouraging their customers to move from Virtuosso host software to Microsoft’s Hyper-V, so I did this about a month ago. Before, my container was a Windows 2003 x64 instance, and the new container is Windows XP 32 bit. Everything works fine and this should remove the x64 limitation with running 32 bit dlls, which I have no need to do right now. It is however the one area in which I have less than 100% satisfaction with CNS: their console which shows details of my virtual instance is still pointing to the old Virtuosso host, and their console upgrade to point to the Hyper-V stuff has been “real soon now” ever since I changed over.
  • The last change is an interesting one: my FXDD live account is now with FXDD Malta. I believe that this is an offshore strategy to circumvent the new NFA FIFO laws in the US. The funds are still with a secure (?) US bank, but when I get a moment I must check out the ping times from New York, which will be a lot slower if the servers are physically in Malta.
So overall, I thoroughly recommend virtual hosting. At $420 per year it’s a reasonably cost effective alternative to having a dedicated Metatrader PC at home, and it’s certainly a lot more reliable.

I have no association with CNS beyond being one of their customers.

Wednesday, March 31, 2010

Two VOM shorts on EURUSD

Earlier, I posted an example of two VOM EAs with opposing virtual orders, resulting in no net position at the broker.

Today, there is an example of the two EAs agreeing on price direction. Both the FrAMA Cross EA and the Support Resistance EA are short on EURUSD, and it is instructive to see how this ends up in the VOM and at the server.

FrAMA Cross EA on daily EURUSD chart
Virtual short 0.1 lots at 1.33978. This has a wide protective virtual stoploss 500 (or 5000, in 5 digit terms) pips away.
This was the first order executed, and the server stoploss was set at 550 pips, ie virtual stop plus 50.

Support Resistance EA on hourly EURUSD chart
Virtual short 0.1 lots at 1.34895. This has a virtual stoploss 85 (or 850, in 5 digit terms) pips away.
This is what happened to the server position when this second order was executed
  • The server lots moved up to 0.2 short
  • The server position open price is now the average of the two orders - this is automatically calculated by the broker.
  • The VOM moved the server stoploss down to the tightest virtual stoploss plus 50 (1.36195), which can be seen on the daily chart.
If the virtual stoploss for this EA is hit, then the server stoploss will revert back to the much wider stoploss of the FrAMA Cross EA

All this careful maintenance of the server stoploss at the tightest stop plus 50 (500) pips is there for disaster protection in the event of Internet or PC failure.

Monday, March 15, 2010

Forward tests suspended for a short time

I run all my MetaTrader accounts (MT4 & MT5 demo and MT4 live) on a Commercial Network Services  virtual desktop (see original post here). Although there have been a couple of negative comments on this blog about CNS, it's been completely trouble-free for me.

CNS has been promoting a move to Hyper-V for some time, so I decided to do it. Hyper-V is apparently even more reliable than Virtuozzo, which is what I was running on before. I received an email this morning that the change had occurred, but I didn't have much time before going to work so have only set up my live account at this stage. It only took me about 20 minutes, and it's running fine - I'll write another blog soon on my impressions of Hyper-V and the general experience of remote desktop working. 

Other than being a customer I have no connection whatsoever with CNS.

[ update 18/3 ]
Just waiting for another MT5 build to come out before I can start the forward tests again.  The last two builds broke my VOM code but as usual MetaQuotes have been very responsive in fixing it.

Thursday, March 11, 2010

A real "hedge" situation with two different timeframe MQL5 EAs using the Virtual Order Manager

I started up the FrAMA Cross EA VOM forward test at least a couple of weeks ago during a massive EURUSD downtrend, and the first cross event occurred yesterday with a long trade which has already reversed this morning to a short after pulling in 111 pips. So it's finally in the market. I am reassured to see a profitable trade first off, but remember that this is such a slow trader that even though the backtest is excellent it is not at all certain that it will be profitable long term.

It's good to see an example of a real "hedge" scenario happening here with the two different timeframe EAs. The Support Resistance EA entered a EURUSD long last week against support in the 1.3450 area (which is off the bottom of the chart screenshot shown below), and the FrAMA Cross EA is now EURUSD short. The result is no open position at the broker, with the VOMs maintaining the opposing long and short trades respectively.

Wednesday, March 3, 2010

Reorganising the forward tests

I have been letting a number of forward tests run for some time without really paying much attention to them, so it's time to clean everything up.

Here's what will continue to run.  These will also have a link in the forward tests section on the right, and I have looked through old posts and updated links so I hope everything works.

MetaTrader 5
I am running two VOM EAs on the one demo account.
Past forward tests on MetaTrader 4
I was running a number of tests on MT4, in particular two versions of a News Trader, but I'm going to stop for the moment.  The best NewsTrader only broke even at best, and obviously needs work.  I'll be converting to MQL5 as some stage.




Thursday, February 4, 2010

Is the FrAMA Cross EA successful?

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.

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.

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.

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. 
  • 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.
Here is a forward test of both EAs running on the one account.
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:

 
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.