Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

An array for least squares regression line slope

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    An array for least squares regression line slope

    I have this code for least squares regression slope

    Have i coded this correctly, I want to find the slope for just 1 line, therefor the Y's are my data and X's the intervals?

    Is this correct?

    LSarrayY[0]=RWasklimit0;
    LSarrayY[1]=RWasklimit1;
    LSarrayY[2]=RWasklimit2;
    LSarrayY[3]=RWasklimit3;
    LSarrayY[4]=RWasklimit4;

    LSarrayX[0]=1;
    LSarrayX[1]=2;
    LSarrayX[2]=3;
    LSarrayX[3]=4;
    LSarrayX[4]=5;

    double LS=LSslope(LSarrayY,LSarrayX,5);

    Code:
            private static double LSslope(double[]y,double[]x,int n)    
            {
            // values needed prior to code snippet
            //
            // int n = number of (x,y) points
            // double x[ i ] for i = 0 to n-1 ... the x points
            // double y[ i ] for i = 0 to n-1 ... the y points
            //
    
            double s0 = 0;
            double s1 = 0;
            double s2 = 0;
            double t0 = 0;
            double t1 = 0;
    
            for (int i=0; i< n; i++)
            {
            s0++;
            s1 = s1 + x[ i ];
            s2 = s2 + x[ i ]*x[ i ];
            t0 = t0 + y[ i ];
            t1 = t1 + x[ i ]*y[ i ];
            }
    
    //        double M = ( s0*t1 - s1*t0 ) / (s0*s2 - s1*s1) ; // slope
            return ( s0*t1 - s1*t0 ) / (s0*s2 - s1*s1) ; // slope
    //        double B = ( s2*t0 - s1*t1 ) / (s0*s2 - s1*s1) ; // y-intercept
            }

    #2
    Hi tinkerz,

    Can you please be more specific...
    Are you receiving certain errors or unexpected behavior?

    Unfortunately, I will not be able to debug or comment on whether the math functions of your code are correct. Same for the arrays, as they are outside of the scope of support we provide.

    I believe there are some default indicators that also use Least Squares calculations.
    TimNinjaTrader Customer Service

    Comment


      #3
      No errors I was asking forum members, I understand its outside the scope of support, i will look least squares code

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by FrancisMorro, Today, 03:24 AM
      0 responses
      1 view
      0 likes
      Last Post FrancisMorro  
      Started by Segwin, 05-07-2018, 02:15 PM
      10 responses
      1,770 views
      0 likes
      Last Post Leafcutter  
      Started by Rapine Heihei, 04-23-2024, 07:51 PM
      2 responses
      31 views
      0 likes
      Last Post Max238
      by Max238
       
      Started by Shansen, 08-30-2019, 10:18 PM
      24 responses
      944 views
      0 likes
      Last Post spwizard  
      Started by Max238, Today, 01:28 AM
      0 responses
      11 views
      0 likes
      Last Post Max238
      by Max238
       
      Working...
      X