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

Passing NijnaScriptBase, NinjaScript refs to non-static class

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

    Passing NijnaScriptBase, NinjaScript refs to non-static class

    I found this post regarding private classes and access to a calling strategies data.
    https://ninjatrader.com/support/foru...les#post692124

    In the example, a static class is created that can act on NijnaScriptBase and NinjaScript methods from the calling strategy.

    I created a non-static class to do the same thing, and it seems to be working. Here's a sample of what I'm doing:

    Code:
    namespace NinjaTrader.NinjaScript.Strategies
    {
    
    public class XoSignal
    {
    
    
       private NinjaScriptBase nsb;
        private NinjaScript ns;
    
        // passed in indicators
        private SMA SMA1;
        private SMA SMA2;
    
        public XoSignal(NinjaScriptBase nsbPassed, NinjaScript nsPassed, SMA s1, SMA s2)
        {
            this.nsb = nsbPassed;
            this.ns = nsPassed;
    
            this.SMA1 = s1;
            this.SMA2 = s2;
        }
    
        public bool DidCrossAbove
        {
            get
            {
                return nsb.CrossAbove(SMA1, SMA2, 1);
            }
        }
    }
    
    }
    The class can be instantiated and called like this:

    Code:
    private XoSignal Sig;
    
    Sig = new XoSignal(this, this, SMA1, SMA2);
    
    if (Sig.DidCrossAbove) {
        // do something
    }
    I'd like to know if there's any reason that static classes and methods should be used, rather than instance classes (for thread-safety, for instance), or if the two methods of doing this are both viable.
    Last edited by Wick Wrangler; 10-08-2020, 09:01 AM.

    #2
    Hello Wick Wrangler,

    The approach really depends on what you are trying to achieve.

    The creation of a new class instance (that does not inherit from Indicator, Strategy, or any other NinjaScript class type) would be the supported approach. And shouldn't cause any issues.

    A partial class just for shared methods, with inheritance, would also be fine.
    See 'Partial Classes (Porting methods and properties from UserDefinedMethods.cs)' in the Code Breaking Changes.
    https://ninjatrader.com/support/help...hangesOverview

    Static classes can sometimes be fine and would use less overhead (static methods would not have to take new instance memory locations) but sharing objects could get messy and would fall outside of support.
    Below is a link to a shared static objects example.
    https://ninjatrader.com/support/foru...245#post712245
    Last edited by NinjaTrader_ChelseaB; 10-11-2020, 06:10 PM.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_ChelseaB View Post

      The creation of a new class instance (that does not inherit from Indicator, Strategy, or any other NinjaScript class type) would be the supported approach. And shouldn't cause any issues.
      Thanks for the clarification.

      Comment


        #4
        Hello Wick Wrangler and Chelsea,

        In the original post, what exactly is being passed in for the SMA indicators - Sig = new XoSignal(this, this, SMA1, SMA2);
        I'm trying to figure out how to pass an indicator.

        Thank you!




        Comment


          #5
          Hello neo302,

          That is likely an ISeries<double> property.
          Chelsea B.NinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by GussJ, 03-04-2020, 03:11 PM
          11 responses
          3,221 views
          0 likes
          Last Post xiinteractive  
          Started by andrewtrades, Today, 04:57 PM
          1 response
          10 views
          0 likes
          Last Post NinjaTrader_Manfred  
          Started by chbruno, Today, 04:10 PM
          0 responses
          6 views
          0 likes
          Last Post chbruno
          by chbruno
           
          Started by josh18955, 03-25-2023, 11:16 AM
          6 responses
          436 views
          0 likes
          Last Post Delerium  
          Started by FAQtrader, Today, 03:35 PM
          0 responses
          9 views
          0 likes
          Last Post FAQtrader  
          Working...
          X