Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Draw.VerticalLine() does not actually create lines

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

    Draw.VerticalLine() does not actually create lines

    I am new to NinjaScript and have written an indicator to read values from a CSV file and plot a vertical line on the day & time read from the file. I have log statements in that show it is getting the date values correctly, and calling Draw.VerticalLine()... Everything appears correct except no lines appear. Or on some versions, the last (most recent) line is drawn, but that is all. I added an iteration to the "tag" value to make sure each one is different, but just cannot get the lines to actually plot.

    I'll attach the .cs file and a sample CSV file in case anyone wants to see.
    Attached Files
    Last edited by DerkWehler; 07-02-2024, 07:23 AM.

    #2
    Hello DerkWehler,

    Thank you for your post.

    The tag for a drawing object is a unique ID used to reference that specific drawing object. If you reuse the same tag, each time you call a drawing object the same drawing object will be referenced and the existing drawing object will be modified instead of a new object being created.

    For example, if you tag your drawing object “myTag”, each time you use “myTag” the same drawing object will be modified.

    To create a new drawing object, use a new unique string as the tag each time you call a Draw method. As an easy way to make a new string unique add ‘CurrentBar’ to the string.

    Help Guide: NinjaScript > Language Reference > Common > OnBarUpdate() > CurrentBar

    The code below will create a new red dot one tick below the low each time it is called.

    Code:
    Draw.Dot(this, "myTag" + CurrentBar, true, 0, Low[0] - TickSize, Brushes.Red);

    If you are not seeing drawing objects when expected, I recommend debugging using prints.

    ​To understand why the script is behaving as it is, such as placing orders or not placing orders (or drawing objects or other actions) when expected, it is necessary to add prints to the script that print the values used for the logic of the script to understand how the script is evaluating.

    In the strategy add prints (outside of any conditions) that print the values of every variable used in every condition that places an order along with the time of that bar.​

    I'm also including a link to a forum post with further suggestions on debugging a script.
    https://support.ninjatrader.com/s/ar...nd-TraceOrders​​

    Comment


      #3
      Gaby:

      Thank you for the reply, but it would seem you neither read my post carefully (where I said "I added an iteration to the 'tag' value to make sure each one is different"), nor read/ran the included code, which has a print after each Draw.VerticalLine(). The log shows that each one is getting called with the correct parameters, yet still none show up.

      Comment


        #4
        Hello DerkWehler,

        My apologies! I was only clarifying what adding CurrentBar would do for the tag, since it seemed from your statement below that it wasn't clear why in some cases only the latest line is drawn.

        Or on some versions, the last (most recent) line is drawn, but that is all.
        I took another look at your .csv file - please note that the data series you apply this to will make a big difference. What data series interval are you testing on?

        For example, if you apply this to a 5 minute chart, a drawing object timestamped at (for example) 5:56:10 will not be drawn on any 5 minute bars since they will be timestamped 5:50:00AM, 5:55:00AM, 6:00:00AM etc. There is no 5:56:10 AM bar on a 5 minute chart. Even on a minute chart, this bar doesn't exist since the bars would be timestamped 5:55:00, 5:56:00, 5:57:00 etc with no second granularity.

        I use 5:56:10 AM as the example since this is info being read from your .csv file. Output below:

        Drew Line @ 6/26/2024 5:26:00 AM, with tag: 6/26/2024 5:26:00 AM_VLine_0


        However, if you apply your script to a 1-second chart, you'll see the drawing objects appear (I tested your script out to confirm). The screenshot below shows the yellow lines being drawn on the chart.
        Attached Files

        Comment


          #5
          Thanks for the reply. Yes, I was thinking of that, but was using 1-minute by default. Shouldn't that work?

          I tried 1-second actually, but still did not see lines. I loaded 8 days of 1-sec data, which covers the whole date range of the input CSV...

          As a side note, do you know why I had to explicitly define the path to the file, when it exists in the default %documents%\NinjaTrader 8 folder?

          Comment


            #6
            Hello,

            1 minute would not work for the reasons stated in my previous reply. From what I can see in the output, most or all of these timestamps from your csv file have seconds specified. 1 minute bars do not have seconds. They are timestamped by the minute.

            Here are just a couple of lines from the output, you can see they all have seconds specified:

            Code:
            Drew Line @ 6/26/2024 5:56:10 AM, with tag: 6/26/2024 5:56:10 AM_VLine_1
            
            Drew Line @ 6/26/2024 6:26:20 AM, with tag: 6/26/2024 6:26:20 AM_VLine_2
            
            Drew Line @ 6/26/2024 6:56:30 AM, with tag: 6/26/2024 6:56:30 AM_VLine_3
            
            Drew Line @ 6/26/2024 7:26:40 AM, with tag: 6/26/2024 7:26:40 AM_VLine_4
            
            Drew Line @ 6/26/2024 7:56:50 AM, with tag: 6/26/2024 7:56:50 AM_VLine_5


            For example, one of the times you pull from your file is 5:56:10 AM. There is no 5:56:10AM 1-minute bar. There is a 5:56:00AM bar and a 5:57:00am bar.

            I am able to see the lines drawn from your script when applied to a 1-second chart. If you look at the Drawing Objects window (right-click in the chart > Drawing tools > Drawing objects) are you seeing objects listed here?


            As far as I know, the path to the file should be explicitly defined, the script needs to be told where to look for the file. Even if it is in the NinjaTrader 8 folder, the script isn't by default going to look in this folder if it isn't told the entire path.
            Last edited by NinjaTrader_Gaby; 07-02-2024, 11:50 AM.

            Comment


              #7
              Very interesting, thank you. I did not catch that before (the idea that values w/seconds would not plot on M1).

              I still do not get lines on S1 chart though. I removed all indicators from the chart, removed all objects too, then loaded 8 days of data, and re-applied the ind. The output window listed the draws as usual, ending in these:

              ...
              Drew Line @ 28-Jun-24 17:30:12, with tag: 28-Jun-24 17:30:12_VLine_144
              Drew Line @ 28-Jun-24 18:00:20, with tag: 28-Jun-24 18:00:20_VLine_145
              Drew Line @ 28-Jun-24 18:30:28, with tag: 28-Jun-24 18:30:28_VLine_146
              Drew Line @ 28-Jun-24 19:00:36, with tag: 28-Jun-24 19:00:36_VLine_147
              Drew Line @ 28-Jun-24 19:30:44, with tag: 28-Jun-24 19:30:44_VLine_148
              Drew Line @ 28-Jun-24 20:00:52, with tag: 28-Jun-24 20:00:52_VLine_149

              When I open the objects list though, it is empty...

              But perhaps it would be better to focus on fixing the problem above first. Is there a way in NT given a time value like "28-Jun-24 20:00:52" to locate the candle closest to that time and then use it's time to send to VLine method?

              Not sure if this helps at all, but in metatrader 4, there is function called iBarShift(time), where you pass in a time and it gives you the index of the closest candle. Not sure if NT has something like that or if I just have to iterate over all the bars each time.
              Last edited by DerkWehler; 07-02-2024, 12:56 PM.

              Comment


                #8
                Hello,

                That is strange - I am seeing drawing objects listed there and displayed on the chart.

                What instrument are you using? How many days to load, and what End Date are you using?


                There is no function that gives you the closest bar index in NinjaTrader unfortunately, you would need to come up with your own logic for rounding the date/time up or down. A quick google search lead me to some results you may be interested in:

                Ive been looking for a proper rounding mechanism but nothing I find seems to be exactly what I need. I need to round up and round down seperately and I also need to account for the the case when its


                Comment


                  #9
                  I'm using MES (Sept exp). I loaded 1 second chart at around 2pm eastern time, with 8 days of data (enough to cover all entries in CSV file).

                  I will implement some logic to find the correct candle to draw to, and let you know how it goes.

                  Comment


                    #10
                    Made some progress... Put in code to use the candle which is <= the given time, where the next candle is > than that time. Now I just have to figure out how/when to plot based on State == Historical or not....

                    But you got me past the hurdle of realizing the time had to match exactly, so much appreciated!

                    ​ ​ Click image for larger version

Name:	image.png
Views:	113
Size:	47.2 KB
ID:	1309566

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                    0 responses
                    633 views
                    0 likes
                    Last Post Geovanny Suaza  
                    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                    0 responses
                    364 views
                    1 like
                    Last Post Geovanny Suaza  
                    Started by Mindset, 02-09-2026, 11:44 AM
                    0 responses
                    105 views
                    0 likes
                    Last Post Mindset
                    by Mindset
                     
                    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                    0 responses
                    567 views
                    1 like
                    Last Post Geovanny Suaza  
                    Started by RFrosty, 01-28-2026, 06:49 PM
                    0 responses
                    568 views
                    1 like
                    Last Post RFrosty
                    by RFrosty
                     
                    Working...
                    X