Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to draw and maintain collection of horizontal draw lines

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

    How to draw and maintain collection of horizontal draw lines

    I want to maintain a collection of lines that are drawn as support and resistance lines thoughout a days price history.


    Should I use GDI calls or DrawLine()? What is the best approach?
    Attached Files

    #2
    DrawLine() just draws the line. Any collection you want will need to be created manually. To modify a line that has been drawn, just call DrawLine() again with the same signal name and it will redraw the new line and remove the old.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Examples?

      Any examples for maintaining a manual set of horizontal lines?

      Originally posted by NinjaTrader_Josh View Post
      DrawLine() just draws the line. Any collection you want will need to be created manually. To modify a line that has been drawn, just call DrawLine() again with the same signal name and it will redraw the new line and remove the old.

      Comment


        #4
        What if I modify the Swing Indicator?

        I am also looking at the CandlestickPatternAll user download for clues.

        Comment


          #5
          Unfortunately we do not have any examples. Maintaining a list would be done by yourself via maybe an array of sorts containing information you want to keep. You cannot programmatically access draw objects after they have been drawn though so I am not sure what exactly you would like to use the collection for.

          NT7 will have some enhancements:
          "
          Draw Objects Enhancements
          Draw objects are now associated to an indicator which means that if a user removes a draw object from the chart, it will also remove the indicator. In addition, all draw objects spawned from an indicator share the indicator's z-order.
          IDrawObject - IDrawObjects are now returned to the caller on all Draw() methods. IDrawObjects expose draw objects properties.
          Work in progress - We will implement programmatic access to draw objects added to a chart manually. For example, you can have a user draw a line on a chart and add a "tag" to this object and you can retrieve this object, read it's value and do something with it."
          Josh P.NinjaTrader Customer Service

          Comment


            #6
            Not sure how the Swing indicator would help you, but you can try.
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              Save GDI for what you can't do whit the Draw... Methods

              Originally posted by DancesWithBears View Post
              I want to maintain a collection of lines that are drawn as support and resistance lines throughout a days price history.
              Originally posted by DancesWithBears View Post


              Should I use GDI calls or DrawLine()? What is the best approach?


              I have done a lot of development with both approaches. And have made a few posts around the largely undocumented & unsupported using GDI+ via the Plot Method.

              If all you want to do is Draw Lines on the chart, (or within an indicator), go with the Draw... methods.
              1. They are much simpler,
              2. Plots on the same Y axis at the Price (or indicator if you aren't in Pane 1). So you don't have to code up scale conversion &
              3. requires significantly less familiarity with .NET graphic objects.
              You can do a lot with a lot less code. ie: less code = less places to put bugs.

              But you will need to go with GDI+ if you want to do anything beyond the visually limited offering of the Draw... methods.
              eg:
              1. In v6.5 you want to draw something with a 2nd Y axis (like painting the "Williams %R" over price)
              2. You want much fancier shapes (than circle, triangle etc) &/or
              3. You want shapes that autoscale ie: shapes that change size with the width of the bar.
              4. You want your line color to change color at a precise value & not wait till it draws the next part of the line. (use Blends & Brushes for this) Or if you want your line to gradually change color based on some other value.
              5. In v6.5 you want multi-colored lines without blending multiple plot lines to pretend that they are one.
              6. You want bands of color, especially if they have a color gradation.
              Summary: Ninja is fairly unique in providing the massive power of coding your indicators in GDI+. I love it. While it is not that hard to use, it requires more coding effort than calling the normal API's. As you can do a lot with the standard API's, So save GDI for the occasions where it is necessary.

              That said, once you've decided to code your indicator in GDI. Stay in GDI. As the Draw Methods, plot on a different scale, attempting to combine both techniques adds another level of complexity that is rarely warranted.

              Comment


                #8
                Example for maintaining a set of Horz Lines. Pt 1

                Originally posted by DancesWithBears View Post
                Any examples for maintaining a manual set of horizontal lines?

                To keep it simple I've isolated the relevant parts of code below. There are other alternatives, I don't know which is better as but I'm still running that thread with the ninja folks. My bet is on this approach.



                I use a .NET list array structure to hold the "set of horizontal lines".
                I create my own class to maintain all the properties of a line in one object (but you could also use a structure), Or a list for each property.
                Then I call the list as a generic.

                STEP 1: Create your own class. IN YOUR OWN UNIQUE NAMESPACE.
                Code:
                [COLOR=blue][FONT=Courier New]namespace [/FONT][/COLOR][COLOR=red][FONT=Courier New]MyUniqueName.MyObjects[/FONT][/COLOR]
                [COLOR=#282828][FONT=Courier New]{[/FONT][/COLOR]
                [COLOR=blue][FONT=Courier New]public class [/FONT][/COLOR][B][COLOR=fuchsia][FONT=Courier New]TDSTLine[/FONT][/COLOR][/B][COLOR=#282828][FONT=Courier New] {[/FONT][/COLOR]
                [COLOR=blue][FONT=Courier New]private int[/FONT][/COLOR][COLOR=#282828][FONT=Courier New] bar;[/FONT][/COLOR]
                [COLOR=blue][FONT=Courier New]private double[/FONT][/COLOR][COLOR=#282828][FONT=Courier New] price; [/FONT][/COLOR]
                [COLOR=gray][FONT=Courier New]///<summary> [/FONT][/COLOR][COLOR=green][FONT=Courier New]The CurrentBar where the Support/Resistance Line starts[/FONT][/COLOR]
                [COLOR=gray][FONT=Courier New]///</summary>[/FONT][/COLOR]
                [COLOR=gray][FONT=Courier New]///<returns>[/FONT][/COLOR][COLOR=green][FONT=Courier New]The X value of the Start of the line.[/FONT][/COLOR][COLOR=gray][FONT=Courier New]</returns>[/FONT][/COLOR]
                [COLOR=blue][FONT=Courier New]public int[/FONT][/COLOR][COLOR=#282828][FONT=Courier New] Bar[/FONT][/COLOR]
                [COLOR=#282828][FONT=Courier New]{[/FONT][/COLOR]
                [COLOR=blue][FONT=Courier New]get[/FONT][/COLOR][COLOR=#282828][FONT=Courier New] { [/FONT][/COLOR][COLOR=blue][FONT=Courier New]return[/FONT][/COLOR][COLOR=#282828][FONT=Courier New] bar; }[/FONT][/COLOR]
                [COLOR=blue][FONT=Courier New]set[/FONT][/COLOR][COLOR=#282828][FONT=Courier New] { bar = value; }[/FONT][/COLOR]
                [COLOR=#282828][FONT=Courier New]}[/FONT][/COLOR]
                 
                [COLOR=gray][FONT=Courier New]///<summary>[/FONT][/COLOR][COLOR=green][FONT=Courier New] The Y value of the Support / Resistance Line[/FONT][/COLOR]
                [COLOR=gray][FONT=Courier New]///</summary>[/FONT][/COLOR]
                [COLOR=gray][FONT=Courier New]///<returns>[/FONT][/COLOR][COLOR=green][FONT=Courier New]The price of the line.[/FONT][/COLOR][COLOR=gray][FONT=Courier New]</returns>[/FONT][/COLOR]
                [COLOR=blue][FONT=Courier New]public double[/FONT][/COLOR][COLOR=#282828][FONT=Courier New] Price[/FONT][/COLOR]
                [COLOR=#282828][FONT=Courier New]{[/FONT][/COLOR]
                [COLOR=blue][FONT=Courier New]   get[/FONT][/COLOR][COLOR=#282828][FONT=Courier New] { [/FONT][/COLOR][COLOR=blue][FONT=Courier New]return[/FONT][/COLOR][COLOR=#282828][FONT=Courier New] price; }[/FONT][/COLOR]
                [COLOR=blue][FONT=Courier New]   set[/FONT][/COLOR][COLOR=#282828][FONT=Courier New] { price = value; }[/FONT][/COLOR]
                [COLOR=#282828][FONT=Courier New]} [/FONT][/COLOR]
                 
                [COLOR=gray][FONT=Courier New]///<summary>[/FONT][/COLOR][COLOR=green][FONT=Courier New] Default Constructor[/FONT][/COLOR]
                [COLOR=gray][FONT=Courier New]///</summary>[/FONT][/COLOR]
                [COLOR=gray][FONT=Courier New]///<param name="Bar">[/FONT][/COLOR][COLOR=green][FONT=Courier New]X value where the line starts[/FONT][/COLOR][COLOR=gray][FONT=Courier New]</param>[/FONT][/COLOR]
                [COLOR=gray][FONT=Courier New]///<param name="Price">[/FONT][/COLOR][COLOR=green][FONT=Courier New]Y value of the line[/FONT][/COLOR][COLOR=gray][FONT=Courier New]</param>[/FONT][/COLOR]
                [COLOR=blue][FONT=Courier New]public[/FONT][/COLOR][COLOR=#282828][FONT=Courier New] TDSTLine([/FONT][/COLOR][COLOR=blue][FONT=Courier New]int[/FONT][/COLOR][COLOR=#282828][FONT=Courier New] Bar, [/FONT][/COLOR][COLOR=blue][FONT=Courier New]double[/FONT][/COLOR][COLOR=#282828][FONT=Courier New] Price){[/FONT][/COLOR]
                [COLOR=#282828][FONT=Courier New]    bar = Bar;[/FONT][/COLOR]
                [COLOR=#282828][FONT=Courier New]   price = Price;[/FONT][/COLOR]
                [COLOR=#282828][FONT=Courier New]}[/FONT][/COLOR]
                [COLOR=#282828][FONT=Courier New]}[/FONT][/COLOR]
                [COLOR=#282828][FONT=Courier New]}[/FONT][/COLOR]

                Comment


                  #9
                  Example for maintaining a set of Horz Lines. Pt 2/3

                  Example for maintaining a set of Horz Lines. Pt 2/3

                  STEP 2: Add a reference to your namespace that containes your classes.
                  Code:
                  [COLOR=blue][FONT=Courier New]using[/FONT][/COLOR][COLOR=black][FONT=Courier New] System.Collections.Generic;[/FONT][/COLOR]
                  [COLOR=blue][FONT=Courier New]using [/FONT][/COLOR][COLOR=red][FONT=Courier New]MyUniqueName.MyObjects[/FONT][/COLOR][COLOR=black][FONT=Courier New];[/FONT][/COLOR]
                  Note: You'll also need the Collections reference for the .NET List<> see below.

                  STEP 3: Declare your generic list to hold your array of lines
                  Code:
                  [COLOR=blue][FONT=Courier New]public class[/FONT][/COLOR][COLOR=#282828][FONT=Courier New] TDST : Indicator[/FONT][/COLOR]
                  [COLOR=#282828][FONT=Courier New]{[/FONT][/COLOR]
                  [COLOR=blue][FONT=Courier New]#region[/FONT][/COLOR][COLOR=#282828][FONT=Courier New] Variables[/FONT][/COLOR]
                  [COLOR=green][FONT=Courier New]// Wizard generated variables[/FONT][/COLOR]
                  [COLOR=green][FONT=Courier New]// User defined variables (add any user defined variables below)[/FONT][/COLOR]
                  [COLOR=blue][FONT=Courier New]private[/FONT][/COLOR][COLOR=#282828][FONT=Courier New] List<TDSTLine> TDSTSupport = [/FONT][/COLOR][COLOR=blue][FONT=Courier New]new[/FONT][/COLOR][COLOR=#282828][FONT=Courier New] List<TDSTLine>();[/FONT][/COLOR]
                  [COLOR=blue][FONT=Courier New]private[/FONT][/COLOR][COLOR=#282828][FONT=Courier New] List<TDSTLine> TDSTResist = [/FONT][/COLOR][COLOR=blue][FONT=Courier New]new[/FONT][/COLOR][COLOR=#282828][FONT=Courier New] List<TDSTLine>();[/FONT][/COLOR]
                  STEP 4: Somewhere in your code you'll need to add lines to the collection.
                  Code:
                  [B][COLOR=#282828][FONT=Courier New]TDSTSupport[/FONT][/COLOR][/B][COLOR=#282828][FONT=Courier New].Add([/FONT][/COLOR][COLOR=blue][FONT=Courier New]new [/FONT][/COLOR][B][COLOR=fuchsia][FONT=Courier New]TDSTLine[/FONT][/COLOR][/B][COLOR=#282828][FONT=Courier New](LLBar, LLV));[/FONT][/COLOR]
                  Last edited by David Lean; 06-13-2010, 08:01 PM. Reason: Code section lost spaces

                  Comment


                    #10
                    Example for maintaining a set of Horz Lines. Pt 3/3


                    STEP 5: You'll need to display existing lines & remove them from the collection when no longer needed.

                    Code:
                    [COLOR=blue][FONT=Courier New]for[/FONT][/COLOR][COLOR=#282828][FONT=Courier New] ([/FONT][/COLOR][COLOR=blue][FONT=Courier New]int[/FONT][/COLOR][COLOR=#282828][FONT=Courier New] i = [B]TDSTSupport[/B].Count - [/FONT][/COLOR][COLOR=purple][FONT=Courier New]1[/FONT][/COLOR][COLOR=#282828][FONT=Courier New]; i >= [/FONT][/COLOR][COLOR=purple][FONT=Courier New]0[/FONT][/COLOR][COLOR=#282828][FONT=Courier New]; i--) {[/FONT][/COLOR]
                    [COLOR=#282828][FONT=Courier New]  [/FONT][/COLOR][B][COLOR=fuchsia][FONT=Courier New]TDSTLine[/FONT][/COLOR][/B][COLOR=#282828][FONT=Courier New] currLine = [B]TDSTSupport[/B][i];[/FONT][/COLOR]
                    [COLOR=#282828][FONT=Courier New]  DrawLine([/FONT][/COLOR][COLOR=maroon][FONT=Courier New]"TDSTSuppEx"[/FONT][/COLOR][COLOR=#282828][FONT=Courier New]+currLine.Bar.ToString(), [/FONT][/COLOR][COLOR=blue][FONT=Courier New]false[/FONT][/COLOR][COLOR=#282828][FONT=Courier New], CurrentBar - currLine.Bar, currLine.Price, [/FONT][/COLOR][COLOR=purple][FONT=Courier New]0[/FONT][/COLOR][COLOR=#282828][FONT=Courier New], currLine.Price, Lines[[/FONT][/COLOR][COLOR=purple][FONT=Courier New]1[/FONT][/COLOR][COLOR=#282828][FONT=Courier New]].Pen.Color, Lines[[/FONT][/COLOR][COLOR=purple][FONT=Courier New]1[/FONT][/COLOR][COLOR=#282828][FONT=Courier New]].Pen.DashStyle, ([/FONT][/COLOR][COLOR=blue][FONT=Courier New]int[/FONT][/COLOR][COLOR=#282828][FONT=Courier New])Lines[[/FONT][/COLOR][COLOR=purple][FONT=Courier New]1[/FONT][/COLOR][COLOR=#282828][FONT=Courier New]].Pen.Width );[/FONT][/COLOR]
                    [COLOR=blue][FONT=Courier New]  if[/FONT][/COLOR][COLOR=#282828][FONT=Courier New] (currLine.Price > close){[/FONT][/COLOR]
                    [COLOR=#282828][FONT=Courier New]     [B]TDSTSupport[/B].RemoveAt(i);[/FONT][/COLOR]
                    [COLOR=#282828][FONT=Courier New]  }[/FONT][/COLOR]
                    [COLOR=#282828][FONT=Courier New]}[/FONT][/COLOR]
                    [COLOR=#282828][FONT=Arial][/FONT][/COLOR]

                    Note: I use a Line object to let the trader set the Color & line styles of my lines. I hide the actual line off the screen by setting its value really low. This is simpler than the other alternatives of creating your own datatype & serialising it, or giving the Trader multiple parameters to change.
                    Hope this helps.

                    Comment


                      #11
                      thx for sharing this! i enjoyed reading thru your implementation.

                      cheers,
                      -e

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                      0 responses
                      605 views
                      0 likes
                      Last Post Geovanny Suaza  
                      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                      0 responses
                      351 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
                      560 views
                      1 like
                      Last Post Geovanny Suaza  
                      Started by RFrosty, 01-28-2026, 06:49 PM
                      0 responses
                      561 views
                      1 like
                      Last Post RFrosty
                      by RFrosty
                       
                      Working...
                      X