MQL5 Tutorial

MQL5 TUTORIAL BASICS – 82 SIMPLE SMA SELL TRAILING STOP


Listen Later

In this video we are going to create an expat advisor for a simple moving average sell trailing stop, so let’s find out how to do that with mql5. 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 delete everything above the ontick function and the two comment lines here. First, we want to include the file trade dot mqh, this one comes with mql5 and it provides trading functions, it also contains the class ctrade, so we are going to create an instance of that class called trade. We start by creating a static variable for the last stop moving average value, now we want to calculate the ask price and the bid price that is done by using symbol info double for the current symbol on the chart, we use either symbol ask or symbol bid and with normalize double and underscore digits we make sure that we calculate the right number of digits behind the dot. To use a stop loss we need to open a test position, we are going to use trade dot sell to do that and the condition is whenever positions total is below one – that means we have no open positions – and that’s when we want to sell ten micro lot. We also want to set the last stop moving average value to a very high value, actually, you could also set it to zero but you would need to add another condition because usually, we are going to check if the current value is lower than the last stop moving average value and that one will never be below zero so let’s go with the high value for now. In the next step, we are going to create an array for the moving average. With array set as series, we are going to sort the array from the current candle downwards and 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 are going to use mode underscore sma as it stands for simple moving average and we want to calculate the results based on the close price. Let’s continue with copy buffer, this one is used to fill the array according to the moving average definition that we have created here, we will grab the values for buffer zero that’s the moving average signal line here, we will start with candle zero – that’s the current candle – and we will copy the values for three candles and store them in the array. And now that we have done that we can calculate the stop moving average value by looking into candle one of our moving average array. The first condition we want to check to move a sell stop loss is if the current stop moving average array is above the ask price otherwise we would not be able to change it. We also want to know if the current stop moving average value is below the last stop moving average value, and if this is true we are going to use a function called check sma sell trailing stop and pass the bid price and the stop moving average value as a parameter. This function doesn’t exist so far, so we need to create it now and please don’t forget to set the last stop moving average value and assign the current stop moving average value because the current one will be the last one the next time we call this function. Our trailing stop function will use void because we don’t need to return anything, the name is check sma sell trailing stop and we pass the bid price and the stop moving average value as parameters here. Now we use a for loop to go through all the open positions, we will count down until there are no other positions left, afterwards we use position get symbol for the current counter of the for loop to get the currency pair symbol for the current position and only if the symbol on the chart matches the currency pair symbol we are going to continue.
...more
View all episodesView all episodes
Download on the App Store

MQL5 TutorialBy Raimund Bauer