am i missing something?

 

i created my 1st indicator, and it doesnt seem to work at all, i think i am missing something or dunno may be many things. can some one say me wat is wrong with this code?

double ExtGreenBuffer[];

int init()
  {

   IndicatorBuffers(1);

   IndicatorDigits(5);
   SetIndexDrawBegin(0,34);
  
   SetIndexBuffer(0, ExtGreenBuffer);
  
   IndicatorShortName("AO");

   return(0);
  }

int start()
  {
   int    limit;
   int    counted_bars=IndicatorCounted();
   
   if(counted_bars<0) return(-1);
   
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;

   for(int i=Bars-1; i>=0; i--)
     {
      if(Close[i+1]>Open[i+1])
      ExtGreenBuffer[i]=((High[i+1]-Low[i+1])+(Open[i+1]-Close[i+1]))/2;
      else if(Close[i+1]<Open[i+1])
      ExtGreenBuffer[i]=((Low[i+1]-High[i+1])+(Close[i+1]-Open[i+1]))/2;
     
     }

   return(0);
  }
 
Yeah you're missing to tell a help-forum what you're trying to accomplish. :)
 

to calculate a moving average like indicator i created this code, i mean, i need to the lines using this forumla

((High[i+1]-Low[i+1])+(Open[i+1]-Close[i+1]))/2;
 
ubzen:
Yeah you're missing to tell a help-forum what you're trying to accomplish. :)

... that

and you haven't posted all of the code.

 

You create this

limit=Bars-counted_bars;

but then don't use it, meaning that on every new price tick you recalculate the whole chart :-(

 

By using i+1 within this loop you are indexing beyond the length of the arrays, which may crash the indicator.

   for(int i=Bars-1; i>=0; i--)
     {
      if(Close[i+1]>Open[i+1])
      ExtGreenBuffer[i]=((High[i+1]-Low[i+1])+(Open[i+1]-Close[i+1]))/2;
      else if(Close[i+1]<Open[i+1])
      ExtGreenBuffer[i]=((Low[i+1]-High[i+1])+(Close[i+1]-Open[i+1]))/2;
     
     }
 

The results of your calculations are too small to be seen in the same context as the price, also you have not defined a color for your line,

change

#property  indicator_chart_window
to
#property  indicator_separate_window

and define a color for the line:

#property  indicator_separate_window
#property  indicator_color1  Yellow
and you will see the results of your indicator (which in its present form doesnt appear to indicate a whole lot of anything, but its a start and really not a bad effort for a first attempt :)
 
dabbler:

By using i+1 within this loop you are indexing beyond the length of the arrays, which may crash the indicator.

He can do that because he is drawing it in reverse

for(int i=Bars-1; i>=0; i--)
although as you said it is better to use limit as is already defined instead of Bars
 
  1. He's not drawing it in reverse. I indexes from the oldest bar to the newest. When i=Bars-1, i+1 is beyond the array's limits. High[0] .. High[Bars-1]
  2. No need to decrement counted
    //   if(counted_bars>0) counted_bars--;
       for(int i=Bars-1-counted_bars; i>=0; i--)
    
    If he really wants to look at earlier bars than i (his high[i+1] then he needs
    //   if(counted_bars>0) counted_bars--;
    #define MAX_LOOKBACK 1 // Looking at High[i+1]
    if (counted_bars < MAX_LOOKBACK) counted_bars = MAX_LOOKBACK;
    for(int i=Bars-1-counted_bars; i>=0; i--)

 

sorry guys, for not having posted the whole code, i corrected what everyone said about, INCREMENTING instead of DECREMENTING, but still it didnt work, i have also tried the "limit"...

#property copyright ""
#property link      ""

#property indicator_chart_window

double ExtGreenBuffer[];

int init()
  {

   IndicatorBuffers(1);

   IndicatorDigits(5);
   SetIndexDrawBegin(0,34);
  
   SetIndexBuffer(0, ExtGreenBuffer);
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,2);
   IndicatorShortName("AO");

   return(0);
  }

int start()
  {
   int    limit;
   int    counted_bars=IndicatorCounted();
   
   if(counted_bars<0) return(-1);
   
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;

   for(int i=0; i<=limit; i++)
     {
      if(Close[i+1]>Open[i+1])
      ExtGreenBuffer[i]=((High[i+1]-Low[i+1])+(Open[i+1]-Close[i+1]))/2;
      else if(Close[i+1]<Open[i+1])
      ExtGreenBuffer[i]=((Low[i+1]-High[i+1])+(Close[i+1]-Open[i+1]))/2;
     
     }

   return(0);
  }

 

the aim was to show the calculated values of previous bar's {(high)-(low)+(open)-(close)}/2, tat's why i chose to index the arrays as i+1, for each i, the i+1 would be the previous bar right?...or is there something else?

Reason: