error using custom indicator in EA

 

Hi all, I've found this article about how to add custom indicator to ea. in my code I already create a variable for the input and create the indicator using iCustom function. But when I backtested, it tells me about 4051 error code which is invalid function parameter. Anyone can help me with this error?


double   MA_Period[2] = {9,21};
double   coef = 0.0;
int      MA_Shift = 0;
int      SetPrice = 0;
   
double EMA1 = iCustom(NULL,0,"ema",MA_Period[0],coef,MA_Shift,SetPrice,0,0);
double EMA2 = iCustom(NULL,0,"ema",MA_Period[1],coef,MA_Shift,SetPrice,0,0);
Chapter 5: Adding a custom indicator to our EA
Chapter 5: Adding a custom indicator to our EA
  • quivofx.com
In this chapter, I will show you how you can add a custom indicator to your EAs with the icustom function. We use the free Triple Bollinger Bands indicator, which you can download below. We start with a new EA project in the MetaEditor. I will name it “icustomEA”. Since you already know how to write an EA, I will only use the OnTick function in...
 
Luandre Ezra:

Hi all, I've found this article about how to add custom indicator to ea. in my code I already create a variable for the input and create the indicator using iCustom function. But when I backtested, it tells me about 4051 error code which is invalid function parameter. Anyone can help me with this error?


what is your "ema" indicators inputs?

and is there an indicator in your indicators folder?

 
Ahmet Metin Yilmaz:

what is your "ema" indicators inputs?

and is there an indicator in your indicators folder?

double   MA_Period[2] = {9,21};
double   coef = 0.0;
int      MA_Shift = 0;
int      SetPrice = 0;

That’s the parameter that my custom ema indicator need. I don’t use input cause I want it to be static. I already copy the custom ema indicator into the indicator folder also.

 
Luandre Ezra:

That’s the parameter that my custom ema indicator need. I don’t use input cause I want it to be static. I already copy the custom ema indicator into the indicator folder also.

if you have an indicator in your indicator folder which name is same "ema" , and you want to use its default parameters try this

may be it helps

//+------------------------------------------------------------------+
double   MA_Period1=9;
double   MA_Period2=21;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double EMA1 = iCustom(Symbol(),0,"ema",MA_Period1,0,0);
double EMA2 = iCustom(Symbol(),0,"ema",MA_Period2,0,0);
//+------------------------------------------------------------------+
 
Luandre Ezra:

That’s the parameter that my custom ema indicator need. I don’t use input cause I want it to be static. I already copy the custom ema indicator into the indicator folder also.

Through iCustom you can send parameters only for the indicator variables that have input or external keyword.

If you removed "input"/"external" keywords from your indicator, then you can't send parameters via iCustom.

 
Drazen Penic:

Through iCustom you can send parameters only for the indicator variables that have input or external keyword.

If you removed "input"/"external" keywords from your indicator, then you can't send parameters via iCustom.

So basically I can’t use iCustom function if it’s not input or external keyword? And the error 4051 is related to that?

 
Luandre Ezra:

So basically I can’t use iCustom function if it’s not input or external keyword? And the error 4051 is related to that?

You can use iCustom even If your indicator does not have any input/extern variables, but you can't send parameters other than minimum required by iCustom.


ERR_INVALID_FUNCTION_PARAMVALUE            4051 - you get because you missed parameters by type and count. 

Check that you use correct values for the first 3 and the last two parameters defined in iCustom.

If you don't have input/extern variables in your indicator, you should use only those 5 values in iCustom call.

iCustom - Technical Indicators - MQL4 Reference
iCustom - Technical Indicators - MQL4 Reference
  • docs.mql4.com
[in]  Custom indicator compiled program name, relative to the root indicators directory (MQL4/Indicators/). If the indicator is located in subdirectory, for example, in MQL4/Indicators/ The passed parameters and their order must correspond with the declaration order and the type of extern variables of the custom indicator...
 
Luandre Ezra: it tells me about 4051 error code which is invalid function parameter.
double   MA_Period[2] = {9,21};
double   coef = 0.0;
int      MA_Shift = 0;
int      SetPrice = 0;
   
double EMA1 = iCustom(NULL,0,"ema",MA_Period[0],coef,MA_Shift,SetPrice,0,0);
double EMA2 = iCustom(NULL,0,"ema",MA_Period[1],coef,MA_Shift,SetPrice,0,0);

You were asked to provide the inputs of your ema.mq4. Why didn't you? Why do you want us to guess? I guess that the first input (MA_Period) is an int, not a double.

Reason: