https://mql5tutorial.com/?s=trailing+...
In this video, we are going to create an Expert Advisor that uses a simple moving average to manage trailing stops for sell trades. Let's see how we can do that.
We start by opening Metaeditor by clicking on the little icon or pressing F4. The code begins by including the Trade library, which provides trading functions. We then create an instance of the CTrade class named trade, which will be used to execute trading operations.
The OnTick function is the main function that runs every time there is a price change. Inside this function, we declare a static variable named LastStopMovingAverageValue to store the last moving average value used for the stop loss.
We calculate the Ask price by using the SymbolInfoDouble function with the SYMBOL_ASK parameter and normalize it to the number of digits for the current symbol. Similarly, we calculate the Bid price using the SYMBOL_BID parameter.
The code checks if there are no open positions by using the PositionsTotal function. If there are no positions, it executes a sell trade of 0.10 lots, which is equivalent to 10 micro lots, at the Bid price. The trade has a take profit set 150 points below the Bid price. After executing the trade, the LastStopMovingAverageValue is reset to zero.
Next, we create an array named myMovingAverageArray to store moving average values. The array is sorted from the current candle downwards using the ArraySetAsSeries function.
We define the moving average by calling the iMA function with parameters for the symbol, period, 500 as the moving average period, zero shift, MODE_SMA for the simple moving average, and PRICE_CLOSE for the closing price. We then copy the moving average values into the array for three candles starting from the current candle using the CopyBuffer function.
We calculate the moving average value for candle one and store it in the variable StopMovingAverageValue. If this value is above the Ask price and either below the LastStopMovingAverageValue or if the LastStopMovingAverageValue is zero, we call the CheckSMASellTrailingStop function with the Bid price and the StopMovingAverageValue.
The CheckSMASellTrailingStop function iterates through all open positions. For each position, it retrieves the symbol and checks if it matches the current symbol. If it does, it gets the ticket number and the current stop loss value. If the current stop loss is above the moving average value or if it is zero, the stop loss is moved to the moving average value using the trade.PositionModify function.
Finally, we press F7 to compile the code. If this was too fast for you or if you don't understand what all the code is doing, you may want to check out the Premium course on our website or watch one of the basic videos first. If everything works, we go back to MetaTrader by pressing F4 or clicking on the icon.
Back in MetaTrader, we press Control and R to start the strategy tester, pick the Expert Advisor that we have just created, enable the visual mode, and start a strategy test. We should see the Expert Advisor on the chart.
If you are already a Premium course member and have an idea for a video like this one, you can send us an email. In this video, we have learned how to create an Expert Advisor that manages trailing stops for sell trades using a simple moving average with just a few lines of MQL code. Thanks for watching, and I will see you in the next video.
Смотрите видео MQL5 TUTORIAL - SMA SELL TRAILING STOP explained онлайн без регистрации, длительностью часов минут секунд в хорошем качестве. Это видео добавил пользователь MQL5 Tutorial 31 Октябрь 2024, не забудьте поделиться им ссылкой с друзьями и знакомыми, на нашем сайте его посмотрели 221 раз и оно понравилось 11 людям.