Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

SharpDX vs. Draw Method

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

    SharpDX vs. Draw Method

    I use several indicators that generate a lot of lines, rays, and text objects on a chart. They are rendered using the Draw method.

    I was wondering if there is a benefit in using SharpDX instead? Primarily I want to know if there is a performance different between the two options? Is there some guidance documentation that would help decide when to use the Draw method vs. SharpDX?

    #2
    The big distinction here is that SharpDX drawing requires a RenderTarget, and OnRender provides a ready-made RenderTarget for you. Thus, you will want to do your SharpDX work within that event. NinjaScript drawing methods (Draw.X) have no such requirement, and are already connected to logic related to slot indexes and the x- and y-axis, so you can call them in a greater scope.

    Help Guide - Using SharpDX for Custom Chart Rendering
    Help Guide - SharpDX SDK Reference
    Josh G.NinjaTrader Customer Service

    Comment


      #3
      I understand the difference in implementation. I have created an indicator using SharpDX as well. What I have a hard time finding is information on the performance difference between the two options. And how to decide which to use when both options provide a feature like drawing a simple line or rendering some text. Is there some decision guidance somewhere? Or performance recommendation/comparison?

      If there is a performance improvement using SharpDX is might consider converting my indicators to using it.

      Comment


        #4
        I am not aware of any type of performance comparison. However, I can tell you that using OnRender() is going to give you much better performance.

        When you use the Draw methods they must store bar and timestamp information and related information in memory before getting rendered on your chart. When you use SharpDX you are essentially cutting out the middle man and rendering it directly on the chart.
        Josh G.NinjaTrader Customer Service

        Comment


          #5
          When using Draw methods, a new instance of the Draw object is created including its custom rendering and calculation logic. These methods are convenient in many situations, but can quickly introduce performance issues if used too liberally. In some situations, you may see better performance for rendering via SharpDX in OnRender().

          why?

          Each draw object instance will see its own OnRender() called to render values. If you instead implement custom rendering in the your object, you would only see a single OnRender() call for your custom created graphics.​

          With just a little extra code (much less than what is in the Draw methods) custom SharpDX rendering greatly reduces CPU and Memory consumption. Please ensure a Direct2D1 factory would only be instantiated from OnRender() or OnRenderTargetChanged() (which run in the UI thread), as access from other threads outside those methods could cause a degradation in performance.​

          One of the advantages of using a Draw.Method is the returned Draw Objects contains metadata which could be used later (such as for obtain the bar index or price value of the dot later on). If you would use this metadata later on, using a Draw method would be in your best interests. However, if you are solely looking to render figures on a chart, favoring your custom SharpDX methods can drastically improve performance.​

          Without the source code of `Draw.` objects, we can't really tell what they done behind the scene. But correct me if I am wrong: Using `Draw.` method NT8 creates a drawing object in the back end, the more `Draw.` method you called, the more objects you creates. At the time when the UI changed, they trigger every drawing object's own built-in "_onRender" method (hypothetically speaking, I don't have the source code), for example:

          Code:
          OnRender(ChartControl chartControl, ChartScale chartScale)
          {
                  Drawobject1._onRender(...);
                  Drawobject2._onRender(...);
                  Drawobject3._onRender(...);
                  ...
          }
          The list goes on until all the existing drawing object called their own "_onRender" method. This introduce performance issues simply because there might be too much drawing objects and each of them have to run their "_onRender" method regardless they really needed an update or not.

          However, with sharpdx, we only call the "onRender" method once and write whatever rending logic we want within it.

          Code:
          OnRender(ChartControl chartControl, ChartScale chartScale)
          {
                  // Fully Custom
          }


          I think the only downside of using sharpdx is: you have to manage what pixels gets what color in front of your face, while the `Draw.` method calculated all of them at the backend for you. For example, you have to draw a trendline, with `Draw.` method you can just draw it and leave it, but with sharpdx, you have to check if the trendline is visible at this specific location, and decide which pixel does it get to use.
          Last edited by Curerious; 04-13-2025, 02:20 PM.

          Comment


            #6
            Originally posted by martyn73 View Post
            I use several indicators that generate a lot of lines, rays, and text objects on a chart. They are rendered using the Draw method.

            I was wondering if there is a benefit in using SharpDX instead? Primarily I want to know if there is a performance different between the two options? Is there some guidance documentation that would help decide when to use the Draw method vs. SharpDX?
            I like to use SharpDX because of few things, among them faster performance, opacity/alpha and because it it's not a one shot (it can draw anywhere on the chart at any time).

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            599 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            344 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            103 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            558 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            557 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X