Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

OnStartUp error

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

    OnStartUp error

    i have been working on a strategy that uses pivot points to set up exits. i don't want to use the pivots indicator but rather prefer coding the formula myself. The problem is when i tried to put it in the OnStartUp method i get the following error:

    **NT** Error on calling 'OnStartUp' method for strategy 'PivotPlay/d93c1269d8904ff99817bfaa496ee2a4': Object reference not set to an instance of an object.

    When i moved the code to OnBarUpdate method i get the following:

    **NT** Error on calling 'OnBarUpdate' method for strategy 'PivotPlay/d93c1269d8904ff99817bfaa496ee2a4': Object reference not set to an instance of an object.

    i only get the error when i try to enable the strategy. Any help would be appreciated.

    Here is the code
    Code:
     
     
    #region Using declarations
    using System;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Data;
    using NinjaTrader.Indicator;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Strategy;
    #endregion
    // This namespace holds all strategies and is required. Do not change it.
    namespace NinjaTrader.Strategy
    {
    ///<summary>
    /// this is a strategythat uses pivot points and support / resistance levels to set exit and entry points
    ///</summary>
    [Description("this is a strategythat uses pivot points and support / resistance levels to set exit and entry points")]
    publicclass PivotPlay : Strategy
    {
    #region Variables
    // Wizard generated variables
    privatedouble pct = 0.005; // Default setting for Pct
    // User defined variables (add any user defined variables below
    privatebool firstTime;
    privatebool islong;
     
    privatedouble pvtu;
    privatedouble pvtl;
    privatedouble rl1u;
    privatedouble rl1l;
    privatedouble rl2u;
    privatedouble rl2l;
    privatedouble rl3u;
    privatedouble rl3l;
    privatedouble sl1u;
    privatedouble sl1l;
    privatedouble sl2u;
    privatedouble sl2l;
    privatedouble sl3u;
    privatedouble sl3l;
     
    privatedouble pvt;
    privatedouble rl1;
    privatedouble rl2;
    privatedouble rl3;
    privatedouble sl1;
    privatedouble sl2;
    privatedouble sl3;
    privatedouble prevDayClose;
    privatedouble prevDayHigh;
    privatedouble prevDayLow;
     
     
    #endregion
    ///<summary>
    /// This method is used to configure the strategy and is called once before any strategy method is called.
    ///</summary>
    protectedoverridevoid Initialize()
    {
    firstTime = false;
    islong = false;
    CalculateOnBarClose = true;
     
     
     
    }
    protectedoverridevoid OnStartUp()
    {
     
     
     
    }
    ///<summary>
    /// Called on each bar update event (incoming tick)
    ///</summary>
    protectedoverridevoid OnBarUpdate()
    {
    if(Bars.FirstBarOfSession && FirstTickOfBar)
    {
     
    prevDayClose = Bars.GetDayBar(1).Close; 
    prevDayHigh = Bars.GetDayBar(1).High; 
    prevDayLow = Bars.GetDayBar(1).Low;
     
    pvt = (prevDayClose + prevDayHigh + prevDayLow) / 3;
     
    pvtu = pvt + (pvt * pct);
    pvtl = pvt - (pvt * pct);
    rl1u = rl1 + (rl1 * pct);
    rl1l = rl1 - (rl1 * pct);
    rl2u = rl2 + (rl2 * pct);
    rl2l = rl2 - (rl2 * pct);
    rl3u = rl3 + (rl3 * pct);
    rl3l = rl3 - (rl3 * pct);
    sl1u = sl1 + (sl1 * pct);
    sl1l = sl1 - (sl1 * pct);
    sl2u = sl2 + (sl2 * pct);
    sl2l = sl2 - (sl2 * pct);
    sl3u = sl3 + (sl3 * pct);
    sl3l = sl3 - (sl3 * pct); 
     
    }
     
     
    /// Rule 1; enter long if close above first bar high...;
    if (Bars.FirstBarOfSession)
    { return;
    }
    else
    { int barOne = Bars.BarsSinceSession;
    if (Close[0] > High[barOne])
    {
     
    islong = true;
    firstTime = true;
    EnterLong("Entry");
    }
    }
    /// rule 2; 
    
     
     
     
     
     
     
     
     
     
    }
    #region Properties
    [Description("")]
    [GridCategory("Parameters")]
    publicdouble Pct
    {
    get { return pct; }
    set { pct = Math.Max(0.001, value); }
    }
    #endregion
    }
    }

    #2
    Welcome to our forums Ragdoll - from a quick visual of the code it seems you miss a null check for accessing the Bars.GetDayBar - http://www.ninjatrader.com/support/h...ightsub=GetDay

    Comment


      #3
      Thanks. That took care of the object not referenced error but the GetDayBar(1) always seems to be null. I want to run this one time.
      Any suggestions

      Comment


        #4
        Glad to hear - how much data are you loading up for your intraday charts?

        Comment


          #5
          I'm just trying to get the trading previous days high, low, and close.

          Comment


            #6
            Right, however if you get no values it would suggest not enough data is likely loaded to build that virtual bar for your script. Thus I would suggest increasing the days loaded range and then rechecking what values you get returned for your GetDayBar calls.

            Comment


              #7
              Forgive my ignorance but how do I do that?

              Comment


                #8
                Please right click on your chart and select the DataSeries section, set the DaysToLoad property for example to 5 or 10 days then and re-add your script to the chart.

                Comment


                  #9
                  OK. I did that and still not getting previous days data. here is the latest code.


                  Code:
                   
                   
                  #region Using declarations
                  using System;
                  using System.ComponentModel;
                  using System.Diagnostics;
                  using System.Drawing;
                  using System.Drawing.Drawing2D;
                  using System.Xml.Serialization;
                  using NinjaTrader.Cbi;
                  using NinjaTrader.Data;
                  using NinjaTrader.Indicator;
                  using NinjaTrader.Gui.Chart;
                  using NinjaTrader.Strategy;
                  #endregion
                  // This namespace holds all strategies and is required. Do not change it.
                  namespace NinjaTrader.Strategy
                  {
                  ///<summary>
                  /// this is a strategythat uses pivot points and support / resistance levels to set exit and entry points
                  ///</summary>
                  [Description("this is a strategythat uses pivot points and support / resistance levels to set exit and entry points")]
                  publicclass PivotPlay : Strategy
                  {
                  #region Variables
                  // Wizard generated variables
                  privatedouble pct = 0.005; // Default setting for Pct
                  // User defined variables (add any user defined variables below
                  privatebool firstTime;
                  privatebool islong;
                   
                  privatedouble pvtu;
                  privatedouble pvtl;
                  privatedouble rl1u;
                  privatedouble rl1l;
                  privatedouble rl2u;
                  privatedouble rl2l;
                  privatedouble rl3u;
                  privatedouble rl3l;
                  privatedouble sl1u;
                  privatedouble sl1l;
                  privatedouble sl2u;
                  privatedouble sl2l;
                  privatedouble sl3u;
                  privatedouble sl3l;
                   
                  privatedouble pvt;
                  privatedouble rl1;
                  privatedouble rl2;
                  privatedouble rl3;
                  privatedouble sl1;
                  privatedouble sl2;
                  privatedouble sl3;
                  privatedouble prevDayClose;
                  privatedouble prevDayHigh;
                  privatedouble prevDayLow;
                   
                   
                  #endregion
                  ///<summary>
                  /// This method is used to configure the strategy and is called once before any strategy method is called.
                  ///</summary>
                  protectedoverridevoid Initialize()
                  {
                  firstTime = false;
                  islong = false;
                  CalculateOnBarClose = true;
                   
                   
                   
                  }
                  protectedoverridevoid OnStartUp()
                  {
                  if (Bars.GetDayBar(10) != null)
                  {
                  prevDayClose = Bars.GetDayBar(10).Close; 
                  prevDayHigh = Bars.GetDayBar(10).High; 
                  prevDayLow = Bars.GetDayBar(10).Low;
                  }
                  pvt = (prevDayClose + prevDayHigh + prevDayLow) / 3;
                   
                  pvtu = pvt + (pvt * pct);
                  pvtl = pvt - (pvt * pct);
                  rl1u = rl1 + (rl1 * pct);
                  rl1l = rl1 - (rl1 * pct);
                  rl2u = rl2 + (rl2 * pct);
                  rl2l = rl2 - (rl2 * pct);
                  rl3u = rl3 + (rl3 * pct);
                  rl3l = rl3 - (rl3 * pct);
                  sl1u = sl1 + (sl1 * pct);
                  sl1l = sl1 - (sl1 * pct);
                  sl2u = sl2 + (sl2 * pct);
                  sl2l = sl2 - (sl2 * pct);
                  sl3u = sl3 + (sl3 * pct);
                  sl3l = sl3 - (sl3 * pct);
                   
                  Print(pvt);
                  Print(pvtu);
                   
                   
                   
                  }
                  ///<summary>
                  /// Called on each bar update event (incoming tick)
                  ///</summary>
                  protectedoverridevoid OnBarUpdate()
                  {
                  if(Bars.FirstBarOfSession && FirstTickOfBar)
                  {
                   
                  }
                   
                   
                  /// Rule 1; enter long if close above first bar high...;
                  if (Bars.FirstBarOfSession)
                  { return;
                  }
                  else
                  { int barOne = Bars.BarsSinceSession;
                  if (Close[0] > High[barOne])
                  {
                   
                   
                  islong = true;
                  firstTime = true;
                  EnterLong("Entry");
                  }
                  }
                  /// rule 2; 
                   
                   
                   
                   
                   
                   
                   
                   
                   
                   
                  }
                  #region Properties
                  [Description("")]
                  [GridCategory("Parameters")]
                  publicdouble Pct
                  {
                  get { return pct; }
                  set { pct = Math.Max(0.001, value); }
                  }
                  #endregion
                  }
                  }

                  Comment


                    #10
                    Thanks, I'm not following this change in your code here -

                    Bars.GetDayBar(10)

                    This was at one in your first post and would mean you seek the data of the prior day?

                    If you have a chart just of 10 days and run this code > I would expect you have to less data to build the bars.

                    Comment


                      #11
                      Sorry, I have corrected that to GetDayBar(1). still not getting anything in the print statements.

                      Comment


                        #12
                        Hi Ragdoll,

                        I had a chance to look over the code and noticed that you are calling the GetDayBar() with in the OnStartUp().

                        This method is not mean't to be used to access data for what you are looking for.

                        I would try moving your GetDayBar into your OnBarUpdate() and use a check such as FirsBarOfSession or FirstTickOfBar to ensure that it is only processed once in your code or when needed.

                        Let me know if this is the answer you are looking for.
                        Cal H.NinjaTrader Customer Service

                        Comment


                          #13
                          that did it!

                          here is the code that works:
                          Code:
                           
                          
                          #region Using declarations
                          using System;
                          using System.ComponentModel;
                          using System.Diagnostics;
                          using System.Drawing;
                          using System.Drawing.Drawing2D;
                          using System.Xml.Serialization;
                          using NinjaTrader.Cbi;
                          using NinjaTrader.Data;
                          using NinjaTrader.Indicator;
                          using NinjaTrader.Gui.Chart;
                          using NinjaTrader.Strategy;
                          #endregion
                          // This namespace holds all strategies and is required. Do not change it.
                          namespace NinjaTrader.Strategy
                          {
                          ///<summary>
                          /// this is a strategythat uses pivot points and support / resistance levels to set exit and entry points
                          ///</summary>
                          [Description("this is a strategythat uses pivot points and support / resistance levels to set exit and entry points")]
                          publicclass PivotPlay : Strategy
                          {
                          #region Variables
                          // Wizard generated variables
                          privatedouble pct = 0.005; // Default setting for Pct
                          // User defined variables (add any user defined variables below
                          privatebool firstTime;
                          privatebool islong;
                           
                          privatedouble pvtu;
                          privatedouble pvtl;
                          privatedouble rl1u;
                          privatedouble rl1l;
                          privatedouble rl2u;
                          privatedouble rl2l;
                          privatedouble rl3u;
                          privatedouble rl3l;
                          privatedouble sl1u;
                          privatedouble sl1l;
                          privatedouble sl2u;
                          privatedouble sl2l;
                          privatedouble sl3u;
                          privatedouble sl3l;
                           
                          privatedouble pvt;
                          privatedouble rl1;
                          privatedouble rl2;
                          privatedouble rl3;
                          privatedouble sl1;
                          privatedouble sl2;
                          privatedouble sl3;
                          privatedouble prevDayClose;
                          privatedouble prevDayHigh;
                          privatedouble prevDayLow;
                           
                           
                          #endregion
                          ///<summary>
                          /// This method is used to configure the strategy and is called once before any strategy method is called.
                          ///</summary>
                          protectedoverridevoid Initialize()
                          {
                          firstTime = false;
                          islong = false;
                          CalculateOnBarClose = true;
                           
                           
                           
                          }
                          protectedoverridevoid OnStartUp()
                          {
                          }
                          ///<summary>
                          /// Called on each bar update event (incoming tick)
                          ///</summary>
                          protectedoverridevoid OnBarUpdate()
                          {
                          if(Bars.FirstBarOfSession && FirstTickOfBar)
                          {
                          if (Bars.GetDayBar(1) != null)
                          {
                          prevDayClose = Bars.GetDayBar(1).Close; 
                          prevDayHigh = Bars.GetDayBar(1).High; 
                          prevDayLow = Bars.GetDayBar(1).Low;
                          }
                          pvt = (prevDayClose + prevDayHigh + prevDayLow) / 3;
                           
                          pvtu = pvt + (pvt * pct);
                          pvtl = pvt - (pvt * pct);
                          rl1u = rl1 + (rl1 * pct);
                          rl1l = rl1 - (rl1 * pct);
                          rl2u = rl2 + (rl2 * pct);
                          rl2l = rl2 - (rl2 * pct);
                          rl3u = rl3 + (rl3 * pct);
                          rl3l = rl3 - (rl3 * pct);
                          sl1u = sl1 + (sl1 * pct);
                          sl1l = sl1 - (sl1 * pct);
                          sl2u = sl2 + (sl2 * pct);
                          sl2l = sl2 - (sl2 * pct);
                          sl3u = sl3 + (sl3 * pct);
                          sl3l = sl3 - (sl3 * pct);
                           
                          Print(pvt);
                          Print(pvtu);
                           
                           
                           
                           
                           
                          }
                           
                           
                          /// Rule 1; enter long if close above first bar high...;
                          if (Bars.FirstBarOfSession)
                          { return;
                          }
                          else
                          { int barOne = Bars.BarsSinceSession;
                          if (Close[0] > High[barOne])
                          {
                           
                           
                          islong = true;
                          firstTime = true;
                          EnterLong("Entry");
                          }
                          }
                          /// rule 2; 
                          
                           
                           
                           
                           
                           
                           
                           
                           
                           
                          }
                          #region Properties
                          [Description("")]
                          [GridCategory("Parameters")]
                          publicdouble Pct
                          {
                          get { return pct; }
                          set { pct = Math.Max(0.001, value); }
                          }
                          #endregion
                          }
                          }

                          Comment


                            #14
                            Ragdoll,

                            Glad to hear its working the way you want it.

                            Please let us know if we can be of further assistance.
                            Cal H.NinjaTrader Customer Service

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                            0 responses
                            580 views
                            0 likes
                            Last Post Geovanny Suaza  
                            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                            0 responses
                            335 views
                            1 like
                            Last Post Geovanny Suaza  
                            Started by Mindset, 02-09-2026, 11:44 AM
                            0 responses
                            102 views
                            0 likes
                            Last Post Mindset
                            by Mindset
                             
                            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                            0 responses
                            554 views
                            1 like
                            Last Post Geovanny Suaza  
                            Started by RFrosty, 01-28-2026, 06:49 PM
                            0 responses
                            552 views
                            1 like
                            Last Post RFrosty
                            by RFrosty
                             
                            Working...
                            X