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
No announcement yet.
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.
Comment
-
Ok, do you ever plan on opening the AI Generate code for us to see?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 NullPointStrategies, Today, 05:17 AM
|
0 responses
52 views
0 likes
|
Last Post
|
||
|
Started by argusthome, 03-08-2026, 10:06 AM
|
0 responses
130 views
0 likes
|
Last Post
by argusthome
03-08-2026, 10:06 AM
|
||
|
Started by NabilKhattabi, 03-06-2026, 11:18 AM
|
0 responses
70 views
0 likes
|
Last Post
|
||
|
Started by Deep42, 03-06-2026, 12:28 AM
|
0 responses
44 views
0 likes
|
Last Post
by Deep42
03-06-2026, 12:28 AM
|
||
|
Started by TheRealMorford, 03-05-2026, 06:15 PM
|
0 responses
49 views
0 likes
|
Last Post
|

Comment