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

OnStartUp() Best Practices

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

    OnStartUp() Best Practices

    Hi there,

    I've been trying to do some research on how best to utilize the OnStartUp() function in relation to my strategy code. Right now, I just have it opening an output window at the start of my script via the following:

    Code:
    protected override void OnStartUp() 
    		{
    			//  Show the output window on startup
    			NinjaTrader.Gui.Globals.OutputWindow.Show();
    		}
    Beyond controlling GUI environments, are there any documented best practices with using OnStartUp()? Anything related to setting variables or??

    Thanks in advance!

    #2
    Hi Spiderbird,

    Generally, anything that you want set that you don't want changed by the user but still have a public parameter, such as CalculateOnBarClose or you can set in OnStartup() and this will superseded anything the user sets.

    Also, any calculations you want done before starting the script would go here as well.

    Other than that, this is to be used by you for anything you want to have happen before the first call of OnBarUpdate().
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_ChelseaB View Post
      Hi Spiderbird,

      Generally, anything that you want set that you don't want changed by the user but still have a public parameter, such as CalculateOnBarClose or you can set in OnStartup() and this will superseded anything the user sets.

      Also, any calculations you want done before starting the script would go here as well.

      Other than that, this is to be used by you for anything you want to have happen before the first call of OnBarUpdate().
      I also do this.

      For example, some indicators require COBC set to False, otherwise they won't work.
      I put COBC = False in OnStartUp(), and therefore the value is always False no matter how users change COBC in the indicator dialog.

      Thanks.
      Pi
      Last edited by ninZa; 01-16-2015, 10:35 PM.
      ninZa
      NinjaTrader Ecosystem Vendor - ninZa.co

      Comment


        #4

        Comment


          #5
          Thanks all!

          Hi Chelsea_B,

          Thanks for the explanation, and sledge's link also tied in Initialize as well. So let me show a couple of code examples I'm using and I'll take a stab at where they could go.

          For review:
          Initialize() - Called once when the strategy is started. Ideal for initializing DataSeries objects.
          OnStartUp() - Ideal for setting things once before processing.
          OnBarUpdate() - Where your strategy code would ideally go.

          Would any/all of the below code examples be ideally planted in OnStartUp()?

          Code Example #1 - Declaring variables
          Code:
          double
          env_StartPrice,
          env_EndPrice,
          env_PriceRange;

          Code Example #2 - Status Messages
          Code:
          //  Print out the current date and time
          	Print("Time Stamp: " + Time[0].ToString("H:mm:ss tt"));
          	Print("----------------------------------------------");
          Code Example #3 - Methods
          Code:
          private void calculate_Performance()
          			{
          				profitLoss_GrossProfit = Math.Round(Performance.AllTrades.TradesPerformance.GrossProfit,2);
          				profitLoss_GrossLoss = Math.Round(Performance.AllTrades.TradesPerformance.GrossLoss,2);
          				profitLoss_Commissions = Performance.AllTrades.TradesPerformance.Commission;
          				profitLoss_Totals = Math.Round((profitLoss_GrossProfit + profitLoss_GrossLoss) - profitLoss_Commissions,2);
          			}
          Code Example #4 - Variable calculations
          Code:
          //  All DoubleStochastics variables are set here
          					stoch = Math.Round(DoubleStochastics(15)[1],2);
          					stoch_Prev = Math.Round(DoubleStochastics(15)[2],2);

          Comment


            #6
            Hi Spiderbird,

            Example #1, #3: put OUTside OnBarUpdate, OnStartUp, Initialize

            Example #2, #4: put INside OnBarUpdate

            Cheers.
            Pi
            ninZa
            NinjaTrader Ecosystem Vendor - ninZa.co

            Comment


              #7
              Great! That's what I currently have. Much appreciated.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by jpapa, Today, 07:22 AM
              1 response
              5 views
              0 likes
              Last Post NinjaTrader_Gaby  
              Started by kevinenergy, 02-17-2023, 12:42 PM
              116 responses
              2,758 views
              1 like
              Last Post kevinenergy  
              Started by franatas, 12-04-2023, 03:43 AM
              7 responses
              106 views
              0 likes
              Last Post NinjaTrader_ChelseaB  
              Started by Jltarrau, Today, 05:57 AM
              3 responses
              9 views
              0 likes
              Last Post Jltarrau  
              Started by f.saeidi, Today, 05:56 AM
              2 responses
              8 views
              0 likes
              Last Post NinjaTrader_Erick  
              Working...
              X