Calling SetIndexStyle() doesn't work from init() when the code is recompiled - a bug?

 

What I noticed that if an indicator is recompiled while still being attached to a chart, the line width specified via SetIndexStyle() is ignored inside init() and the default width is used (1). Here is the code, you can try it yourself:


#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Red

double buf0[];

int init() {
SetIndexBuffer(0, buf0);
SetIndexStyle(0, DRAW_LINE, EMPTY, 2); // <- line width is 2
return(0);
}

int start() {
int countedBars = IndicatorCounted();

if (countedBars == 0) {
}

int firstIndex = Bars - countedBars - 1;

for(int i = firstIndex; i >= 0; i--) {
buf0[i] = Low[i];
}

return(0);
}


However if we move SetIndexStyle() to start() and have it called on the first run (when all bars are uncounted) then it works.


#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Red

double buf0[];

int init() {
return(0);
}

int start() {
int countedBars = IndicatorCounted();

if (countedBars == 0) {
SetIndexBuffer(0, buf0);
SetIndexStyle(0, DRAW_LINE, EMPTY, 2);
}

int firstIndex = Bars - countedBars - 1;

for(int i = firstIndex; i >= 0; i--) {
buf0[i] = Low[i];
}

return(0);
}


Is this a bug? I verified that the init() is being called when the code gets recompiled.


Lukasz

 

Lukasz,


That always seemed like a bug to me. In case you're interested, here's how I work around it.


Expanding on your first example:


#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Red
#property indicator_style1 0 // <- set the style here
#property indicator_width1 2 // <- set the width here

double buf0[];

int init() {
SetIndexBuffer(0, buf0);
SetIndexStyle(0, DRAW_LINE); // <- set the draw_type here
return(0);
}

int start() {
int countedBars = IndicatorCounted();

if (countedBars == 0) {
}

int firstIndex = Bars - countedBars - 1;

for(int i = firstIndex; i >= 0; i--) {
buf0[i] = Low[i];
}

return(0);
}


Cheers,

Raider

 
Raider:

Lukasz,


That always seemed like a bug to me. In case you're interested, here's how I work around it.


Expanding on your first example:


#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Red
#property indicator_style1 0 // <- set the style here
#property indicator_width1 2 // <- set the width here

double buf0[];

int init() {
SetIndexBuffer(0, buf0);
SetIndexStyle(0, DRAW_LINE); // <- set the draw_type here
return(0);
}

int start() {
int countedBars = IndicatorCounted();

if (countedBars == 0) {
}

int firstIndex = Bars - countedBars - 1;

for(int i = firstIndex; i >= 0; i--) {
buf0[i] = Low[i];
}

return(0);
}


Cheers,

Raider





Yes, I just encountered this phenom (bug)..... was changing the color of a signal a bar in the chart and found that it was being ignored the first time through the Init. However by opening the params for the Indi and making any change, it would work imediately. I no longer do any real logic in my Inits.