Need some help with a bug in this indicator

 

Hi

i m coding this simple custom indicator. But i ve got a bug.

When i first attach the indicator to the chart it works great. Then i let it run for a few minutes. After that i change timeframe and then i switch back to the previous timeframe i came from (eg. M1->M5->M1). By doing that the indicator looks totally different.

let me attach some snapshots:

first i attach the indicator on M1

then i let it run for a few minutes

and then i switch from M1 timeframe to M5 timeframe and back to M1 timeframe again. The indicator has totally changed:

here is the code:

#property indicator_level1 0

#property indicator_separate_window

#property indicator_buffers 1 // not 2

#property indicator_color1 White

double Buf_0[];

// --- parameters

extern int history = 180;

extern bool log_on = false;

extern int MAperiod = 14;

string build=" rap4";

bool isHistoryLoading;

int lastperiod=0;

int init()

{

isHistoryLoading = true;

SetIndexStyle(0,DRAW_LINE,DRAW_SECTION,2);

SetIndexBuffer(0,Buf_0);

SetIndexLabel (0,"MA");

IndicatorShortName("MA ("+MAperiod+") "+build);

SetLevelValue (0, 0.0000); // The horizontal line level is set

return(0);

}

int deinit()

{

return(0);

}

int start()

{

double LARM=0 , ASB=0;

double O,C,H,L;

datetime dt,mdt,dt1,dtTemp;

int i, j, k, candles, counted_bars;

double Buffer[10000];

int LARMCandles; // RaptorUK

if (isHistoryLoading)

{

dt=iTime(Symbol(),PERIOD_M1,0);

if (dt == 0)

{

isHistoryLoading = true;

return;

}

isHistoryLoading = false;

counted_bars = 0;

}

else

counted_bars = IndicatorCounted();

if (candles < 0)

return (0);

candles = Bars - counted_bars; // Index of the first uncounted

if (candles > history - 1) // If too many bars ..

{

candles = history-1; // ..calculate specified amount

}

if (candles < MAperiod) LARMCandles = MAperiod;

else LARMCandles = candles;

i=1;

while ( i <= LARMCandles )

{

L = Low;

C = Close;

LARM = LARM + C - L;

Buffer= LARM; //store in a buffer for use in the second while loop

i++;

LARM=0;

}

int vi=1;

double sum=0.0;

log("candles:"+candles);

while ( vi <=candles) //CALCULATE MA

{

for (int vk=vi; vk<vi+MAperiod; vk++) // vi+MAperiod einai swsto? mipws 8elei -1 'h mipws 8elei <=

{

sum=sum+Buffer[vk];

} //for

sum=sum / MAperiod;

Buf_0[vi]= sum;//;

sum=0;

vi++;

}//while

return(0);

}

Any ideas on how i could solve this?

Thanks

Files:
01.gif  15 kb
02.gif  16 kb
03.gif  16 kb
 

Try it out like this :

#property indicator_separate_window

#property indicator_buffers 1

#property indicator_color1 White

#property indicator_width1 2

#property indicator_level1 0

extern int MAperiod = 14;

double Buf_0[];

double Buffer[];

int init()

{

IndicatorBuffers(2);

SetIndexBuffer(0,Buf_0); SetIndexLabel (0,"MA");

SetIndexBuffer(1,Buffer);

IndicatorShortName("MA ("+MAperiod+")");

return(0);

}

int deinit()

{

return(0);

}

//

//

//

//

//

int start()

{

int counted_bars=IndicatorCounted();

if(counted_bars < 0) return(-1);

if(counted_bars > 0) counted_bars--;

int limit = MathMin(Bars-counted_bars,Bars-1);

for(int i = limit; i >= 0; i--)

{

Buf_0 = 0;

Buffer = Close-Low;

for (int vk=0; vk<MAperiod; vk++) Buf_0+=Buffer[vk+i];

Buf_0/=MAperiod;

}

return(0);

}
athan00:
Hi

i m coding this simple custom indicator. But i ve got a bug.

When i first attach the indicator to the chart it works great. Then i let it run for a few minutes. After that i change timeframe and then i switch back to the previous timeframe i came from (eg. M1->M5->M1). By doing that the indicator looks totally different.

let me attach some snapshots:

first i attach the indicator on M1

then i let it run for a few minutes

and then i switch from M1 timeframe to M5 timeframe and back to M1 timeframe again. The indicator has totally changed:

here is the code:

#property indicator_level1 0

#property indicator_separate_window

#property indicator_buffers 1 // not 2

#property indicator_color1 White

double Buf_0[];

// --- parameters

extern int history = 180;

extern bool log_on = false;

extern int MAperiod = 14;

string build=" rap4";

bool isHistoryLoading;

int lastperiod=0;

int init()

{

isHistoryLoading = true;

SetIndexStyle(0,DRAW_LINE,DRAW_SECTION,2);

SetIndexBuffer(0,Buf_0);

SetIndexLabel (0,"MA");

IndicatorShortName("MA ("+MAperiod+") "+build);

SetLevelValue (0, 0.0000); // The horizontal line level is set

return(0);

}

int deinit()

{

return(0);

}

int start()

{

double LARM=0 , ASB=0;

double O,C,H,L;

datetime dt,mdt,dt1,dtTemp;

int i, j, k, candles, counted_bars;

double Buffer[10000];

int LARMCandles; // RaptorUK

if (isHistoryLoading)

{

dt=iTime(Symbol(),PERIOD_M1,0);

if (dt == 0)

{

isHistoryLoading = true;

return;

}

isHistoryLoading = false;

counted_bars = 0;

}

else

counted_bars = IndicatorCounted();

if (candles < 0)

return (0);

candles = Bars - counted_bars; // Index of the first uncounted

if (candles > history - 1) // If too many bars ..

{

candles = history-1; // ..calculate specified amount

}

if (candles < MAperiod) LARMCandles = MAperiod;

else LARMCandles = candles;

i=1;

while ( i <= LARMCandles )

{

L = Low;

C = Close;

LARM = LARM + C - L;

Buffer= LARM; //store in a buffer for use in the second while loop

i++;

LARM=0;

}

int vi=1;

double sum=0.0;

log("candles:"+candles);

while ( vi <=candles) //CALCULATE MA

{

for (int vk=vi; vk<vi+MAperiod; vk++) // vi+MAperiod einai swsto? mipws 8elei -1 'h mipws 8elei <=

{

sum=sum+Buffer[vk];

} //for

sum=sum / MAperiod;

Buf_0[vi]= sum;//;

sum=0;

vi++;

}//while

return(0);

}

Any ideas on how i could solve this?

Thanks
 

Thanks so much for your reply.

This looks like it works. Although i need to keep the two while loops structure as it is if that possible or embed them in worst case scenario.

Any ideas?

mladen:
Try it out like this :
#property indicator_separate_window

#property indicator_buffers 1

#property indicator_color1 White

#property indicator_width1 2

#property indicator_level1 0

extern int MAperiod = 14;

double Buf_0[];

double Buffer[];

int init()

{

IndicatorBuffers(2);

SetIndexBuffer(0,Buf_0); SetIndexLabel (0,"MA");

SetIndexBuffer(1,Buffer);

IndicatorShortName("MA ("+MAperiod+")");

return(0);

}

int deinit()

{

return(0);

}

//

//

//

//

//

int start()

{

int counted_bars=IndicatorCounted();

if(counted_bars < 0) return(-1);

if(counted_bars > 0) counted_bars--;

int limit = MathMin(Bars-counted_bars,Bars-1);

for(int i = limit; i >= 0; i--)

{

Buf_0 = 0;

Buffer = Close-Low;

for (int vk=0; vk<MAperiod; vk++) Buf_0+=Buffer[vk+i];

Buf_0/=MAperiod;

}

return(0);

}
 

If you need two loops you can do something like this :

#property indicator_separate_window

#property indicator_buffers 1

#property indicator_color1 White

#property indicator_width1 2

#property indicator_level1 0

extern int MAperiod = 14;

double Buf_0[];

double Buffer[];

int init()

{

IndicatorBuffers(2);

SetIndexBuffer(0,Buf_0); SetIndexLabel (0,"MA");

SetIndexBuffer(1,Buffer);

IndicatorShortName("MA ("+MAperiod+")");

return(0);

}

int deinit()

{

return(0);

}

//

//

//

//

//

int start()

{

int counted_bars=IndicatorCounted();

if(counted_bars < 0) return(-1);

if(counted_bars > 0) counted_bars--;

int limit = MathMin(Bars-counted_bars,Bars-1);

for(int i = limit; i >= 0; i--) Buffer = Close-Low;

for( i = limit; i >= 0; i--)

{

Buf_0 = 0;

for (int vk=0; vk<MAperiod; vk++) Buf_0+=Buffer[vk+i];

Buf_0/=MAperiod;

}

return(0);

}
athan00:
Thanks so much for your reply.

This looks like it works. Although i need to keep the two while loops structure as it is if that possible or embed them in worst case scenario.

Any ideas?