Making a simple EMA cross strategy

What we'll learn:

  • How to build conditions to trigger trading rules
  • How to open new order
  • How to close an open order

Contents:

  • Step 1: Creating new empty strategy
  • Step 2: Defining the trading rule to go Long - Condition part
  • Step 3: Defining the trading rule to go Long - Action part
  • Step 4: Defining the trading rule to go Short
  • Step 5: Saving the strategy as EA for MetaTrader

In this tutorial we'll make a simple strategy that trades when faster EMA crosses slower EMA.

The EMA Cross strategy is a simple, popular strategy of entering the market when faster EMA crosses slower EMA, we enter at the direction fo the crossing. The strategy can work very well on trending market, the results are not very good on choppy markets.

The startegy uses two Exponential Moving Averages on the chart as you can see on the picture above.

The trading rules are:

  • Enter Long when faster EMA crosses above slower EMA
  • Enter Short when faster EMA crosses below slower EMA

There will be no Stop loss or Profit target defined. The strategy will be in a position all the time, just reversing the positions between Long and Short.

Step 1: Creating new empty strategy

Open EA Wizard and click on the New in the top menu of EA wizard and choose Empty strategy. Then switch to Strategy tab and name the new strategy EMA cross.

The strategy consists of trading rules that tell the EA when to buy or sell, when to close the position, etc. Our next step is to define the trading rules for our strategy. Switch to Trading rules tab.

Note

Trading rule is the basic building block of the strategy. Before you start building your strategy you need to know how it should trade - when it should open new trade and when it should close it.

Once you know your trading rules you can easily define them as CONDITION and ACTION. It is simple, when the CONDITION is true, the ACTION is executed.

The CONDITION can be anything, for example CCI > 0, or as in our case EMA(10) crosses above EMA(20).

The ACTION can be opening new order or closing an existing order.

Step 2: Defining the trading rule to go Long - Condition part

First we'll define the rule to go long. Double click on Rule 1 and rename it to Go long.

Then click on Add new on the right side of the screen to open the condition dialog.

Here we have to specify our rule. Remember, we should open long position when faster EMA (with period of 10) crosses above slower EMA (with period of 20).

We'll start with the left part. You'll find EMA listed under Indicators. You can also type "ema" into the search field on the top to search for indicator using its name.

Open the Indicators select box and choose Exponential Moving Average. Now do the same also in for the right part.

We have to set also the operator between the two values, to crosses above.

Note that both indicators (on the left and right side) have their own parameters. We have to change these parameters now to finish our condition.

We want to go long when faster EMA crosses above slower EMA. In our case we'll use period of 10 for faster EMA and period of 20 for slower EMA.

So click inside the period field and change it to 10 for the left EMA and to 20 for the right EMA.

The result will look like on the image below. Click OK and close the dialog.

Step 3: Defining the trading rule to go Long - Action part

We just specified the IF condition, now we have to specify the THEN action - it is the action that will be activated when the condition is true. Click on Add new button in the THEN part.

This will open the Add action dialogue.

First of all we have to close the potential Short position. If you don't understand why, imagine how our EA will be trading - it will be always in position. So it is possible that the EA will have Short order opened when we receive a Long signal. We want to reverse the trade, so we have to first close the Short position and then open a Long one.

Click on the Order management and choose Close position.

Closing an open position is simple, you just have to enter the order Magic Number.

Note

Every order must use its unique Magic Number. The Magic Number helps EA to recognize and handle the order.

We can use any value for Magic Number, as long as it is unique. In this sample I decided to use 100 for Long order and 200 for Short order.

So here we'll be closing order with Magic Number 200, which is a Short order. If the Short order doesn't exists, nothing happens, the action is ignored.

After clicking OK and closing the dialog we can see that we just added the Close position action. Now we'll add one more action to open a new Long position. Click on the Add new again and once more the Add action dialogue is opened.

Here we'll choose Enter at Market and change the Magic Number for Long order to 100.

That's all, we can close the dialog.

We have just defined half of our strategy - a complete rule to go long. The Long rule will be activated when EMA(10) crosses above EMA(20) and will close the (potential) Short order and open a new Long order.

Step 4: Defining the trading rule to go Short

Now we can repeat the whole process also for the Go short rule, but there is an easier way. We can Close the existing rule and just modify it. This way we can save a lot of time and work.

Click on the Clone this rule button.

It will create a copy of our first rule. Let's rename it to Go short.

You can see that the new rule is an exact copy of Go long rule, so we have to change the conditions. Double click on line with condition EMA(10)[1] crosses above EMA(20)[1] in the IF window.

This will open the Edit condition dialog. Because this is rule to go Short, we want to go Short when faster EMA crosses below slower EMA. So we have to change the operator from crosses above to crosses below.

We also have to change the THEN actions. Double click on the Close position in THEN rules. We have to set it to close position with Magic number 100 (which is Long order), so set the Magic number to 100.

We also you have to change the direction of the opening order in the THEN rule from Long to Short. So double click on Enter at market and change the Direction to Short and also Magic number to 200 (remember, we'll use 200 for Short orders).

The strategy is complete. We can recapitulate what we done so far.

We defined a Go long rule that is activated when faster EMA crosses above slower EMA. It first closes the Short position and opens the Long position.

Then we defined Go short rule that is activated when faster EMA crosses below slower EMA. It first closes the Long position and opens a new Short position.

Step 5: Saving the strategy as EA for MetaTrader

Now that we are finished we can save our new strategy as an Expert advisor.

We'll switch to Source code tab and change the Source Code Type to Expert advisor for Metatrader4(.MQ4).

We can immediately see the expert advisor code for the strategy we've just created.

All we have to do now is to save it to MetaTrader4/experts directory and our new EA is ready to be tested or traded live.