Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Declaring Lines at class level

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

    Declaring Lines at class level

    Hello

    I am trying to draw some lines that could later on be removed in certain conditions, but when I try to declare the line at class level, I am getting two errors:
    - Line is an ambiguous reference between NinjaTraderGUILine and NinjaTraderScriptDrawingToolsLine
    - Cannot implicitly convert type NinjaTraderScriptDrawingToolsLine to NinjaTraderGuiLine
    My code:
    Code:
    public class DivergenceIndicator : Indicator
    {
         private NinjaTrader.NinjaScript.DrawingTools.Line negDiv1;
    ...
    protected override void OnBarUpdate()
    {
        Line negDiv1 = Draw.Line(this...)
    If I did this code, it works, but I was hoping I was gonna be able to do away with that when declaring every line and, as I expected, I can't use the negDiv1 line out of scope...

    Code:
    NinjaTrader.NinjaScript.DrawingTools.Line negDiv1 = Draw.Line(this...)
    I am trying to replicate this code from Ninja Script Best Practices - Performance - Marking Objects for Garbage Collection, but they don't say how do they do myDot in the first place

    Code:
    protected override void OnBarUpdate()
    {
      // saving "myDot" creates an additional reference in memory
      Dot myDot = Draw.Dot(this, "myDot" + CurrentBar, false, Time[0], Close[0], Brushes.Blue);
    
      if (conditionToRemove)
      {
        // remove draw object will remove the object from the chart
        RemoveDrawObject("myDot");
    
        // but your local object "myDot" is still stored in memory.
        // Explicitly setting to null will ensure object is marked for garbage collection
        myDot = null;
      }
    }
    Last edited by itrader46; 02-06-2020, 09:58 AM.

    #2
    Hello itrader46,

    Thanks for your post.

    The help guide example is defining myDot locally as a Dot object in OnBarUpdate(), there is no other creation needed as myDot is only accessed in OnBarUpdate(). This would be the same as if you created a local variable in OnBarUpdate such as double MyDouble = Close[0]; Local variables are only valid in the method, so when created in OnBarUpdate() you would not be able to use MyDouble in say OnRender().

    You can define your line in OnBarUpdate() in the same manner as the help guide, as you showed with Line negDiv1 = Draw.Line(this...) BUT you cannot also use the class level declaration or you will get the errors you observed.

    If you want to keep it as a class level, then in OnBarUpdate() you only need: negDiv1 = Draw.Line(this...);

    Comment


      #3
      If you want to keep it as a class level, then in OnBarUpdate() you only need: negDiv1 = Draw.Line(this...);
      That gives me 'The name negdiv1 does not exist in the current context' error - because it is not declared at class level, as I can't find how to do that.

      Also, I can't just declare it in OnBarUpdate, as it will be drawn under certain conditions and then I'm back to the first part of my post, not being able to use it later

      Comment


        #4
        Hello itrader46,

        Thanks for your reply.

        Sorry, I was not clear, to use at the class level you need your private NinjaTrader.NinjaScript.DrawingTools.Line negDiv1; but in the OnBarUpDate() what I was meaning was you only need:negDiv1 = Draw.Line(this...);

        Comment


          #5
          Got it now... didn't realise I was declaring it twice.

          Is there a way to remove the line after that by its name, rather by the tag? My tags are quite long and error-prone if I want change something, then I have to change the tag of the line to be removed, so any workaround that would be useful.

          How about that?

          Code:
          negDiv1 = Draw.Line(this,...)
          
          if (conditionToRemove && negDiv1 != null)
             negDiv1 = null;
          Last edited by itrader46; 02-06-2020, 11:04 AM.

          Comment


            #6
            Hello itrader46,

            Thanks for your reply.

            The recommended way would be to use RemoveDrawObject("tagname"); https://ninjatrader.com/support/help...drawobject.htm

            Comment


              #7
              Hi Paul
              I have one more question about this subject: how can I know the different paths for declaring different types of objects?

              I found this path " NinjaTrader.NinjaScript.DrawingTools.Line " for declaring lines somewhere in the help guide and I don't remember where now, but what if I wanted to declare something else in the future?

              Comment


                #8
                Hello itrader46,

                Thanks for your reply.

                In the Ninjascript editor, if you just type NinjaTrader.NinjaScript.DrawingTools and then the period key, you will see a drop down list of all items.

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                0 responses
                647 views
                0 likes
                Last Post Geovanny Suaza  
                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                0 responses
                369 views
                1 like
                Last Post Geovanny Suaza  
                Started by Mindset, 02-09-2026, 11:44 AM
                0 responses
                108 views
                0 likes
                Last Post Mindset
                by Mindset
                 
                Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                0 responses
                572 views
                1 like
                Last Post Geovanny Suaza  
                Started by RFrosty, 01-28-2026, 06:49 PM
                0 responses
                573 views
                1 like
                Last Post RFrosty
                by RFrosty
                 
                Working...
                X