AI Trading Tutorial - MQL5 Double Exponential Moving Average With Trailing Stops

Опубликовано: 16 Март 2026
на канале: MQL5 Tutorial
224
5

https://mql5tutorial.com/?s=idema

In this video, we are going to create an Expert Advisor that utilizes the Double Exponential Moving Average, or DEMA, to make trading decisions based on price movements. Let's see how we can do that.
To begin, we start Metaeditor by clicking a little icon or pressing F4. This opens the environment where we can write and edit our MQL code.
The first part of our code includes the necessary libraries. We use the include directive to bring in the CTrade class from the standard library. This class provides simplified trading methods, such as PositionOpen and PositionModify, which we will use to manage our trades.
Next, we instantiate a CTrade object named Trade. This object will manage all trade operations, including opening and modifying positions.
We then define our strategy configuration parameters. These parameters are adjustable in the Expert Advisor properties. The LookbackBars parameter is set to 50, which indicates the historical range for highs and lows that we will analyze, allowing us to look back at one to one thousand candles. The MinCandlesBack parameter is set to 5, which specifies the minimum age for a valid high or low, ensuring that we only consider recent price action. The TrailingOffset parameter is set to 10.0, which provides a safety margin for our trailing stop in points, with a range of one to twenty. The DemaPeriod parameter is set to 14, which defines the period for the DEMA calculation, and the DemaPrice parameter is set to PRICE CLOSE, indicating that we will use the closing price for our DEMA calculations.
Following the parameters, we declare global variables and handles. The demaHandle variable will store the DEMA indicator handle, while the demaBuffer array will hold the DEMA values. The priceData array will be used to store the Open, High, Low, and Close price data.
The OnInit function is executed once at the start of the Expert Advisor for indicator setup. Inside this function, we create the DEMA indicator handle using the iDEMA function. This function takes several parameters: the symbol we are trading, the time period we are analyzing, the DEMA period, a shift value of zero, and the price type for the DEMA calculation. We then configure our arrays as time series, ensuring that the newest data is at index zero.
Next, we define the functions for opening positions. The OpenBuyPosition function is responsible for executing a buy order. It retrieves the current ask price using the SymbolInfoDouble function, which provides the ask price for the specified symbol. We then call the PositionOpen method from the Trade object to execute the buy order. The parameters for this method include the symbol, the order type indicating a long position, the volume set to 0.10, which means we are buying 10 micro lots, the entry price at the current ask, and both the initial stop loss and take profit set to zero, indicating that they are disabled. A comment is added to the position for identification.
Similarly, the OpenSellPosition function executes a sell order. It retrieves the current bid price and calls the PositionOpen method with the appropriate parameters for a short position.
The OnTick function is the core of our strategy logic and is executed on every tick. In this function, we first retrieve historical price data for analysis using the CopyRates function. This function takes the symbol, the time period, the starting index, the number of candles to copy, and the array where the data will be stored. We also load the DEMA values from the indicator buffer using the CopyBuffer function, which takes the indicator handle, the buffer index, the starting index, the number of values to copy, and the array for the DEMA values.
Next, we check the status of open positions. We initialize a boolean variable named hasOpenPosition to false and set the currentPositionType to POSITION TYPE BUY as a default value. We then loop through all open positions using the PositionsTotal function to determine if there is an open position for the current symbol. If we find an open position, we set hasOpenPosition to true and update the currentPositionType accordingly.
Now we move on to trend detection and position management. If the closing price of the most recent candle is above the DEMA value, it indicates upward momentum. If there is no open position, we call the OpenBuyPosition function to enter a long trade.