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

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 and order string values, with or without case, with or without culture specific ordering.


    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 judysamnt7, 03-13-2023, 09:11 AM
            4 responses
            57 views
            0 likes
            Last Post DynamicTest  
            Started by ScottWalsh, Today, 06:52 PM
            4 responses
            36 views
            0 likes
            Last Post ScottWalsh  
            Started by olisav57, Today, 07:39 PM
            0 responses
            7 views
            0 likes
            Last Post olisav57  
            Started by trilliantrader, Today, 03:01 PM
            2 responses
            19 views
            0 likes
            Last Post helpwanted  
            Started by cre8able, Today, 07:24 PM
            0 responses
            9 views
            0 likes
            Last Post cre8able  
            Working...
            X