Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Call custom indicator, when name in string

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

    Call custom indicator, when name in string

    Hello!
    I'm writing some indicator and have the following situation. The user imports the some required indicator in Ninja Trader. Then enter this indicator name in one of the parameters defined as string in my indicator. In my indicator code, I have a string variable with the name of another indicator. How I can call this another indicator if I have his name in string variable? If anyone can, please give an example, please.

    #2
    HotBird,

    The best way to do this is to create a switch or a bunch of if / if else statements comparing an input string to something you expect then executing different code based on what the input string is.

    For example :

    Code:
    double value;
    
    if( inputString == "EMA")
    {
        value = EMA(period)[0];
    }
    else if ( inputString == "SMA")
    {
       value = SMA(period)[0]
    }
    
    //etc,

    Theres also other ways of comparing strings but I chose the simplest example. See the following link for more information.

    Learn how to compare strings in C# for equality and sort order, how ordinal and culture-aware comparisons differ, and how to choose a StringComparison value.


    Please let me know if I may assist further.
    Adam P.NinjaTrader Customer Service

    Comment


      #3
      Yes, thank you. I know this code. But the fact is that I don`t know what exactly indicator import a user and can` t predict what code to run. I mean that it can be a new specific name.

      Comment


        #4
        Hi HotBird,

        Sorry, I'm not sure how you would solve this. What might you be looking for in NinjaTrader that would allow you to progress on this?
        Ryan M.NinjaTrader Customer Service

        Comment


          #5
          I have read many forums for C #. I found that this problem can be solved through reflection.
          Like this.
          Code:
          using System;
          using System.Reflection;
          
          static class Methods
          {
              public static void Inform(string parameter)
              {
          	Console.WriteLine("Inform:parameter={0}", parameter);
              }
          }
          
          class Program
          {
              static void Main()
              {
          	// Name of the method we want to call.
          	string name = "Inform";
          
          	// Call it with each of these parameters sequentially.
          	string[] parameters = { "Sam", "Perls" };
          
          	// Get MethodInfo.
          	Type type = typeof(Methods);
          	MethodInfo info = type.GetMethod(name);
          
          	// Loop over parameters.
          	foreach (string parameter in parameters)
          	{
          	    info.Invoke(null, new object[] { parameter });
          	}
              }
          }
          How can I use reflection in NinjaTrader script for calling indicator as method?

          Comment


            #6
            HotBird,

            NinjaTrader uses C# so anything you can do with C# will work in NinjaTrader as long as you remain within the context of its engineering (i.e. namespaces, etc.). If you are already aware of a method to do this, it should work. However, unfortunately this level of coding is unsupported.

            Please let me know if I may assist further.
            Adam P.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by SalmaTrader, 07-07-2026, 10:26 PM
            0 responses
            26 views
            0 likes
            Last Post SalmaTrader  
            Started by CarlTrading, 07-05-2026, 01:16 PM
            0 responses
            15 views
            0 likes
            Last Post CarlTrading  
            Started by CaptainJack, 06-17-2026, 10:32 AM
            0 responses
            9 views
            0 likes
            Last Post CaptainJack  
            Started by kinfxhk, 06-17-2026, 04:15 AM
            0 responses
            10 views
            0 likes
            Last Post kinfxhk
            by kinfxhk
             
            Started by kinfxhk, 06-17-2026, 04:06 AM
            0 responses
            17 views
            0 likes
            Last Post kinfxhk
            by kinfxhk
             
            Working...
            X