• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Access caller indicator

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

    Access caller indicator

    in indicator, lets say i call:

    Code:
    Namespace NinjaTrader.NinjaScript.Indicators {
    
      public class Sample_Indi: Indicator
      {
          MyAddon x= new MyAddon();
          ....
          OnBarUpdate(){
              x.ExampleMethod();   //note, i am not passing `this`
          }
      }
    however, in MyAddon , i have ExampleMethod method, where i want to get the caller indicator instance. How to achieve that? this doesnt work
    Code:
    public void ExampleMethod(){
    
           base.TickSize;    // <--- doesnt work
           this.TickSize;      // <--- doesnt work
           parent.TickSize; // <--- doesnt work
    
    }

    #2
    Hello,

    Yes, I wouldn't expect this to work, you cannot call "new" on any NinjaScript type. The platform would need to instantiate the Addon in the correct way for it to be active and used in the platform as an addon. There is no use case for an indicator to call on an addon specifically because you would need the addons instance. Did you instead mean to make a standard Class instead inheriting from an Addon?

    Can you provide details on what specifically your overall goal is with this syntax? it is not apparent what you are trying to do here.

    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Jesse,
      i am confused.
      I have my library methods in addon, whiich I instantiate in every indicator, exactly like the above example, and then I call my external methods, like this:

      OnBarUpdate(){
      .....
      my.SomethingMethod();
      ....
      }

      Just now i needed a way to access the caller indicators instance inside them, and dont know how to do that.

      I know using:

      my.SomethingMethod( this );

      works, but i want to achieve that without passing the `this`

      Comment


        #4
        Hello,

        To clarify, are you referring to "MyAddon" as a standard class or as an Addon? I want to be specific here because you should not be calling:
        Code:
        MyAddon x= new MyAddon();
        if this is inheriting from: NinjaTrader.NinjaScript.AddOnBase
        If this is just a class that inherits from nothing and is just named like an addon, that is different.

        If you are trying to access methods in an addon by doing this, I really wouldn't advise using this process, you should instead use a partial indicator class or a static class which is not in an addon specifically.

        I still want to clarify what your overall goal is here so I can provide help, are you trying to make a utility class that has functions that can be used by any indicator?

        If so, you should likely be using a partial indicator class or a static class instead of an addon. Can you provide details on what your end goal is for this question?

        Please let me know if I may be of further assistance.
        JesseNinjaTrader Customer Service

        Comment


          #5
          1) can you tell me why i shouldnt use "new MyAddon();" ? I have a class in my addon (not static), which contains a list of methods, and I use this approach already for months. What is the problem? why i should use static? and yes, it inherits "NinjaTrader.NinjaScript.AddOnBase"

          2) yes, i want to have a class, that i use from any indicators. and no, using partial class is not a solution for me, because that library file is shared to other platforms too (based on c#), so everything works well till now. Just wanted to know, how to access current indicator instance. is passing the only way?

          Comment


            #6
            Hello,

            Thank you for clarifying.

            In this case, I would suggest making just a normal class instead of an addon class based on your requirements.

            The platform automatically instanciates classes that inherit from AddonBase at startup and during compiles so it would not be suggested to use this type of class if you will be calling new yourself. Because of how the platform creates addons you will end up with extra instanciated classes for no reason other than the platform did that because it's supposed to. Also if you are going to export this, if you include an addon that will require that the end user to agree to import the addon which they may not be expecting if they are importing an indicator.

            For this purpose, you would instead want a standard C# class with no inheritance if the purpose will be for extra methods or shared code for indicators that can be exported. This class can be placed in the addons folder and can also be generated using the addon wizard, you would simply remove the overrides and inheritance of AddonBase from the class. After doing that, when exporting you can still select the file as normally would to be included.

            In your first example, you are calling base. properties which would only be relevant to the context you are in which is an Addon. There is no way to access the indicator without its instance, so yes you would need to pass in the instance of the indicator to access its properties from another class.

            Your method would need to have an overload for your indicator if you want to use it in that class:

            Code:
            public void ExampleMethod(MyIndicatorType indicator){
                double tickSize =  indicator.TickSize;
            }

            Please let me know if I may be of further assistance.
            JesseNinjaTrader Customer Service

            Comment


              #7
              Excellent answer, now i understand it.

              1 question. instead of

              public void XYZ (MyIndicatorType indi)

              can I use generic, i.e.


              public void XYZ (Indicators.Indicator indi)


              Because specific indicator may be or not installed on NT, and if not installed, then the first example gives error (MyIndicatorType not found) and my whole library file wont work.

              Instead, I want to just declare the generic method, where any indicator's `this` can be passed.

              I am sure you undertand what i say.
              Last edited by TazoTodua; 02-26-2018, 03:36 PM.

              Comment


                #8
                Hello,

                Yes, you can use the indicators base type which all indicators inherit from, but you would not see any indicator specific properties. For example, if you passed in the Bollinger bands, you could see its TickSize but not the Bollinger's plots like Upper.
                Code:
                public void ExampleMethod(NinjaTrader.NinjaScript.IndicatorBase indicator){
                    double tickSize =  indicator.TickSize;
                	indicator.Print("My Tick Size is: " + tickSize);
                	//or more preferred use the output.process method when not in a NinjaScript file
                	NinjaTrader.Code.Output.Process("My Tick Size is: " + tickSize, PrintTo.OutputTab1);
                }
                Please let me know if I may be of further assistance.
                JesseNinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by akuntysh, Today, 12:02 AM
                0 responses
                2 views
                0 likes
                Last Post akuntysh  
                Started by Revan, Yesterday, 11:53 PM
                0 responses
                2 views
                0 likes
                Last Post Revan
                by Revan
                 
                Started by allangl, Yesterday, 10:50 PM
                0 responses
                7 views
                0 likes
                Last Post allangl
                by allangl
                 
                Started by jeffm, Yesterday, 10:47 PM
                0 responses
                4 views
                0 likes
                Last Post jeffm
                by jeffm
                 
                Started by quinton77, Yesterday, 09:59 PM
                0 responses
                3 views
                0 likes
                Last Post quinton77  
                Working...
                X