MQL5 Wizard - Trading Signals Based on Price Cross and Moving Average Indicators - MetaTrader 5 Expert



MQL5 Wizard allows automatic creation of Expert Advisor codes. See Creating a Ready-made Expert Advisor in MQL5 Wizard for details.
Here we will consider a strategy based on price crossing with the Moving Average indicator. This strategy is called "Signal based on price crossing MA" (when automatically creating an EA in the MQL5 Wizard).
Trade signals:
This strategy is implemented in the C-Signal MA category trading strategy course MQL5 standard library (located in MQL5\Include\Expert\Signal\SignalMA.mqh).

Figure 1. Trading signals based on price crossing moving averages
The implementation of the trading strategy is in the C-Signal MA class, which has some protected methods to simplify access to indicator and price values:
Double MA ( Integer Industrial) // Returns the moving average value of the bar double open( integer ind) // returns the opening price of the bar double close( integer ind) // returns the closing price of the bar Double state MA ( Integer Industrial) // Returns a positive value if the average is increasing, a negative value if it is decreasing DOUBLE STATE OPEN( INTEGER INDUSTRIAL) // Returns the difference between the opening price and the moving average double status closed( integer -industrial) // Returns the difference between the closing price and the moving average
1. Open a long position
Conditions for opening a long position (price crosses MA and checks for MA rising):
//+------------------------------------------------------------------+ //|Check the conditions for opening a long position (Buy) | //+------------------------------------------------------------------+ Boolean value CSignalMA::CheckOpenLong( double & price, double & SL, double & TP, date time and validity period) { price = 0.0 ; SL = 0.0 ; tp = 0.0 ; //--- price crosses MA upward and MA increases back (state open( 1 )< 0 && state closed( 1 )> 0 && state MA( 1 )> 0 ); }
2. Long position closing
Long closing conditions (price crosses MA and checks for MA falling):
//+------------------------------------------------------------------+ //|Check closing conditions| //+-----------------------------------------------------------------+ Boolean CSignalMA::CheckCloseLong( double and price) { price = 0.0 ; //--- The price crosses the moving average downward and the moving average returns downward (state open ( 1 )> 0 && state closed ( 1 )< 0 && state MA( 1 )< 0 ); }
3. Open a short position
The conditions for opening a short position are the same as those for closing a long position.
//+------------------------------------------------------------------+ //|Check the conditions for opening a short position (sell) | //+------------------------------------------------------------------+ Boolean value CSignalMA::CheckOpenShort ( double & price, double & SL, double & TP, date time and validity period) { price = 0.0 ; SL = 0.0 ; tp = 0.0 ; //--- the price crosses the moving average upward and the moving average returns downward (state open ( 1 )> 0 && state closed ( 1 )< 0 && state MA( 1 )< 0 ); }
4. Close position
The conditions for closing a short position are the same as those for opening a long position.
//+------------------------------------------------------------------+ //|Check short position closing conditions| //+----------------------------------------------------------------------------------+ Boolean CSignalMA::CheckCloseShort( double and price) { Price = 0.0 ; //--- Price crosses the moving average upward and the moving average returns upward (state open ( 1 ) < 0 && state closed ( 1 ) > 0 && state MA( 1 ) > 0 ); } //+------------------------------------------------------------------+
Create an Expert Advisor using the MQL5 Wizard
To create a trading robot based on a strategy, you need to select the signal property as " Signal based on price crossing with MA " in the "Create ready-made Expert Advisor" option in the MQL5 Wizard :

Figure 2. Select "Signal based on price and MA cross" in the MQL5 Wizard
Next you must specify the required trailing stop algorithm and money and risk management system. The code of the Expert Advisor will be automatically created and you can compile and test it with the Strategy Tester MetaTrader 5 client terminal.
Test results
Let's consider the EA's backtesting of historical data (EURUSD H1, custom period: January 1, 2010 - January 5, 2011, MA_period=12, MA_Shift=0).
When creating the EA, we used a fixed trading volume ( fixed lot size for trading , 0.1) and did not use the trailing stop algorithm ( trailing was not used ).

Figure 3. Historical backtesting results of EA trading (based on price crosses with MA)
Attachment: SignalMA.mqh has the C signal MA class (included in the MQL5 standard library ) located in the MQL5\Include\Expert\Signal folder. The file crossoverma.mq5 contains the code of the Expert Advisor created using the MQL5 Wizard.
Attachment download
📎 signalma.mqh (12.33 KB)
📎 crossoverma.mq5 (6.09 KB)
Source: MQL5 #248
💡 Featured Recommendations
✍️ Latest by the author
- •
- •
- •
- •
- •
- •
📌 Popular topics
- •
- •
- •
- •
- •
- •
- •
- •
🔗 You May Be Interested In
- •
- •
- •
- •
- •
- •