Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Find and assign a specific horizontal line

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

    Find and assign a specific horizontal line

    Hi, my indicator draws a horizontal line, with tag name "My_Horizontal_Line".
    When this horizontal line is moved, its y-value changes. I capture this value and make other calculations with it.

    This is my current code:

    PHP Code:
    
    HorizontalLine HL;
    private double HLValue;
    .
    .
    .
    
    Draw.HorizontalLine(this, "My_Horizontal_Line", Close[0], Brushes.Blue)
    .
    .
    .
    
    
    protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
    {
         try
         {
              base.OnRender(chartControl, chartScale);
    
              foreach (Gui.NinjaScript.IChartObject thisObject in ChartPanel.ChartObjects)
              {
                   if(thisObject is NinjaTrader.NinjaScript.DrawingTools.HorizontalLine &&
                      DrawObjects["My_Horizontal_Line"] as DrawingTools.HorizontalLine != null )
                   {
                            
                        HL          = thisObject as NinjaTrader.NinjaScript.DrawingTools.HorizontalLine;
                        HLValue = HL.StartAnchor.Price;
                   }
              } 
    
    When I have many horizontal lines on the chart, I have a problem identifying this particular line and assign HL and HLValue.

    I would appreciate it if you could share a way to find this line and assign HL to it.

    Thanks.

    #2
    Hello Rainmakersg,

    If your horizontal line has a fixed name then you should be able to just always select that tool. You don't need to loop over the collection if you know the tag name.

    You just need the following line of code: DrawObjects["My_Horizontal_Line"]

    Code:
    if (DrawObjects["My_Horizontal_Line​"] != null && DrawObjects["My_Horizontal_Line​"] is DrawingTools.HorizontalLine​)
    {
        // Do something with the drawing tool line
    }​

    Comment


      #3
      Hi Jesse,

      I need this line to run within OnRender:

      HL = thisObject as NinjaTrader.NinjaScript.DrawingTools.HorizontalLin e

      The loop looks for all horizontal lines and assigns the found line as HL.


      Inserting this line in your if statement doesn't work.

      Comment


        #4
        Hello Rainmakersg,

        I would suggest taking a look at first sample that we have in the help guide, that shows how to find an object based on its tag directy. As mentioned you just need to use this syntax to find the line, you wouldn't need the loop.

        DrawObjects["My_Horizontal_Line​"]

        That extracts the object from the collection based on its tag. To make that into a variable you can use the code you have shown with that syntax. You still need to use a condition that checks if its not null.

        if (DrawObjects["My_Horizontal_Line​"] != null && DrawObjects["My_Horizontal_Line​"] is DrawingTools.HorizontalLine​)
        {
        HL = DrawObjects["My_Horizontal_Line​"] as NinjaTrader.NinjaScript.DrawingTools.HorizontalLin e;
        ​}​


        Comment


          #5
          Hi Jesse, I need it to be in OnRender and not in OnBarUpdate. Reason is because:

          1. When the line is moved, the y value moves smoothly independently of the bar update status
          2. I need to attach a label to the line that moves smoothly too, as per this indicator by Chris: https://ninjatraderecosystem.com/use...izontal-lines/

          I am mostly modifying Chris' code. Problem with his code is he labels ALL horizontal lines, but I only need to find the line that is drawn by my indicator and label that one only.

          Comment


            #6
            Hello Rainmakersg,

            I did not suggest putting that code in OnBarUpdate, you can use that from OnRender if you want however constantly checking for an object in OnRender will cause a performance impact. For better performance you could find the object based on bar update events and assign a variable.

            The code to find an object by its tag is the following syntax:

            DrawObjects["YourObjectsTag​"]

            Comment


              #7
              Thanks Jesse, I got it resolved

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
              0 responses
              576 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
              553 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