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

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.
      Chris L.NinjaTrader Customer Service

      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.

          Chris L.NinjaTrader Customer Service

          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 ghoul, Today, 06:02 PM
              2 responses
              12 views
              0 likes
              Last Post ghoul
              by ghoul
               
              Started by jeronymite, 04-12-2024, 04:26 PM
              3 responses
              44 views
              0 likes
              Last Post jeronymite  
              Started by Barry Milan, Yesterday, 10:35 PM
              7 responses
              20 views
              0 likes
              Last Post NinjaTrader_Manfred  
              Started by AttiM, 02-14-2024, 05:20 PM
              10 responses
              180 views
              0 likes
              Last Post jeronymite  
              Started by DanielSanMartin, Yesterday, 02:37 PM
              2 responses
              13 views
              0 likes
              Last Post DanielSanMartin  
              Working...
              X