Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Dynamically instantiate indicators

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

    Dynamically instantiate indicators

    Hey NinjaTrader team, I have a strategy where I want to dynamically instantiate indicators in State.Configure (kinda like AI Generate). I am able to create a instance but each indicator has it's own unique constructor and I want to dynamically generate it. In this example I want to instantiate ADX(12) then say for the next iteration I want to instantiate SMA(14) Below is my code :

    Code:
    public class AIStrategyGenerator : Strategy
    {[INDENT]//Array of indicators I want to try
    private Indicator[] indicators;
    
    protected override void OnStateChange()
    {[/INDENT][INDENT=2]if (State == State.Configure)
    {[/INDENT][INDENT=3]int iter = 1;
    
    //Get all indicators from the Indicators Class
    var indicator_type = typeof (Indicator);
    
    var sub_indicator_types =
    indicator_type
    .Assembly
    .DefinedTypes
    .Where(x => indicator_type.IsAssignableFrom(x) && x != indicator_type)
    .ToList();
    Type t = Type.GetType("NinjaTrader.NinjaScript.Indicators.Indicator");
    
    //Iterate of each indicator and instantiate into the strategy
    while (iter < iterations) {[/INDENT][INDENT=4]try {
    
    //Create the instance
    Indicator sub_indicator = (Indicator)Activator.CreateInstance(sub_indicator_ types[iter]);
    
    // split the indicator parameter string to name and param list eg iName="ADX" and iParams="12,34"
    //get all methods with the corresponding indicator name, using the least number of params
    
    MethodInfo meth = t.GetMethods(BindingFlags.Public|BindingFlags.Inst ance|BindingFlags.DeclaredOnly)
    .Where(x=>x.Name==sub_indicator.Name)
    .OrderBy(x=> x.GetParameters().Length)
    .First();
    
    [B][SIZE=16px]//call method. This is equivalent to indy=ADX(12); ERROR HERE Error Message : Object does not match target type using C# Reflection
    sub_indicator = (Indicator)meth.Invoke(this,GetParams("12",meth.GetParameters()));[/SIZE][/B]
    
    Print(sub_indicator);
    indicators[iter] = sub_indicator;
    } catch (Exception e) {
    Print(e.Message);
    }
    iter++;[/INDENT][INDENT=2]}[/INDENT][INDENT]
    
    //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.Number Format);
    
    return obj;
    
    }
    [/INDENT]
     }

    #2
    This post : https://ninjatrader.com/support/foru...ode#post675795 seems to be similar but it's archived and can't get it to work. Any guidance would be appreciated.

    Comment


      #3
      Hello JakeOfSpades, thank you for your post.

      This project goes out of the scope of support that the NinjaScript team is able to provide. NinjaTrader's object creation is done by a factory that is not documented or meant to be accessible to those writing Strategies and other general-purpose addons for NinjaTrader. Your post will remain public for other members of the community to provide input.

      Kind regards.

      Comment


        #4
        Originally posted by NinjaTrader_ChrisL View Post
        Hello JakeOfSpades, thank you for your post.

        This project goes out of the scope of support that the NinjaScript team is able to provide. NinjaTrader's object creation is done by a factory that is not documented or meant to be accessible to those writing Strategies and other general-purpose addons for NinjaTrader. Your post will remain public for other members of the community to provide input.

        Kind regards.
        Ok, do you ever plan on opening the AI Generate code for us to see?

        Comment


          #5
          Hello JakeOfSpades, thanks for your reply.

          We have an open feature request ID SFT-4056 requesting the ability for scripts to use the AI Generation tool in NInjaScript. I will add a vote to this for you.

          Best regards.

          Comment


            #6
            Originally posted by NinjaTrader_ChrisL View Post
            Hello JakeOfSpades, thanks for your reply.

            We have an open feature request ID SFT-4056 requesting the ability for scripts to use the AI Generation tool in NInjaScript. I will add a vote to this for you.

            Best regards.
            Thanks Chris!

            Comment


              #7
              Chris I have made some progress and sent you a DM.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Mindset, 04-21-2026, 06:46 AM
              0 responses
              90 views
              0 likes
              Last Post Mindset
              by Mindset
               
              Started by M4ndoo, 04-20-2026, 05:21 PM
              0 responses
              137 views
              0 likes
              Last Post M4ndoo
              by M4ndoo
               
              Started by M4ndoo, 04-19-2026, 05:54 PM
              0 responses
              68 views
              0 likes
              Last Post M4ndoo
              by M4ndoo
               
              Started by cmoran13, 04-16-2026, 01:02 PM
              0 responses
              120 views
              0 likes
              Last Post cmoran13  
              Started by PaulMohn, 04-10-2026, 11:11 AM
              0 responses
              71 views
              0 likes
              Last Post PaulMohn  
              Working...
              X