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

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 cummish, Today, 08:43 PM
        0 responses
        5 views
        0 likes
        Last Post cummish
        by cummish
         
        Started by Option Whisperer, Today, 07:58 PM
        4 responses
        18 views
        0 likes
        Last Post Option Whisperer  
        Started by ETFVoyageur, 05-07-2024, 07:05 PM
        13 responses
        86 views
        0 likes
        Last Post ETFVoyageur  
        Started by cupir2, Today, 07:44 PM
        0 responses
        9 views
        0 likes
        Last Post cupir2
        by cupir2
         
        Started by reynoldsn, Today, 07:23 AM
        6 responses
        20 views
        1 like
        Last Post reynoldsn  
        Working...
        X