Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Drawing a line in strategy at the value of an indicator

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

    Drawing a line in strategy at the value of an indicator

    Hi,

    I would like to draw a line in my Strategy, but the value of the line should be the value of the indicator.

    Are there examples out there or this would be a good place for it?

    Thanks


    #2
    Hello AgriTrdr,

    This would be possible using Draw.Line() or Draw.HorizontalLine().



    For example to draw at the price of a 14 SMA:
    if (CurrentBar > 10) // we'll draw the line over the last 10 bars.
    Draw.Line(this, "tag1", false, 10, SMA(14)[0], 0, SMA(14)[0], Brushes.LimeGreen, DashStyleHelper.Dot, 2);
    Also, below is a link to a forum post with helpful resources on getting started with NinjaScript and C#.


    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      So where you call "SMA(14)[0]", is the place where the indicator setting from what would be in a strategy State.Dataloaded would go?

      Comment


        #4
        Hello AgriTrdr,

        This is correct, calling the indicator would return the price for the plot as a double for that barsAgo index.

        This would be the startY and EndY parameters.

        Draw.Line(NinjaScriptBase owner, string tag, int startBarsAgo, double startY, int endBarsAgo, double endY, Brush brush)
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          If this was on a secondary data series, would it be:

          Code:
          if (CurrentBars[1] > 10) // we'll draw the line over the last 10 bars.
          Draw.Line(this, "tag1", false, 10, [B]SMA(BarrsArray[1], 14)[0][/B], 0, [B]SMA(BarrsArray[1], 14)[0][/B], Brushes.LimeGreen, DashStyleHelper.Dot, 2);​

          Comment


            #6
            Hello AgriTrdr,

            Drawing objects can only be drawn on the primary series, but you can use data from other series.

            The code you've suggested should work.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Hi Chelsea,

              Once a line is drawn how can any point of the line be accessed? For example, if Close[0] >= the equivalent to Line[0], do something. So, how can Line[0] be called?

              Thanks,

              Bob Perez

              Comment


                #8
                Hello Bob,

                You can assign the drawing object to a variable and then access this from the variable. The help guide has sample code.


                Line myLine = Draw.Line(this, "tag1", false, 10, SMA(14)[0], 0, SMA(14)[0], Brushes.LimeGreen, DashStyleHelper.Dot, 2);

                Print(myLine.StartAnchor.Price);
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Chelsea,

                  Can the line be accessed from the script if drawn manually by the user?

                  Comment


                    #10
                    Hello bobperez,

                    Yes, you can loop through the DrawObjects collection. The help guide provides sample code.
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      Thank you.

                      Comment


                        #12
                        Originally posted by NinjaTrader_ChelseaB View Post
                        Hello bobperez,

                        Yes, you can loop through the DrawObjects collection. The help guide provides sample code.
                        https://ninjatrader.com/support/help...rawobjects.htm
                        Chelsea,

                        How can I programmatically detect when a user-drawn line has been crossed over, either above or below it?

                        Bob

                        Comment


                          #13
                          Hello Bob,

                          From a script, you would need to calculate the y-intercept of the object and use this to determine if the price is greater or less than the y-intercept on this bar and is less than or greater on the previous bar.

                          There is an example of calculating the y-intercept.
                          Chelsea B.NinjaTrader Customer Service

                          Comment


                            #14
                            Thank you

                            Bob

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by NullPointStrategies, Today, 05:17 AM
                            0 responses
                            50 views
                            0 likes
                            Last Post NullPointStrategies  
                            Started by argusthome, 03-08-2026, 10:06 AM
                            0 responses
                            126 views
                            0 likes
                            Last Post argusthome  
                            Started by NabilKhattabi, 03-06-2026, 11:18 AM
                            0 responses
                            69 views
                            0 likes
                            Last Post NabilKhattabi  
                            Started by Deep42, 03-06-2026, 12:28 AM
                            0 responses
                            42 views
                            0 likes
                            Last Post Deep42
                            by Deep42
                             
                            Started by TheRealMorford, 03-05-2026, 06:15 PM
                            0 responses
                            46 views
                            0 likes
                            Last Post TheRealMorford  
                            Working...
                            X