Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Draw.VerticalLine barsAgo not working

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

    Draw.VerticalLine barsAgo not working

    have been trying to add a basic vertical line to my chart that appears on 25 bars back however, the bar will only show if I put Zero or a Negative number in the barsAgo parameter, which of course pushes it out to the future. I have successfully added a vertical bar using the time parameter, but that is not what I am looking for.

    Some code I have tried with no luck is:

    Code:
    Draw.VerticalLine(this, "vlBarsBack", 25, Brushes.Red); //-FAILS
    VerticalLine vl = Draw.VerticalLine(this, "vlBarsBack", 25, false, ""); //-FAILS
    
    DateTime date1 = new DateTime(2019, 5, 15, 18, 0, 0);
    Draw.VerticalLine(this, "vlBarsBackTime", date1, Brushes.Blue); //- WORKS but not what I am looking for.
    Any thoughts? This seems like a bad question, but I cannot figure out where I am going wrong. All code is located in OnBarUpdate method.

    #2
    Hello [email protected],

    Thanks for your post.

    A 0 bars ago would place the vertical line at the current bar. A negative bars ago would, as you have observed, place the line in the future. Your code as written will only show 1 vertical line at a time and I am wondering if that is the issue. It only places 1 line because you are using the same "tag name" for each line and when the same tag name is used, a previously drawn object with the same name would be removed and replaced by the latest occurrence of the tag name.

    I've attached a screenshot of a quick example using a 10 period SMA crossing above a 30 period SMA as a trigger and the action is to draw a dot below the bar where the cross occurs and the also places a vertical line 25 bars ago. I then used the ruler tool to measure the number of bars between the dot and the vertical line, in each case was 25 bars ago. You can see the code if you wish to replicate. The tag name is supplemented with the CurrentBar number to create a unique per bar tag name allowing all the lines to be seen. reference: https://ninjatrader.com/support/help...currentbar.htm


    Click image for larger version

Name:	tmac-1.PNG
Views:	339
Size:	128.8 KB
ID:	1057605


    Comment


      #3
      Hi, yes the action I want is to only have 1 vertical line present, 25 bars back. As I showed I tried Draw.VerticalLine(this, "vlBarsBack", 25, Brushes.Red); and it draws nothing.

      Comment


        #4
        Hello [email protected],

        Thanks for your reply and clarification.

        When you apply the script to a chart, please check the "log" tab of the control center, do you see any errors related to the script?

        Comment


          #5
          Hi, yes the log has the following:

          Error on calling 'OnBarUpdate' method on bar 0: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.

          I as using on a USDJPY 5 minute chart with 10 days worth of data, so there are more than 25 bars.

          Comment


            #6
            Hello [email protected],

            Thanks for your reply.

            While you may have more than enough bars in your data series you need to understand that when a script is added to a chart, the script will start with the very first bar (bar 0) in the data series in this case 10 days ago. So if your code is trying to draw a line 25 bars ago on the very first bar it will produce the error message you see.

            What you would need to do is to have your script delay processing the code in OnBarUpdate() until the script has processed the first 25 or more bars. This is typically done by adding a CurrentBar check as the first line in the OnBarUpdate() method. If you look at my script example, you will see this line if CurrentBar < 25) return;

            Reference: https://ninjatrader.com/support/help...currentbar.htm

            Comment


              #7
              Thanks for the clarification. This now works after adding the if (CurrentBar < 25) check along with refreshing both the NinjaScript and this Historical data.

              Comment

              Latest Posts

              Collapse

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