Download MetaTrader 5
To add comments, please log in or register
Do you need a programmer? Make an order to let the developer find you!
Ian Venner
2583
Ian Venner 2014.02.27 02:21 

example function to initialize buffers

So I might want to pass ENUM_INDEXBUFFER_TYPE as INDICATOR_DATA, or as INDICATOR_CALCULATIONS but I cant use it like a variable data type, is there another way of doing this or do I need to learn something in mql5 ?

void initbuffer(double &array[], ENUM_INDEXBUFFER_TYPE, int type=DRAW_NONE, int style=EMPTY,int width=EMPTY)

{
 SetIndexBuffer(Buffers,array,ENUM_INDEXBUFFER_TYPE);
 SetIndexStyle(Buffers, type, style, width); 
 Buffers++
} 
qjol
3243
qjol 2014.02.27 02:44  

INDICATOR_DATA = 0

INDICATOR_COLOR_INDEX = 1

INDICATOR_CALCULATIONS = 2

Ian Venner
2583
Ian Venner 2014.02.27 02:57  

ahh they are ints, nice, thanks qjol .. so are ENUMs a similar thing to a #define ?

qjol
3243
qjol 2014.02.27 03:01  

i think so

i didn't checked all the ENUMs only those 3

Alain Verleyen
Moderator
28669
Alain Verleyen 2014.02.27 11:37  
SDC:

example function to initialize buffers

So I might want to pass ENUM_INDEXBUFFER_TYPE as INDICATOR_DATA, or as INDICATOR_CALCULATIONS but I cant use it like a variable data type, is there another way of doing this or do I need to learn something in mql5 ?


void initbuffer(double &array[], ENUM_INDEXBUFFER_TYPE buffertype, int type=DRAW_NONE, int style=EMPTY,int width=EMPTY)
{
 SetIndexBuffer(Buffers,array,buffertype);
 SetIndexStyle(Buffers, type, style, width); 
 Buffers++;
} 
Ian Venner
2583
Ian Venner 2014.02.27 17:06  

Thanks angevoyaguer it seems to work both ways, I found it also works if you declare it as an int in the function parameters and pass either the integer value or pass it as INDICATOR_DATA, INDICATOR_CALCULATIONS etc in the function call. SetIndexBuffer also accepts either the integer value or the ENUM identifier for the the third parameter.

So I am thinking ENUMs are like #defines, you can use either the formal identifier or the value it represents is that correct ?

void initbuffer(double &array[], int buffertype, int type=DRAW_NONE, int style=EMPTY,int width=EMPTY)
{
 SetIndexBuffer(Buffers,array,buffertype);
 SetIndexStyle(Buffers, type, style, width); 
 Buffers++;
} 

   InitBuffer(drawbuffer1,INDICATOR_DATA,DRAW_LINE);
   InitBuffer(calcbuffer1,INDICATOR_CALCULATIONS);
   InitBuffer(calcbuffer2,INDICATOR_CALCULATIONS);

Either way I like it, no need to simulate extra buffers for calculating anymore, SetIndexBuffer() does it all :)

Alain Verleyen
Moderator
28669
Alain Verleyen 2014.02.27 18:34  
SDC:

Thanks angevoyaguer it seems to work both ways, I found it also works if you declare it as an int in the function parameters and pass either the integer value or pass it as INDICATOR_DATA, INDICATOR_CALCULATIONS etc in the function call. SetIndexBuffer also accepts either the integer value or the ENUM identifier for the the third parameter.

So I am thinking ENUMs are like #defines, you can use either the formal identifier or the value it represents is that correct ?

Either way I like it, no need to simulate extra buffers for calculating anymore, SetIndexBuffer() does it all :)

ENUMs are not exactly as #define :

If you declare a function this way :

void initbuffer(double &array[], ENUM_INDEXBUFFER_TYPE buffertype, int type=DRAW_NONE, int style=EMPTY,int width=EMPTY)

If you try to call it with :

initbuffer(array[],18);

The compiler will catch 18 as a invalid value for this parameter. If you declared it as an int the compiler don't worry. To resume an ENUM is an int, but an int is not an ENUM.

  • Also when you declare an ENUMs values are automatically incremented (or you can set the values yourself).
whroeder1
13644
whroeder1 2014.02.27 18:46  
SDC: So I am thinking ENUMs are like #defines, you can use either the formal identifier or the value it represents is that correct ?
Like #defines except better type checking.
Old mql4 allowed
// double iMA( string symbol, int timeframe, int period, int ma_shift, int ma_method, int applied_price, int shift)
... iMA(NULL,0, -1, 0, PRICE_CLOSE, MODE_SMA, 0);
^TF ^ma_method ^applied_price
New mql4 won't compile
double ima(string symbol, int timeframe, ENUM_TIMEFRAMES ma_period, int ma_shift, ENUM_MA_METHOD ma_method, ENUM_APPLIED_PRICE applied_price, int shift);
... iMA(NULL,0, -1, 0, PRICE_CLOSE, MODE_SMA, 0);
^TF ^ ma_method ^ applied_price
int not ENUM_TIMEFRAMES
^ ENUM_APPLIED_PRICE is not ENUM_MA_METHOD
^ ENUM_MA_METHOD is not ENUM_APPLIED_PRICE
/
To add comments, please log in or register