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 Rapine Heihei, Today, 07:51 PM
      0 responses
      3 views
      0 likes
      Last Post Rapine Heihei  
      Started by frslvr, 04-11-2024, 07:26 AM
      5 responses
      96 views
      1 like
      Last Post caryc123  
      Started by algospoke, 04-17-2024, 06:40 PM
      6 responses
      49 views
      0 likes
      Last Post algospoke  
      Started by arvidvanstaey, Today, 02:19 PM
      4 responses
      11 views
      0 likes
      Last Post arvidvanstaey  
      Started by samish18, 04-17-2024, 08:57 AM
      16 responses
      61 views
      0 likes
      Last Post samish18  
      Working...
      X