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

Best way to draw lines into the future?

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

    Best way to draw lines into the future?

    I often want to draw horizontal lines that terminate, sometime in the future, when they are penetrated by price.

    Which approach is more efficient / performant for ninja?

    Option 1: Redraw the entire line on each new bar.
    Remember the name, start X,Y coords & extend it to the new bar [0]

    Option 2: Create new line segments from Bar[1] to Bar[0] that just extend the line(s).


    The Option 2 is easier to code as there is less information to track, but I'm concerned about causing Ninja to track 100's of little "1 bar width" line segments that are used to draw the complete line.

    That said, it may be easier as no need to clip the longer lines that extend off the plot area.

    Could someone from Ninja tell me which approach works better for the Ninja Charting engine?

    #2
    Hello,

    I am sorry, this statement doesn't make sense to me:
    "I often want to draw horizontal lines that terminate, sometime in the future, when they are penetrated by price"

    If you are drawing the "future" (to the right of bar 0) there won't be any price to penetrate any lines. Can you clarify?
    DenNinjaTrader Customer Service

    Comment


      #3
      Some examples

      Thanks for your reply. I hope these examples clarify.

      Example 1:
      Imagine you are drawing the Lowest Low of the day. At 9:30am Bar[0] it is 1500. For each new "Bar[0]" you need to continue the line to the right, until at 10:45 am the price closes below 1500, when you will draw the line a little lower.

      Example 2. (Support/Resistance lines)
      For each Major Price extreme (Peak or Valley) you draw a line to the right until it is exceeded by a higher peak or lower valley. Thus you don't know in advance how many lines you will need to draw at any one time as that will be determined by the price action of the day.

      Example 3: Trailing Stops
      When I enter a trade, I start drawing a line showing the current value of my ATM stops & targets, I keep extending those lines until, the trade exits. That way I can review my trades & see how efficient my stops & targets were.
      Because I can't draw to the right past Bar[0] I need to wait for a new bar & then extend the lines into that new bars location.
      (this is especially handy when backtesting with Strategy Analyzer as Ninja doesn't provide ATM nor draw in the Stops & Targets, so we have to write it all ourselves. )

      ie: at 9:30 am I will not know the bar will terminate at 10:45am, so I just keep extending the lines one bar at a time until some condition is satisfied when I stop drawing the lines.

      I hope this makes more sense.
      Last edited by David Lean; 06-13-2010, 08:55 AM.

      Comment


        #4
        Hello,

        I suggest drawing a mockup so we can visualize exactly what you want. If I understand you, just plot the indicator like any other indicator (SMA for example) and have a condition to not plot if price broke the plot value. Then just continue the plot in a different spot once you want it to start drawing again. You can also change the color of the plot if you want.

        So the value you are plotting doesn't change (you said horizontal line...flat lines) until x happens.

        I hope that helps.
        DenNinjaTrader Customer Service

        Comment


          #5
          Which approach is more performant?

          Hi Ben,
          Please re-read this thread carefully.
          I know of multiple ways to achieve what I want. The question is about performance. I am looking for insight on which approach is more efficient for the Ninja Charting engine.

          I am not asking for your assistance to help me code an indicator. See attached picture, it is not a mockup, it works correctly. What I need to understand is performance; Memory usage vs CPU.

          I speculate that multiple line segments might be stored in an array taking up more memory, but I may be wrong, a) the engine might compress multiple line segments into a single line. & b) lots of smaller lines might make searching the array & clipping those lines simpler.
          That is the type of insight I'm looking for.

          So I have a choice of 2 algorithms. (unless Ninja recommend a third)


          Algorithm 1:
          • Create an array that holds the Endpoints of each line (x,y & LineName)
          • Redraw the entire line on each new bar. (by using DrawLine ("LineName",.startX,.., 0, ...)
            eg: On each new bar, Use the same name & cause the line to be redrawn from bar[startX] to bar[0]
          Algorithm 2:
          • Create an array of Y values
          • Draw a new line segment for each new bar. Give the line segment a new name eg:
          • DrawLine("LineName"+CurrentBar.ToString(),..., 1, ... , 0, ....)
            eg: On each new bar Use a new unique line name & only draw the line from bar[1] to bar[0]
          Algorithm 2 is much simpler to code as it only requires a 1 dimensional array. But I suspect is it not very efficient. Algorithm 2 requires a Generic List, your own class or structure & thus unique namespace.

          Note using Plots or Lines is not appropriate as the number of Plots or Lines are fixed in the initialization method AND each one creates a cache of values for every bar. It would be very inefficient to create 10 or 20 Plots just to ensure I have sufficient Plots to cover the worst case. The DrawLine works will, I just need to know the best way to use it.

          I'm happy to send you code, but hope to save you time by focusing the discussion on just the key design question.

          Does this make sense?
          Thanks for your time.
          Attached Files

          Comment


            #6
            Hello,

            I'd have to test it to be sure and look at my PC resources in the Task Manager, however I would say the first option you gave is the best because it is not create new line memory which will use up your PC memory.

            If I were doing it I may just use a standard plot and change the location of the plot as I described in my other post.

            However, reply if you want someone else (NT support or otherwise) to give their opinion. I would test it as suggested above.
            DenNinjaTrader Customer Service

            Comment


              #7
              Thanks,
              I have looked at the memory & CPU usage of Ninja with tools much more precise than Task Manager (or Resource Manager). There are many great add-ins for Visual Studio.

              But that is blackbox testing. It is subject to a multitude of errors, including other co-incidental activities that Ninja might be doing at the time of my testing.

              That was why I was hoping you could ask someone familiar with the code to give an answer rather than speculate as we are doing here.


              I note that you've pushed for the Plot approach twice now. Why do you think that would be more efficient than Drawing lines as they are required? As you saw in the picture I attached, sometimes there are 3 lines on the screen, at other times there are 12, then others there are none.
              As each plot can only have one value at a time, I'd need to create an unknown number of "Plots" in advance.
              Also I was lead to believe that each PLOT maintains a cache of double values for each bar. Wouldn't this require much more memory than just maintaining the endpoints for the lines I need.

              Am I overlooking something?

              Comment


                #8
                David, while you would certainly have to continue testing your custom code for performance, we believe Algorithm 1 outlined in one of your posts below would be higher performing for your tasks.
                BertrandNinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by DJ888, 04-16-2024, 06:09 PM
                6 responses
                18 views
                0 likes
                Last Post DJ888
                by DJ888
                 
                Started by Jon17, Today, 04:33 PM
                0 responses
                1 view
                0 likes
                Last Post Jon17
                by Jon17
                 
                Started by Javierw.ok, Today, 04:12 PM
                0 responses
                6 views
                0 likes
                Last Post Javierw.ok  
                Started by timmbbo, Today, 08:59 AM
                2 responses
                10 views
                0 likes
                Last Post bltdavid  
                Started by alifarahani, Today, 09:40 AM
                6 responses
                41 views
                0 likes
                Last Post alifarahani  
                Working...
                X