MQL4 BASICS - CREATE YOUR OWN AUTOTRADING EXPERT ADVISORS FOR FREE

How to create a trailing stop loss

One of the handiest tools to have in an auto-trader is a trailing stop.

It works simply on the OrderModify function, combined with whatever condiitons you apply to it.

A trailing stop will continually adjust your stoploss levels to follow the price as it drops or raises.

For example; You place a sell order... The currencies price drops dramatically... Your original stoploss is set far above where your order was placed... Now your trailing stop kicks in and re-modifies your stoploss... It is now below your OrderOpenPrice... The price moves up a bit and floats... then it drops once more... A big drop! Your trailing stoploss catches this and lowers your stoploss even more... Now the price spikes up! Your trailing stoploss kicks in and closes the trade, leaving you will a hefty profit... Minutes later the price has jumped higher than where your trade opened. The trailing stop allowed you to come away with a nice and clean profit.

This is how a trailing stop works. It follows the price and adjusts accordingly.

Step 1:Choose the Symbol set you are working on

Is this EA for EurUsd? Is it Gold? Is it Silver?

Lets pretend it is EurUsd.

Now we go to the specifications for that symbol, in the MarketWatch tab.

We see that the price is at 1.05650

So now we know the number of digits it contains. It has the 1 then 5 decimal places.

Step 2:Creating a condition for the trailing stop

Now we might think that just saying "If price drops a total amount less than .005, from the OrderOpenPrice, then we should readjust our stoploss, however this will result in our stoploss readjusting on every single bar that is below that spot, even if the price is moving back towards the OrderOpenPrice.

So, we have to be tricky. One great technique is to add in the condition that the RSI indicator must also be less than 30. This way you only readjust stoplosses when the RSI is below 30, which is generally a good rule, which means you will have maximized your earning potential.

So to do this we can first figure out our condition...

This above code states that if the trade is identified as magicnumber 1, the RSI is less than 30, and the Bid is less than the OrderOpenPrice minus .005, then it is to modify the stoploss and take profit levels.

"sls" and "tps" are connected to the upper portion of the template code. They stand for StopLossSell and TakeProfitSell.

If you were modifying a buy order, you would use "slb" and "tpb" instead.

If you don't have the template EA, download it free at the MQL4 BASICS page to follow along.

Step 3:Extra Precautions

So maybe you want to make sure the conditions are as tight as possible, you could also create a simple bool.

Up where you have your indicators listed, in your indicators section, you could add in the following code. It can be altered, as this is just an example...

As you can see, we have called these two bangsell and bangbuy. You can call them what ever you want. So now, instead of rewriting all that code again, we can just use those names when we add them into our trailing stops condition.

Our new condition is going to be that bangsell==true .

What it means... is that if Close[1] is less than Close[5], Close[15], Close[45], Close[75], then the statement is "true".

If all of those do not match up, then it is false.

So now, we just add it into our previous condition... like so:

As you can see now, we have some really great conditions in there, to ensure that the Expert Advisor only modifies our Stops at the best possible spots.

Hopefully this helps! If you have any questions, feel free to contact us on our facebook page.

NEXT UP --->

What is a MagicNumber