Hi folks,
We talked before about the iCustom function and how can we use it to embed custom indicators in our expert advisors code.
We said that we have to have the .ex4 file of the indicator installed in indicators folder to be able to use iCustom, then all what we need is to call the iCustom function passing to it the name of the .ex4 file, the parameters the indicators use and decide which indicator's line we want to retrieve its value.
The only problem of using iCustom is that you have to distribute the indicator you use with your expert advisor and the user of your expert advisor have to be sure he is using the right version of the indicator.
Today we are going to take another approach of embedding indicators in expert advisors, we are going to rewrite the code of the indicator into our expert advisor and use it as a function which we can call it within the expert advisor.
By this way (writing the code of the indicator in the expert advisor) you can freely distribute only the expert advisor without bothering yourself and your user.
Our indicator is a very simple indicator, yet it will give a good picture of how to embed an indicator in an expert advisor, This is the code of the indicator:
//+------------------------------------------------------------------+
//| SmCCI.mq4 |
//| Copyright © 2006, MetaQuotes Software Corp. |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net"
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Red
//---- input parameters
extern int t3_period=21;
extern double b=0.7;
//---- buffers
double ExtMapBuffer1[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0,ExtMapBuffer1);
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
double e1, e2, e3, e4, e5, e6, c1, c2, c3, c4, n, w1, w2, b2, b3;
double dpo, t3;
b2=b*b;
b3=b2*b;
c1=-b3;
c2=(3*(b2+b3));
c3=-3*(2*b2+b+b3);
c4=(1+3*b+b3+3*b2);
n=t3_period;
if (n<1) n=1;
n = 1 + 0.5*(n-1);
w1 = 2 / (n + 1);
w2 = 1 - w1;
for(int i=Bars; i>=0; i--)
{
dpo=Close[i];
e1 = w1*dpo + w2*e1;
e2 = w1*e1 + w2*e2;
e3 = w1*e2 + w2*e3;
e4 = w1*e3 + w2*e4;
e5 = w1*e4 + w2*e5;
e6 = w1*e5 + w2*e6;
t3 = c1*e6 + c2*e5 + c3*e4 + c4*e3;
ExtMapBuffer1[i]=t3;
}
Print ("SmCCI : " , ExtMapBuffer1[0], ":", ExtMapBuffer1[1]);
return(0);
}
//+------------------------------------------------------------------+
Note: We have added the line:
Print ("SmCCI : " , ExtMapBuffer1[0], ":", ExtMapBuffer1[1]);
Because we want to compare the value returned by the indicator with the value returned by the expert advisor.
Which part of the code above we are going to add to the expert advisor and how and where, these are the questions.
There are two important pieces of codes in any indicator that we have to duplicate them in the expert advisor if we want to get the same values of the indicators:
The most of the indicators enable the user to change the parameters of the indicator by declaring this set of variables with extern keyword.
Besides the external variable the indicator may use global variables - global variables are the variables that has been declared at the top of the indicator code and outside any user custom or special function.
If we want to embed the code of indicator into an expert advisor we have to know how and what kind of global/external variables the indicator use.
The indicator land of battle is inside the start() function, and if we want to embed the indicator code into an expert advisor we have to duplicate the start() block of code into the expert advisor.
How could we use the indicator's start() block of code in our expert advisor? That's what this article for!
Our expert advisor will not do anything more than duplicating the code of the indicator and printing the values of the embedded indicator code.
We are going to convert the indicator to function that we can call it in our expert advisor, let's see how can we do it!
//+------------------------------------------------------------------+
//| Coders Guru |
//| http://www.forex-tsd.com |
//+------------------------------------------------------------------+
#property copyright "Coders Guru"
#property link "http://www.forex-tsd.com"
//+------------------------------------------------------------------
int init()
{
return(0);
}
int deinit()
{
return(0);
}
//+------------------------------------------------------------------+
int start()
{
double trend ;
if(Bars<100) {Print("bars less than 100"); return(0);}
Print ("PipHunter :" , SmCCI(21,0.7,0) , ":" , SmCCI(21,0.7,1));
return(0);
}
//+------------------------------------------------------------------+
double SmCCI (int t3_period , double b,int bar)
{
double e1, e2, e3, e4, e5, e6, c1, c2, c3, c4, n, w1, w2, b2, b3;
double dpo, t3;
b2=b*b;
b3=b2*b;
c1=-b3;
c2=(3*(b2+b3));
c3=-3*(2*b2+b+b3);
c4=(1+3*b+b3+3*b2);
n=t3_period;
if (n<1) n=1;
n = 1 + 0.5*(n-1);
w1 = 2 / (n + 1);
w2 = 1 - w1;
for(int i=Bars; i>=bar; i--)
{
dpo=Close[i];
e1 = w1*dpo + w2*e1;
e2 = w1*e1 + w2*e2;
e3 = w1*e2 + w2*e3;
e4 = w1*e3 + w2*e4;
e5 = w1*e4 + w2*e5;
e6 = w1*e5 + w2*e6;
t3 = c1*e6 + c2*e5 + c3*e4 + c4*e3;
}
return (t3);
}
The trick behind the expert advisor code and all the work is in the SmCCI function.
In the SmCCI function we have used the indicator's external variables (t3_period and b) as parameters for the function.
The third parameter in the function is int bar which we use it as a reference to the bar we want to calculate its indicator value (current bar = 0 and the previous bar = 1 etc).
The indicator's start() block of code is duplicated with very small changes:
1- In the for loop instead looping to 0 we are looping to the bar parameter.
2- The line: ExtMapBuffer1[i]=t3; was removed from the function because we are not in an indicator so we are painting on buffer.
3- The variable t3 is holding the indicator calculation value, so we return it.
The final result of the expert advisor and when we call the SmCCI function like this:
Print ("PipHunter :" , SmCCI(21,0.7,0) , ":" , SmCCI(21,0.7,1));
We are expecting to get the same values of the indicator. And that's what we have got (Figure 1).
I hope you find it a useful lesson and see u!
Coders' Guru