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 ageeholdings, Today, 07:43 AM
              0 responses
              3 views
              0 likes
              Last Post ageeholdings  
              Started by pibrew, Today, 06:37 AM
              0 responses
              4 views
              0 likes
              Last Post pibrew
              by pibrew
               
              Started by rbeckmann05, Yesterday, 06:48 PM
              1 response
              14 views
              0 likes
              Last Post bltdavid  
              Started by llanqui, Today, 03:53 AM
              0 responses
              6 views
              0 likes
              Last Post llanqui
              by llanqui
               
              Started by burtoninlondon, Today, 12:38 AM
              0 responses
              12 views
              0 likes
              Last Post burtoninlondon  
              Working...
              X