video

 

In this video we are going to create a sell trailing stop for this indicator here. This is the envelopes indicator, so let’s find out how to do that with MQl4. To get started, please click on a little icon here or press f4 on your keyboard. Now you should see the MetaEditor window and here you want to click on File, new File, Expert Advisor from Template. Continue.

 

I will call this file Simple Envelopes Sell trailing stop. Click on continue, continue and finish. Now you can delete everything above the Ontick function and let’s also remove the two comment lines here. We start by creating a static variable for the last stop loss value of the envelopes indicator. Static means that this variable is going to hold the values, even when we leave the Ontick function, so that we can use the last stop value when the price changes the next time. And to test a trailing stop, we obviously need to create an open position, so we check if orders total equals zero.

 

If that is the case, that would mean we have no open positions and that’s when we use OrderSend to open a sell trade for ten micro lot. Afterwards we need to reset the last value, because once we open a new position we need to reset the stop loss level. Now let’s calculate the values for the lower band and for the upper band of the indicator. That can be done by using Ienvelopes for the current symbol on the chart and the currently selected period on that chart.

 

We want to calculate the indicator for 14 candles.

 

We use mode underscore SMA, that stands for Simple Moving average. This value is a shift value. We don’t need that, so we set it to zero. The result should be calculated based on the close price. This value here is what you see when you right click on the indicator properties.

 

It’s for 14 candles and the deviation value is 00.10. When you open an empty chart and click on insert, Indicators, trend, envelopes, you will see that this is the default value. Now please click on OK. Here is the indicator, now rightclick into the chart. Select template, save template and save it as tester.tpl.

 

You can override the current one because tester.tpl is what we are going to see in the strategy tester. This parameter here is for the buffer. We have either two for the lower band or one for the upper band. When you mark I envelopes and press F1, you will see the description and when you scroll down and click on this link for indicators line identifiers, you will see here is the Ienvelopes function. Mode_Upper equals one, Mode_Lower equals two and that’s exactly what we are going to use here.

 

The last parameter here is for the number of the candle. We want to calculate the results for candle one. Now we can try to change the stop loss. By using this function, it is called Check Envelopes Sell trailing stop. We will pass a parameter here, the one for the upper band.

 

Currently, this function doesn’t exist, so we need to create it in a few seconds. But before we do that, we want to set the variable for a last stop envelopes value to the current value of the upper band, so we can use this one the next time. That’s it for the Ontick function. So let’s create the one for the Sell trailing stop. The name is Check Envelopes Sell trailing stop.

 

We use void because we don’t need a return type. We pass a double variable called upper band, and now we need to use a for loop to go through all the open orders. We use OrderSelect for each counter value in the for loop to select the order. Afterwards we want to check if the current symbol of the order equals the symbol on the chart. We want to know if the order type of the current order equals Op_Sell, so it is a sell position and only if this is true, we want to check if the current order stop loss is above the value for the upper band. And if this is true, we want to use OrderModify for the current order ticket and the order open price. We want to change the current stop loss to the value for the upper band. The take profit value is unchanged. We have no expiration date and no color here.

 

Finally, let’s close the for loop and the function and that’s about it. Well, if this was too fast for you or if you have no idea what all the code here does, maybe you want to watch one of the other videos in the basic video series, or maybe even the premium course on our website might be interesting for you. That’s also where we have a shop link if you need some source codes or anything else. But for now, please click on the compile button or Press F seven on your keyboard.

 

You shouldn’t get any errors here, and if that is the case, you can click on a little button here or press F4 to go back to MetaTrader. And in MetaTrader, you want to click on View, Strategy Tester or Press Control and R.

 

Please pick the new file simple Envelopes Sell Trailing Stop.ex4, mark the option for the visual mode here and start your test. Here we are. Our expert advisor is running. Now, let’s speed that up a little bit and we should see that the trailing stop is modified when the blue line goes down.

 

We need to find a market situation where we can see it a little bit better like here. So our little expert advisor is working as expected. And in this little video you have learned how to create a sell trailing stop for this envelopes indicator and you have coded it yourself with a few lines of MQL4 code.