In this video, we are going to create a sell trailing stop for a simple moving average, 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 sma sell trailing stop, click on continue, continue and finish.
Now you can remove everything above the ontick function and we can delete the two comment lines here.
First, we want to create a static double variable for the last stop moving average value and to use a trailing stop we also need to have open positions, so when orders total equals zero that means we have no open positions and that’s when we use order send to open a test position that is ten micro lot, we use op underscore sell to open a sell position and afterwards we set the last stop moving average value to an unrealistic high value of one thousand, so when we call this function the next time you could also use zero here but you would need to implement another condition check, so for this simple example we go with this solution.
Now we can use the integrated ima function for the current symbol on the chart and the currently selected period on that chart to create a simple moving average for five hundred candles, we don’t use a shift value here, we use the parameter mode underscore sma because this stands for simple moving average and we would like to have the results based on the close price for candle one. Actually, this part needs to be outside of this if condition, now we can check if the current stop moving average value is below the last stop moving average value and we also want to know if the current value is bigger than the ask price otherwise it will be hard to set the stop loss and if both conditions are true we want to call a function called check sma sell trailing stop and pass the stop moving average value as a parameter. This function doesn’t exist, so we need to create it in a few seconds but before we do that we assign the current stop moving average value to the variable last stop moving average value because the next time we call this function the current value will be the last value.
Now let’s continue with our new function called check sma sell trailing stop we use void here because we don’t have a return value and we pass the stop moving average value as a parameter.
Afterwards, we use a for loop to go through all the open orders and with order select, we select each position, we want to check if the order symbol is equal to the symbol on the chart, we also want to know if the current order type is a sell order and finally we need to check if the current order stop loss is above the stop moving average value that we have calculated otherwise we won’t change anything and if all those conditions are true we use order modify for the current order ticket, we don’t change the order open price but we do change the stop moving average value for the stop loss value here, we leave the order take profit as it is and we also don’t want to change the value for the expiration or for the color.
Now let’s close the for loop and the function here and that’s about it.
If you have no idea what all the code here does or if this was too fast for you 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, for now, please click on the compile button or press F7, I didn’t get any errors 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 insert, indicators, trend, moving average, please pick a period for five hundred candles, select the method simple for simple sma, apply to the close price, pick a color and click on okay, now let’s zoom into the chart and here is the simple moving average.
Now we right-click, select template, save template and save it as tester dot tpl because this is the template that we are going to see in the strategy tester. Afterwards, you can click on view, strategy tester or press control and r, please pick the new file, simple sma sell trailing stop dot ex4, mark the option for the visual mode here and start a test.
Here we are! This is our first position and it looks like we have a trailing stop for this simple moving average here, so our little expert advisor is working as expected and in this little video you have learned how to create an ea for a simple moving average that is able to trail a stop loss for a sell position and you have coded it yourself with a few lines of mql4 code.