Very strange Error...

 

Hi 

 i ve got the following code that is trying to make an MA out of the C-L calculation. My main problem is tha:

When i first place the indicator it is drawn fine. But after it it starts drawing a straight line... cant really figure out why.

Here is some snapshots. 

Pic A when i first place the indicator:

 

 

 

 

after a few minutes on the M1 timeframe... you can clearly see the straight line i m talking about .. that keeps like that forever

 here is the code.. if anyone could have a look... thanks

#property indicator_level1 0
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 White

double Buf_0[];
  

// --- parameters
extern int history=180;
extern bool log_on=false;
extern int MAperiod=14;

string build=" ver 0.29g-TEST-MA";

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];

   
   
   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
         }
     
//********************************* MAIN WHILE LOOP ****************************                            
    while ( (i<=candles))
    {

                  L=Low[i];C=Close[i];
                  LARM=LARM+C-L;                                 

                     wlog("i change i["+i+"]");
               
                      Buffer[i]= LARM; //store in a buffer for use in the second while loop

                   i++;
                   
               LARM=0;         
          
      } 
      
                int vi=0;          
                double sum=0.0;
                         
                while ( vi<=candles) //CALCULATE MA
                {  
                
                         vi++;
                                                 
                                   for (int vk=vi; vk<vi+MAperiod; vk++)
                                   {
                                                  sum=sum+Buffer[vk];
                                   }  //for
                                  
                                  sum=sum / MAperiod; 

                                   Buf_0[vi]= sum;//;   

                                   sum=0; 
                 }//while

  return(0);
}         


 
anyone that can help with that?
 
athanfx:
anyone that can help with that?
Your code does not compile !
 
athanfx:
anyone that can help with that?

After the first tick when you have process all the bars you are then just processing one bar max,  at this time and onwards what are the first 14 elements in array Buffer[] ?  you are only updating the first element.

 

Try this . . .

Edit:  file updated 

Files:
 
RaptorUK:

After the first tick when you have process all the bars you are then just processing one bar max,  at this time and onwards what are the first 14 elements in array Buffer[] ?  you are only updating the first element.

 

Try this . . .

Edit:  file updated 


thanks so much for your reply. i totally missed that out!!! 

although now it looks it is working, meaning it is drawing something and not a straight line, i dont think it draws what it should. Why?  

Cause when i m eg in M1 TF and i go to M5 and then back to M1 TF the indicator totally changes. In order to double check that i saved the indicator under another name and i replaced the two while loops with the following:

while (i<=history)

and

while (vi<=history)

this way it keeps repainting all the bars again and again so i can check if the index .

Anyway i ll try to find out whats wrong... if you ve got any idea let me know

thanks once again 

 
athanfx:


thanks so much for your reply. i totally missed that out!!! 

although now it looks it is working, meaning it is drawing something and not a straight line, i dont think it draws what it should. Why?  

Cause when i m eg in M1 TF and i go to M5 and then back to M1 TF the indicator totally changes. In order to double check that i saved the indicator under another name and i replaced the two while loops with the following:


It's a little hard to test while the Markets are closed,  I'll take another look on Sunday night.
Reason: