Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

partial class example in NT8 help guide

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

    partial class example in NT8 help guide

    In the NT8 help guide code breaking changes section you provide this code example for a partial class

    namespaceNinjaTrader.NinjaScript.Indicators
    {
    publicpartialclassMyMethods // : parent class to inherit from
    {
    //Sample method which calculates the delta of two prices
    publicdoublecalculateDelta(doublefirstPrice,doublesecondPrice)
    {
    returnMath.Abs(firstPrice-secondPrice);
    }

    //Sample method which prints Position information
    publicvoidprintPositionInfo(Positionposition)
    {
    Print(String.Format("{0}: {1} {2} at {3}",position.Instrument,position.Quantity,position.MarketPosition,position.AveragePrice));
    }

    }
    }
    then give an example of calling one of the methods in this class from another indicator

    protectedoverridevoidOnBarUpdate()
    {
    if(CurrentBar<1)return;

    // Use the static calculateDelta method to calculate the difference between the close of each bar
    doubledelta=MyMethods.calculateDelta(Close[0],Close[1]);

    Print(delta);
    }

    When I tried this code out I had to add "static" modifier to the calculateDelta method in the MyMethods class.
    Is that correct? If so, then can you fix the typo in the help guide.
    Otherwise, what do I need to do to make this work without the "static" modifier.

    #2
    You missed the significance of the comment,

    Code:
    [COLOR=#008000][I]// : parent class to inherit from[/I][/COLOR]
    You should place the class name of the NinjaScript objects you wish
    to augment -- such as "Indicator" or "Strategy". Even then, I agree,
    the example looks to be incorrect.

    Try learning from the example here:
    This is a partial class indicator. Its purpose is to provide fractional price markers to all indicators on Bond instrument charts. Forum member bltdavid created the methods in NT7 and these were applied to the User Defined Methods. These have been converted to NT8 and have put them in a partial class because there are [&#8230;]


    The class name "MyMethods" is not needed, and neither is "static".

    Comment


      #3
      Originally posted by bltdavid View Post
      You missed the significance of the comment
      @NinjaTrader_Paul, bltdavid Can one apply partial class concept to combine BarTypes class and Indicators class?

      For example, I want to create a BarTypes (BarTypes Class) add-on to paint the bars based on calculations results from an indicator (Indicator Class).

      Comment


        #4
        Originally posted by aligator View Post

        @NinjaTrader_Paul, bltdavid Can one apply partial class concept to combine BarTypes class and Indicators class?

        For example, I want to create a BarTypes (BarTypes Class) add-on to paint the bars based on calculations results from an indicator (Indicator Class).
        No.

        This would require classes be able to inherit from multiple base classes, a feature
        which is not supported in C#. Yes, C# allows multiple inheritance from interfaces,
        but an interface is not a class.

        You may still be able to achieve what you want, but you cannot inherit from both
        Indicator and BarsType -- pick one, fake the other.

        Comment


          #5
          Originally posted by aligator View Post
          For example, I want to create a BarTypes (BarTypes Class) add-on to paint the bars based on calculations results from an indicator (Indicator Class).
          Paint the bars?
          Based upon calculations from an indicator?

          Sounds like hundreds of other indicators -- by definition, your idea already fits well in the Indicator class model.

          Comment


            #6
            Originally posted by bltdavid View Post

            No.

            This would require classes be able to inherit from multiple base classes, a feature
            which is not supported in C#. Yes, C# allows multiple inheritance from interfaces,
            but an interface is not a class.

            You may still be able to achieve what you want, but you cannot inherit from both
            Indicator and BarsType -- pick one, fake the other.
            Thanks bltdavid,

            I am not familiar with "fake the other".

            Let's say I want a to create a BarType that would paint the bars in two colors based on RSI() being > 50 or < 50. How would one "fake" (or call) the results from the indicator RSI?

            Comment


              #7
              Originally posted by aligator View Post
              I am not familiar with "fake the other".
              Sorry, 'fake it' is an old-timer's software engineering inside joke.

              That just means re-implement portions of the other thing that you couldn't
              use directly -- which, frankly, is why some (most?) of those other things in
              the C# language are interfaces, and not classes.

              In fact, that's exactly what a C# interface is -- an interface mandates that
              you provide an implementation for the methods it defines -- so an interface is
              giving a voice (sort of) to that old-fashioned technique I used to call 'faking it'.

              Originally posted by aligator View Post
              Let's say I want a to create a BarType that would paint the bars in two colors based on RSI() being > 50 or < 50. How would one "fake" (or call) the results from the indicator RSI?
              Using a BarsType?
              I have no idea.

              Using an Indicator?
              Dead simple.

              As you describe it, your choices:
              1. Create an indicator to paint the bars from RSI values.
              2. Create a BarsType to paint the bars from RSI values.

              I don't think #2 is even possible.
              That's just not how it's done.

              Why do you want to use a BarsType?
              This approach seems backwards.
              Use #1 instead.

              Comment


                #8
                We're getting a bit off-topic here, I think ...

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                0 responses
                663 views
                0 likes
                Last Post Geovanny Suaza  
                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                0 responses
                376 views
                1 like
                Last Post Geovanny Suaza  
                Started by Mindset, 02-09-2026, 11:44 AM
                0 responses
                110 views
                0 likes
                Last Post Mindset
                by Mindset
                 
                Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                0 responses
                575 views
                1 like
                Last Post Geovanny Suaza  
                Started by RFrosty, 01-28-2026, 06:49 PM
                0 responses
                580 views
                1 like
                Last Post RFrosty
                by RFrosty
                 
                Working...
                X