Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Drawing a line between two times

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

    Drawing a line between two times

    I wish create an indicator that draws a line the open of the 10:30 am 1 minute candle to the close of the 10:50 am candle in both Ninja 7 then Ninja 8. How would I go about this?

    #2
    Hello cptrader,

    Thanks for your post.

    In general you would test for the end time, something like:

    NT7 or NT8: if (ToTime(Time[0]) == ToTime(10, 50, 00))
    {
    // draw the line)
    }


    The above will work if you are using time based bars. If you are using non time based bars, such as range or renko then you would not be sure to get a tick at that precise moment. In that case you would want to use a range of time such as:

    NT7 or NT8: if (ToTime(Time[0] >= ToTime(10, 50, 00) && ToTime(Time[0]) < ToTime(10, 55, 00) && doitonce)
    {

    // draw line
    doitonce = false; // set to false so we only draw line once on the first time the condition is true
    }

    doitonce would be a class level bool that you would create/declare and initialize to True (you can use any name you like). A bool here is needed so that you only draw the line on the first time that the range of time becomes true which would likely be close to 10:50 without needing to be exactly 10:50:00. You would also need to reset the bool to true on the first bar of the session, something like:

    NT7: if (Bars.FirstBarOfSession)
    NT8: if (Bars.IsFirstBarOfSession)
    {
    doitonce = true;
    }


    Once you have a valid condition you can then draw the line.
    NT7: http://ninjatrader.com/support/helpG...m?drawline.htm
    NT8: http://ninjatrader.com/support/helpG...?draw_line.htm

    In the above methods you have the option to specify the start and end of the line by a datetime or by a bars ago.
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      I played with draw line to get code below which works.
      The line only shows up on current day. What would I add to get it to show on all days.
      Also what would I add to increase the line thickness

      if (ToTime(Time[0]) == ToTime(10, 50, 00))
      DrawLine("line", 50, Open[50], 0, Close[0], Color.Blue);

      Comment


        #4
        Hello cptrader,

        Thanks for your reply.

        The draw methods will draw an object with a specific tag name that you assign. In this case you used "line". Draw objects must have a unique tag name so if a draw object with that tag exists on the chart already, it is erased and the newest one is drawn. To show all of the lines you would need to create a new tagname each time you draw the line. An easy way to accomplish this is to change to a tag line that includes the current bar number. For example DrawLine("line"+CurrentBar, 50, Open[50], 0, Close[0], Color.Blue);
        Reference: http://ninjatrader.com/support/helpG...currentbar.htm

        To draw a line and be able to specify the line characteristics you would need to use the other syntax of: DrawLine(string tag, bool autoScale, int startBarsAgo, double startY, int endBarsAgo, double endY, Color color, DashStyle dashStyle, int width);

        Reference: http://ninjatrader.com/support/helpG...m?drawline.htm
        Paul H.NinjaTrader Customer Service

        Comment


          #5
          Excellent. Lines drawn as I was hoping. I use FXCM for some trading. I notice that for some charts a few minute bars are occasionally not printed. Possibly no trades in that minute?? As a result I need to use the method you described for the non time based bars.
          When I insert your code I get errors.
          if (ToTime(Time[0] >= ToTime(10, 50, 00) && ToTime(Time[0]) < ToTime(10, 55, 00) && doitonce)
          {DrawLine("line4"+CurrentBar, false, 39, Open[39], 0, Close[0], Color.Blue, DashStyle.Solid, 5);}
          Can you help me as to how and where I declare doitagain?
          Also where would I insert the reset
          if (Bars.FirstBarOfSession)
          {doitonce = true;}
          into code?

          Many thanks

          Comment


            #6
            Hello cptrader,

            Thanks for your reply.

            On this line: if (ToTime(Time[0] >= ToTime(10, 50, 00) && ToTime(Time[0]) < ToTime(10, 55, 00) && doitonce) I left off the last ")" so change it to look like: if (ToTime(Time[0] >= ToTime(10, 50, 00) && ToTime(Time[0]) < ToTime(10, 55, 00) && doitonce))

            In NT7 you can declare the bool in the #region Variables like: private bool doitonce = true;

            In NT8 you can delcare the same way but locate it just below the Public Class name...: Indicator line. Its actually the same place in both it is just that in NT7 we built the region Variables in.

            The reset statements can be anywhere in the OnBarUpdate() method, really should not matter.
            Paul H.NinjaTrader Customer Service

            Comment


              #7
              I declared the bool, Included the reset in on bar update and used the code
              if (ToTime(Time[0] >= ToTime(10, 50, 00) && ToTime(Time[0]) < ToTime(10, 55, 00) && doitonce))
              {DrawLine("line4"+CurrentBar, false, 5, Open[5], 0, Close[0], Color.Blue, DashStyle.Solid, 5);}
              and get error
              Operator '>=' cannot be applied to operands of type 'System.DateTime'and 'int'

              Comment


                #8
                Hello cptrader,

                Thanks for your reply.

                Please replace the if statement with:

                if (ToTime(Time[0]) >= ToTime(10, 50, 00) && ToTime(Time[0]) < ToTime(10, 55, 00) && doitonce)

                The error is created because I left off the ")" after the first Time[0]. Note that I also then needed to remove the last ")".
                Paul H.NinjaTrader Customer Service

                Comment


                  #9
                  The code now works but gives multiple lines from the open to the close of all candles in the 5 minute period. What would I need to change to just get on line,
                  if (ToTime(Time[0]) >= ToTime(10, 50, 00) && ToTime(Time[0]) < ToTime(10, 55, 00) && doitonce)
                  {DrawLine("line4"+CurrentBar, false, 5, Open[5], 0, Close[0], Color.Blue, DashStyle.Solid, 5);}

                  Comment


                    #10
                    Hello cptrader,

                    Thanks for your reply.

                    To draw the line all three conditions must be true. I think you are missing the statement to set doitonce to false when you draw the line:

                    Code:
                    if (ToTime(Time[0]) >= ToTime(10, 50, 00) && ToTime(Time[0]) < ToTime(10, 55, 00) && doitonce)
                    {
                    DrawLine("line4"+CurrentBar, false, 5, Open[5], 0, Close[0], Color.Blue, DashStyle.Solid, 5);
                    [B]doitonce = false;[/B]
                    }
                    Paul H.NinjaTrader Customer Service

                    Comment


                      #11
                      From looking at the code on a chart the line ends at 105000 and goes back 5 bars. I am not sure of its purpose of the <105500 time. You said the code was for non time based bars eg Renko and range.A fixed bar back will not give a time range. What would I need to change to achieve this purpose?
                      Also I wanted to put three time based lines on the chart using a repeat of code, with different parameters. When I did this only one line appeared. What would I need to change to get all three lines to show?

                      Thanks

                      Comment


                        #12
                        Hello cptrader,

                        Thanks for your post.

                        For non time based bars there is no guarantee of a bar close at a specific time so trying to do anything with code that is written like if (ToTime(Time[0]) == ToTime(10, 50, 00)) would be hit or miss at any given day as the code is looking at a very specific moment in time.

                        So a work around is to use a range of time so that you would be relatively assured that a bar is going to close in that range of time. The bool was used so that it would only trigger the draw line on the first instance of a bar close within the time period specified.Sort of like using a baseball mitt to catch a ball as opposed to using your bare hand.

                        Regarding multiple draw lines, you would need to ensure that you are using a different tag name for each line.

                        Paul H.NinjaTrader Customer Service

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by sidlercom80, 10-28-2023, 08:49 AM
                        166 responses
                        2,234 views
                        0 likes
                        Last Post sidlercom80  
                        Started by thread, Yesterday, 11:58 PM
                        0 responses
                        1 view
                        0 likes
                        Last Post thread
                        by thread
                         
                        Started by jclose, Yesterday, 09:37 PM
                        0 responses
                        6 views
                        0 likes
                        Last Post jclose
                        by jclose
                         
                        Started by WeyldFalcon, 08-07-2020, 06:13 AM
                        10 responses
                        1,414 views
                        0 likes
                        Last Post Traderontheroad  
                        Started by firefoxforum12, Yesterday, 08:53 PM
                        0 responses
                        11 views
                        0 likes
                        Last Post firefoxforum12  
                        Working...
                        X