NexusFi: Find Your Edge


Home Menu

 





OnStartUp error


Discussion in NinjaTrader

Updated
      Top Posters
    1. looks_one Ragdoll with 7 posts (1 thanks)
    2. looks_two Big Mike with 2 posts (0 thanks)
    3. looks_3 Tasker_182 with 2 posts (0 thanks)
    4. looks_4 Fat Tails with 2 posts (0 thanks)
    1. trending_up 4,980 views
    2. thumb_up 1 thanks given
    3. group 4 followers
    1. forum 15 posts
    2. attach_file 0 attachments




 
Search this Thread

OnStartUp error

  #1 (permalink)
 Ragdoll 
Walterboro, South Carolina
 
Experience: Intermediate
Platform: ninjatrader, amibroker, thinkorswim
Trading: futures
Posts: 13 since Aug 2013
Thanks Given: 1
Thanks Received: 6

Hi all,

I need to buy a vowel. I have been working on a strategy that bases exits on the Pivots indicator. I was having some problems getting the indicator to properly report the pivots and s/r levels so I decided to write my own pivot and s/r level calculations. The problem is I'm getting a "**NT** Error on calling 'OnStartUp' method for strategy 'PivotPlay/d93c1269d8904ff99817bfaa496ee2a4': Object reference not set to an instance of an object."

I have troubleshot it down to the statements in bold in the code that follows. Warning, this is a work in progress. it compiles fine but pitches the error on enabling.

Any help you can provide on this would be greatly appreciated.
Thanks

Ragdoll

 
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")]
    public class PivotPlay : Strategy
    {
        #region Variables
        // Wizard generated variables
        private double pct = 0.005; // Default setting for Pct
        // User defined variables (add any user defined variables below
		private bool firstTime;
		private bool islong;
		
		private double pvtu;
		private double pvtl;
		private double rl1u;
		private double rl1l;
		private double rl2u;
		private double rl2l;
		private double rl3u;
		private double rl3l;
		private double sl1u;
		private double sl1l;
		private double sl2u;
		private double sl2l;
		private double sl3u;
		private double sl3l;
		
		private double pvt;
		private double rl1;
        private double rl2;
		private double rl3;
		private double sl1;
		private double sl2;
		private double sl3;
		private double prevDayClose;
		private double prevDayHigh;
		private double prevDayLow;
		
		
        #endregion

        /// <summary>
        /// This method is used to configure the strategy and is called once before any strategy method is called.
        /// </summary>
        protected override void Initialize()
        {
			firstTime = false;
			islong = false;
			CalculateOnBarClose = true;
			
					
		
        }
		protected override void OnStartUp()
		{
			
			prevDayClose = Bars.GetDayBar(1).Close; 
			prevDayHigh = Bars.GetDayBar(1).High; 
			prevDayLow = Bars.GetDayBar(1).Low;
			
			pvt = (prevDayClose + prevDayHigh + prevDayLow) / 3;
			rl1 = Pivots(PivotRange.Daily, HLCCalculationMode.CalcFromIntradayData, 0, 0, 0, 20).R1[0];
			rl2 = Pivots(PivotRange.Daily, HLCCalculationMode.CalcFromIntradayData, 0, 0, 0, 20).R2[0];
			 rl3 = Pivots(PivotRange.Daily, HLCCalculationMode.CalcFromIntradayData, 0, 0, 0, 20).R3[0];
			 sl1 = Pivots(PivotRange.Daily, HLCCalculationMode.CalcFromIntradayData, 0, 0, 0, 20).S1[0];
			 sl2 = Pivots(PivotRange.Daily, HLCCalculationMode.CalcFromIntradayData, 0, 0, 0, 20).S2[0];
		     sl3 = Pivots(PivotRange.Daily, HLCCalculationMode.CalcFromIntradayData, 0, 0, 0, 20).S3[0];
			
			 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 is "+pvt);
			Print("pvtu is "+pvtu);
			Print("rl1l is "+rl1l);
	
		}
	    /// <summary>
        /// Called on each bar update event (incoming tick)
        /// </summary>
        protected override void OnBarUpdate()
        {
		
				
			
			/// 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; 
			
			
			
			
			
			
			
		   if (islong)
		    { if (Close[0] > pvtu)
				{ SetStopLoss("Entry",CalculationMode.Price,pvtu,true);
					
					
				}else if ( Close[0] > rl1l)
				{ SetStopLoss("Entry",CalculationMode.Price,rl1l,true);
		 
			}else if ( Close[0] > rl1u)
				{ SetStopLoss("Entry",CalculationMode.Price,rl1,true);
				} 
			else
				{ 
					return;
				}
				
			}
			
		
		
			
        }

        #region Properties
        [Description("")]
        [GridCategory("Parameters")]
        public double Pct
        {
            get { return pct; }
            set { pct = Math.Max(0.001, value); }
        }
        #endregion
    }
}

Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Better Renko Gaps
The Elite Circle
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
Exit Strategy
NinjaTrader
MC PL editor upgrade
MultiCharts
Trade idea based off three indicators.
Traders Hideout
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Just another trading journal: PA, Wyckoff & Trends
31 thanks
Spoo-nalysis ES e-mini futures S&P 500
28 thanks
Tao te Trade: way of the WLD
24 thanks
Bigger Wins or Fewer Losses?
20 thanks
GFIs1 1 DAX trade per day journal
17 thanks
  #3 (permalink)
 
Fat Tails's Avatar
 Fat Tails 
Berlin, Europe
Market Wizard
 
Experience: Advanced
Platform: NinjaTrader, MultiCharts
Broker: Interactive Brokers
Trading: Keyboard
Posts: 9,888 since Mar 2010
Thanks Given: 4,242
Thanks Received: 27,102


You cannot access bar series in OnStartUp().

The following code section needs to be transferred to OnBarUpdate():

 
Code
prevDayClose = Bars.GetDayBar(1).Close;
prevDayHigh = Bars.GetDayBar(1).High;
prevDayLow = Bars.GetDayBar(1).Low;

pvt = (prevDayClose + prevDayHigh + prevDayLow) / 3;
rl1 = Pivots(PivotRange.Daily, HLCCalculationMode.CalcFromIntradayData, 0, 0, 0, 20).R1[0];
rl2 = Pivots(PivotRange.Daily, HLCCalculationMode.CalcFromIntradayData, 0, 0, 0, 20).R2[0];
rl3 = Pivots(PivotRange.Daily, HLCCalculationMode.CalcFromIntradayData, 0, 0, 0, 20).R3[0];
sl1 = Pivots(PivotRange.Daily, HLCCalculationMode.CalcFromIntradayData, 0, 0, 0, 20).S1[0];
sl2 = Pivots(PivotRange.Daily, HLCCalculationMode.CalcFromIntradayData, 0, 0, 0, 20).S2[0];
sl3 = Pivots(PivotRange.Daily, HLCCalculationMode.CalcFromIntradayData, 0, 0, 0, 20).S3[0];

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);


Also make sure that your only try to access Bars.GetDayBar(1), if a prior daily bar exists.

Reply With Quote
  #4 (permalink)
 Ragdoll 
Walterboro, South Carolina
 
Experience: Intermediate
Platform: ninjatrader, amibroker, thinkorswim
Trading: futures
Posts: 13 since Aug 2013
Thanks Given: 1
Thanks Received: 6

I tried that and got the following error:

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

thanks for the reply.


Started this thread Reply With Quote
  #5 (permalink)
 Ragdoll 
Walterboro, South Carolina
 
Experience: Intermediate
Platform: ninjatrader, amibroker, thinkorswim
Trading: futures
Posts: 13 since Aug 2013
Thanks Given: 1
Thanks Received: 6

just tried again. same error message

Started this thread Reply With Quote
  #6 (permalink)
 
Big Mike's Avatar
 Big Mike 
Manta, Ecuador
Site Administrator
Developer
Swing Trader
 
Experience: Advanced
Platform: Custom solution
Broker: IBKR
Trading: Stocks & Futures
Frequency: Every few days
Duration: Weeks
Posts: 50,442 since Jun 2009
Thanks Given: 33,215
Thanks Received: 101,602

First of all it's cleaner to just reuse some classes like

#variables

private Pivots myPivots;

#onbarupdate

myPivots = Pivots(PivotRange.Daily, HLCCalculationMode.CalcFromIntradayData, 0, 0, 0, 20);

double r1 = myPivots.R1[0];
double s1 = myPivots.S1[0];

Your error message is most likely because you aren't listening to @Fat Tails and didn't check for CurrentBar or a session count before trying to call Bars.GetDayBar() 1 bar back. I haven't tested your code to know for sure.

Wrap that stuff around a (Bars.FirstBarOfSession && FirstTickOfBar) in #onbarupdate so it's only called at the beginning of a new session, and set a counter so you don't look to call it on the first session in the chart, wait until the second day to call it.

Mike

We're here to help: just ask the community or contact our Help Desk

Quick Links: Change your Username or Register as a Vendor
Searching for trading reviews? Review this list
Lifetime Elite Membership: Sign-up for only $149 USD
Exclusive money saving offers from our Site Sponsors: Browse Offers
Report problems with the site: Using the NexusFi changelog thread
Follow me on Twitter Visit my NexusFi Trade Journal Reply With Quote
  #7 (permalink)
 
Fat Tails's Avatar
 Fat Tails 
Berlin, Europe
Market Wizard
 
Experience: Advanced
Platform: NinjaTrader, MultiCharts
Broker: Interactive Brokers
Trading: Keyboard
Posts: 9,888 since Mar 2010
Thanks Given: 4,242
Thanks Received: 27,102


Ragdoll View Post
I tried that and got the following error:

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

thanks for the reply.


Did you take off the Print instructions?

The NinjaTrader pivots indicator only returns values, once it has detected a day break. Therefore it does not return any values for the first day. Before you access the pivots you actually need to check, whether a value is returned. This can be done with

 
Code
if(Pivots(PivotRange.Daily, HLCCalculationMode.CalcFromIntradayData, 0, 0, 0, 20).R1.ContainsValue(0))
  rl1 = Pivots(PivotRange.Daily, HLCCalculationMode.CalcFromIntradayData, 0, 0, 0, 20).R1[0];
else
  // do something here


Also follow the suggestion by @Big Mike to use an instance of mypivots, but do that in OnStartUp()!

 
Code
# variables
    private Pivots myPivots;

private override void OnStartUp()
{
    myPivots = Pivots(PivotRange.Daily, HLCCalculationMode.CalcFromIntradayData, 0, 0, 0, 20);
........
}

// now it is easier to access the pivots in OnBarUpdate()

if(myPivots.R1.ContainsValue(0))
   rl1 = myPivots.R1[0];
else
   ........

Reply With Quote
  #8 (permalink)
 
Big Mike's Avatar
 Big Mike 
Manta, Ecuador
Site Administrator
Developer
Swing Trader
 
Experience: Advanced
Platform: Custom solution
Broker: IBKR
Trading: Stocks & Futures
Frequency: Every few days
Duration: Weeks
Posts: 50,442 since Jun 2009
Thanks Given: 33,215
Thanks Received: 101,602

I reread your post and saw where you said the Print statement was giving you the errors.

I use a snippet like this when I am using indicators that take a few sessions to populate values, just to prevent me from taking signals due to missing or invalid data.

#variables
private int _totalsessions = 0;

#onbarupdate
if (Bars.FirstBarOfSession && FirstTickOfBar)
{
_totalsessions++;
}

if (_totalsessions < 5) return;

So I don't process any signal code unless we are in session 5 (day 5) or higher, when using weekly indicators like for example a weekly VWAP.

Mike

We're here to help: just ask the community or contact our Help Desk

Quick Links: Change your Username or Register as a Vendor
Searching for trading reviews? Review this list
Lifetime Elite Membership: Sign-up for only $149 USD
Exclusive money saving offers from our Site Sponsors: Browse Offers
Report problems with the site: Using the NexusFi changelog thread
Follow me on Twitter Visit my NexusFi Trade Journal Reply With Quote
  #9 (permalink)
 Ragdoll 
Walterboro, South Carolina
 
Experience: Intermediate
Platform: ninjatrader, amibroker, thinkorswim
Trading: futures
Posts: 13 since Aug 2013
Thanks Given: 1
Thanks Received: 6

My apologies for not being clear in my earlier post. I don't want to use the Pivots indicator. I want to write my own pivots based on the previous trading days high, low, and close. I know the formula and I have coded it in Thinkscript before. It should be fairly straight forward. I have cleaned up the code a bit to remove extraneous stuff and enclosed the code in the if section as @Big Mike suggested. I have two basic questions. How do I ensure the GetDayBar call returns data? And how do I put my code in a code box like you did in the previous post?

Here is the latest code:
I got the second question figured out

 
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")]
    public class PivotPlay : Strategy
    {
        #region Variables
        // Wizard generated variables
        private double pct = 0.005; // Default setting for Pct
        // User defined variables (add any user defined variables below
		private bool firstTime;
		private bool islong;
		
		private double pvtu;
		private double pvtl;
		private double rl1u;
		private double rl1l;
		private double rl2u;
		private double rl2l;
		private double rl3u;
		private double rl3l;
		private double sl1u;
		private double sl1l;
		private double sl2u;
		private double sl2l;
		private double sl3u;
		private double sl3l;
		
		private double pvt;
		private double rl1;
        private double rl2;
		private double rl3;
		private double sl1;
		private double sl2;
		private double sl3;
		private double prevDayClose;
		private double prevDayHigh;
		private double prevDayLow;
		
		
        #endregion

        /// <summary>
        /// This method is used to configure the strategy and is called once before any strategy method is called.
        /// </summary>
        protected override void Initialize()
        {
			firstTime = false;
			islong = false;
			CalculateOnBarClose = true;
			
					
		
        }
		protected override void OnStartUp()
		{
	
			
		
		}
	    /// <summary>
        /// Called on each bar update event (incoming tick)
        /// </summary>
        protected override void 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")]
        public double Pct
        {
            get { return pct; }
            set { pct = Math.Max(0.001, value); }
        }
        #endregion
    }
}

Started this thread Reply With Quote
  #10 (permalink)
 Ragdoll 
Walterboro, South Carolina
 
Experience: Intermediate
Platform: ninjatrader, amibroker, thinkorswim
Trading: futures
Posts: 13 since Aug 2013
Thanks Given: 1
Thanks Received: 6


OK,

I've had my RTFM moment and have solved the object problem with a little help from Bertrand. I added an if statement to check for a null on GetDayBar(1). The problem now is it is always null.

Started this thread Reply With Quote




Last Updated on September 17, 2013


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts