User Name:  
Forgot Password?
Sign up with

Join now (it's free)

User Name:
First Name:
Last Name:

Building Strategy Conditions with Indicators

The majority of trading systems use indicators to determine trading signals. Metatrader includes over 20 common indicators, including moving average, MACD, RSI, and stochastics. MQL has built in functions for the stock indicators. You can also use custom indicators in your expert advisor. 

Trend Indicators

The moving average is most well known of the trend indicators. It shows whether the price has moved up or down over the indicator period. We have seen how to contruct the conditions of a moving average crossover. Let us examine the conditions of other trend indicators for a strategy entry and exit

If you go to your MT4 console and you click Insert / Indicators / Trend, you get the following list of indicators: 
All of these can be converted into buy and sell conditions within your expert advisors, as you shall see below. 

Note on Usage if you are interested in inserting any of the code in your custom EA, you can copy and paste the extern variables into your extern variables section. Then you copy and paste the indicator calling variables somewhere within your start() function, using my templates as guide, and just below these, you can copy and paste your buy and sell conditions. Alternatively, you download and use the EAs that I have constructed around each indicator. 

Params Average Directional Index
Intent //Buy: +DI line is above -DI line, ADX is more than a certain value
and grows (i.e. trend strengthens)
//Sell: -Dline is above +DI line, ADX is more than a certain value
and grows (i.e. trend strengthens)                                                                    
Extern extern int adx=0; //Indicator period
extern int adu=14; //Period of averaging for index calculation
extern double minadx=20; //Minimal threshold value of ADX
BuyCond if (PosDLine > NegDLine && ADXCurrent >= minadx
&& ADXCurrent > ADXPrevious) 
if (NegDLine > PosDLine && ADXCurrent >= minadx
&& ADXCurrent > ADXPrevious)

Params Bollinger Band
Intent //Buy: price crossed lower line upwards (returned to it from below)
//Sell: price crossed upper line downwards (returned to it from above)
Extern extern int bandp=0; //Indicator period
extern int bandpx=20; //Period of averaging for indicator calculation
extern int banddev=2; //Deviation from the main line

BBLowCurrent=iBands(NULL,bandp, bandpx, banddev,0,PRICE_CLOSE,MODE_LOWER,0);
BBLowPrevious=iBands(NULL,bandp, bandpx, banddev,0,PRICE_CLOSE,MODE_LOWER,1);
BBUpCurrent=iBands(NULL,bandp, bandpx, banddev,0,PRICE_CLOSE,MODE_UPPER,0);
BBUpPrevious=iBands(NULL,bandp, bandpx, banddev,0,PRICE_CLOSE,MODE_UPPER,1);
BBCurrentClose = iClose (NULL, 0,0);
BBPreviousClose = iClose (NULL, 0,1);
if (BBLowPrevious<BBPreviousClose
&& BBLowCurrent>=BBCurrentClose)
if (BBUpPrevious>BBPreviousClose
&& BBUpCurrent<=BBCurrentClose)

Commodity Chanel Index
Intent //Buy: 1. indicator crosses +100 from below upwards. 2. Crossing -100 from below upwards. 3.
//Sell: 1. indicator crosses -100 from above downwards. 2. Crossing +100 downwards. 3.
Extern extern int CCp=0; //Indicator period
extern int CCpx=14; //Period of averaging for indicator calculation
extern int CCLine = 100; 
CCCrossLinePos = CCLine;
CCCrossLineNeg =-CCLine;
if ((CCPrevious<CCCrossLinePos && CCCurrent >= CCCrossLinePos) || (CCPrevious <=CCCrossLineNeg&& CCCurrent>=CCCrossLineNeg) )
SellCond if ((CCPrevious>CCCrossLinePos && CCCurrent <= CCCrossLinePos)||
(CCPrevious >=CCCrossLineNeg&& CCCurrent<=CCCrossLineNeg) )

Parabolic Sar
Note //Buy: Parabolic SAR crosses price downwards
//Sell: Parabolic SAR crosses price upwards                                                      
Extern extern int sar=0; //Indicator period
extern double sarstep=0.02; //Stop level increment
extern double sarstop=0.2; //Maximal stop level
extern int sar2=0; //Price period
sarcurrent = iSAR(NULL,sar,sarstep,sarstop,0);
sarprevious = iSAR(NULL,sar,sarstep,sarstop,1);
closecurrent = iClose(NULL,0,0);
closeprevious = iClose(NULL,0,1);
BuyCond if (sarprevious>closeprevious&&sarcurrent<=closecurrent)
SellCond if (sarprevious<closeprevious&&sarcurrent>=closecurrent)

MA Rising or Falling
Intent //Buy: MA grows
//Sell: MA falls                                                                                                   
Extern extern int maperiod=14; //Period of averaging for indicator calculation
extern int mamode = 1; // Type of moving average, 1= Exponential 
macurrent = iMA(NULL,0,maperiod,0,mamode,0);
maprevious = iMA(NULL,0,maperiod,0,mamode,1);
maprevious2 =iMA(NULL,0,maperiod,0,mamode,2);
BuyCond if(maprevious2 > maprevious && maprevious > macurrent)
SellCond if (maprevious2 < maprevious && maprevious < macurrent)

An e-mail with your verification code has been sent to your e-mail address. Please access your in-box and use the verification button or verification code to complete your registration.
You already have an account linked with this E-mail (it maybe standard or social login). Please, sign in with it.
Please, provide us your e-mail so we can verify your account.