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 ttrader23, Today, 09:04 AM
        2 responses
        10 views
        0 likes
        Last Post ttrader23  
        Started by geotrades1, Today, 07:54 AM
        10 responses
        26 views
        0 likes
        Last Post NinjaTrader_BrandonH  
        Started by ninza33, Yesterday, 12:31 PM
        3 responses
        16 views
        0 likes
        Last Post NinjaTrader_LuisH  
        Started by patrickmlee007, Today, 09:33 AM
        1 response
        10 views
        0 likes
        Last Post NinjaTrader_LuisH  
        Started by Vulgoth_t_Destroyer, 05-09-2022, 04:45 PM
        53 responses
        5,309 views
        0 likes
        Last Post BartMan
        by BartMan
         
        Working...
        X