Random Stuff

MetaTrader History ToolKit
My ATC2012 EA
Heinz Engine
Honda Steed Exhaust Mod
3DS Retro Game Gallery 
Frypan-Lamp Biquad Antenna
HTPC 001
Aerospace AL CPU Cooler
PHP Pager
Developer's Notes

Dissecting my ATC2012 Expert Advisor

Heinz Advisor v2 + Martingale on the Rocks
(EURUSD 30 Minutes)




Expert Advisor (EA), also known as automated trading strategy, is still a young field of research in trading which I find very attractive and really fun. An EA is basically an automated robot (software program) made with the MQL5 language, that automatically trades for you while you are sleeping or getting drunk at parties (most accurate definition ever).
In this article I'm going to do something I should have done in 2012: Review my ATC2012 proposal. Though it is irrelevant by the time of this writing 6 years after the championship, it could be of interest for enthusiasts. After all this time I've been away from trading, the MT5 platform and MQL5 language. So, for this review I had to study all over again, familiarize with the platform, analyze and adapt the source code to the latest MT5 version. The only problem is that after all these years many things have changed in the platform: Hedging accounts were not supported before, that changed the whole game. I did manage to compile and run the EA successfully but something is wrong still, maybe is the history data, something changed there that now I can not replicate the same results. So, I will not be able to introduce new optimizations nor do simulations.

The core

MT5+MQL5 offers a complete set of pseudo-low-level APIs for creating EAs. Although it is entirely possible to create one or more EAs using the native calls, it is quite impractical and unproductive to create scalable and robust strategies without a proper backbone: Pretty much like coding many video games from scratch without using a game engine. So I created a framework called "Heinz Advisor" (version 2.0 for the ATC2012). This framework operates on top of the MQL5 APIs, handling all the native calls internally while still providing a structured (but flexible) layout for creating mono and multi symbol EAs. The concept is to attach one or more "Logic" to the framework's pipeline. A logic is an abstract object that executes, well, the strategy's program logic. An attached logic has an associated symbol and time frame, thus there can be as many logics attached as there are symbols per the available time frames. Many logics can reference the same symbol and communicate between them if such degree of fine work is required, in fact all the available logics can communicate with each other, same symbol or not, for maximum flexibility. Overall, the framework is responsible for providing all the necessary services for the correct operation of logics such as, for example: Validations, tick forwarding, position opening/closing/recovery...and many, many more functionality. My framework has proven to be productive and the workflow has been comfortable so far. I was even able to port this work model to Dukascopy's JForex platform. Next, I'll explain the strategy (logic) I attached to the framework for the ATC2012.

The strategy

My proposal for the ATC2012 was very simple and beginner oriented. It was roughly an heuristic price analyzer I created for detecting trend reversals. This algorithm traces a symbol's price variations over a time period and when the price has crossed a specific threshold then it triggers a command to open or close a position, bidirectional way (open long/short, close short/long). Here are a couple of charts to demonstrate the concept:

Case for a long position: It begins by detecting a floor. Then if the price has reached a certain threshold value relative to the floor then it opens up a position. Now, this is when time comes in; the EA will register a roof, if after a minimum set of bars have elapsed since the opening of the position and if the price has reached a certain threshold value relative to the roof then it closes the position.

Case for a short position: It begins by detecting a roof. Then if the price has reached a certain threshold value relative to the roof then it opens up a position. Now, this is when time comes in; the EA will register a floor, if after a minimum set of bars have elapsed since the opening of the position and if the price has reached a certain threshold value relative to the floor then it closes the position.

Most of the time both cases are contiguously executed because both conditions occur simultaneously. A long position is followed by a short position and vice-versa, again, most of the time.

By nature, as with most heuristic systems, it is based on a "trial and error" methodology, meaning that a false positive can be generated. A false positive, in this case, will translate directly to a drawdown (closing an opened position at a loss). This algorithm might take a couple of mistakes to finally hit a correct trend, in fact it needs a set of false positives to score a reasonable profit. This sounds counterproductive but it is entirely possible because of the money management system that was paired with the strategy, one that is able to benefit or (better said) do its tricks with every fail...the more the better: We are talking about Martingale here. A Martingale money management system is one of the most basic ones used by beginners when out of time or ideas, it is also one of the most feared ones because it is very dangerous: If used incorrectly or under the wrong circumstances then it can wipe your balance dry. If you want to learn more about Martingale (betting system) then follow and read this link.

To put it simple, the idea was to use the false positives to pump up the Martingale multiplier to later take a big piece of the cake. Here is a screenshot that demonstrate the concept:

The EA is always trying to enter and get a winning position, even if it takes many tries.


Symbol: EURUSD. I went with this symbol only because it was a currency pair I was very familiarized with. I did try to apply the same logic to other symbols but had bad results while optimizing for them.

Period: M30. I am most of the time an H1 or even H4 guy but this time I had the pressure of an ATC rule that required a minimum number of trades. I got a little worried so I decreased my average timeframe and optimized for it, allowing the EA to perform double the operations. In the end it was nothing to worry at all, it did way more trades than the required, it was just a guarantee.

Initial_Volume: 0.001. Initial round volume. This is the trade volume when starting a Martingale sequence. I knowledge that I set it too low.

Volume_Step: 2. This value indicates how much the volume factor will increase after a drawdown. 2 is the default value for a Martingale sequence (1, 2, 4, 8, 16, ...) but other more aggressive or conservative values can be set. Even if the value is set to increase to X1 (1, 2, 3, 4, 5, ...) is possible to get profit. This is possible because profit is not fixed: You can place an order for the same or even half the volume of the previous loser position and if the price margin is big enough then you can recover the losses plus taking a gain.
I just noticed by analyzing the strategy tester report's deals that somehow the martingale factor was not applied correctly, this resulted in a more conservative sequence (0.01, 1.01, 2.00, 3.00, 4.00, 5.00......please note that by the ATC rules, 5.00 is the limit trading lot and if the EA required more multipliers then it kept submitting 5.00, 5.00, 5.00...). I can't complain, it did pretty well like this.

Delta_Long_Open: 0.0040. Percentage variation threshold value. If the price had this percent variation relative to the floor then a long position is open.

Delta_Long_Close: 0.0040. Percentage variation threshold value. If the price had this percent variation relative to the roof then the long position is closed.

Delta_Short_Open: 0.0045. Percentage variation threshold value. If the price had this percent variation relative to the roof then a short position is open.

Delta_Short_Close: 0.0040. Percentage variation threshold value. If the price had this percent variation relative to the floor then the short position is closed.

Position_Period_Long: 12. Minimum number of periods (bars) that a long position must be alive before evaluating a close operation. This value removes "noise" from price volatility and keeps the position away from sensitive false positives.

Position_Period_Short: 24. Minimum number of periods (bars) that a short position must be alive before evaluating a close operation. This value removes "noise" from price volatility and keeps the position away from sensitive false positives. I set this value to double of the long version because I noticed that for the current market at that time, downtrends moved way too faster than uptrends and I needed more data to get a stable trend reversal.

Strategy Tester Report

If you have never participated in an ATC before then I'll explain to you that months previous to the start of the championship, the organizer provides you with a web based backtester for your strategy that is run with championship environment conditions. If you submit your EA, it gets tested once in a day, if you fail the test then you can apply fixes and resubmit the next day, if your EA is within the ATC parameters and pass the test with good profits then it gets accepted to the competition. Even if you succeed you can resubmit another EA version if you think that one will do better, it is the last submission that counts. In all cases you are sent a report after the test is done. This report contains all the tester's parameters, results, orders and deals plus a nice balance chart:

History data is from 2012.01.01 to 2012.08.01 as the ATC rules required the EA to be testable within that time period. Balance ended up with $44 533.31 total net profit.

Here I give you access to the tester report so that you can analyze that data if you are interested:

Download ATC strategy tester report here.

ATC progress

During the championship, I took a couple of screenshots from time to time so I could record the progress and my top position:

Capture from the strategy tester graph in MT5 that was on par with the ATC tester. This screenshot, before the championship, gave me an idea of what to expect from my EA for the current market. I used to follow my EA during the competition by running a parallel EA in MT5 Terminal, where I could make optimizations and forecasting by using other indicators.

And here is the progress from the start of the championship:

Oh! And there was this cool widget that the organizer created and you could embed live ATC rankings and news anywhere on the Internet:

What went wrong

Well, the EA did exactly what it was programmed for. I would say that if something went wrong was timing (and bad luck). When the competition ended, my EA was caught with an open position and with a high Martingale multiplier, jumping my place far back.

Here you can see in the deals history that the EA had a Martingale sequence of up to 6 numbers before the position was auto-closed.

And here you can see each entry in the balance graph mapped to its corresponding deal in the deals history (for the last Martingale sequence). This can be confirmed by going to the "Review" tab, "Trade" section and then hovering the balance graph line with your mouse cursor to popup each entry.

As simulations are not possible, there is no precise nor certified way to know how many bars were required for the Martingale sequence to end with a big profit but I do remember it was a matter of days (like three) to head back to a winning position. Bad timing, bad luck.

How to improve

I could have hardcoded the EA to stop trading after a big profit trade and near the end of the ATC but that was against the rules.

Something I did not do back in the day that today I consider negligence is: Volume analysis. That's right, I did not took volume into consideration. That was a big mistake. Volume (period trade volume) tells a lot and it can be used by a strategy or by a money management system. There is no good price analysis without a volume analysis complement.

More indicators (this goes against my minimalistic nature). I think EAs should be simple, like a couple of indicators, some formulas and finally some magic powder; but I mean nothing like artificial intelligence for an ATC. I kept the EA simple, too much simple. Being out of time before the ATC is not an excuse, I could have implemented a more complex logic by using indicators as a second opinion to know if a position should be opened or closed. But if such strategy would have been successful then the money management system would not have been compatible. In real life trading I have experience using mostly stochastic oscillators, Bollinger bands and RSI.

I wanted to made modifications and optimizations to the source code to see how much would have improved but somehow the EA is not working correctly with the latest build of the platform. Simulations are not possible.


Price analysis + Martingale ended up being an interesting combination, it worked pretty well for up and down trends but it could have been a suicidal strategy for side trends as it could have been trapped in a drawdown spree, until all the balance was lost. Martingale as a money management was "a big bet". It was very risky and do not recommend something like this, better take the time to develop something less profitable but more stable.
As for price analysis, I like its reactive nature to the market, I think it should be the core of every EA and it should be complemented with another formula.

I really enjoyed the ATC2012, there were those epic moments for everyone, like seriously one of the most exciting moments in my life. I enjoy the ATC more than real life manual trading. I love to see what the other participants come up with. I like to see every new invention and crazy idea. And now maybe we'll get hedging capabilities!
I hope next time you are one of those other participants. Don't be afraid of entering the competition, we are all beginners when we talk about EAs. Every EA is so different that when creating one is like start learning all over again. Don't be afraid to experiment with your ideas, my recomendation would be to keep things simple. Be part of this experience, you won't regret it. Trust me, you will have so much fun.

See you in the next ATC!!!