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

Understanding OnStartUp better

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

    Understanding OnStartUp better

    from the descriptions of OnStartUp, I think it is used to define a few constants once between OnBarUpdate.

    However, I came across some sample codes that look like this:

    #region Variables

    private int period = 14; // Default setting for Period
    private int smooth1 = 5; // Default setting for Smooth1
    private int smooth2 = 3; // Default setting for Smooth2
    private int smooth3 = 9; // Default setting for Smooth3
    private EMA average1;
    private SMA average2;
    #endregion

    protected override void Initialize()
    {
    Add(new Plot(..., "MyFavPlot"));
    Overlay = false;
    }

    protected override void OnStartUp()
    {
    average1 = EMA(EMA(RSI(Input,period,3), smooth1), smooth2);
    average2 = SMA(average1, smooth3);
    }

    protected override void OnBarUpdate()
    {
    ....
    }


    Why is average1, average2 defined within OnStartUp?

    #2
    Originally posted by uday12 View Post
    from the descriptions of OnStartUp, I think it is used to define a few constants once between OnBarUpdate.

    However, I came across some sample codes that look like this:

    #region Variables

    private int period = 14; // Default setting for Period
    private int smooth1 = 5; // Default setting for Smooth1
    private int smooth2 = 3; // Default setting for Smooth2
    private int smooth3 = 9; // Default setting for Smooth3
    private EMA average1;
    private SMA average2;
    #endregion

    protected override void Initialize()
    {
    Add(new Plot(..., "MyFavPlot"));
    Overlay = false;
    }

    protected override void OnStartUp()
    {
    average1 = EMA(EMA(RSI(Input,period,3), smooth1), smooth2);
    average2 = SMA(average1, smooth3);
    }

    protected override void OnBarUpdate()
    {
    ....
    }


    Why is average1, average2 defined within OnStartUp?
    It is more an assignation than a definition.

    It has to be done after Initialize() because, barring a few documented internal objects that are created by the NT core, objects cannot really be initialized or created in Initialize(). It is a matter of how the NT framework was written to behave.
    Last edited by koganam; 02-14-2016, 03:31 PM. Reason: Corrected spelling and grammar.

    Comment


      #3
      Hello,
      Koganam is correct in his explanation that you would need to set these in OnStartup(). For more information I would recommend to review our help guide documentation on this event method: http://ninjatrader.com/support/helpG.../onstartup.htm
      Cody B.NinjaTrader Customer Service

      Comment


        #4
        Originally posted by koganam View Post
        It has to be done after Initialize() because, barring a few documented internal objects that are created by the NT core, objects cannot really be initialized or created in Initialize(). It is a matter of how the NT framework was written to behave.
        Huh?
        Can you elaborate on that a bit?

        For a Strategy, creating references to indicators must be done in Initialize() in order to add those indicators to the chart.

        Comment


          #5
          Originally posted by uday12 View Post
          Why is average1, average2 defined within OnStartUp?
          Is your code from a strategy?

          Good information here:

          Comment


            #6
            Originally posted by bltdavid View Post
            Huh?
            Can you elaborate on that a bit?

            For a Strategy, creating references to indicators must be done in Initialize() in order to add those indicators to the chart.
            Completely different matter. He is creating a (class scope) named instance of an indicator for use. It has nothing to do with anything else.

            You can do the exact same in a strategy, and Add() it in Initialize() for visualization purposes. In the case of a strategy, when the indicator is simply being Add()'ed, the object so created does not even have to have class scope.
            Last edited by koganam; 02-19-2016, 07:19 PM.

            Comment


              #7
              Originally posted by koganam View Post
              Completely different matter. He is creating a (class scope) named instance of an indicator for use. It has nothing to do with anything else.

              You can do the exact same in a strategy, and Add() it in Initialize() for visualization purposes. In the case if a strategy, the object so created does not even have to have class scope.
              I submit that we're probably contributing different pieces to the same answer.

              But I also submit that these two lines of code,

              Code:
              average1 = EMA(EMA(RSI(Input,period,3), smooth1), smooth2);
              average2 = SMA(average1, smooth3);
              could be in Initialize() or OnStartUp() and the script will work exactly the same.

              Comment


                #8
                Originally posted by bltdavid View Post
                I submit that we're probably contributing different pieces to the same answer.

                But I also submit that these two lines of code,

                Code:
                average1 = EMA(EMA(RSI(Input,period,3), smooth1), smooth2);
                average2 = SMA(average1, smooth3);
                could be in Initialize() or OnStartUp() and the script will work exactly the same.
                Try it in Initialize(), and you will find out the reality. (Objects which always return zero).

                Comment


                  #9
                  Originally posted by bltdavid View Post
                  Is your code from a strategy?
                  I took a closer look at the cope snippet in OP's original post. I can tell the code is from an indicator (due to use of Plot method and Overlay property in Initialize). My bad.

                  Koganam, good point. I will experiment with both Initialize and OnStartup and report back what I find. (It is quite possible I am wrong, I was going from a faded memory from what I coulda swore I've done in the past. If am wrong, I will gladly admit it.)

                  Comment


                    #10
                    Originally posted by bltdavid View Post
                    I took a closer look at the cope snippet in OP's original post. I can tell the code is from an indicator (due to use of Plot method and Overlay property in Initialize). My bad.

                    Koganam, good point. I will experiment with both Initialize and OnStartup and report back what I find. (It is quite possible I am wrong, I was going from a faded memory from what I coulda swore I've done in the past. If am wrong, I will gladly admit it.)
                    I don't remember if it was from an indicator. I have been browsing sample codes quite a bit.

                    Comment


                      #11
                      Originally posted by uday12 View Post
                      I don't remember if it was from an indicator. I have been browsing sample codes quite a bit.
                      Since the Plot and Overlay would cause a compilation error if it was used in a strategy, one can easily deduce that your code snippet must have come from an indicator.

                      Plot and Overlay are available in the Indicator class, but not the Strategy class.

                      Comment


                        #12
                        Originally posted by bltdavid View Post
                        Since the Plot and Overlay would cause a compilation error if it was used in a strategy, one can easily deduce that your code snippet must have come from an indicator.

                        Plot and Overlay are available in the Indicator class, but not the Strategy class.
                        Well, if there is a saving grace, I also seem to remember that in an earlier version of NT7, I could do that in Initialize()! The only reason that I discovered that it no longer seems to work is because I just wrote something to help out someone, and could not for the life of me see why my object returned only zero. I finally just moved the object assignation to OSU, and bingo!

                        Here is the relevant post:

                        ref: http://ninjatrader.com/support/forum...29&postcount=1

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by Stanfillirenfro, Today, 07:23 AM
                        8 responses
                        23 views
                        0 likes
                        Last Post Stanfillirenfro  
                        Started by DayTradingDEMON, Today, 09:28 AM
                        2 responses
                        15 views
                        0 likes
                        Last Post DayTradingDEMON  
                        Started by navyguy06, Today, 09:28 AM
                        1 response
                        6 views
                        0 likes
                        Last Post NinjaTrader_Gaby  
                        Started by cmtjoancolmenero, Yesterday, 03:58 PM
                        8 responses
                        32 views
                        0 likes
                        Last Post NinjaTrader_ChelseaB  
                        Started by helpwanted, Today, 03:06 AM
                        2 responses
                        22 views
                        0 likes
                        Last Post NinjaTrader_LuisH  
                        Working...
                        X