• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

REQUEST: Indicator Startup by Reflection code

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

    REQUEST: Indicator Startup by Reflection code

    In the past, I wrote a library to initialize an indicator dynamically after the strategy/indicator has started. I did this by passing a string such as "SMA(7)" to the function and it would return an Indicator.

    Looking at the Market Analyzer "Indicator" feature, it appears you have similar code internally to do the same thing. Would it be possible to get documentation/access to the same code so I don't need to re-invent the wheel?

    For some reason the "Indicator" Market Analyzer code doesn't exist in the NinjaScript Editor for review.

    #2
    Hello,

    I believe you should be able to find what you are looking for in the C:\Users\<username>\Documents\NinjaTrader 8\bin\Custom\MarketAnalyzerColumns directory on your PC. This folder contains all of the indicator code for the Market Analyzer.

    You should also be able to view these scripts directly in the NinjaScript Editor in NinjaTrader 8, but expanding the "MarketAnalyzerColumns" namespace in the NinjaScript Explorer.
    Dave I.NinjaTrader Product Management

    Comment


      #3
      Hi Dave,

      Thanks, I did look through that code and the only "Market Analyzer" that imports an Indicator dynamically is the "Indicator" column type.

      Unfortunately, the source code for that column does not exisit in the directory. This might be an internal function. It would be good to know how to pull these in and add them to the dialog box as well if possible.

      Again, I am looking for something I can send "SMA(7)" and it return an indicator. Doesn't mater if it needs to be passed differently, but I am looking to get an indicator back out from ultimately a STRING input.

      Thanks

      Comment


        #4
        Before I dive into the reflection stuff (Which I may or may not be able to support). Just curious what exactly is the use case for this? As you can initialize an indicator after a strategy has started without reflection so I'm confused why the complexity of reflection is needed. If you could explain perhaps I note down a feature to do what you guys are trying to do without requiring "re-inventing the wheel"
        BrettNinjaTrader Product Management

        Comment


          #5
          for example starting up an SMA indicator 200 bars in.

          OnBarUpdate()
          {
          If (CurrentBar == 200)
          {
          SMA mySMA = new SMA();
          }
          }
          BrettNinjaTrader Product Management

          Comment


            #6
            Sure, that works, but it requires you to recompile the code to get the functionality.

            The simple use is more like this feature:

            string MAforProcessing = "SMA(7)";

            OnBarUpdate()
            {
            If (CurrentBar == 200)
            {
            Indicator m_MA= GetIndicator(MAforProcessing);
            // Now m_MA is an SMA(7) indicator, change the string to get HMA or EMA, etc.
            }
            }

            This will help avoid LARGE CASE statements creating different Indicator Types for MA Crossover and other features.

            My use case is a little more specialized.

            I create a TEXT FILE with the indicators and preprocessing instructions for a "generic" strategy to load. The strategy loads the indicators and then I can execute Machine learning algorithms upon those particulate indicators.

            I can then change the number of indicators and types of indicators without changing the strategy each time. Some experiments might have 100+ indicators loaded (different periods, etc).

            So in a sense it is a TEXT -> INDICATOR converter and would rather leverage anything within NT8 before recoding my own tools.


            Originally posted by NinjaTrader_Brett View Post
            for example starting up an SMA indicator 200 bars in.

            OnBarUpdate()
            {
            If (CurrentBar == 200)
            {
            SMA mySMA = new SMA();
            }
            }

            Comment


              #7
              Ah I see, thanks for clarifying the use case understood.

              Internally we use the following which is publicly accessible and you should be able to use as well. It does not use reflection but I think ultimately what your looking for since you could have some parameter that you could then select the indicator you want. If you still want to use reflection to toggle based on string name then you would need to "re-invent the wheel"

              Type[] availableIndicatorTypes = NinjaTrader.Core.Globals.AssemblyRegistry.GetDeriv edTypes(typeof(IndicatorBase));
              BrettNinjaTrader Product Management

              Comment


                #8
                It works if you change the namespace to "NinjaTrader.NinjaScript.Indicators.Indicator"

                Code:
                        //convert comma separated string of parameters to array of objects using method parameter types
                        object[] GetParams(string input,ParameterInfo[] pi)
                        
                        {
                            string[] parameters=input.Split(',');
                            
                            Object[] obj=new Object[parameters.Length];
                            
                            // convert each string to the correct param type
                            for(int i=0;i<parameters.Length;i++)
                                obj[i]=Convert.ChangeType(parameters[i],pi[i].ParameterType,CultureInfo.InvariantCulture.NumberFormat);
                        
                            return obj;
                        }
                        
                        
                        protected void OnStartUp()
                        {
                            // split the indicator parameter string to name and param list eg iName="ADX" and iParams="12,34"
                            string[] NameParams=paramIndicator.Split(new char[]{'(',')'});
                            
                            string iName=NameParams[0];
                            string iParams=NameParams[1];
                            
                            
                            Type t = Type.GetType("NinjaTrader.NinjaScript.Indicators.Indicator");
                            
                            //get all methods with the corresponding indicator name, using the least number of params
                            MethodInfo meth =  t.GetMethods(BindingFlags.Public|BindingFlags.Instance|BindingFlags.DeclaredOnly)
                                                .Where(x=>x.Name==iName)
                                                .OrderBy(x=> x.GetParameters().Length)
                                                .First();
                            
                            //call method. This is equivalent to indy=ADX(12);
                            indy=(Indicator)meth.Invoke(this,GetParams(iParams,meth.GetParameters()));
                        }

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by XimoTrader, Today, 12:57 PM
                1 response
                6 views
                0 likes
                Last Post NinjaTrader_PatrickG  
                Started by FxInception, Today, 12:27 PM
                2 responses
                8 views
                0 likes
                Last Post FxInception  
                Started by siroya, Today, 10:34 AM
                1 response
                20 views
                0 likes
                Last Post NinjaTrader_ShawnB  
                Started by Leeroy_Jenkins, Today, 11:22 AM
                2 responses
                12 views
                0 likes
                Last Post Leeroy_Jenkins  
                Started by RedDuke, Today, 10:27 AM
                2 responses
                15 views
                0 likes
                Last Post RedDuke
                by RedDuke
                 
                Working...
                X