Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

TextLayout in State.DataLoaded

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

    TextLayout in State.DataLoaded

    Hey guys,

    I have some questions about how to create a "TextLayout" in "State.DataLoaded".

    Should I dispose the "TextLayout" somewhere?
    Is there any performance gain in creating it there since there will be no creation and recreation of if?
    Is this bad practice?

    The only example I found doing this is the "SymbolWatermark" indicator:
    https://ninjatraderecosystem.com/use...bol-watermark/

    The author don't dispose his "TextLayout".

    In the indicator I'm creating, I will not change​ the string in the "TextLayout" after creation.​

    #2
    Hello rafaelcoisa,

    Thanks for your post.

    TextLayout should be defined in OnRender(), not in State.DataLoaded, as seen in the SampleCustomRender sample indicator that comes default with NinjaTrader.

    Further, TextLayout objects must be disposed of after they have been used.

    Please see the SampleCustomRender indicator code which demonstrates the use of TextLayout and disposing it. To view the script, open a New > NinjaScript Editor window, open the Indicators folder, and double-click on the SampleCustomRender file.

    Also, see the help guide documentation linked below for more information about TextLayout and SharpDX.

    TextLayout: https://ninjatrader.com/support/help...sub=TextLayout
    Using SharpDX for Custom Chart Rendering: https://ninjatrader.com/support/help..._rendering.htm
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment


      #3
      Hey NinjaTrader_BrandonH,

      Do you mean then that it is bad practice to define "TextLayout" in "State.DataLoaded"?
      And, that there is no gain in performance doing so?​

      Comment


        #4
        rafaelcoisa Yes, that is absolutely a bad practice to allocate or deallocate SharpDX resources in State.DataLoaded. Never do that. DirectX/SharpDX resources should ONLY be allocated in OnRender or OnRenderTargetChanged. To do it any other way is to invite a crash. It has nothing to do with performance and everything to do with the fact that OnStateChanged is running in a different thread.
        Bruce DeVault
        QuantKey Trading Vendor Services
        NinjaTrader Ecosystem Vendor - QuantKey

        Comment


          #5
          Thanks for the reply QuantKey_Bruce ​,
          Your posts are very helpful.

          Another thing Bruce.
          I saw you using "SetZOrder(-1)" in State.Historical.
          Other people put it in "State.Configure" or "State.DataLoaded".
          What is your opinion on this?
          And what about setting a background brush just once to use in "OnRender", something like this:

          Code:
          Brush temp1 = xColor.Clone();
          temp1.Opacity = ColorOpacity / 100.0;
          temp1.Freeze();
          xColor = temp1;
          Where do you prefer to put it? "State.Configure" or "State.DataLoaded"?​
          And from a performance perspective, if you need to use multiple brushes in "OnRender", do you go for a "string/DXMediaMap" dictionary, multiple brushes configured only once in some "state", or keep converting the brush to DxBrush on each "OnRender" rendenrization routine?
          Last edited by rafaelcoisa; 06-26-2023, 06:53 AM.

          Comment


            #6
            It doesn't really matter where you set ZOrder in OnStateChange. I would typically put it in State.Configure. I'm not sure what code you're referring to when you say "I saw you using..." but it might have been me fixing someone else's example or something like that.

            Regarding allocation of brushes (not SharpDX brushes but something like a SolidColorBrush) I would again typically do that in State.Configure, but you could do it in State.DataLoaded. If you refer to these in OnRender, you should always check to see if they're null before you try to convert them to a SharpDX brush.

            From a performance perspective, you should deallocate your SharpDX brushes in OnRenderTargetChanged if the SharpDX brushes references are not null, and then allocate them again after that in OnRenderTargetChanged if RenderTarget is not null. You could store them in a dictionary if that is really necessary. Most people just use variables, which is what I would do unless there were more than a few dozen. If you're making 256 of them to cover every shade of alpha or something like that, then sure, put them in an array or something for efficiency of coding.

            From a safety perspective, if absolutely fastest performance is not required, don't even use OnRenderTargetChanged - just use a "using" clause and convert them in OnRender because this is not likely to get messed up. From an absolute fastest performance perspective, use OnRenderTargetChanged as described above. Do not under any circumstance allocate or deallocate any SharpDX resources outside of OnRender or OnRenderTargetChanged.
            Bruce DeVault
            QuantKey Trading Vendor Services
            NinjaTrader Ecosystem Vendor - QuantKey

            Comment


              #7
              Hello rafaelcoisa,

              QuantKey_Bruce is correct. It is bad practice and not recommended to define TextLayout in State.DataLoaded.

              TextLayout must also be disposed of after use and not disposing of it would also be bad practice and would likely lead to problems occurring when running the script as QuantKey_Bruce mentioned.
              <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

              Comment


                #8
                Thanks QuantKey_Bruce and NinjaTrader_BrandonH.
                Your posts were very helpful.​

                Comment

                Latest Posts

                Collapse

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