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

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:	227
Size:	296.0 KB
ID:	1210214


    Click image for larger version

Name:	2022-07-29_12h38_42.png
Views:	125
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.
    Kate W.NinjaTrader Customer Service

    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:	123
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.
        Kate W.NinjaTrader Customer Service

        Comment


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

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by fx.practic, 10-15-2013, 12:53 AM
          5 responses
          5,403 views
          0 likes
          Last Post Bidder
          by Bidder
           
          Started by Shai Samuel, 07-02-2022, 02:46 PM
          4 responses
          94 views
          0 likes
          Last Post Bidder
          by Bidder
           
          Started by DJ888, Yesterday, 10:57 PM
          0 responses
          6 views
          0 likes
          Last Post DJ888
          by DJ888
           
          Started by MacDad, 02-25-2024, 11:48 PM
          7 responses
          158 views
          0 likes
          Last Post loganjarosz123  
          Started by Belfortbucks, Yesterday, 09:29 PM
          0 responses
          8 views
          0 likes
          Last Post Belfortbucks  
          Working...
          X