Welcome Forex EA downloads & MT4/MT5 auto-trading resources — EAs, Gold EAs, quant tools and real-world automation.
Sign In Sign Up

MQL5 Wizard - Trading signals based on price crossing with moving averages, confirmed by ADX - MetaTrader 5 Expert

author EAcpu | 5 reads | 0 comments |

MQL5 Wizard - Trade Signals Based on Price Crossover with Moving Average, confirmed by ADX - expert for MetaTrader 5

MQL5 Wizard - Trade Signals Based on Price Crossover with Moving Average, confirmed by ADX - expert for MetaTrader 5

MQL5 Wizard - Trade Signals Based on Price Crossover with Moving Average, confirmed by ADX - expert for MetaTrader 5

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 the crossover of price with the Moving Average indicator, confirmed by the ADX indicator. The strategy is called " ADX Confirmed Signal Based on Price Crossing with MA " (when automatically creating the EA in the MQL5 Wizard).

Trade signals:

The strategy is implemented in the CSignalADX_MA class (it must be placed into terminal_data_folder\MQL5\Include\Expert\Signal\SignalADX-MA.mqh).

MQL5 Wizard - Trade Signals Based on Price Crossover with Moving Average, confirmed by ADX - expert for MetaTrader 5

Figure 1. Trading signal based on price crossing a moving average, confirmed by ADX

The trading strategy is implemented in the CSignalADX_MA class, which has some protected methods to simplify access to indicator and price values:

 Double plus ADX ( integer industry) // Return the value of the bar DI+ line
Double main ADX ( integer industrial) // returns the main line value of the bar
Double minus ADX ( integer industrial) // returns the DI- line value of the bar
Double Exponential Moving Average ( Integer Industrial) // Returns the moving average value of the bar
double close( integer ind) // returns the closing value of the bar
DOUBLE STATUS ADX( INTEGER INDUSTRIAL) // Returns the difference between the DI+ and DI- lines
Double state EMA ( Integer Industrial) // Returns a positive value if the EMA is increasing, a negative value if it is decreasing
double status closed( integer -industrial) // Returns the difference between the closing price and the moving average
This implementation features an additional check for the existence of a trend (using the directional movement indicator). It allows filtering error signals and checking trading conditions using the value of the current (unfinished) bar.


1. Open a long position

Conditions for opening a long position:

//+------------------------------------------------------------------+
//|Check the conditions for opening a long position (Buy) |
//+------------------------------------------------------------------+
Boolean value CSignalADX_MA::CheckOpenLong( double & price, double & SL, double & TP, date time and validity period)
  { //--- Condition 1: Moving Average increases on the current and last completed bar Boolean Buy_Condition_1=(StateEMA( 0 ) > 0 && State EMA( 1 ) > 0 ); //--- Condition 2: The closing price of the last completed bar is above the Moving Average Boolean Buy_Condition_2= (State Close( 1 ) > 0 ); //--- Condition 3: ADX of the current bar Value is greater than the specified minimum value (trend threshold) Boolean Buy_Condition_3=(MainADX( 0 )>m_minimum_ADX); //--- Condition 4: DI+ value of the current bar is greater than DI- Boolean Buy_Condition_4=(StateADX( 0 )> 0 ); //--- Price = 0.0 ;
  sl =m_symbol.Ask()-m_stop_loss*m_adjusted_point;
  tp =m_symbol.Ask()+m_take_profit*m_adjusted_point; //--- Check all conditions and return (Buy_Condition_1 && Buy_Condition_2 && Buy_Condition_3 && Buy_Condition_4);
  }


2. Long position closing

Long position closing conditions:

 //+------------------------------------------------------------------+
//|Check closing conditions| //+-----------------------------------------------------------------+
boolean CSignalADX_MA::CheckCloseLong( double &price)
  { //--- Condition 1: Moving average falls on the current and last completed bar Boolean Sell_Condition_1=(StateEMA( 0 ) < 0 && State EMA( 1 ) < 0 ); //--- Condition 2: The closing price of the completed bar is below the moving average Boolean Sell_Condition_2= (State Close ( 1 ) < 0 ); //--- Condition 3: ADX of the current bar Value is greater than the specified minimum (trend threshold) Boolean Sell_Condition_3=(MainADX( 0 )>m_minimum_ADX); //--- Condition 4: The value of DI- is greater than the DI of the current bar - Boolean Sell_Condition_4 =(StateADX( 0 )< 0 ); //--- Price = 0.0 ; //--- Check all conditions return (Sales_Condition_1 && SALE_CONDITION_2 && SALE_CONDITION_3 && SALE_CONDITION_4);
  }


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 CSignalADX_MA::CheckOpenShort ( double & price, double & SL, double & TP, date time and validity period)
  { //--- Condition 1: Moving average falls on the current and last completed bar Boolean Sell_Condition_1=(StateEMA( 0 ) < 0 && State EMA( 1 ) < 0 ); //--- Condition 2: The closing price of the completed bar is below the moving average Boolean Sell_Condition_2= (State Close ( 1 ) < 0 ); //--- Condition 3: ADX of the current bar The value is greater than the specified minimum (trend threshold) Boolean Sell_Condition_3=(MainADX( 0 )>m_minimum_ADX); //--- Condition 4: The value of DI- is greater than the DI of the current bar - Boolean Sell_Condition_4=(StateADX( 0 )< 0 ); //--- Price = 0.0 ;
  sl =m_symbol.Bid()+m_stop_loss*m_adjusted_point;
  tp =m_symbol.Bid()-m_take_profit*m_adjusted_point; //--- Check all conditions return (sales_condition_1 && sales_condition_2 && sales_condition_3 && sales_condition_4);
  }


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 CSignalADX_MA::CheckCloseShort( double &price)
  { //--- Condition 1: Moving Average increases on the current and last completed bar Boolean Buy_Condition_1=(StateEMA( 0 ) > 0 && State EMA( 1 ) > 0 ); //--- Condition 2: The closing price of the last completed bar is above the Moving Average Boolean Buy_Condition_2= (State Close( 1 ) > 0 ); //--- Condition 3: ADX of the current bar Value is greater than the specified minimum value (trend threshold) Boolean Buy_Condition_3=(MainADX( 0 )>m_minimum_ADX); //--- Condition 4: DI+ value of the current bar is greater than DI- Boolean Buy_Condition_4=(StateADX( 0 )> 0 ); //--- Price = 0.0 ; //--- check all conditions
Return (Buy_Condition_1 && Buy_Condition_2 && Buy_Condition_3 && Buy_Condition_4);
  }

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 " ADX confirmed signal based on price crossing with MA " in the "Create ready-made Expert Advisor" option in the MQL5 Wizard :

MQL5 Wizard - Trade Signals Based on Price Crossover with Moving Average, confirmed by ADX - expert for MetaTrader 5

Figure 2. Select "Signal based on price cross with MA confirmed by ADX" in 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 back testing the Expert Advisor based on historical data (EURUSD H1, test period: January 1, 2010 to January 5, 2011, PeriodADX=33, MinimumADX=22, PeriodMA=39, StopLoss=400, TakeProfit=900).

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 ).

MQL5 Wizard - Trade Signals Based on Price Crossover with Moving Average, confirmed by ADX - expert for MetaTrader 5

Figure 3. Historical backtesting results of EA trading, based on price crosses with MA confirmed by ADX

Attachment: SignalADX-MA.mqh with CSignalADX_MA class must be placed into terminal_data_folder\MQL5\Include\Expert\Signal\.

ma_crossover_adx.mq5 contains the code of the Expert Advisor created using the MQL5 Wizard.


Attachment download

📎 signaladx-ma.mqh (13.12 KB)

📎 ma_crossover_adx.mq5 (6.14 KB)

Source: MQL5 #258

Verification code Refresh