mt4 a Lucky Bag?

 

Hi,

I wrote an indicator that just has to show some global variables.

So I deleted all the stuff of indicatorCounted() in start(), and therefore I want to give the indi.-buffers some initial values in init():

int init()  {

   SetIndexBuffer(0,xBuff1e);
   SetIndexBuffer(1,xBuff1w);
   SetIndexBuffer(2,xBuff1s);
   
   SetIndexBuffer(3,xBuff2e);
   SetIndexBuffer(4,xBuff2w);
   SetIndexBuffer(5,xBuff2s);


   SetIndexStyle(0,DRAW_LINE,STYLE_DOT);
   SetIndexStyle(1,DRAW_LINE,STYLE_DOT);
   SetIndexStyle(2,DRAW_LINE,STYLE_DOT);
   SetIndexStyle(3,DRAW_LINE,STYLE_DASH);
   SetIndexStyle(4,DRAW_LINE,STYLE_DASH);
   SetIndexStyle(5,DRAW_LINE,STYLE_DASH);
   
   sGlbVarEqu1 = "GlbVar E "+p1;
   sGlbVarWor1 = "GlbVar W "+p1;
   sGlbVarSwi1 = "GlbVar S "+p1;
   sGlbVarEqu2 = "GlbVar E "+p2;
   sGlbVarWor2 = "GlbVar W "+p2;
   sGlbVarSwi2 = "GlbVar S "+p2;

   int i=1500;
   while(i>0) {i--;
           xBuff1e[i] = GlobalVariableGet( sGlbVarEqu1 );
           xBuff1w[i] = GlobalVariableGet( sGlbVarWor1 );
           xBuff1s[i] = GlobalVariableGet( sGlbVarSwi1 );

           xBuff2e[i] = GlobalVariableGet( sGlbVarEqu2 );
           xBuff2w[i] = GlobalVariableGet( sGlbVarWor2 );
           xBuff2s[i] = GlobalVariableGet( sGlbVarSwi2 );
   }       

   Comment("Waiting for the next tick..");
   return(0);
}

int start(){
     ...
     Comment("start() ..");
     return(0);
}

Well the buffers aren't 'filled' if I apply this indicator new to a chart??? Only after I open the edit-option-window of this indicator and press OK - then the values are set?

Even the Comment("Waiting for the next tick..") isn't shown - during the whole Sunday while the markets were closed!

But despite there was no tick (it was Sunday afternoon, about 4pm GMT!!) the Comment from start() was shown.

Somehow it is not what I expect. :(

I expect init() to be executed at the start of the indicator(), after: applying, change of options, compiling.

and start() to be executed after a tick..

But somehow it was mixed up??

gooly

 
gooly:

Hi,

I wrote an indicator that just has to show some global variables.

So I deleted all the stuff of indicatorCounted() in start(), and therefore I want to give the indi.-buffers some initial values in init():

Well the buffers aren't 'filled' if I apply this indicator new to a chart??? Only after I open the edit-option-window of this indicator and press OK - then the values are set?

Even the Comment("Waiting for the next tick..") isn't shown - during the whole Sunday while the markets were closed!

But despite there was no tick (it was Sunday afternoon, about 4pm GMT!!) the Comment from start() was shown.

Somehow it is not what I expect. :(

I expect init() to be executed at the start of the indicator(), after: applying, change of options, compiling.

and start() to be executed after a tick..

But somehow it was mixed up??

gooly

Did you create the global variables?

Do you know that new comments overwrite the old comments?

Have you tried using Print() instead of Comment()?

Have you tried including the deinit() and putting Print() in there?

 

1) Well the global variables exist! As soon as I open the option-window and close with ok the buffers are filled and showen - but not before that.

2) I know old comments are overwritten, but there is no tick so why and how can the Comment of init() been overwritten by start().

My problem of understanding is:

A) why init() seems not to by started if the indicator is installed - only after I edit the options => shown by not drawn buffers

B) why start() is started yesterday, Sunday, while the markets are closed and the chart hasn't got any tick?

add to A) the while loop is not carried out - or may be the buffers are re initialized by whatever and the result of the loop was 'emptied' again, but where and why?

This behaviour would be hard to understand.

gooly

 
gooly:

1) Well the global variables exist! As soon as I open the option-window and close with ok the buffers are filled and showen - but not before that.

2) I know old comments are overwritten, but there is no tick so why and how can the Comment of init() been overwritten by start().

My problem of understanding is:

A) why init() seems not to by started if the indicator is installed - only after I edit the options => shown by not drawn buffers

B) why start() is started yesterday, Sunday, while the markets are closed and the chart hasn't got any tick?

add to A) the while loop is not carried out - or may be the buffers are re initialized by whatever and the result of the loop was 'emptied' again, but where and why?

This behaviour would be hard to understand.

gooly

B) The start() is always ran Once when you attach an indicator to the chart (even if there are no ticks). This would overwrite your comment.

A) The init function must return quickly. Its not the place for doing huge loops. Maybe thats part of your problem. I'm sure the while loop would execute. Either its not shown in the buffer windows until ticks ... or your loop is too long.

 

B) start() ist started on installation without the presence of a tick only by indicators or an EAs too?

A) init() should be the perfect function for initializations in start I would need extra semaphores to be asked again and again and again..

But thanks ubzen!

gooly

 
gooly:

B) start() ist started on installation without the presence of a tick only by indicators or an EAs too?

A) init() should be the perfect function for initializations in start I would need extra semaphores to be asked again and again and again..

But thanks ubzen!

gooly

B) Only happens with Indicators

A) Move your While loop into the start(). I just tested it and it doesn't like to assign buffers values (during the 1st Attach to chart).

 

Here this works allot better.

#property indicator_chart_window
#property indicator_buffers 1

double xBuff1e[];

int init(){
   SetIndexBuffer(0,xBuff1e);
   return(0);
}

int start(){
    static bool Once; if(Once)  return(0);
    if(!Once){InitializeMyBuffer();}
    Print("Start().................");
    Once=true;                  return(0);
}

void InitializeMyBuffer(){
   int i=5;
   while(i>0) {i--;
            double dValue=GlobalVariableGet("sGlbVarEqu1");
            Print("dValue=",dValue);
            xBuff1e[i] = dValue;
            Print("i=",i,"__xBuff1e[i]=",xBuff1e[i]);
   }       
   Print("Waiting for the next tick..");
}

 

Thank you!

But to find a work around was not my problem, in fact I want to avoid this work around and I wanted to understand it as this behaviour is not what you expect from a function called init().

All the Best for 2014,

Gooly

 
Init tells mq4 about the buffers. mq4 hasn't yet allocated and initialized them. You must wait for start.
Reason: