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] }
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!
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
NinjaTrader
Dynamically instantiate indicators
Collapse
X
-
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:Tags: None
-
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.
-
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
-
Originally posted by NinjaTrader_ChrisL View PostHello 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
-
-
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by rbeckmann05, Yesterday, 06:48 PM
|
1 response
12 views
0 likes
|
Last Post
by bltdavid
Today, 05:43 AM
|
||
Started by llanqui, Today, 03:53 AM
|
0 responses
6 views
0 likes
|
Last Post
by llanqui
Today, 03:53 AM
|
||
Started by burtoninlondon, Today, 12:38 AM
|
0 responses
10 views
0 likes
|
Last Post Today, 12:38 AM | ||
Started by AaronKoRn, Yesterday, 09:49 PM
|
0 responses
15 views
0 likes
|
Last Post
by AaronKoRn
Yesterday, 09:49 PM
|
||
Started by carnitron, Yesterday, 08:42 PM
|
0 responses
11 views
0 likes
|
Last Post
by carnitron
Yesterday, 08:42 PM
|
Comment