MQL5 Tutorial

MQL5 TUTORIAL BASICS – 81 SIMPLE SMA BUY TRAILING STOP


Listen Later

In this video, we are going to create an expert advisor that is able to create a a simple buy trailing stop that is based on a simple moving average value, 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 buy 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 provides simplified trading functions afterwards we create an instance of the class ctrade and call it trade. Inside of the ontick function we start with a static double variable called last stop moving average value, we continue by calculating the ask price and the bid price that can be done by using normalize double, symbol info double for the current symbol on the chart and the second parameter is symbol underscore ask or underscore bid, with normalize double and underscore digits we make sure to calculate the right number of digits behind the dot. To have something that we can use for the trailing stop we need to open a test position, so whenever positions total is smaller than one or in other words when we have no open position we use trade dot buy to buy ten micro lot. That will open a new position and that’s when we set the last stop moving average value to zero because for each position we need a new value. Let’s go on and create an array for the moving average, now we want to sort the array from the current candle downwards that is done by using array set as series for our moving average array that we have created here. Now let’s define the properties, we want to use the ima function for the current symbol on the chart and the currently selected period on that chart, we want to use it for five hundred candles starting from the current candle zero, we don’t use a shift value and we use mode underscore sma because this stands for simple moving average and the calculation should be done based on the close price. Afterwards, we use copy buffer to fill our moving average array according to the definition that we have created here for buffer zero, we start with candle zero – that’s the current candle – and we copy the values for three candles and store them in the array. And to get the value that we need we simply have a look at the value for candle one in our moving average array and we assign the value to stop moving average value. Now that we have done that we can check if the stop moving average value is below the current bid price and the second condition will be if the stop moving average value is bigger than the last stop moving average value and if that is the case we want to call a function that is called check sma buy trailing stop, we will pass the ask price and the stop moving average array value as parameters here and afterwards we want to assign the current value for the next function call, so we store the value for stop moving average value in the variable called last stop moving average value. Please remember this was a static variable so it will keep the values inside of this function, well this function doesn’t exist so far, so we need to create it now, the name of the function will be check sma buy trailing stop, we use void because we don’t use a return value here and we pass the ask price and the stop moving average value as parameters. Now let’s use a for loop to go through all the open positions, we use position get symbol to get the symbol for the current position because we need to check if the current symbol on the chart is equal to the position symbol and if this is true we get the position ticket by using the function position get integer position underscore ticket. To get the current stop loss we use position get double and the parameter i...
...more
View all episodesView all episodes
Download on the App Store

MQL5 TutorialBy Raimund Bauer