Dark Theme
Light Theme
Trading Articles
Article Categories
Article Tools
Welcome to NexusFi: the best trading community on the planet, with over 150,000 members Sign Up Now for Free
Genuine reviews from real traders, not fake reviews from stealth vendors
Quality education from leading professional traders
We are a friendly, helpful, and positive community
We do not tolerate rude behavior, trolling, or vendors advertising in posts
We are here to help, just let us know what you need
You'll need to
register in order to view the content of the threads and start contributing to our community.
It's free for basic access, or support us by becoming an Elite Member -- see if you qualify for a discount below.
-- Big Mike, Site Administrator
(If you already have an account, login at the top of the page)
OnStartUp error
Updated September 17, 2013
Top Posters
looks_one
Ragdoll
with 7 posts (1 thanks)
looks_two
Big Mike
with 2 posts (0 thanks)
looks_3
Tasker_182
with 2 posts (0 thanks)
looks_4
Fat Tails
with 2 posts (0 thanks)
trending_up
4,995 views
thumb_up
1 thanks given
group
4 followers
forum
15 posts
attach_file
0 attachments
OnStartUp error
September 16th, 2013, 04:25 PM
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
}
}
Can you help answer these questions from other members on NexusFi?
Best Threads (Most Thanked) in the last 7 days on NexusFi
September 16th, 2013, 05:21 PM
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,103
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.
September 16th, 2013, 05:36 PM
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.
September 16th, 2013, 05:59 PM
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
September 16th, 2013, 06:21 PM
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,463 since Jun 2009
Thanks Given: 33,236
Thanks Received: 101,661
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
September 16th, 2013, 06:32 PM
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,103
Ragdoll
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
........
September 16th, 2013, 06:53 PM
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,463 since Jun 2009
Thanks Given: 33,236
Thanks Received: 101,661
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
September 16th, 2013, 07:46 PM
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
}
}
September 17th, 2013, 12:54 PM
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.
Last Updated on September 17, 2013