how to put custom indicator to ea?

 
hello every one..
i have this indicator, but don't know to input it to ea..
this is my indicator code:
extern int period = 15;
extern int method = 1;
extern int price = 0;
double g_ibuf_88[];
double g_ibuf_92[];
double g_ibuf_96[];

int init() {
IndicatorBuffers(3);
SetIndexBuffer(0, g_ibuf_88);
SetIndexBuffer(1, g_ibuf_92);
SetIndexBuffer(2, g_ibuf_96);
ArraySetAsSeries(g_ibuf_96, TRUE);
SetIndexStyle(0, DRAW_LINE, STYLE_SOLID, 2);
SetIndexStyle(1, DRAW_LINE, STYLE_SOLID, 2);
IndicatorShortName("Slope Direction Line(" + period + ")");
return (0);
}

int deinit() {
return (0);
}

double WMA(int ai_0, int a_period_4) {
return (iMA(NULL, 0, a_period_4, 0, method, price, ai_0));
}

int start() {
double lda_16[];
double lda_20[];
int l_ind_counted_0 = IndicatorCounted();
if (l_ind_counted_0 < 0) return (-1);
int li_4 = 0;
int l_period_8 = MathSqrt(period);
int li_12 = Bars - l_ind_counted_0 + period + 1;
if (li_12 > Bars) li_12 = Bars;
ArrayResize(lda_16, li_12);
ArraySetAsSeries(lda_16, TRUE);
ArrayResize(lda_20, li_12);
ArraySetAsSeries(lda_20, TRUE);
for (li_4 = 0; li_4 < li_12; li_4++) lda_16[li_4] = 2.0 * WMA(li_4, period / 2) - WMA(li_4, period);
for (li_4 = 0; li_4 < li_12 - period; li_4++) g_ibuf_96[li_4] = iMAOnArray(lda_16, 0, l_period_8, 0, method, li_4);
for (li_4 = li_12 - period; li_4 >= 0; li_4--) {
lda_20[li_4] = lda_20[li_4 + 1];
if (g_ibuf_96[li_4] > g_ibuf_96[li_4 + 1]) lda_20[li_4] = 1;
if (g_ibuf_96[li_4] < g_ibuf_96[li_4 + 1]) lda_20[li_4] = -1;
if (lda_20[li_4] > 0.0) {
g_ibuf_88[li_4] = g_ibuf_96[li_4];
if (lda_20[li_4 + 1] < 0.0) g_ibuf_88[li_4 + 1] = g_ibuf_96[li_4 + 1];
g_ibuf_92[li_4] = EMPTY_VALUE;
} else {
if (lda_20[li_4] < 0.0) {
g_ibuf_92[li_4] = g_ibuf_96[li_4];
if (lda_20[li_4 + 1] > 0.0) g_ibuf_92[li_4 + 1] = g_ibuf_96[li_4 + 1];
g_ibuf_88[li_4] = EMPTY_VALUE;
}
}
}
return (0);
}


what about in the ea?
thx..
 

I assume that you have already read the book from beginning to end and understood it since this is essential. For your problem you want to use the iCustom() function. There is no need to decompile the indicator!

To refresh your memory here are the related links:

iCustom() in the book: https://book.mql4.com/samples/shared

iCustom() in the reference manual: https://docs.mql4.com/indicators/iCustom

 

thx for replying..

every i ask in many forums, the answer always the link https://docs.mql4.com/indicators/iCustom

the problem is using formula.. i see in that link, put indicator that have not formula to ea, so more easy..

and in the https://book.mql4.com/samples/shared, he teach to put rocseparate.mq4 indicator to ea, but seems simple only using like this :

double L_1=iCustom(NULL,0,"rocseparate",H,P,B,A,1,0);

maybe the indicator not have many formula.. so can direct using that code...almost same with Ima coding..

but my indicator using array like

double WMA(int ai_0, int a_period_4) {

return (iMA(NULL, 0, a_period_4, 0, method, price, ai_0));


i don'tknow how to put it..

i put in ea, like this :

int ai_0,a_period_4;

double WMA(ai_0, a_period_4)=iCustom(NULL, 0, a_period_4, 0, method, price, ai_0);


but error " '(' - function definition unexpected ....."

if i put the formula from indicator to ea, it will error at WMA..

i don't know how to make it clear..

please help..

thx..

 

First of all, you never assigned a value to ai_0 or a_period_4 . When you pass them to the WMA function (which then passes it to iCustom) you are passing the maximum integer value, which the indicator cannot use. From what I understand ai_0 is the bar shift and a_period_4 is the period length.... try assigning 0 to ai_0 (if you are interested in the current bar's readout) and a valid period length to a_period_4.

Second, the iCustom function requires the index buffer as the second-to-last input and the shift as the last input. You do not seem to be passing an index buffer number at all.

Try this:

int ai_0 = 0, a_period_4=14;
double WMA(ai_0, a_period_4)=iCustom(NULL, 0, a_period_4, 0, method, price, 0, ai_0);
 

As 7bit said, there is no need to decompile an indicator to use it in an EA... iCustom wants the Extern values and the buffer ID. When you load the indicator onto a chart, all the externs are listed in the input tab and (normally) the buffers are listed in order (0 to 7max) in either the colours tab or in the info window. Sometimes it can be trial and error to get the right buffer but it's only 1 of 8 possible digits so doesn't take long. So for your indicator, if you wanted the first buffer it would be something like...

myvalue=iCustom(NULL,0,"[iny name here]",15,1,0,0,0); Green numbers are the extern values listed in order and red the buffer number.

hth

V

 

thx mfurlend, but still error at the line

double WMA(ai_0, a_period_4)=iCustom(NULL, 0, a_period_4, 0, method, price, 0, ai_0);

'(' - function definition unexpected ....."

it's seems the "(" can not used..

my simple code ea like this:



//+------------------------------------------------------------------+
extern double TakeProfit = 50;
extern double StopLose = 40;
extern double Lots = 0.1;
double Pip;
//+------------------------------------------------------------------+

int init()

{

Pip = Point;
if(Digits==3||Digits==5)Pip*=10;
TakeProfit *= Pip;
StopLose *= Pip;

}


int start()
{

int total, ticket;
int ai_0 = 0, a_period_4=14;


double MacdCurrent, MacdPrevious, SignalCurrent,MacdPrevious2,MacdPrevious3,MacdPrevious4,Ma;


double WMA(ai_0, a_period_4)=iCustom(NULL, 0, a_period_4, 0, method, price, 0, ai_0);

Ma=iMA(NULL,0,9,0,MODE_SMA,PRICE_CLOSE,0);


if (High[1]>Ma)

{
ticket=OrderSend(Symbol(),OP_BUY,Lots,NormalizeDouble(Ask,4),3,NormalizeDouble(Ask-StopLose,Digits),NormalizeDouble(Ask+TakeProfit,Digits),0,Green) ;
}

return(0);
}


the Ma is normal indicator, so can be using simple coding..

the WMA is custom indicator, but using array.. so more difficult..

so, what should i do at double WMA(ai_0, a_period_4) ?

thx..

 

viffer, are u mean the custom indicator coding can be passed the parameter to ea without need input custom indicator formula again?

it's means my code be like this:

myvalue=iCustom(NULL,0,"[customindicator.mq4]",15,1,0,0,0);


is it like that or not?

thx,,,

 
hardyyanto:

viffer, are u mean the custom indicator coding can be passed the parameter to ea without need input custom indicator formula again?

it's means my code be like this:

myvalue=iCustom(NULL,0,"[customindicator.mq4]",15,1,0,0,0);


is it like that or not?

thx,,,

Drop the .mq4 and brackets around the indicator name parameter and it's exactly like that. The EA will see the value that the indicator would calculate. You just need to make sure the indicator code is in the correct location (ie the indicators folder) and it's a s simple as that.

V

 

ok, thx very much viffer... you are great...

thx..

may not using .mq4, so can work..

myvalue=iCustom(NULL,0,"customindicator",15,1,0,0,0);


o ya, i have one problem in 2 digit pair...

this is my code :

extern double TakeProfit = 15;
extern double StopLose = 15;
extern double Lots = 0.1;
extern double MACDOpenLevel=3;
extern double MACDCloseLevel=2;
extern double MATrendPeriod=26;
double Pip;
//+------------------------------------------------------------------+

int init()
{
Pip = Point;
if(Digits==3||Digits==5)Pip*=10;
TakeProfit *= Pip;
StopLose *= Pip;
}


//+--------------------------------------------------------- ---------+
int start()
{
int total, ticket;
double MacdCurrent;
MacdCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0);

if(New_Bar()){

if (High[1]>MacdCurrent)
{
ticket=OrderSend(Symbol(),OP_BUY,Lots,NormalizeDouble(Ask,4),3,NormalizeDouble(Ask-StopLose,Digits),NormalizeDouble(Ask+TakeProfit,Digits),0,Green) ;
}
}
return(0);
}



//+------------------------------------------------------------------+
bool New_Bar()
{
static datetime New_Time=0;
if(New_Time!=Time[0]){
New_Time=Time[0];
return(true);
}
return(false);
}



it can work on 4 digit like euro usd, gu..

but test in euro jpy, the sl and tp will be direct jump to more than 100, but my sl and tp only 15..

so, what should i change?

i think maybe the error at line :

int init()
{
Pip = Point;
if(Digits==3||Digits==5)Pip*=10;
TakeProfit *= Pip;
StopLose *= Pip;

}


what should i add or change?

thx..

 

  1. double WMA(int ai_0, int a_period_4) {

    return (iMA(NULL, 0, a_period_4, 0, method, price, ai_0));

    i don'tknow how to put it..

    i put in ea, like this :

    int ai_0,a_period_4;

    double WMA(ai_0, a_period_4)=iCustom(NULL, 0, a_period_4, 0, method, price, ai_0);

    // double WMA(int ai_0, int a_period_4) {
    // return (iMA(NULL, 0, a_period_4, 0, method, price, ai_0));
    
    double WMA(int ai_0, int a_period_4) {
    return (iCustom(NULL, 0, "indicator NAME", a_period_4, method, price, ai_0));
    

  2. int init(){
      Pip = Point;
      if(Digits==3||Digits==5)Pip*=10;
      TakeProfit *= Pip;
      StopLose *= Pip;
    }
    • Don't do that like that. When you change the chart (pair, timeframe, or refresh) the EA will go through a deinit/init sequence. Therefor TakeProfit/StopLose will go to 50, 500, 5000, etc. Either use a seperate variable SL = StopLose * Pip; OrderModify( ..., SL,...) or just use OrderModify(..., StopLose*Pip, ...).
    • Also slippage needs to be modified for 5 digit brokers.
    • In English, the word is StopLoss.
    //++++ These are adjusted for 5 digit brokers.
    double  pips2points,    // slippage      3 pips    3=points        30=points
                    pips2dbl;               // Stoploss 15 pips        0.0015          0.00150
    int             Digits.pips;    // DoubleToStr(dbl/pips2dbl, Digits.pips)
    int init(){
            if (Digits == 5 || Digits == 3){        // Adjust for five (5) digit brokers.
                            pips2dbl        = Point*10; pips2points = 10;   Digits.pips = 1;
            } else {        pips2dbl        = Point;        pips2points =  1;       Digits.pips = 0; }
            // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl
    

    ticket=OrderSend(Symbol(), OP_BUY, Lots, NormalizeDouble(Ask,4), 3, NormalizeDouble(Ask-StopLose,Digits), 
    NormalizeDouble(Ask+TakeProfit,Digits), 0, Green) ;
    You don't have to normalize Ask or Bid. The hard coded 4 should be Digits. The hard coded 3 is slippage 3 pips must be adjusted.
 

ok, thx roeder..

i am testing my ea again, suddendly sl and tp can work well on euro jpy,

yesterday i am testing on ej, always invalid price or error send 138..

but now suddendly work..

hehe..

ok, thx for all help..

it's seems in this forum that have very good programmer..

:)

Reason: