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

calling indicator in OnStartUp() produces different results than in OnBarUpdate()

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

    calling indicator in OnStartUp() produces different results than in OnBarUpdate()

    Hi,

    I've read somewhere on the forums that it is more efficient to assign a frequently used variable under OnStartUp() instead of calling it separately under OnBarUpdate().

    For example, I have the following code:

    Code:
    private SMA           sma3;
    private double        tempVar1 = 0, tempVar2 = 0;
    
    protected override void Initialize()
    {
         Add(PeriodType.Day,1);
    }
    
    
    protected override void OnStartUp()
    {
         if (sma3 == null)
              sma3 = SMA(3);
    }
    
    protected override void OnBarUpdate()
    {
         if (BarsInProgress == 1)
         {
              tempVar = sma3[0];
              tempVar1 = SMA(3)[0];
         }
    
    }
    For some reason, the results for tempVar and tempVar1 are different. This only happens under (BarsInProgress == 1) though. On the primary bars, using either "sma3" or "SMA(3)" produces the correct/same results.

    Could someone tell me what I'm doing wrong?

    Thanks.

    -Nick

    #2
    Hello,

    Thanks for the note.

    There are a few things off on your code sample here.

    One is I'm not sure what you mean on your statement its better of to assign more frequently used variables in on start up.

    OnStartUp() is only called once on start up of the i indicator. If you only want to do an action once you would do it here.

    OnBarUpdate() is called every tick based on CalculateOnBarClose = true or false.

    Also in your code you assign in OnStartUp() the sma without any indexer bracket at the end.

    []

    This would cause a difference as you really are assigning two different things.

    SMA(3)[0] is the current bars sma
    SMA(3) is the SMA object itself.

    -Brett

    Comment


      #3
      Originally posted by nicbizz View Post
      Hi,

      I've read somewhere on the forums that it is more efficient to assign a frequently used variable under OnStartUp() instead of calling it separately under OnBarUpdate().

      For example, I have the following code:

      Code:
       
      private SMA           sma3;
      private double        tempVar1 = 0, tempVar2 = 0;
       
      protected override void Initialize()
      {
           Add(PeriodType.Day,1);
      }
       
       
      protected override void OnStartUp()
      {
           if (sma3 == null)
                sma3 = SMA(3);
      }
       
      protected override void OnBarUpdate()
      {
           if (BarsInProgress == 1)
           {
                tempVar = sma3[0];
                tempVar1 = SMA(3)[0];
           }
       
      }
      For some reason, the results for tempVar and tempVar1 are different. This only happens under (BarsInProgress == 1) though. On the primary bars, using either "sma3" or "SMA(3)" produces the correct/same results.

      Could someone tell me what I'm doing wrong?

      Thanks.

      -Nick
      Your sma3 assignation in OSU has no bar filter, so it is being created relative to barSeries0, and will be called against it at all times, whereas your direct SMA(3) code in OBU is filtered by whatever BarsInProgress you process it under. So if BarsInProgress is 1, then SMA(3) is being processed against BarsArray[1], but sma3 is still being processed against BarsArray[0]. The results are more likely to be different than identical.
      Last edited by koganam; 11-29-2012, 07:43 AM. Reason: Corrected grammar.

      Comment


        #4
        Originally posted by NinjaTrader_Brett View Post

        One is I'm not sure what you mean on your statement its better of to assign more frequently used variables in on start up.
        Hi Brett,

        Sorry, I meant "frequently used indicator" instead of "variables"

        Also in your code you assign in OnStartUp() the sma without any indexer bracket at the end.

        []

        This would cause a difference as you really are assigning two different things.

        SMA(3)[0] is the current bars sma
        SMA(3) is the SMA object itself.
        Are you saying I should assign "sma3 = SMA(3)[0]" instead of "sma3 = SMA(3)", and simply use sma3 (instead of sma3[0]) in my code? Hmm, I'll give it a shot to see if there's any difference.

        Your sma3 assignation in OSU has no bar filter, so it is being created relative to barSeries0, and will be called against it at all times, whereas your direct SMA(3) code in OBU is filtered by whatever BarsInProgress you process it under. So if BarsInProgress is 1, then SMA(3) is being processed against BarsArray[1], but sma3 is still being processed against BarsArray[0]. The results are more unlikely to be different than identical.
        Koganam, you're absolutely right! Thanks for the tip. I used sma3 = SMA(Closes[1], 3) instead, and it worked.

        My only problem now is when I tried it with something like "sma3 = SMA(Closes[1], CurrentBars[1]" in OnStartUp(). Instead of using the CurrentBar number on BarsArray[1] when the code is called, the period for the SMA is fixed at 1. Any idea on how I can fix this?

        -Nick

        Comment


          #5
          Code:
           
          sma3 = SMA(Closes[1], [COLOR=red]CurrentBars[1][/COLOR])
          I am not sure what that is supposed to mean. The second parameter to your sma3 definition should be an integer. It seems that your CurrentBars[1] is being automatically cast to an integer, which would be a value of "1".

          What are you actually trying to code?

          Comment


            #6
            No need to assign anything in OnStartUp().

            Just use the SMA(3)[0] in your code where you want the SMA value. No need to store it in a variable that is static and doesnt update automaticly vs SMA(3)[0] updates automatically by NinjaTrader each OnBarUpdate().

            -Brett

            Comment


              #7
              Originally posted by koganam View Post
              Code:
               
              sma3 = SMA(Closes[1], [COLOR=red]CurrentBars[1][/COLOR])
              I am not sure what that is supposed to mean. The second parameter to your sma3 definition should be an integer. It seems that your CurrentBars[1] is being automatically cast to an integer, which would be a value of "1".

              What are you actually trying to code?
              I'm trying to use sma3 in (BarsInProgress == 1), and have the period (i.e. second parameter) of the SMA indicator update automatically based on the CurrentBar value of BarsArray[1].

              I think I just realized that's not possible.

              Comment


                #8
                Originally posted by NinjaTrader_Brett View Post
                No need to assign anything in OnStartUp().

                Just use the SMA(3)[0] in your code where you want the SMA value. No need to store it in a variable that is static and doesnt update automaticly vs SMA(3)[0] updates automatically by NinjaTrader each OnBarUpdate().

                -Brett
                Previously, I was doing so, but I'm having severe memory leaks during optimization. Assigning a static variable to an indicator is one of the solutions I've read that might help.

                Comment


                  #9
                  Understood,in this case you are actually introducing more variables and the assigning of it in OnStartUp() really doesn't save you any performance nor would it help you with any memory leak issue.

                  Can you describe your memory leak that you are running into? What exactly is going on? Is memory starting slow and keeps going high and higher or does it all the sudden jump in memory. Are you running into performance problems elsewhere?

                  Furthermore please note that the .net framework and c# is managed which means it has a garbage collector that runs on collects no longer needed memory. The CLR will clear memory as its no longer referenced by anything and you really don't have control on when the garbage collector will run. Really the only thing you needed to insure you are not continuing to reference something that is taking up memory and the .net framework will take care of the rest.

                  Perhaps I can offer some advice to further assist with the primary issue.

                  -Brett

                  Comment


                    #10
                    Originally posted by nicbizz View Post
                    I'm trying to use sma3 in (BarsInProgress == 1), and have the period (i.e. second parameter) of the SMA indicator update automatically based on the CurrentBar value of BarsArray[1].

                    I think I just realized that's not possible.
                    In that case, that may be syntactically correct after all. Print() out the value of CurrentBars[1] value, to see if that is so.

                    That means, I presume that you are trying to use a variable period for the SMA, instead of a fixed one. Unfortunately, examining the code of the SMA, as written, will show that it is not written to be capable of correctly using a non-fixed period.

                    I found this out, much to my chagrin, when, a few years ago, I tried to use a varying period, based on the dominant wave. I had to rewrite the SMA, in order to be able to use a varying period.

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by andrewtrades, Today, 04:57 PM
                    1 response
                    4 views
                    0 likes
                    Last Post NinjaTrader_Manfred  
                    Started by chbruno, Today, 04:10 PM
                    0 responses
                    3 views
                    0 likes
                    Last Post chbruno
                    by chbruno
                     
                    Started by josh18955, 03-25-2023, 11:16 AM
                    6 responses
                    436 views
                    0 likes
                    Last Post Delerium  
                    Started by FAQtrader, Today, 03:35 PM
                    0 responses
                    6 views
                    0 likes
                    Last Post FAQtrader  
                    Started by rocketman7, Today, 09:41 AM
                    5 responses
                    19 views
                    0 likes
                    Last Post NinjaTrader_Jesse  
                    Working...
                    X