• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

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:	1
Size:	128.8 KB
ID:	1057605


    Paul H.NinjaTrader Customer Service

    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?
        Paul H.NinjaTrader Customer Service

        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
            Paul H.NinjaTrader Customer Service

            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


                #8
                i want to say thanks to share these taps
                Redtube Beeg Spankbang
                Last edited by touchara; Yesterday, 01:50 PM.

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by roblogic, Yesterday, 04:06 AM
                1 response
                10 views
                0 likes
                Last Post koganam
                by koganam
                 
                Started by nepenthean, Today, 08:01 AM
                0 responses
                7 views
                0 likes
                Last Post nepenthean  
                Started by bxl2019, Today, 07:59 AM
                0 responses
                4 views
                0 likes
                Last Post bxl2019
                by bxl2019
                 
                Started by tcfcigvo, Yesterday, 11:39 PM
                0 responses
                8 views
                0 likes
                Last Post tcfcigvo  
                Started by BobbyWan, Yesterday, 09:15 PM
                0 responses
                11 views
                0 likes
                Last Post BobbyWan  
                Working...
                X