Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

adress NinjaScript object from a nested class

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

    adress NinjaScript object from a nested class

    Hello,

    What I would like to do is to use the command Draw.Dot(this, ...from a nested class.
    From NinjaTrader Help, I understand that the NinjaScriptBase owner "this" has to be replaced by the NinjaScript object which is calling the draw method.
    I tried to create a object of the main class within the nested class, its compiles but after that NinjaTrader crashes as soon as I intend to open the indicator window.

    I've been through the help, the forum and google but i cant figure out what is the syntax for what should replace "this", any clue would be welcome.


    namespace NinjaTrader.NinjaScript.Indicators
    {
    public class MainClass : Indicator
    {
    public class NestedClass: Indicator
    {
    public int Dot;
    // MainClass mainClass = new MainClass(); //compiling but crashes NinjaTrader when any indicator is added to a chart

    public void DRAW()
    {
    Dot += 1;
    Print(String.Format("NestedClass Dot: {0}", Dot ));
    Draw.Dot(this, String.Format("Dot{0}" , Dot ), true, 0, 1284, Brushes.Red); // compiling but doesnt display, "this": what I would like to replace
    }
    }
    NestedClass nestedClass = new NestedClass();


    protected override void OnBarUpdate()
    {
    if ( CurrentBars[0] < 1 ) return;
    if ( High[0] > High[1]) nestedClass.DRAW(); // what I would like to do

    // all below is OK
    // if ( High[0] > High[1]) DRAW();
    }

    // public int Dot;

    // public void DRAW()
    // {
    // Dot += 1;
    // Draw.Dot(this, String.Format("Dot{0}" , Dot ), true, 0, High[0], Brushes.Red);
    // }
    }
    }
    Attached Files

    #2
    Hello hallex,

    Just pass the instance into your method or classes constructor, heres a quick example:

    Code:
    public class MyClass
    {
        private Indicator instance;
        public MyClass(Indicator indicator)
        {
            instance = indicator;
        }
    
        public void MyDraw()
        {
            Draw.Dot(instance, "tag", true, 0, instance.Close[0], Brushes.Red);
        }
    }
    public class Test : Indicator
    {
        private MyClass myClassInstance;
    
        protected override void OnStateChange()
        {
            if (State == State.DataLoaded)
            {
                myClassInstance = new MyClass(this);
            }
    
        }
    
        protected override void OnBarUpdate()
        {
            myClassInstance.MyDraw();    
        }
    }
    Your class won't need to inheret from Indicator, you just need to pass the Indicator type instance to the class, or "this":
    Code:
    myClassInstance = new MyClass(this);
    Once you pass an instance, you can use the instance to access any of the public methods or properties of the Indicator base. One example is using it with Draw:

    Code:
    Draw.Dot(instance, "tag", true, 0, instance.Close[0], Brushes.Red);

    I look forward to being of further assistance.
    Last edited by NinjaTrader_Jesse; 10-08-2019, 09:09 AM. Reason: Draw is actually not valid for the public method name
    JesseNinjaTrader Customer Service

    Comment


      #3
      Thanks Jesse, i was so far from the truth

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by mmckinnm, Today, 01:34 PM
      3 responses
      5 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Started by f.saeidi, Today, 01:32 PM
      2 responses
      5 views
      0 likes
      Last Post f.saeidi  
      Started by alifarahani, 04-19-2024, 09:40 AM
      9 responses
      55 views
      0 likes
      Last Post NinjaTrader_Jesse  
      Started by Conceptzx, 10-11-2022, 06:38 AM
      3 responses
      60 views
      0 likes
      Last Post NinjaTrader_SeanH  
      Started by traderqz, Today, 12:06 AM
      9 responses
      16 views
      0 likes
      Last Post NinjaTrader_Gaby  
      Working...
      X