Forex Blog

First-hand Forex trading experience and information about foreign exchange market that will be useful to traders

Archives

Self-Learning Forex Expert Advisor

April 12, 2010 by

After a little e-mail conversation with one of my site’s visitors about the statistical expert advisor, example of which I shared publicly in August 2008, I’ve remembered about a more advanced version, which, while still isn’t profitable, can be a much better example and foundation for the future self-learning statistical EAs in Forex. The expert advisor that I’ll share in this post is a corrected and then slightly modified version of the EA that was used by me in MetaQuotesAutomated Trading Championship 2008. Although I’ve started very well in that competition I finished with a very bad result. There were several problems in the version used in the championship that were fixed in the current version of the EA. But it’s still not profitable and shouldn’t be used for live trading without a thorough modification and testing.

If you aren’t acquainted with concept of the self-learning EAs or statistical trading systems, please read the following posts:

In short, the self-learning EA “learns” from the market and places the positions according to his previous “knowledge” about the market. The presented EA — RowLearnerCorrectedFinal, uses OHLC (Open, High, Low, Close) data of the 6 previous bars and the resulting outcome of the latest bar (drop, rise, or flat) as the primary knowledge about market. The OHLC data is also normalized by subtracting the current Open level to make the “knowledge” more independent from the actual market levels. To decide the direction of the positions, the EA finds the lowest average Euclidean distance between the current market state and all the remembered states. It also has a money management system implemented (quite aggressive, left from the championship). You can download RowLearnerCorrectedFinal (or zipped) for free.

The expert advisor is available only for MetaTrader 4, because I still have a lot of issues with the MetaTrader 5 order handling. Because it’s not profitable, I didn’t add it to the list of MT4 EAs on my site.

How to use it:

  1. Download and copy to your MetaTrader’s /experts/ folder.
  2. You’ll need to create a map-file with the market “knowledge” for EA.
  3. Open the EA in MQL Editor and comment out lines 59 and 87–106.
  4. Compile it.
  5. Run it in a strategy tester on EUR/USD, H1, Open prices only, any period (the longer is the better).
  6. “rl.txt” file will be created inside /tester/files/ folder of your MetaTrader.
  7. Copy it to /experts/files/ folder.
  8. Open the EA in MQL Editor and uncomment lines 59 and 87–106.
  9. Compile it.
  10. You can now attach it to EUR/USD H1 chart. It will trade and continue learning. On deinitialization it will save its data to “rl.txt” located in your /experts/files/ folder.

There are three known problems with this Forex expert advisor that make its use unprofitable:

  1. Basing the market knowledge on the OHLC patterns of the previous bars proved to be ineffective.
  2. If the market significantly changes its behavior compared to the period of learning, the EA fails to trade profitably.
  3. If the range of prices changes significantly, the previous data proves to be pretty useless.

Possible ways to improve this self-learning MT4 EA:

  1. Find better market parameters to store as the EA’s knowledge (Heiken-Ashi, various pivots, indicator values, etc.)
  2. Provide better learning pattern — not just buy, sell or hold if the Close-Open difference of the next candle after the pattern was inside/outside a certain range of pips.
  3. Find better coefficient system to weight the older data compared to the newer.
  4. Add more maps with different data that can’t be compared with the data stored in the primary map (for example, a separate map for Volume data).
  5. Use other distance measuring method instead of Euclidean (Mahalanobis, Manhattan, Chebyshev, Minkowski, etc.)
  6. Find a different way of applying the distance — using the average distance, using the shortest distance, using the median distance, etc.

You can freely modify, upgrade and use the pieces of code from this EA as long as you leave the credit to the original author (me) inside the MQL file. If you manage to get something worthy out of it, please let me know.

If you have any questions or comments regarding this self-learning expert advisor, please, feel free to reply via the form below.

21 Responses to “Self-Learning Forex Expert Advisor”

  1. Roseline Reigleman

    All that said is fine, but let’s not forget about other things that are important to take into account. Based on my experience, the Forex market is constantly changing and evolving so you want a software program program that changes with you. There are many software, you need to pick something that fits well with your knowledge and strategy and consider risk.

    Reply

    Andrei Reply:

    Yeah, that’s why such self-learning EAs are good. They are constantly learning as the market changes.

    Reply

  2. Amber

    Hi there,
    please tell me how to comment out and uncomment lines in editor as you said(59 and 87-106).
    And after running the tester it is not saving any ‘rl.txt’ file or any file in the tester/files folder,
    Now please tell me what to do??

    Reply

    admin Reply:

    To comment a line, just add “//” in front of it. Or use “/*” and “*/” to comment many lines at once. To uncomment, remove “//” or “/*” and “*/”.

    The file will be created after you do all the steps up to step 6 listed above. If it’s not created, please, check if there were any errors in the Journal or the Expert tab during the back-test.

    Reply

  3. Erick

    Can you give an visual example on how to comment line 59 so I can make sure to do it right?

    Reply

    Erick Reply:

    I keep getting errors

    Reply

    admin Reply:

    What kind of errors?

    Reply

    admin Reply:

    Uncommented line:

       LoadKohonenMap();

    Commented line:

    //   LoadKohonenMap();

    Reply

    Erick Reply:

    Thanks

    Reply

  4. Erick

    I’m trying to do a backtest but its not showing and buying or selling during the visual. Is that suppose to happen?

    Reply

    admin Reply:

    When you comment out the lines 59 and 87–106, you put the EA into a learning mode. Running a backtest creates a file named “rl.txt”. Now you can uncomment those lines and run it on demo (make sure to copy “rl.txt” from /tester/files/ to /experts/files/) or run it on some different backtest date range. That’s all actually written in the post above.

    Reply

  5. Erick

    I got everything worked out…Do you know any other good EA thats self-learning or self-adapting thats good?

    Reply

    admin Reply:

    By googling for “better ea” you will find a lot of clones of the neural network expert advisor used by the trader Better, who was rather successful in MetaQuotes’ automated trading championships.

    Reply

  6. Erick

    ok thanks

    Reply

    admin Reply:

    Welcome! I would appreciate if you could share anything interesting you find :-).

    Reply

  7. Tom

    Hi Admin,

    thanks for giving me such an easy and explanatory way to study selflearning systems. I would have thought those systems much more complicated than you’ve solved this.

    But: Where is the place in “TeachMap()” where the EA “forgets” values?

    I mean: It introduces new values, until the entire map is filled and never ever forgets any learned state, except the highest X. Or did I miss something from your code?

    Thanks for the explanation

    Tom.

    Reply

    Andriy Moraru Reply:

    Yes you are right, it does not forget anything. When the entire map is filled it will be rewriting vector number (MAPBASE – 1) or (HOLDBASE – 1).

    Reply

    Tom Reply:

    Ah – thanks for the clarification.

    This system trades a “known” month with a 90% hit rate and a 1:2 risk/reward.

    But it fails – as you already mentioned it – on a unknown month. After it “learned” this losing month it trades it as mentioned above.

    It is – so you can say a wonderful patternmatcher which trades a known month with a superb accuracy.

    All we have to do is, to get the system to forget failures and to keep them forgotten, I assume?

    Reply

    Andriy Moraru Reply:

    No, there is no reason to forget anything on purpose. It does not record ‘failures’, it only records conditions that led to some directed market movement. Of course, there is no guarantee that the next such market conditions will lead to the same direction in future.

    I would not treat this EA as some money-making opportunity. It is just a basic example of a self-learning algorithm in MT4.

    Reply

  8. Harish

    Hi Sir,

    I’m very much interested in the self learning trading systems. Can you please explain a little more about the trading logic of the “RowLearnerCorrectedFinal ” EA. I’m fascinated with the subject and putting serious efforts in learning and coding such systems. Any help will be highly valuable.

    Regards,
    Harish

    Reply

    Andriy Moraru Reply:

    The trading logic is pretty much explained in the 5th paragraph of this blog post. The provided source code is also quite explicit. What exactly don’t you understand in its working?

    Reply

Leave a Reply

required
required (will not be published)
optional