Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Using the LinReg Indicator with Mathematical Operators on the Source/Input Series

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

    Using the LinReg Indicator with Mathematical Operators on the Source/Input Series

    Hello,

    I am attempting to replicate an indicator I have used on other platforms. I am getting an error on one portion of the code when attempting to compile. The error seems to be related to the LinReg indicator/function.

    The indicator is named Squeeze Momentum Indicator in the Ninjatrader public library, and it attempts to mimic the TTM Squeeze from Thinkorswim. This iteration seems to have been taken from Tradingview Pinescript. The LinReg function is used to plot the histogram of the indicator.

    ------------------------------------------------------------------------------------------------------------

    Thinkorswim Thinkscript Code (Inertia is the Thinkorswim version of LinReg):

    def K = (Highest(high, length) + Lowest(low, length)) / 2 + ExpAverage(close, length);
    def momo = Inertia(price - K / 2, length); <<<< histogram values

    -------------------------------------------------------------------------------------------------------

    Tradingview Pinescript Code:

    K = (highest(high,length) + lowest(low,length)) / 2 + ema(close,length)
    Momo = linreg(source-K/2,length,0) <<<< histogram values

    -------------------------------------------------------------------------------------------------------

    Ninjatrader Code that isn't working:

    double h = High[HighestBar(High, LengthKC)];
    double l = Low[LowestBar(Low, LengthKC)];

    // calculate Value
    data[0] = Close[0];
    double ema = EMA(data,LengthBB)[0];
    double avg = ((h + l) / 2) + ema;

    IsSqueezes[0] = 0.0;
    SqueezeDef[0] = LinReg(data[0]-avg/2,LengthBB); <<<< histogram values

    -------------------------------------------------------------------------------------------------------

    I've attached a screenshot of the bulk of the code in Ninjatrader, which also includes the error codes that have been generated. It looks like LinReg might require the 'series' data type in the first input (second screenshot attached), so I'm not sure how to proceed here, but there must be a way to do this if other similar trading applications can manage it.

    Any help would be much appreciated!!

    Thank you


    Click image for larger version

Name:	2022-07-29_12h36_25.png
Views:	352
Size:	296.0 KB
ID:	1210214


    Click image for larger version

Name:	2022-07-29_12h38_42.png
Views:	219
Size:	11.8 KB
ID:	1210216
    Attached Files

    #2
    Hello beejinator,

    Thank you for your note.

    I note that you're not accessing the actual indicator value for the LinReg at 111. You have the following:

    SqueezeDef[0] = LinReg(data[0] - avg/2, LengthBB);

    And you should have the following instead:

    SqueezeDef[0] = LinReg(data[0] - avg/2, LengthBB)[0];

    Does making that change rectify the compile error?

    Thanks in advance; I look forward to assisting you further.

    Comment


      #3
      Thanks Kate! I did actually try that, below is a screenshot using that version. I think it's having a problem with me calculating the LinReg input as a "series<double>" minus a "double", specifically the subtraction, instead of just using a series<double> by itself for the input.

      Click image for larger version  Name:	2022-07-29_16h35_54.png Views:	0 Size:	479.1 KB ID:	1210252

      I also tried it using just the "data" series, without the [0] on the end, and received this slightly different error about using a subtraction operation:

      Click image for larger version

Name:	2022-07-29_16h46_53.png
Views:	216
Size:	593.0 KB
ID:	1210253
      Last edited by beejinator; 07-29-2022, 03:48 PM.

      Comment


        #4
        Hello beejinator,

        Thank you for your reply.

        I think you need to save your calculated value to a custom series first:

        Code:
        // instantiate series at class level
        private Series<double> myCustSeries;
        
        //in State.DataLoaded
        myCustSeries = new Series<double>(this);
        
        // in OnBarUpdate
        myCustSeries[0] = data[0] - avg/2;
        SqueezeDef[0] = LinReg(myCustSeries, LengthBB)[0];
        I would give that a try there and see if that works better for you.

        Comment


          #5
          This worked! You're a genius! Thank you so much!

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          563 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          329 views
          1 like
          Last Post Geovanny Suaza  
          Started by Mindset, 02-09-2026, 11:44 AM
          0 responses
          101 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
          0 responses
          547 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          548 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X