Trouble with my first custom indicator

 

Hello,

I tried writing my own custom indicator and I have run into 2 problems. The first problem is when the indicator runs it kicks out the same value for every bar and I don't know why this is. The second is this value is being calculated wrong. What I am looking to do is create a simple indicator that shows the correlation between the close and the CCI indicator over the past 10 periods. I set the periods as a variable so when I change the CCI calc, it also changes the range for which the correlation calculation calculates. I know my equation is not being calculated correctly because it the output is greater then 1 when the output should be between 1 and -1. I don't know if the fact that I get the same output for every bar, if this is making the equation calculate inn accuratly. I listed how correlation should be calculated as a comment field in too parts of the file. If any one could please help with either of these two problem I would really appreciate it.

-Chris

Files:
 

Freebird83 wrote >>

I listed how correlation should be calculated

Your loop is running one time too much, you want I+0, I+1,... I+count-1, for a total of count
for (q = i; q < i+count; q++)  
  { 
  x = Close[q];
  y = iCCI(NULL,0,count,PRICE_TYPICAL,q);             // The indicator to be correlated
  Ex += x;
  Ey += y; // This form shows the summation 
  //...
My correlation code matches yours with the exception I'm using C squared:
        for(int line = TL_UPPER; line >= TL_LOWER; line--) {
            double  X           =  when[shift][line],
                    Y           = price[shift][line];
            sum.x[line] += X;   sum.xx[line]    += X*X;
            sum.y[line] += Y;   sum.yy[line]    += Y*Y; sum.xy[line]    += X*Y;
            if (duration.Time >= TL.Time.Min && N > 1) {
                double  M[2], B[2],
                        num =  N*sum.xy[line] - sum.x[line]*sum.y[line],
                        div = (N*sum.xx[line] - sum.x[line]*sum.x[line])
                            * (N*sum.yy[line] - sum.y[line]*sum.y[line]);
                M[line] = num / (N*sum.xx[line] - sum.x[line]*sum.x[line]);
                B[line] = (sum.y[line] - M[line]*sum.x[line])/N;
                if (NormalizeDouble(div, 9) > 0) {
                        TL.corr[line] = MathPow( num / MathSqrt(div), 2 );
                } else  TL.corr[line] = 1;  // A perfect horrizontal line.
        }   }
Reason: