Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Optimizing MA perfomance

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

    Optimizing MA perfomance

    Hi!

    I'm trying to use MA to trigger alerts when the price is inside two custom indicator lines. Math of the two indicator seems heavy for CPU because it cannot calculate them successfully in real time with every tick incoming. It only gives the true value when setting them to calculate on bar close.

    The trick I have been using to overcome this problem is to program an indicator that gives 1 when the condition is met (price between two lines) but using the previous bar value of indicator lines to avoid calculating them in real time. But a new problem arise, this indicator need to be reloaded after each bar closes. With an indicator based in 60 minutes bars is manageable, but with an indicator based in 5 minutes bars is a headache.

    What can I do?

    Sergio

    #2
    If you're calculating on from a previous bar, you cannot ask the market analyzer to recalculate on historical data.

    Can you clarify why you can't run it as calculate on bar close set to true on the current bar? This should stop the indicator from calculating in real-time and will only update when a bar closes.

    If you would like us to help optimize the performance of this indicator, would you be able to share the code you're using for us to better understand what you're running into?
    Last edited by NinjaTrader_Matthew; 06-12-2012, 07:54 AM.
    MatthewNinjaTrader Product Management

    Comment


      #3
      Let's clarify a bit:

      I use an indicator that compares real time price (incoming tick) with the values of two indicator lines calculated in previous closed bar.

      I need to calculate it with CalculateOnBarClose = false because I need alerts triggering when the price reach those levels in real time, not when the bars closes.

      This is the code. Primary bars 5', secondary bars 15'

      Code:
      protected override void OnBarUpdate()
              {
                  // Use this method for calculating your indicator values. Assign a value to each
                  // plot below by replacing 'Close[0]' with your own formula.
      			if (CurrentBars[0] <= 210 || CurrentBars[1] <= 70)
              	return;
      			if(Input[0]> SercavaEnvelope3(BarsArray[1],1,20).Upper[1] && Input[0] > SercavaEnvelope3(1,20).Upper[1])
      				a = 1;
      			else if(Input[0]< SercavaEnvelope3(BarsArray[1],1,20).Lower[1] && Input[0] < SercavaEnvelope3(1,20).Lower[1])
      				a = -1;
      			else
      				a = 0;
                  Plot0.Set(a);
              }
      I need to reload MA each 5' to get proper results.

      Thanks in advance,

      Sergio

      Comment


        #4
        Some screenshots where you can see when calculating in real time gives completely different results.

        Left: calculating with each incoming tick
        Right: calculating on bar close
        Attached Files

        Comment


          #5
          What are the errors you are getting on the Log tab of the Control Center in yellow?

          When you say the indicator needs to be reloaded, do you mean that it stops updating as 1, 0 -1? Or it gives you different values that you would expect?
          MatthewNinjaTrader Product Management

          Comment


            #6
            I have no errors in Control Center Log.

            When I say that it needs to be reloaded is because it is giving abnormal values (0 when it must be 1 or -1, or -1 when it must be 0..) or in the case of the moving average pics I posted totally different values that you can see comparing the two charts.

            If you see the screenshot I posted, if I want the true values of that moving average, I need to reload the chart. Reloading gives me the correct value from the beginning to the last closed bar. It begins to act weirdly again when calculating the still developing bar once it receives more data.

            Comment


              #7
              Hello,

              Since you are running this from two series, you will want to ensure your calculations are being done on the primary series. Please try putting your logic in BarsInProgress 0 and let me know if you still need to manually reload:

              Code:
              if(BarsInProgress == 0)
              {
              	if(Input[0]> SercavaEnvelope3(BarsArray[1],1,20).Upper[1] && Input[0] >    SercavaEnvelope3(1,20).Upper[1])
              	                        a = 1;
              
                      else if(Input[0]< SercavaEnvelope3(BarsArray[1],1,20).Lower[1] && Input[0] < SercavaEnvelope3(1,20).Lower[1])
              				a = -1;
              			else
              				a = 0;
                          Plot0.Set(a);
              }
              MatthewNinjaTrader Product Management

              Comment


                #8
                Hi!

                Didn't work. To try to keep the problem as simple as possible I have the same problem with this code. Without multi time frame:

                Code:
                if (CurrentBar < 1)
                return;
                Plot0.Set(Math.Abs(Input[0]-SercavaMA1(1,true,40)[1])/TickSize/10);
                SercavaMA1 is the same moving average I posted the pics below. The indicator is a simple distance meter of the price to the average. I use the value of the last closed bar to calculate the distance. I evaluate this indicator in MA in real time (each incoming tick) to check if the price is moving near the average. I use the average in 60' bars and each hour I need to reload the MA window to get the correct value of the average and, of course, the correct distance.

                Comment


                  #9
                  I am not able to reproduce this behavior using standard indicators. In order to look into this further, I will need the indicators you are calculating from.

                  Please send me either the SercavaMA1 or the SercavaEnvelope3 indicators in an email to support[at]ninjatrader[dot]com so I can look into the further.
                  MatthewNinjaTrader Product Management

                  Comment


                    #10
                    As you recommended me I've been working in optimizing my code. As a result, NT can now calculate with every incoming tick the correct value of indicator.

                    I focused in:

                    - Reducing declared variables.
                    - Eliminating dataseries not strictly needed.
                    - Using no looping branches if there is another way to do it.
                    - Using OnStartUp() to make calculations valid for all bars
                    - Separating the logic in two branches, one calculated in the first tick of the bar and the other with each incoming tick.

                    The last is the most efficient way to optimize code.

                    Sergio

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by helpwanted, Today, 03:06 AM
                    1 response
                    5 views
                    0 likes
                    Last Post sarafuenonly123  
                    Started by Brevo, Today, 01:45 AM
                    0 responses
                    7 views
                    0 likes
                    Last Post Brevo
                    by Brevo
                     
                    Started by aussugardefender, Today, 01:07 AM
                    0 responses
                    5 views
                    0 likes
                    Last Post aussugardefender  
                    Started by pvincent, 06-23-2022, 12:53 PM
                    14 responses
                    242 views
                    0 likes
                    Last Post Nyman
                    by Nyman
                     
                    Started by TraderG23, 12-08-2023, 07:56 AM
                    9 responses
                    384 views
                    1 like
                    Last Post Gavini
                    by Gavini
                     
                    Working...
                    X