Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

SharpDX vs NinjaTrader Custom (Draw.Text...)

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

    SharpDX vs NinjaTrader Custom (Draw.Text...)

    This is more of a chart performance question.
    I'm trying to understand if SharpDX will perform any better than Draw.Text.
    I'd like to label a bar on the chart with some text above the bar or below the bar.

    So....

    For example does the SharpDX version of code for plotting text above a bar:

    Code:
    [COLOR=#222222][FONT=Arial][FONT=monospace][COLOR=#0b5394]using SharpDX.DirectWrite;
    .
    .
    .
    var x = .......;[/COLOR][/FONT][/FONT][/COLOR]
    [COLOR=#222222][FONT=Arial][FONT=monospace][COLOR=#0b5394]var y = .......;[/COLOR][/FONT][/FONT][/COLOR]
    [COLOR=#222222][FONT=Arial][FONT=monospace][COLOR=#0b5394]var textRectangle = new SharpDX.RectangleF(x- 15, y, 100, 100);[/COLOR][/FONT][/FONT][/COLOR]
    [COLOR=#222222][FONT=Arial][FONT=monospace][COLOR=#0b5394]RenderTarget.DrawText(pivotName, format, textRectangle, color);[/COLOR][/FONT][/FONT][/COLOR]


    perform any better than doing a simpler NinjaScript.DrawingTools implementation... something like this:

    Code:
    [COLOR=#222222][FONT=Arial][FONT=monospace][COLOR=#0b5394]using NinjaTrader.NinjaScript.DrawingTools
    .
    .
    .
    Draw.Text(this, "Bar" + CurrentBar, true, "H", 0, High[0] + (TextOffset * TickSize), 0, Brushes.Lime,
         myFont, TextAlignment.Center, Brushes.Transparent, Brushes.Transparent, 0);[/COLOR][/FONT][/FONT][/COLOR]



    I ask because:
    1. It seems that SharpDX is being retired. It is no longer being developed nor maintained. (For reference: link and link )
    2. the simpler implementation is... well... simpler! :-)

    Seeking replies, many thanks!

    -- Gregory





    #2
    Hello wildwex,

    Have a look at the Text script in the DrawingTools. This also uses SharpDX to render the text.

    Calling a drawing object will instantiate an instance of the class which will take CPU and memory.

    Directly rendering in OnRender will be faster and use less resources but will be more complex.

    If you drawing a few dozens of Draw objects and you are happy with the performance and look, use Draw methods.
    If you have hundreds of objects being drawn, or if you want to customize the position or look of the text that the Draw methods are not capable of, custom render it.

    SharpDX may no longer be maintained on their documentation, but this works for our purposes and based on DirectX.

    Below are a few examples you may find helpful.
    Simple script that adds a background watermark of the instrument symbol (and expiry if a future) to the chart. (Updated July 10th, 2018 ‐ The opacity needed to be casted as a double and divided by 100 to be between 0 and 1) (Update: August 24th, 2021 – Subtracted the ChartPanel.Y so indicator can be […]





    The NinjaTrader Ecosystem website is for educational and informational purposes only and should not be considered a solicitation to buy or sell a futures contract or make any other type of investment decision. The add-ons listed on this website are not to be considered a recommendation and it is the reader's responsibility to evaluate any product, service, or company. NinjaTrader Ecosystem LLC is not responsible for the accuracy or content of any product, service or company linked to on this website.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thank you ChelseaB - delving into SharpDX based on your recommendation, above, and I noticed that there is commentary on two methods to render text on the chart:
      • DrawText method
      • DrawTextLayout method
      There are some notes that appear on https://ninjatrader.com/support/help...textlayout.htm suggesting that DrawTextLayout() is more efficient than DrawText() because text doesn't need to be formatted and the layout processed with each call.

      I'd like to better understand what is meant here.
      1. I assume that formatted as to do with the format of the text (font family name, font size, font style, etc).
      2. I assume that layout processed may have to do with where it's positioned on the chart (is that right?)
      Let's take an example:
      I want to label a chart's pivots for higher-highs and lower-lows,
      and I want to place an H on the higher-highs and an L on the lower lows,
      and my chart is large (it could have several thousand text objects of 'H' and 'L' on it).

      Question:
      Since all my text labels H and L will be of the same font family, size, and style, it seems I would gain performance benefit leveraging DrawTextLayout method here instead of DrawText.
      However, as far as layout processed, each H and L will appear a tick or so above or below each pivot candle so I imagine 'layout process' would still need to be done.
      Thusly, perhaps I could not use DrawTextLayout() in my case? Can you confirm?

      Also, it seems that DrawTextLayout() requires the use of ... vectors rather than a layout rectangle? Can you confirm?

      I wonder if there is some code, too, that might offer some demonstration of this in the example I refer to above?

      Seeking replies, many thanks!

      -- Gregory

      Comment


        #4
        Hello wildwex,

        You do not have to use RenderTarget.DrawTextLayout() if this doesn't suit what you are trying to accomplish with the code, you can use RenderTarget.DrawText() instead.
        https://ninjatrader.com/support/help...t_drawtext.htm

        Below is a publicly available link to an example of using DrawText().
        https://csharp.hotexamples.com/examp...-examples.html
        Last edited by NinjaTrader_ChelseaB; 01-11-2021, 12:09 PM.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Hi ChelseaB -

          I understand that. I am now using DrawText() without issue.
          However, because I have thousands of text items on the chart (leveraging my example), I am concerned about performance since I see NT8 rendering slowing down.

          I had a question with regards to gaining a performance improvement by using DrawTextLayout() instead.

          For the example I outlined, where I am labeling pivots across a chart (higher highs with 'H' and lower lows with 'L') if I could do away with the formatting and layout processing of those chart text elements each time they are rendered with DrawText.

          I imagine that I could benefit from not having to reformat the text elements each time.

          However, I am in need of some further information regarding what is meant by 'layout processing'. I imagine that has to do with the positioning the text on the chart (which is different for every bar since each bar prints it's own higher high and lower low. It appears that a 'vector' needs to be used instead referring to a rectangle.

          Could you provide me further insight with regards to how I might apply DrawTextLayout() instead of DrawText() in this scenario?

          Best,
          -- Gregory

          Comment


            #6
            Hello Gregory,

            Specifically I was meaning RenderTarget.DrawText(), of which I provided a link to in the help guide, and is SharpDX and as efficient as its going to get. This is different than Draw.Text().

            The TextLayout is used to position the text and set the width and height.

            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Hi ChelseaB -

              Yes, I totally understand the difference between the declarations
              Ninjatrader.Ninjascript.DrawingTools which contains Draw.Text()

              and

              SharpDX.DirectWrite
              which contains .DrawText and .DrawTextLayout methods

              I, too, am referring to SharpDX.DirectWrite methods as well.

              The NOTES on this page:


              Specifically state:

              Notes:

              1.When drawing the same text repeatedly, using the DrawTextLayout() method is more efficient than using the DrawText() method because the text doesn't need to be formatted and the layout processed with each call.

              2.This method doesn't return an error code if it fails.

              While I understand the CPU savings in using the SharpDX.DirectWrite.DrawText method, the note suggests that there is a performance benefit to using SharpDX.DirectWrite.DrawTextLayout
              which refers to a vector (as opposed to a rectangle) for placement.

              Do you have an example by which this could be used to assign text to bars on a chart (if they are a pivot bar). I'm unsure how to use this vector parameter or if it would even apply to bars vs. the whole chart.

              Please advise.

              Best,
              ​​​​​​​Gregory

              Comment


                #8
                Hello Gregory,

                The @Text.cs open source drawing tool script included with NinjaTrader is a great example of drawing text.
                This uses a TextLayout and supplies the DrawTextLayout() method a new Vector that uses the supplied x and y coordinates.

                Below is a link to the Microsoft Documentation on Vector.


                If you want to get x and y values from a bar you can use <chartScale>.GetYByValue() and <ChartControl>.GetXByBarIndex().



                I do not have an example that assigns text to bars. I do have an example that renders a line.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Thank you ChelseaB. This thread has been a great help.

                  Comment

                  Latest Posts

                  Collapse

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