Monday, January 18, 2010

MetaTrader - Donchian EA

Welcome back Forex Fans.

In this post, let's take a quick look at trading functions and then my first fully-functioning Expert Advisor.

Trading functions are described in the MQL programming book here at Trading Functions. New orders are opened using the OrderSend() function. Here's a typical function call:

iTicket = OrderSend(Symbol(), OP_BUY, dLots, NormalizeDouble(Ask,Digits), Slippage, dStopLossPrice, dTakeProfitPrice, "Buy(#" + MagicNumber + ")", MagicNumber, 0, DodgerBlue);

A few key points here:

  • Don't trade a fixed lot size, calculate lots ahead of time using the money management algorithm described in my previous post here MetaTrader Account and Position Sizing
  • For market orders, buy and the Ask and sell at the Bid using the built-in keywords.
  • Round the price to the correct number of digits by calling the NormalizeDouble() function with the Ask (or Bid) and Digits built-in keywords. Otherwise, you will get OrderSend 4107 error.
  • Specify the StopLoss and TakeProfits prices if you have them or set them to zero to exit on the reverse of our entry condition.
  • Once your order is submitted, you can review each of the open orders with a loop structure which is too much trouble to include here, but can be found in my first EA described below.
Let's move on to our first full-functioning EA called Donchian. This EA is named after Richard Donchian who popularized a simple method of trend-following that takes a position when a bar closes above the highest high or lowest low for a given lookback period.

You can find the complete code for the Donchian EA on my new Yahoo group http://finance.groups.yahoo.com/group/fx-mon/. You will also find the code for each of the learning EA's i've created so far as part of the blog. To get access to the Yahoo group, you can leave me your e-mail address as a comment, or send your e-mail address to me via direct message using Twitter at: http://twitter.com/tcxmon. I'll invite you to the group and you can download the EA's and upload your own.

So how does the EA perform? I tested it with a 6-bar lookback against an hourly chart of EUR/USD. The good news is that the system never fails to catch a big trend. The bad news is that it gets chopped up on sideways or ranging markets, which the markets do most of the time. So its clearly not a profitable EA, but a good learning exercise.

So what I have learned from this exercise?
  • Don't try to write your own EA from scratch, use someone else's as a model and modify it
  • Parameters add a lot of complexity to EA testing, and the ideal EA should have as few parameters as possbile.

  • My sample EA has parameters for Lookback (# of bars breakout), Risk, Slippage, and Boolean for TakeProfit, StopLoss and Trailing Stop modes as well as values for those items
  • I added those parameters so I can use this EA as a model and move forward to test other ideas.


Overall, finding a profitable EA isn't going to be easy, but i'm off the ground now as an EA coder a ready to start testing some ideas. Leave me a comment if you have any ideas and i'll code it and review the results here.

Thanks for reading and drop me a line if you want access to the Yahoo group.

Chris

11 comments:

  1. Here's a quick tip I use in EA's.
    For easy of optimisation I use int datatypes rather than booleans and then test for 1 or 0 rather than true or false. This allows you easily flip the flag during optimisations.

    Also, not sure if I've posted this link here but there's an online EA creator here.

    http://sufx.core.t3-ism.net/ExpertAdvisorBuilder/

    I always use this to create the basic bones of my EA.

    Steve

    ReplyDelete
  2. good post,

    Last night I did some coding on my second EA and was running into the OrderSend 4107 error. I had to stop coding before I figured it out, (put my son to bed, and hang out with my wife). I can now start up again knowing what the issue is. Thanks. Odd thing is I did not have that problem on my first EA (I'll have to go back and fix just in case), and it seemed kind of sporadic in-between changes of my 2nd EA.

    JT

    ReplyDelete
  3. Steve-

    Thanks for the site and the tip regarding using numerics instead of binaries for optimization. That's a great tip and allows the entire feature to be switched on and off during optimization, brilliant!

    JT-

    I'm intrigued regarding your Wednesday afternoon system. I'm looking for systems that will not trade often, but have a very high success rate. My target is 1% per week, not a amazing, but could really compound over time. I'm not sure if your EA is the answer, but i'll follow this path wherever it goes, as long as its profitable!

    So let me know if you interested in sharing it, or I may have to try and code it myself! If you rather I look elsewhere, that's fine too.

    Thanks again guys for the great comments,

    Chris

    ReplyDelete
  4. Wednesday afternoon? Not sure which one that is. Right now I have my first EA, which I am currently calling my townjet_breakout, thinking about calling it the Tiger Tail EA. This one can be played on a few different currency pairs, but works better on EUR/USD, and GBP/USD. Tracing back to Jan. 2005 GBP/USD has 61 trades with 50 wins and 11 losses with the following setup; I go for 42-45 pips and risk 90-85 at a stop loss. (1:2, reward to risk). I don't have any demo forward testing, but 2 1/2 months of live testing doing it manually (with 4 wins right now). This method could probably be tweaked for better output. Right now this is my holy grail(the one I have been looking three years for), I am a bit squeamish about giving out what I am doing... If I risk $200 to trade with the GBP/USD alone I can pull 10 wins and 2 losses in a year that equals $250 or roughly 112.5% (Manual testing on FXCM charts showed only 3 fails in 4 years!). ...So you can see why I would want to keep it under my hat until I made some money with it. I have only officially been running this EA in demo for about 4 trading days. So there could be flaws in it. But as far as my manual trading and tracking, things have looked really good.

    My second EA I am playing around with is still kind of in the works. It has proven to be quite difficult to work out. I can see what I need to do, but working out the logic is very difficult. I may be willing to let you in on this one, may actually be better than my Tiger Tail EA if I can ever work it out.

    ReplyDelete
  5. ... If you are interested in my first EA... Let me run it for awhile. I still have some slight doubts in it (must be my 3 year bad trading history causing caution). If you noted that 12 trades a year with a couple of failures is not bad. But if more than 3 bad failures occur the method will have problems working. The profitability hangs pretty close to failure. The only thing that keeps me interested is the last few years have proven only 1 or 2 failures at the most. In addition I can trade this on a few different currency pairs. If my $200 allotted for trading the GBP/USD is not being used, I can have it ready when the EUR/USD signal comes along... At any rate, let me work with it for a bit, and then you would have to promise me to keep it under your hat if I shared it. :)

    ReplyDelete
  6. JT-

    Thanks very much. I understand your reluctance to share what you are doing. So if i'm infringing by asking, just say so and no harm intended.

    Thanks for reading and commenting,

    Chris

    ReplyDelete
  7. No problem,

    I consider you a friend, we have been through at least 6 months of Zulutrade hell together. :)
    I take it I can find your email using your username and @gmail.com? I'll put something together to describe what I am doing. It is rather a very easy method, and I would not be surprised if you had kind of seen the system before but not really thought about it.
    You may even find it is not for you. As I am at times skeptical of it as well.

    ReplyDelete
  8. Hey Chris,

    Ive started from the beginning of your blog and just reading through id love to join your Yahoo group.

    Im a C# programmer whose been manual trading for 1 year now, 6 months live. Ive pretty much broken even and realised it is tough work. The other guys i trade with (skype group) have pretty much been trading a similar method, but ive realised through manual backtesting that id like to use my skills as a programmer to just start coding my own EA's. Ive fiddled with some stuff but i think, like you said, that if i can leverage an existing EA template, i might be able to progress at a quicker pace.

    If you could follow me on twitter (courtzz) then i can DM you my email as im hestiant to leave it on a blog (for spam reasons)

    thanks again for all your ideas so far. ill keep reading!

    ReplyDelete
  9. Hi Courtz-

    Thanks for the comment. I sent you my contact info via Twitter Direct Message. Once you get added to the group, take a look at FX-Regression and you can use that as a template. Also, Daniel offers an EA template using his own coding framework which is a lot more robust than mine.

    Good luck and have fun,

    Chris

    ReplyDelete
  10. Hey Ya'll,

    Below is a list of the highest ranking forex brokers:
    1. Most Recommended Forex Broker
    2. eToro - $50 min. deposit.

    Here is a list of top forex instruments:
    1. ForexTrendy - Recommended Probability Software.
    2. EA Builder - Custom Strategies Autotrading.
    3. Fast FX Profit - Secret Forex Strategy.

    Hopefully these lists are helpful to you...

    ReplyDelete