Download MetaTrader 5
To add comments, please log in or register
Find out how social trading can improve your profits
Carl Schreiber
5966
Carl Schreiber 2014.08.11 21:03 

Hi,

first I tried this - which works fine:

...
extern ENUM_MA_METHOD      Ma1_Method = 0;
...

Now I wanted to add (for the tester easy to vary) another not standard moving average, so I wrote:

enum ma {
   SMA =0, // Simple moving average, 
   EMA =1, // Exponential moving average, 
   SMMA=2, // Smoothed moving average, 
   LWMA=3, // Linear weighted moving average.
   DEMA=4  // Double Exponential Moving Average. 
};
...
extern ma                  Ma1_Method = 0;
...
if ( Ma1_Method < 4 ) {
    ma0a = iMA(NULL, 0, Ma1_Period, 0, Ma1_Method, Ma1_Price, shift);
else
    ma0d = iCustom(NULL,0,   "DEMA",Ma1_Period,    0,shift);
...

And this causes the error: "improper enumerator cannot be used" and points to the line of

ma0a = iMA(NULL, 0, Ma1_Period, 0, Ma1_Method, Ma1_Price, shift);

Why do I get this error?

What can I do?

Thanks!

Keith Watford
Moderator
8017
Keith Watford 2014.08.11 22:34  

That is strange.

It seems that the enum is being cast as something other than an integer.

If you insert

   int ma_method=Ma1_Method;

 and then

    ma0a = iMA(NULL, 0, Ma1_Period, 0, ma_method, Ma1_Price, shift);

 

 it will compile.

whroeder1
13656
whroeder1 2014.08.12 01:09  
gooly: Why do I get this error?
  1. extern ma                  Ma1_Method = 0;
    if ( Ma1_Method < 4 ) {
    If you're going to use enumerations, use them, don't use integers.
  2. IMA takes a ENUM_A_METHOD and you are passing a enum ma, so of course you get an error.
  3. Take care and properly cast.
    enum ma {
       SMA =0, // Simple moving average,
       EMA =1, // Exponential moving average,
       SMMA=2, // Smoothed moving average,
       LWMA=3, // Linear weighted moving average.
       DEMA=4  // Double Exponential Moving Average.
    };
    ...
    extern ma Ma1_Method = 0;
    ...
    if ( Ma1_Method < 4 ) {
        ma0a = iMA(NULL, 0, Ma1_Period, 0, Ma1_Method, Ma1_Price, shift);
    else
        ma0d = iCustom(NULL,0, "DEMA", Ma1_Period, 0, shift);
    enum ma {
       SMA =MODE_SMA,  // \
       EMA =MODE_EMA,  //  \ These must match
       SMMA=MODE_SMMA, //  / ENUM_MA for casting
       LWMA=MODE_LWMA, // /
       DEMA  // < just let it choose next available
    };       // You shouldn't care what the value is.
    ...
    extern ma Ma1_Method = SMA; // < Don't mix ints
    ...
    if ( Ma1_Method < DEMA ) {
        ma0a = iMA(NULL, 0, Ma1_Period, 0, ENUM_MA_METHOD(Ma1_Method), Ma1_Price, shift);
           // cast it from ma to actual ENUM_MA_METHOD
    else
        ma0d = iCustom(NULL,0, "DEMA", Ma1_Period, 0, shift);
Carl Schreiber
5966
Carl Schreiber 2014.08.12 05:34  

Thank you, WHRoeder!

What you have written should be part of the Help-Documentation.

Such a nice idea, but what a complicated solution.

Gooly

Carl Schreiber
5966
Carl Schreiber 2014.08.12 06:30  

Well I have another solution:

enum ma {
   SMA =MODE_SMA,  // Simple moving average, 
   EMA =MODE_EMA,  // Exponential moving average, 
   SMMA=MODE_SMMA, // Smoothed moving average, 
   LWMA=MODE_LWMA, // Linear weighted moving average.
   DEMA            // Double Exponential Moving Average. 
};
...
extern ma                  Ma1_Method = SMA;
...
int _Ma1_Meth;
...
int Oninit() {
   _Ma1_Meth = Ma1_Method;
...
}
void OnTick(){
   ma0a = iMA(NULL, 0, Ma1_Period, 0, _Ma1_Meth, Ma1_Price, x);
   ... 
}

Again, enum, a nice idea, but what a complicated realisation.

Keith Watford
Moderator
8017
Keith Watford 2014.08.12 12:14  
gooly:

Well I have another solution:

 Isn't that the exact same solution that I posted earlier?
/
To add comments, please log in or register