In this video, we are going to create an sar based sell trailing stop, actually, this is an improved version of the previous video, I have confused a few words in the last one so let’s find out how to do that with mql4.
To get started please click on a little button 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 sar sell trailing stop, click on continue, continue and finish.
Now you can delete everything above the on tick function and the to comment lines here, first, we need to create a variable called sar value and inside of the on tick function we want to open a test sell position in case we don’t have an open position.
To check that we want to know if the return value for orders total is zero and now we check if the sar value is above the bid price. I messed that up in the first video, here I was looking for the ask price and the ask price, of course, is relevant for buy orders so if we have no open orders and the current sar value is above the bid price we would use order send to sell ten micro lot.
To calculate the sar value we use the included function isar that comes with mql4 for the current symbol on the chart and the currently selected period on that chart and these values here is what you see if you click on insert, indicators, trend, parabolic sar.
We have a step value of zero point zero two and a max value of zero point two, so let’s click on ok, right-click into the chart, select template, save template and save it as tester dot tpl because this is what we will see in the next backtest.
You can replace the old value, the last parameter here zero is for a shift value, we don’t use that so it is zero and if the current sar value is above the bid price we want to check the sar sell stop.
In the previous video, I accidentally called this the check sar buy stop function, even it was for the sell stop, so let’s do the correct version now.
The name of our customized function will be check sar sell stop, we will use a for loop to go through all the open orders and for each order we find we will use order select, select by position and find out if we have a trade.
Mode trades is for the current trades and the pending orders while mode history would be for historical data, that means for closed and cancelled orders.
Now we want to check if the current order symbol equals the symbol on our chart and we also want to find out if the order type equals op underscore sell or in other words if we have a sell trade and when it is a sell trade we want to know if the order stop loss is bigger than the current sar value or if the order stop loss is zero.
If all those conditions are true we use order modify to modify the current order ticket, we don’t change the order open price but we change the stop loss to the sar value, the order take profit will remain unchanged, we don’t use any expiration and we also don’t need any color and finally we close all the loops here and that’s about it.
Well if you don’t understand what all the code here does or if this was too fast for you, maybe you want to check out one of the other basic videos in this video series or maybe even the premium course might be interesting for you but for now please click on the compile button or press F7, that should work without any errors and if this is the case you can click on the little button here or press F4 to go back to Metatrader.
And in Metatrader we want to click on view, strategy tester or press control and r, please pick the new file simple sar sell trailing stop dot ex4, mark the visual mode here and start a test.
And here’s our little expert advisor at work, we see that the trailing stop is working and in this little video you have learned how to code the sar sell trailing stop for Metatrader 4 and you have coded it yourself with a few lines of mql4 code.