Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to call an NT method inside a user defined class?

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

    How to call an NT method inside a user defined class?

    Hello,
    I've created my own class that is nested inside a partial class Indicator. I've created several very simple bool methods that use NT methods. I figured that having my class inherit from Indicator would give me access to NT methods. The code does compile when I implement the NT methods, but when the object is called inside another class that inherits from Indicator the NT methods all return false. I will give one example.

    The code to return:
    namespace NinjaTrader.NinjaScript.Indicators
    {​
    public partial class Indicator
    {
    public class MyClass : Indicator
    {​
    public bool JKWilliamsFastLine24Range_IsFalling
    (ISeries<double> jkWilliamsFastLine_24Range)
    {
    return IsFalling(jkWilliamsFastLine_24Range);
    }​
    }
    }
    }

    The code to implement
    namespace NinjaTrader.NinjaScript.Indicators
    {
    public class A1TradeChart : Indicator
    {​
    protected override void OnBarUpdate()
    {​
    Print(CurrentBar);
    Print("Falling = " + myClass.JKWilliamsFastLine24Range_IsFalling(jKWill iams_24Range.JKWilliams_Fast));
    }
    }
    }

    Output returns false on every bar.

    If I place the method
    public bool JKWilliamsFastLine24Range_IsFalling
    (ISeries<double> jkWilliamsFastLine_24Range)
    {
    return IsFalling(jkWilliamsFastLine_24Range);
    }​​
    inside public partial class Indicator and outside MyClass the method works as expected.

    What must I do to gain access to NT methods inside my personal classes?

    Thanks

    #2
    Hello CaptainAmericaXX,

    Thanks for your post.

    To clarify, are these custom methods within a custom AddOn script?

    You can create a new AddOn script and then create variables and methods that can be accessed from all indicators and strategies.

    To access the method from another script, you will need to use the class name and then the method name.

    You could find information about partial classes and sample code on the help guide page below.
    https://ninjatrader.com/support/helpGuides/nt8/index.html?code_breaking_changes.htm#Implementatio nChangesOverview

    Information about creating partial classes in C# could also be found on the forum thread linked below.

    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment


      #3
      BrandonH,
      Thanks for your reply,

      These are custom methods, but return a NT bool method.

      My scripts are partial classes within the AddOn folder.

      As per your suggestion from the thread, if I make my class static then I can't inherit from Indicator and can't use the IsFalling().

      public static class Test: Indicator
      {
      public static bool JKWilliamsFastLine24Range_IsFalling
      (ISeries<double> jkWilliamsFastLine_24Range)
      {
      return IsFalling(jkWilliamsFastLine_24Range);
      }
      }​

      This is not allowed as a static class can't inherit.
      Last edited by CaptainAmericaXX; 03-31-2024, 05:15 PM.

      Comment


        #4
        Hello CaptainAmericaXX,

        Thanks for your notes.

        You would need to pass the indicator instance this method is in as a parameter to your custom method.

        As an example, you would add YourIndicatorsType myIndicator as a parameter to the method, and then inside the method you could use myIndicator.SomeNinjaScriptMethod().

        Here is a help guide page with information about partial classes and sample code on the help guide page below.

        <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        576 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        334 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
        553 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        551 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X