Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to move a user drawn object

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

    How to move a user drawn object

    How can I move a user drawn object (in this case a vertical line) without changing its other properties?

    I've gotten this far:

    Code:
    foreach (IDrawObject draw in DrawObjects) 
    				{
    					if (draw.UserDrawn && draw is IVerticalLine) 
    					{

    #2
    Hello,

    Thanks for the forum post.

    You are almost there! You simply can use the draw object and modify any of its properties by setting it to a different value. This will only affect this property.



    The below code changes the lines color for example and there are more properties you can change.

    foreach (IDrawObject draw in DrawObjects)
    {
    // Finds line objects that are attached globally to all charts of the same instrument
    if (draw.Tag.StartsWith("@") && draw is ILine)
    {
    ILine globalLine = (ILine) draw;

    // Changes the line color and prints its starting and end points
    globalLine.Pen.Color = Color.Black;
    Print("Start: " + globalLine.StartBarsAgo + " End: " + globalLine.EndBarsAgo);
    }
    BrettNinjaTrader Product Management

    Comment


      #3
      Thanks Brett,

      I guess I need a further pointer on how to access and alter the BarsAgo property itself, I am not finding the documentation for it.

      I tried globalLine.BarsAgo, but it doesn't look accessible that way.

      Comment


        #4
        Hello,

        Well you wouldn't use line since the drawing object is a verticle line. They are close but slightly different.



        Then BarsAgo is available.

        IVerticalLine vLine = (vLine) draw;
        // Print the number of bars ago the line is located at
        Print(vLine.BarsAgo);
        BrettNinjaTrader Product Management

        Comment


          #5
          Sorry if I am missing something simple here but how do I translate that into actually moving the line.

          I am thinking I need to be able to say something like:

          vLine.BarsAgo = 0;

          In other words, how do I change (not just display) that property?

          Comment


            #6
            Hello,

            I just went ahead and created some sample code for you. In this case every object that starts with an @ sign will be moved on each onBarUpdate to the current bar.

            foreach (IDrawObject draw in DrawObjects)
            {
            // Finds line objects that are attached globally to all charts of the same instrument
            if (draw.Tag.StartsWith("@") && draw is IVerticalLine)
            {
            IVerticalLine globalLine = (IVerticalLine) draw;

            // Changes the line color and prints its starting and end points
            globalLine.BarsAgo = 0;
            }
            }
            BrettNinjaTrader Product Management

            Comment


              #7
              Here's what I have that isn't working, The lines change color, but they do not move. I checked and the lines are not locked.

              Code:
              foreach (IDrawObject draw in DrawObjects) 
              				{
              					if (draw.UserDrawn && draw is IVerticalLine) 
              					{
              						IVerticalLine measuringLine = (IVerticalLine) draw;
              						measuringLine.Pen.Color = Color.Red;
              						Print(measuringLine.BarsAgo);
              						measuringLine.BarsAgo = 0;
              					}
              				}

              Comment


                #8
                Hello,

                Thanks for the note.

                The object you have drawn is a vertical line correct? Do you get the print that you added in? What does it Print?

                Can you try printing it after you make the change as well?

                I look forward to assisting you further.
                BrettNinjaTrader Product Management

                Comment


                  #9
                  Yes they are 2 vertical lines. The updated code and output are below:

                  Code:
                  foreach (IDrawObject draw in DrawObjects) 
                  				{
                  					if (draw.UserDrawn && draw is IVerticalLine) 
                  					{
                  						IVerticalLine measuringLine = (IVerticalLine) draw;
                  						measuringLine.Pen.Color = Color.Red;
                  						Print(measuringLine.BarsAgo);
                  						measuringLine.BarsAgo = 0;
                  						Print("ok, I shoulda moved but I didn't see: " + measuringLine.BarsAgo);
                  					}
                  				}
                  231
                  ok, I shoulda moved but I didn't see: 231
                  332
                  ok, I shoulda moved but I didn't see: 332

                  Comment


                    #10
                    strange. Would expect this to work let me test this really quick on my side.
                    BrettNinjaTrader Product Management

                    Comment


                      #11
                      Still checking into, thanks for your patience.
                      BrettNinjaTrader Product Management

                      Comment


                        #12
                        Hello,

                        Checked into with development.

                        This is actually purposely not allowed, as development does not want malicious code capable of moving peoples drawing objects.

                        Therefor you would need to draw this vertical line with the script to be able to move it unfortunately.

                        Let me know if I can be of further assistance.
                        BrettNinjaTrader Product Management

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                        0 responses
                        579 views
                        0 likes
                        Last Post Geovanny Suaza  
                        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                        0 responses
                        334 views
                        1 like
                        Last Post Geovanny Suaza  
                        Started by Mindset, 02-09-2026, 11:44 AM
                        0 responses
                        101 views
                        0 likes
                        Last Post Mindset
                        by Mindset
                         
                        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                        0 responses
                        554 views
                        1 like
                        Last Post Geovanny Suaza  
                        Started by RFrosty, 01-28-2026, 06:49 PM
                        0 responses
                        551 views
                        1 like
                        Last Post RFrosty
                        by RFrosty
                         
                        Working...
                        X