Download MetaTrader 5
To add comments, please log in or register
Do you know C++? MQL5 has much in common with it. Just try it yourself!
Pedro Sanz
Pedro Sanz 2015.04.29 07:09 


I am quite new coding with mql4, so I have some doubts that might be basic...

I am trying to write my first EA, and I am doing it step by step.

The fisrt thing I would like to achieve is to compare the current value of a  Williams' Percent Range (16) indicator with an EMA indicator (20) from Williams previous indicator's data.

For that purpose, at the very beginning, I have this piece of code that I have copied and adapted , however, I have some basic questions:

    1. How to set the values of i, since Williams has period 16 and EMA has period 20: could you help me to understand "i"??

    2. The result of the EMA, since it has many values inside the array, if I need to compare the current Williams with the current EMA values, how to do that?, is the curent value array_of_WILL[0] and EMA_of_WILL[0]

for(int i=0; i<limit; i++)
   array_Of_WILL[i] = iWPR(Symbol(),15,16,0);

for(i=0; i<limit; i++)
   EMA_of_WILL[i]   = iMAOnArray(array_Of_WILL,0,20,0,MODE_EMA,i);


Keith Watford
Keith Watford 2015.04.29 07:34  

It may be better to consider writing an indicator that has 2 buffers, the Williams %R and the MA on Array

That way, you can use iCustom to get the values 

Pedro Sanz
Pedro Sanz 2015.04.29 07:55  

Hi GumRai,

Thanks for your answer, could you go a bit further with it?.

When you say "writing an indicator", you mean in the same EA file or in another script, etc.

Then, let's say that I am able to somehow write in, I guess I would have to call it with iCustom, but it does not sem to solve my problem about how to compate current williams and EMA values.....sorry again if I sound basic...

Thanks for the comment!

Keith Watford
Keith Watford 2015.04.29 08:54  

Here is the Williams %R that I modified to include the EMA

//|                                               Williams %R MA.mq4 |
//|                                                           GumRai |
//|                                                             none |
#property copyright "GumRai"
#property link      "none"
#property version   "1.00"
#property strict
#property indicator_separate_window
#property indicator_minimum -100
#property indicator_maximum 0
#property indicator_buffers 2
#property indicator_plots   2
//--- plot WilliamsR1
#property indicator_label1  "WilliamsR1"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- plot WilliamsMA
#property indicator_label2  "WilliamsMA"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrDodgerBlue
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1
#property indicator_level1 -20
#property indicator_level2 -50
#property indicator_level3 -80
//--- input parameters
input int      Period1=16;//Williams Period 
input int      PeriodMA=21;//EMA Period 
//--- indicator buffers
double         WilliamsR1Buffer[];
double         WilliamsMABuffer[];

//| Custom indicator initialization function                         |
int OnInit()
//--- indicator buffers mapping
   SetIndexDrawBegin(0, Period1);
   SetIndexDrawBegin(1, Period1+PeriodMA);
   SetIndexLabel(0, "Williams %R "+(string)Period1);
   SetIndexLabel(1, "Williams MA "+(string)PeriodMA);
//| Custom indicator iteration function                              |
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
   int i, limit;
   if(rates_total <= Period1+PeriodMA) 
       double period_high = High[Highest(NULL, 0, MODE_HIGH, Period1, i)];
       double period_low = Low[Lowest(NULL, 0, MODE_LOW, Period1, i)];      
       if(!CompareDouble((period_high - period_low), 0.0))
           WilliamsR1Buffer[i] = -100*(period_high - Close[i]) / (period_high - period_low);
//--- return value of prev_calculated for next call
//| CompareDouble function                                           |
bool CompareDouble(double Number1, double Number2)
    bool Compare = NormalizeDouble(Number1 - Number2, 8) == 0;


 You can use iCustom calls for the Williams %R and EMA values

Pedro Sanz
Pedro Sanz 2015.04.29 09:50  

Thanks for your effort GumRai.

Are the current values located in the 0 position of the array for williams and EMA?

Keith Watford
Keith Watford 2015.04.29 10:58  

Thanks for your effort GumRai.

Are the current values located in the 0 position of the array for williams and EMA?

To add comments, please log in or register