Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Indicator instanciation in class other than strategy or indicator

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

    Indicator instanciation in class other than strategy or indicator

    Hi,

    I'd like to instantiate an indicator in a class usable by both a strategy and and other indicators.
    One solution I have found is functional but ugly:

    Code:
    class HelperCode {
        private SMA sma
        public HelperCode(NinjaScriptBase ns) {
            if(ns is Indicator) {
                sma = ((Indicator)ns).SMA(10);
            } else if(ns is Strategy) {
                sma = ((Strategy)ns).SMA(10);
            }
        }
    }
    Unfortunately I've not been able to identify a proper class/interface to cast to or create an instance with new.
    Any help would be appreciated.

    #2
    Hello MojoJojo,

    This kind of programming would be outside of what is supported by NinjaTrader Support.
    This thread will remain open for any community members that would like to provide undocumented/unsupported suggestions.

    As a heads up, Indicators are read by NinjaTrader which requires a specific format as this generates other code wrappers behind the scenes.
    Attempting to extend classes will cause issues.

    Below is a link to an example of shared classes that uses unsupported code you may find helpful.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hello Chelsea,

      thanks for your reply. I can understand that NT has no simple way for instantiating indicators outside of a strategy and indicator classes.

      Judging by the auto generated code, there is no common interface or class.
      Maybe something could be done via reflection but this would hardly be an improvement.

      Example:

      Code:
      namespace NinjaTrader.NinjaScript.Strategies
      {
          public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
          {
              public Indicators.ADX ADX(int period)
              {
                  return indicator.ADX(Input, period);
              }
      
              public Indicators.ADX ADX(ISeries<double> input , int period)
              {
                  return indicator.ADX(input, period);
              }
          }
      }
      Last edited by MojoJojo; 04-14-2020, 08:23 AM.

      Comment


        #4
        Just because I enjoy a good puzzle, here is a possible solution which avoids code duplication.
        Instead it breaks code encapsulation which can hardly be encouraged.

        Code:
        class HelperCode {
            private SMA sma
            public HelperCode(NinjaScriptBase ns) {
                Indicator indicator = null
                if(ns is Indicator) {
                    indicator = (Indicator)ns;
                } else if(ns is Strategy) {
                    indicator  = GetInstanceField<Indicator>(
                          ns
                        , "indicator"
                        , typeof(Strategy)
                    );
                }
                sma = indicator.SMA(10);
            }
        
            internal static T GetInstanceField<T>(object instance, string fieldName, Type type) {
                BindingFlags bindFlags =
                      BindingFlags.Instance
                    | BindingFlags.Public
                    | BindingFlags.NonPublic
                    | BindingFlags.Static;
                FieldInfo field = type.GetField(fieldName, bindFlags);
        
                return (T)field.GetValue(instance);
            }
        
        }
        Last edited by MojoJojo; 04-28-2020, 05:20 PM.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        581 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        338 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        103 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        554 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        552 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X