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

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

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 break and retrace, or pull back strategy.

So let’s go over our Break & Retrace strategy.

The first step in our Break & Retrace strategy is determining what constitutes as a breakout. Now, as you know breakouts come in all shapes and sizes, and there are several methodologies behind this too. For our system, we’ll be using the break of a trendline. Also, 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

Break & Retrace Details

I’m using a break & retrace strategy simply because I believe that trading the pull back is much higher probability than entering when the initial breakout is extended. Also, trading the pullback offers much lower risk as we’ll be entering at a key area of price action that has shown previous support or resistance characteristics. Ideally, what we’re looking for is a large move in our favour after a brief pull back.

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, although for now, let’s assume that it’s the 100 SMA which will mean that we only trade long when price is above our 100 SMA and only short when price is below.

As my coding skills are at this point non-existent, I’ll wait until I have the structure of a system coded before figuring out how to determine a non-trending market.

Defining our Breakout

To define our breakout, I’ll simply be using trendlines. We can choose whether these trendlines will be diagonal, horizontal, or both, and we’ll be defining a valid trendline as one that has AT LEAST 2 proven rejections by price. We will be determining these swing highs and lows that form our trendlines either by fractals or zig-zag, with a slight % buffer in regard to our horizontal levels.

Defining our Retrace

Our pull back or retrace will be determined by price pulling back to our breakout area. The pullback will be invalidated should price retrace beyond the swing prior to the breakout and we will be looking for confirmation that price is looking to resume movement in the direction of the breakout.

At this stage, we’ll be confirming the resumption of trend through candle patterns as well as a rejection of our breakout trendline.

Our Entry, SL & TP 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 we’ll be testing two options: Our stop just outside the high/low of the prior to the breakout; and, our stop just outside of the pullback rejection.

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

Our entry will be defined by a confirmed rejection of the breakout area through candle patterns (pin bars, engulfing, etc.)

What’s it Look Like?

This is a basic illustration of the setups we’ll be attempting to code.

As you can see the resistance area is tried and tested and as anticipated, after the breakout price retraces back to our break out zone.

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

Source: Vantage FX Blog