Chapter 3

Include Indicators

Now I show you how to add indicators in our EA. We will add an RSI with a Moving Average filter (MA). First, we need a global variable to store our indicators value so that we can work with them later. We write this below our global variables MyPoint and MySlippage:
//--- indicators
double RSI;
double MA;
Now we want to create a function which gives us the current price from the indicators and stores them into your defined indicator variables. We write this function again in the custom function area of our file:
// Initialize Indicators
void InitIndicators()
{
   // RSI
   RSI = iRSI(_Symbol,PERIOD_CURRENT,14,PRICE_CLOSE,1);
   
   // Moving Average
   MA = iMA(_Symbol,PERIOD_CURRENT,200,0,MODE_SMA,PRICE_CLOSE,1);
}
You can see that we call the iRSI and iMA function and store these prices in our indicator variables. In both functions, we first insert the symbol and the timeframe we want to receive the data. In our case, it’s the symbol and timeframe (period) we attach the EA on. For the RSI we want the period of 14 calculated with the Close Price. The Moving Average should be a Period 200, Shift 0, a Simple Moving Average and calculated from the ClosePrice. But what’s the 1 at the end? Here we define from which candle we want this values. 0 means the current candle, 1 the previous and so on (like the Close[] or High[]). So in our case, we want the prices from the previous candle. Here is a list of all indicators we can work with in Metatrader 4: http://docs.mql4.com/indicators To make this function work we have of course to call it in the OnTick section. We want to call this function before we calculate the EntrySignals and only if there in not already an open position. We therefore put it here:
   if(TotalOpenOrders() == 0 && IsNewBar() == true)
   { 
      // Initialize Indicators
      InitIndicators();
   
      // Check Buy Entry
      if(BuySignal() == true)
         {
            OpenBuy();
         }
      
      // Check Sell Entry
      else if(SellSignal() == true)
         {
            OpenSell();
         }  
   }
Now we have all the necessary values to create our Entry Logics. What we want to do is to open a Buy order if the RSI is below the Level 30 and the Low of the previous candle is above the Moving Average. Vice versa. Here’s the code:
// Buy Logic
bool BuySignal()
{
   if(RSI <= 30 && Low[1] >= MA)
   {
      return(true);
   }
   else
   {
      return(false);
   }
}


// Sell Logic
bool SellSignal()
{
   if(RSI >= 70 && High[1] <= MA)
   {
      return(true);
   }
   else
   {
      return(false);
   }
}
Voila. The EA work now pretty well. (Test it in the MT4 Strategy Tester)
MQL4 tutorial: RSi MovingAverage EA

Variable Inputs for our Indicators

For now if we want to change lets say the RSI Period from 14 to 20 we have to do this in the source code. Now I show you how you can simply change all parameters of the indicators as a normal input parameter in the Expert Advisor Properties. We want to be able to change the RSI Period, RSI Level, MA Period and the MA Method. We write this below our input parameters:
//--- input parameters
input int            TakeProfit=50;
input int            StopLoss=50;
input double         LotSize=0.1;
input int            Slippage=3;
input int            MagicNumber=5555;

//--- indicator inputs
sinput string        indi = "";                // ------ Indicators -----  
input int            RSI_Period = 14;          // RSI Period
input int            RSI_Level  = 30;          // Above RSI Level
input int            MA_Period  = 200;         // MA Period
input ENUM_MA_METHOD MA_Method  = MODE_SMA;    // MA Method
the string indi is only for a nice looking in the properties. All that is writtern after the // is also for looking good in the properties. It look like this now:
RSI Moving Average EA Properties
Looks nice he? 😉 The method for the Moving Average is even a little drop down menu 🙂 But we are not finished yet. We now have to link these inputs with the indicator and the trade logic. Simply change all like this and you are finished. The Indicators:
  // RSI
   RSI = iRSI(_Symbol,PERIOD_CURRENT,RSI_Period,PRICE_CLOSE,1);
   
   // Moving Average
   MA = iMA(_Symbol,PERIOD_CURRENT,MA_Period,0,MA_Method,PRICE_CLOSE,1);
The Buy Logic:
   if(RSI <= RSI_Level && Low[1] >= MA)
The Sell Logic:
   if(RSI >= 100-RSI_Level && High[1] <= MA)
Please download the file to see the whole source code. Have questions about this mql4 tutorial? Write a comment or open a topic in the forum (if there is not already an answer for it) If you see an error please contact me. This MQL4 tutorial was created on July 23, 2015. Next Chapter Download the source code
4.9/512 ratings

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.