Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to write CPU efficient script for DrawObjects

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

    How to write CPU efficient script for DrawObjects

    I just learned in another thread about how there is a more efficient way to write the code for DrawRegion by indexing the String Tag with an index instead of CurrentBar which cuts down on the number of DrawRegion Objects.

    Below is a more efficient way of writing the DrawObject code:


    if(!priorBull)
    index = CurrentBar;
    if (Opacity !=0)
    DrawRegion("triggerBands" + index, CurrentBar - index + 1, 0, Trigger, TriggerAverage, colorTransparent, upColor, SmallTriggerOpacity);
    bullish = true;

    Thus using "triggerBands" + index is more CPU efficient than "triggerBands" + CurrentBar since the later is called on each bar.

    Does anyone know if this is the same for all DrawObjects or is it unique to DrawRegion?

    regards,
    taddypole...

    #2
    Or you could just remove the +index altogether and just use "triggerbands".
    eDanny
    NinjaTrader Ecosystem Vendor - Integrity Traders

    Comment


      #3
      Hello Taddypole,

      Thank you for writing in. As a general rule, the less objects you are drawing, the more CPU efficient the code will be. The only difference between what you have provided:
      Code:
      if(!priorBull)
      index = CurrentBar;
      if (Opacity !=0)
      DrawRegion("triggerBands" + index, CurrentBar - index + 1, 0, Trigger, TriggerAverage, colorTransparent, upColor, SmallTriggerOpacity);
      bullish = true;
      and the following code:
      Code:
      DrawRegion("triggerBands" + CurrentBar, CurrentBar - index + 1, 0, Trigger, TriggerAverage, colorTransparent, upColor, SmallTriggerOpacity);
      is that you are potentially creating less unique drawing objects. This same concept would apply to all drawing objects.

      Please let me know if I may be of further assistance.
      Michael M.NinjaTrader Quality Assurance

      Comment


        #4
        Originally posted by eDanny View Post
        Or you could just remove the +index altogether and just use "triggerbands".
        eDanny,

        If I remove the + index, then I only get the last region painted. Very CPU efficient, but not what I wanted.

        see attached:

        regards,
        taddypole...
        Attached Files

        Comment


          #5
          I have tried the code both ways and have found using Visual Studio that there is a significant drop in the number of DrawObjects but I was just trying to get more detail.

          Hoping someone could give me a greater understanding of what happens under the covers and if I can make my code more efficient.

          taddypole...
          Attached Files

          Comment


            #6
            My mistake, I hadn't noticed what you were trying to do by using:
            if(!priorBull)
            index = CurrentBar;
            eDanny
            NinjaTrader Ecosystem Vendor - Integrity Traders

            Comment


              #7
              Hello Taddypole,

              In the background the computer has to keep track of where each object is in relation to the visible area on the chart to know whether to draw it and if so, where to draw it in relation to everything else. With that being said I believe it is clear why having less draw objects on the chart would yield greater performance. For greater detail, I recommend going through the WinForms code itself, as Ninjatrader 7 uses the WinForms library in .NET 3.5 to render graphics.
              If you wish to test the execution time of different methods yourself, you can try working with the System.Diagnostics.Stopwatch

              You could also try drawing a single region. Then redrawing that region anytime it is updated. You would simply have to make sure the data series' you pass to it are updated.

              Please let me know if I may be of further assistance.
              Michael M.NinjaTrader Quality Assurance

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by xiinteractive, 04-09-2024, 08:08 AM
              3 responses
              11 views
              0 likes
              Last Post NinjaTrader_Erick  
              Started by Johnny Santiago, 10-11-2019, 09:21 AM
              95 responses
              6,193 views
              0 likes
              Last Post xiinteractive  
              Started by Irukandji, Today, 09:34 AM
              1 response
              3 views
              0 likes
              Last Post NinjaTrader_Clayton  
              Started by RubenCazorla, Today, 09:07 AM
              1 response
              6 views
              0 likes
              Last Post RubenCazorla  
              Started by TraderBCL, Today, 04:38 AM
              3 responses
              26 views
              0 likes
              Last Post NinjaTrader_Jesse  
              Working...
              X