A simple guide to code your first EA

Hello everyone, this is going to be a long but easy to follow guide on how to build your first simple Expert Advisor using MetaQuote language in MT4.

[B]Some background:[/B] I started making my own robots in 2014, very simple ones, which traded MA crossovers on EURUSD.
I backtested it, I optimized it, I found out that in only 2 months I could make 2000€ of profit from my 2000€ deposit! Woooow I thought.

I was at work that day but my focus was absolutely elsewhere. Think about it, you have a robot that trades for you 24/5 and can make 100% return over 2 months how wonderful it is! Unfortunately that wasn’t the case. At all.

I deposited my first 200€, traded only 0.01 lots and used my optimized parameters. In 3 weeks I lost almost everything.

I looked at the code, I backtested again my EA (at the time I didn’t know how about curve fitting issues or walking forward optimization!), I tried to optimize my EA again.

Then I changed strategy because I thought that the problem was the strategy I used.

Backtest OK.
Optimization OK.
Live deposit 100€, lost everything in 5 weeks.

One year later, after I studied some programming, some algorithmic trading books on optimizing and testing I tried with my first EA.
This time I used a new approach, the one that we are going to explore in this thread.

I earned the 40% in 2 months which is not bad at all!

I’m at the airport now, leaving for London.

Tomorrow I’ll post the first part of the guide. If you want to learn subscribe to this thread!

Enjoy the ride!

[B]I am going to use MQL4, not MQL5 because of my preferencies.[/B]

Warning!

[B]This isn’t a method to become rich quick and effortless. [/B]
You will have to practice a lot, you are going to program your simple EA, you’re going to learn how to spot market imbalances and how to trade them automatically.

I won’t recommend any software, any robot-that-makes-you-money, anything!

Are you ready to sweat a lot? To put in the required effort?

Welcome aboard my friend.

1 Like

I won’t recommend any software, any robot-that-makes-you-money, anything!

Are you ready to sweat a lot? To put in the required effort?

Welcome aboard my friend.[/QUOTE]

very excited, hope to see your guide soon

[B]Guide summary[/B]

[ol]
[li]How to find a strategy that gives you an edge on the market
[/li][li]Setting the rules of the strategy
[/li][li]Algorithmic robot building blocks
[/li][li]How to backtest your new robot in MT4
[/li][li]Forward test
[/li][li]Demo trade with your robot
[/li][li]Robot live trading
[/li][/ol]

Here’s the summary of this guide. We are going to start tomorrow with “How to find a strategy that fives you an edge on the market”.

[B]Remember that we are going to build a real robot. Not a useless piece of software![/B]

Hope to see you here tomorrow.

[I]Disclaimer: Do not use the software that you are going to build here in live trading because it can cause huge losses of money.[/I]

How to find a strategy that gives you an edge on the market

The title is self explanatory. Here we are going to learn how to research a strategy to beat the market consistently to achieve a win/loss (from now on referred as W/L) ratio of at least 55%.

Achieving such a W/L is going to give us an edge over the market to gain some pips and grow our account. To create a strategy we have to learn how we can beat the market.

Market is built up by human psychology and behavior. Think about it: humans, just like me and you, are making price go higher or lower, right?
So each human and each institution has their own time to trade the market, their own entry and exit rules and their own fears and greed/excitement.

So when everyone saw that British people said “NO, thanks” to Europe, the Brexit had a very large impact on GBP/USD, right? If market is human and humans are organisms that have needs and emotion how can an algorithm exploit the masses behavior?

By finding a Market imbalance. A market imbalance is a repeated behavior that is very similar from day to day, week to week, month to month and so on. Usually it is time bound.

Let’s jump straight into a chart to see if we can spot some repeated behavior on a 5 minute chart of EUR/USD.


In this particular case as you can see, price is below a 200 period SMA with a shift of 3 and when London opens, (my broker is using GMT +3 so London opens at 10 here) price spikes UP until 15:00 when NY Opens. Around 12:30 and 13:30 price reverses. You could make an EA that trades Long when price is above MA and at 12:30 the EA closes the trade and opens a Short order.

Let’s take another 2 or three days to confirm this market imbalance.


The same pattern, again.


Good, another confirm.

Here we found a simple, yet powerful market imbalance on London Open that we could exploit to make some pips, let’s take a look at how many pips we could potentially make.


We are looking at approximately 20 pips of profit with a risk of 8-10 pips (for the stop loss).

So, as we saw, we can find market imbalances in a pretty simple way, by taking a look at the charts (every timeframe), noticing the session openings, the price action and the indicators!

Let’s see how we can make our first EA: a Bollinger’s bands EA trader with a filter to enter and exit the trades.

But let’s first see how we can use the Bollinger’s Bands.


Can you see what is happening here?

The white bullish candle that you see in the circle, closed above the upper band and, in the same moment, the RSI crossed the 50 mid-point.

Let’s see another example.

Continue below…

Looking at the last printscreen, it seems there was a ranging market (consolidation) and where you marked the price with a white circle, price breaks out, as expected cause the bolinger band upper and lower lines are so narrow. RSI was bullish at that time.

Now you open a buy order.

Second hit on upper BB, confirmed no bounce. Finally, breakout was confirmed.

Then, three waves after the marked white circle, the biggest white bullish candle rose. When this closed in combination with RSI overbought, it’s a confirmation that the uptrend may end.

Either close or half your lots and move SL in profit.

What is your opinion about this? Do you agree?

I’m re-reading your explanations, but the two MT4 charts don’t really match in my eyes. Maybe I’m just not seeing the similarities? Where is the imbalance you were talking about? Could you perhaps make this more clear in your printscreen or explanation?

Hope you continue with your guide.

The change in market trading sessions is a good point. Finding a way to take advantage of the session times, or to find a profitable system in a particular system can easily be tested.
That’s the best things about trading robots. You can easily and quickly test any system to see if it is realy profitable.