Compare Williams indicator with EMA indicator

 


Hi,

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


Thanks!

 

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 

 

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!

 

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
   SetIndexBuffer(0,WilliamsR1Buffer);
   SetIndexBuffer(1,WilliamsMABuffer);
   SetIndexDrawBegin(0, Period1);
   SetIndexDrawBegin(1, Period1+PeriodMA);
   SetIndexLabel(0, "Williams %R "+(string)Period1);
   SetIndexLabel(1, "Williams MA "+(string)PeriodMA);
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| 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) 
       return(0);
   if(prev_calculated==0)
      limit=rates_total-Period1;
   else
      limit=rates_total-prev_calculated+1;
   
   for(i=limit;i>=0;i--)
     {
       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);
     }
   if(limit>PeriodMA)
      limit-=PeriodMA;
   for(i=limit;i>=0;i--)
     {
      WilliamsMABuffer[i]=iMAOnArray(WilliamsR1Buffer,0,PeriodMA,0,MODE_EMA,i);
     }
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//| CompareDouble function                                           |
//+------------------------------------------------------------------+
bool CompareDouble(double Number1, double Number2)
  {
    bool Compare = NormalizeDouble(Number1 - Number2, 8) == 0;
    return(Compare);
  } 

 

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

Files:
 

Thanks for your effort GumRai.

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

 
eddieTrader:

Thanks for your effort GumRai.

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

Yes
Reason: