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

Performance optimization

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

    Performance optimization

    Hi,

    I just want to make sure that my code is optimize for best performance.

    I am painting lots of numbers and letters to the chart window through a loop and these objects are using different brushes.

    I notice that sometimes it slows performance when I move the chart with my mouse.

    Here is the structure of my code
    I have all objects to be painted in a SortedDictionary and it is part of the drawing method which is called from OnRender :

    foreach(KeyValuePair<int, List<string> k in dic)
    {
    List<string> list = k.Value as List<string>();

    for(int i=0; i<list.Count; i++)
    {

    curElement = list[i];
    // do stuff

    if(xxxx)
    brushColor = Brushes.Yellow
    else
    brushColor = Brushes.Red;

    brushColorDx = brushColor.ToDxBrush(RenderTarget);

    RenderTargert.DrawText(xxxxxxxxx, brushColorDx);

    }

    }

    And OnRenderTargetChanged I have :

    if(brushColorDx != null)
    brushColorDx.Dispose();


    I am just wondering if I manage the brushDx the right way for performance optimization.

    It seems that more and more painting (shrinking the chart as much as I can ) )slows down the indicator a bit

    #2
    Hello blar58,

    May I confirm that you are reusing both sharpdx brushes and media brushes and that the sharpdx brushes are being created in OnRendrTargetChanged from the media brush?



    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hi Chelsea

      Yes I am reusing sharpdx and media brushes

      No brushdx are created on a method that is being called from OnRender.

      I have no choice because I know only the brushes when I loop through the dictionary which is being called from OnRender.

      The brushdx is disposed from OnRenderTargetChanged though.

      Comment


        #4
        Hello blar58,

        I would recommend creating/recreating the sharpdx brushes in OnRenderTargetChanged() so that these do not collect in memory.

        This can be done with a loop over a dictionary in OnRenderTargetChanged().
        Attached is an example that demonstrates.

        (edit, I've made some small changes to the script, but none that change what I am demonstrating)

        (second edit, since I'm posting how to do this with a dictionary here, I'm adding the example I've made without using a dictionary as well for convenience and to compare with.)
        Attached Files
        Last edited by NinjaTrader_ChelseaB; 06-07-2017, 08:26 AM.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Thank you Chelsea

          I think I will solve my perfromance problem.

          Quick question :
          Looking at the code you sent me what do these two lines from OnRender means :

          previousAntialiasMode = RenderTarget.AntialiasMode;

          RenderTarget.AntialiasMode = previousAntialiasMode;


          Should I use that in my code

          Thanks

          Comment


            #6
            Hello blar58,

            If you are changing the AntialiasMode, then yes, you should change this back.

            If you choose not to change the AntialiasMode, then no, no need to track the original mode and change it back.

            With the AntialiasMode set to Alias, the rendering is jagged, with AntialiasMode set to PerPrimitive the rendering is smooth.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Hello NT Community,

              Here's another example with brushes that demonstrates how to add a set of brushes to a expandable / collapsible class (using the ExpandableObjectConverter typeconverter) so these are grouped together in the properties window.

              (edited Nov 30, 2017 to correct a public variable that should have been a private variable which causes issues with saving an indicator preset)
              Attached Files
              Last edited by NinjaTrader_ChelseaB; 11-30-2017, 01:01 PM.
              Chelsea B.NinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by NRITV, Today, 01:15 PM
              2 responses
              8 views
              0 likes
              Last Post NRITV
              by NRITV
               
              Started by frankthearm, Today, 09:08 AM
              7 responses
              31 views
              0 likes
              Last Post NinjaTrader_Clayton  
              Started by maybeimnotrader, Yesterday, 05:46 PM
              5 responses
              26 views
              0 likes
              Last Post NinjaTrader_ChelseaB  
              Started by quantismo, Yesterday, 05:13 PM
              2 responses
              19 views
              0 likes
              Last Post quantismo  
              Started by adeelshahzad, Today, 03:54 AM
              5 responses
              33 views
              0 likes
              Last Post NinjaTrader_BrandonH  
              Working...
              X