Learning how to code in MQL4. Part 1 – Our Strategy


Ok traders, this is a bit of a new one for me, but today we’re going to get cracking with our first lesson in MQL4 coding for MetaTrader 4.

As I’m learning at the same time as I’m writing this, we’ll be starting off with the very basics and hopefully (successfully) progress through to developing a custom indicator or EA.

First and foremost, we need to know what we’ll be coding, so let’s get stuck into developing our strategy.

The Strategy we’ll be Coding

The strategy I’ll be looking to code is a relatively simple price action system based on a candle pattern occurring in the direction of the trend.

So let’s go over the nuts & bolts of our strategy.

First, we’re going to establish the rules of our candle pattern. For this exercise we’re using bullish and bearing engulfing candles that occur in the direction of the current market trend.

So what is an engulfing candle? An engulfing pattern is a candlestick chart pattern that forms when the body of a candle completely overlaps or engulfs the body of the prior candlestick. Since we’ll be only trading in the direction of the overall prevailing trend, so we’ll need a method of defining whether the trend is bullish or bearish.

Determining the Trend

So, I want this system to maintain its simplicity so to determine our trend we’re simply going to use an MA. We’ll create a custom input for this to set the period and type of MA, although for now, let’s assume that it’s the 30EMA which will mean that we only trade bullish engulfing patterns when price is above our 100 SMA and only take short entries when price is below.

Our Entry, Stop Loss & Take Profit Rules

For our first version of this coded system (and given me non-existent coding skills), we’ll keep our trading rules quite simple and potentially refine as we move forward.

So, the very first thing I like to do when trading, is define my stop loss. For this system I’ll be forward testing two different stop loss methods. The first will be arbitrarily placed beneath/above the high/low of the signal candle contingent on trend direction, and the second will be above/below the most recent swing high/low, again contingent on trend direction.

While both have their merits, I anticipate that the first option will get hit more often, however the loss value will be significantly lower.

Our entry will simply be placed after the close of the signal candle on the open of the new candle. Again, I’ll test a second entry method whereby a buy/sell stop order is placed at the high/low of the signal candle.

Our profit areas will be determined in one of two methods also. The first being an arbitrary 2R level, so the TP will be twice that of our SL. Secondarily, we will stagger our exits, taking half profit at 1R and then the remainder of the trade can either be closed at a significant S&R level, or simply at 2R, or even higher.

At this stage the entries, SL & TP will be placed manually so they won’t be coded into this version of the indicator.

What’s it Look Like?

This is a basic illustration of the setup I’ll be attempting to code.

Bullish Engulfing Setup

In this particular example, we’re looking at a bullish setup. Note our two criteria have been met, and I’ve adjusted the Fibonacci indicator to indicate our R-Ratios. As you can see, a position was taken at the open of the candle after the signal candle.

We’re going to leave it here for now, and start getting our hands dirty with some code in MT4 in the next lesson.

Pembukaan Akun Cepat & Mudah

  • Daftar

    Pilih tipe akun dan ajukan aplikasi pendaftaran Anda.

  • Danai

    Danai akun Anda dengan menggunakan beragam cara pendanaan.

  • Perdagangan

    Akses 300+ instrumen pada semua kelas aset di MT4/MT5