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

Performace problem

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

    Performace problem

    Hello,

    I wrote the code below in my indicator, it gives me the info I need but it results in performance probllems. Could you tell me if there is a better way to draw the objets with a lower performance impact?

    Code:
    protected override void OnBarUpdate()
            {
                Value.Set(Volume[0]);
    
                if (ActivarVolRelevantes) // if true
                    {
                    if (Volume[0] >= VolRelev1)                   
                        {
                        PlotColors[0][0] = ColorVolRel1;
                        }    
                    }
    
                if (ActivarVolRelevantes)
                    {    
                    if (Volume[0] >= VolRelev2)                   
                        {
                        PlotColors[0][0] = ColorVolRel2;  
                        }
                    }    
    
                if (CurrentBar < 1)                        // avoid out-of-range error
                   return;
                if (FirstTickOfBar)                   // Do on bar close    
                    {
                        if (Volume[1] >= VolRelev1)
                            {
                                if (Close[1] > Open[1])
                                    {    
                                        if (ActivarZonas)
                                        {    
                                        DrawRectangle("ZonaUp1" + CurrentBar, false, 1, Close[1] - TickSize, LongitudZona*-1 , High[1] + TickSize, ColorMarcoZonaUp, ColorFondoZonaUp, OpacidadFondoZona);
                                        }
                                        if (ActivarMarcadoresVelas)
                                        {    
                                        DrawLine("MarcaHUp1" + CurrentBar, false, 1, 0, 1,  Low[1] - TickSize, ColorMarcaHorVelaUp, EstiloMarcaHorVelaUp, GrosorMarcaHorVelaUp);
                                        }    
                                    }    
    
                                if (Close[1] < Open[1])
                                    {    
                                        if (ActivarZonas)
                                        {
                                        DrawRectangle("ZonaDown1" + CurrentBar, false,1, Close[1] + TickSize, LongitudZona*-1, Low[1] - TickSize, ColorMarcoZonaDown, ColorFondoZonaDown, OpacidadFondoZona);
                                        }
                                        if (ActivarMarcadoresVelas)
                                        {
                                        DrawLine("MarcaHDown1" + CurrentBar, false, 1, 0, 1,  Low[1] - TickSize, ColorMarcaHorVelaDown, EstiloMarcaHorVelaDown, GrosorMarcaHorVelaDown);
                                        }
                                    }    
    
                                if (Close[1] == Open[1])
                                        {    
                                    DrawOnPricePanel            = true;
                                    DrawRectangle("ZonaEven1" + CurrentBar, false, 2, Close[1] + TickSize, 0, Open[1] - TickSize, Color.Transparent, Color.Blue, 3);
                                        }
                            }

    #2
    Hello superg3,

    Thanks for your post.

    Your
    code runs either Calculate.OnEachTick or Calculate.OnPriceChange so that means your code will execute many hundreds to thousands of time per bar (depending on volatility and bar size).

    If you have conditions that once they occur as the bar is building no longer need to be checked you can skip that code section on future ticks until the next bar. For example, you can use an int variable to save the bar number when the condition is true and check the bar number on the conditions so that once the condition is true and the action is executed it will no longer be executed until the next bar. For example (assuming you create an int variable named savedBar):


    if (ActivarVolRelevantes && CurrentBar != savedBar) // check that the CurrentBar is not the same as the saved bar
    {
    if (Volume[0] >= VolRelev1)
    {
    PlotColors[0][0] = ColorVolRel1;
    savedBar = CurrentBar; // save the current bar number to prevent repeating until the very next bar
    }
    }
    CurrentBar is the bar number currently being processed, reference: https://ninjatrader.com/support/help...currentbar.htm

    Another performance issue could be the number of unique draw objects being drawn over the historical data. If you are drawing the objects from one bar to the next and you have many bars in a row then you have many draw objects that really could be just one object that is updated from its beginning point to the current bar. This would require additional logic in your coding to note the beginning bar to reference back to and then using the same tagname until the condition changes the beginning point.


    For example (assuming a bool called firstTime set to false and an int called beginBar)
    if (some condition to draw rectangle)
    {
    if (firstTime)
    {
    beginBar = CurrentBar; // this will be the beginning point
    firstTime = true; // change so that we don't process again
    }
    Draw.Rectangle(... using tag name with +begInBar... use start bar of (CurrentBar - beginBar), draw to the currentbar 0...);
    }

    else
    {
    firstTime = false; // if the condition is no longer true then reset so we can draw the new rectangle the next time the condition is true.
    }
    Last edited by NinjaTrader_PaulH; 03-06-2019, 07:40 AM. Reason: needed to reformat the text
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Hello dear Paul,

      please ¿could I send you an email with my full code?

      Comment


        #4
        Hello superg3,

        Thanks for your reply.

        Please note that we do not provide debugging services.

        You asked for performance assistance which has been provided. We cannot go through your code line by line if this is what you are asking for.

        If you have a specific Ninjascript question, please feel free to ask here or you can alternatively write into PlatformSupport[at]Ninjatrader[dot]Com and mark the e-mail atten Paul and include a link to this thread for reference.

        Alternatively, if you would like your script professionally coded we can provide references to 3rd party coders.
        Paul H.NinjaTrader Customer Service

        Comment


          #5
          Excuse me Paul, I should explain it better. Actually what I wanted to show you is what happens when I introduce the code to get better performance. It compiles but the objets are not draw.
          With the original code and performace issues:
          Click image for larger version  Name:	original.JPG Views:	1 Size:	140.8 KB ID:	1050650



          and with the additional code for better performance:

          Click image for larger version

Name:	image_52949.jpg
Views:	528
Size:	125.1 KB
ID:	1050651

          I can't see what is wrong:

          Code:
                      if (CurrentBar < 1)                        // avoid out-of-range error
                         return;
                      {    
                      if (FirstTickOfBar)                   // Do on bar close    
                          {
                              if (Volume[1] >= VolRelev1)
                                  {
                                      if (Close[1] > Open[1])
                                          {    
                                              if (ActivarZonas)
                                                  {
                                                      if (firstTime)
                                                      {    
                                                          beginBar = CurrentBar; // this will be the beginning point
                                                          firstTime = true; // change so that we don't process again
                                                      }    
                                                      DrawRectangle("ZonaUp1" + beginBar, false, (CurrentBar - beginBar), Close[1] - TickSize, LongitudZona*-1 , High[1] + TickSize, ColorMarcoZonaUp, ColorFondoZonaUp, OpacidadFondoZona);    
                                                  }    
                                                      else
                                                      {
                                                          firstTime = false; // if the condition is no longer true then reset so we can draw the new rectangle the next time the condition is true.    
                                                      }
          
          
          
          
                                              if (ActivarMarcadoresVelas)
                                                  {    
                                                  DrawLine("MarcaHUp1" + CurrentBar, false, 1, 0, 1,  Low[1] - TickSize, ColorMarcaHorVelaUp, EstiloMarcaHorVelaUp, GrosorMarcaHorVelaUp);
                                                  }
          
                                              }
                                        }
                               }          
                        }
                  }
          Last edited by superg3; 03-07-2019, 01:35 PM.

          Comment


            #6
            Hello superg3,

            Thanks for your reply.

            The example code was intended to give you an idea that you could then code, as you have.

            When your code compiles fine but is not working as expected, please check the "log" tab of the control center for any run-time type errors.

            If errors, or even if no errors found, then you will need to debug your code. Typically this is done by using print statements just ahead of an instead of conditional code blocks. The print statements can be used to output the state of the variables so you can then understand why it is or is not making the decisions needed to draw. The print statements send their output to the New>Ninjascript output window. Here is a link to our debugging tips for further detail: https://ninjatrader.com/support/help...script_cod.htm
            Paul H.NinjaTrader Customer Service

            Comment


              #7
              Originally posted by NinjaTrader_PaulH View Post
              Hello superg3,

              Thanks for your reply.

              The example code was intended to give you an idea that you could then code, as you have.

              When your code compiles fine but is not working as expected, please check the "log" tab of the control center for any run-time type errors.

              If errors, or even if no errors found, then you will need to debug your code. Typically this is done by using print statements just ahead of an instead of conditional code blocks. The print statements can be used to output the state of the variables so you can then understand why it is or is not making the decisions needed to draw. The print statements send their output to the New>Ninjascript output window. Here is a link to our debugging tips for further detail: https://ninjatrader.com/support/help...script_cod.htm
              Thenk you very much Paul.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by maybeimnotrader, Today, 05:46 PM
              0 responses
              6 views
              0 likes
              Last Post maybeimnotrader  
              Started by quantismo, Today, 05:13 PM
              0 responses
              6 views
              0 likes
              Last Post quantismo  
              Started by AttiM, 02-14-2024, 05:20 PM
              8 responses
              166 views
              0 likes
              Last Post jeronymite  
              Started by cre8able, Today, 04:22 PM
              0 responses
              8 views
              0 likes
              Last Post cre8able  
              Started by RichStudent, Today, 04:21 PM
              0 responses
              5 views
              0 likes
              Last Post RichStudent  
              Working...
              X