NinjaTrader Support Forum  

Go Back   NinjaTrader Support Forum > NinjaScript Development Support > Strategy Development

Strategy Development Support for the development of custom automated trading strategies using NinjaScript.

Reply
 
Thread Tools Display Modes
Old 02-07-2009, 08:43 PM   #1
kcsystemtrader
Member
 
Join Date: Dec 2008
Location: Kansas City
Posts: 90
Thanks: 0
Thanked 2 times in 2 posts
Default Object reference not set to an instance of an object

Need some help here. Getting the error "Object reference not set to an instance of an object" in the log. This is all I get in the output window...all of which printed between 83000 and 83030:

WITHIN ONBARUPDATE
WITHIN ONBARUPDATE
WITHIN BARSINPROGRESS = 1 SECTION

The OnBarUpdate section then stops running and I get the error message. I'm applying this on a 1 min chart and adding a secondary bars set to tick by tick.

[code]
protectedoverridevoid Initialize()
{
Add(PeriodType.Tick, 1);
Add(SMA(Period));
Add(BarTimer());
TraceOrders = true;
CalculateOnBarClose = true;
}
protectedoverridevoid OnBarUpdate()
{
Print("WITHIN ONBARUPDATE");
if (BarsInProgress == 0)
return;
if (BarsInProgress == 1)
{
Print("WITHIN BARSINPROGRESS = 1 SECTION");
if (ToTime(Time[0]) >= MarketOpen && ToTime(Time[0]) < MarketClose)
{
Print("WITHIN MARKET HOURS SECTION");
if (myEntryOrder == null && Position.MarketPosition == MarketPosition.Flat)
{
//Buy if current tick is above SMA of primary bars previous bar
Print("WITHIN ENTRY CONDITIONS SECTION");
if (Close[0] > SMA(BarsArray[0], Period)[1])
{
myEntryOrder = EnterLongLimit(1, true, 100, Closes[0][1], "Buy");
SetStopLoss("Buy",CalculationMode.Ticks, 10, false);
barNumberOfOrder = CurrentBar;
Print("WITHIN ENTRY CONDITIONS SATISFIED SECTION");
}
}
elseif (myEntryOrder != null && Position.MarketPosition == MarketPosition.Flat)
{
Print("WITHIN ENTRY CONDITIONS SECTION 2");
if (Close[0] > SMA(BarsArray[0], Period)[1])
{
myEntryOrder = EnterLongLimit(1, true, 100, Closes[0][1], "Buy");
SetStopLoss("Buy",CalculationMode.Ticks, 10, false);
barNumberOfOrder = CurrentBar;
Print("WITHIN ENTRY CONDITIONS SATISFIED SECTION 2");
}
}

}
}

if (CurrentBar > barNumberOfOrder + 2)
CancelOrder(myEntryOrder);
}

protectedoverridevoid OnOrderUpdate(IOrder order)
{
if (myEntryOrder != null && myEntryOrder.Token == order.Token)
{
Print(order.ToString());
if (order.OrderState == OrderState.Filled)
myEntryOrder = null;
}
}
kcsystemtrader is offline  
Reply With Quote
Old 02-07-2009, 11:05 PM   #2
NinjaTrader_Ben
NinjaTrader Customer Service
 
NinjaTrader_Ben's Avatar
 
Join Date: May 2008
Location: Denver, CO
Posts: 3,157
Thanks: 0
Thanked 3 times in 3 posts
Default

Hello,

Try commenting out lines of code and conditions until it progresses, then you will know what is the cause. You might want to start by commenting out this line since the Print() right above it was the last thing to work:
if (ToTime(Time[0]) >= MarketOpen && ToTime(Time[0]) < MarketClose)
NinjaTrader_Ben is offline  
Reply With Quote
Old 02-08-2009, 10:26 PM   #3
kcsystemtrader
Member
 
Join Date: Dec 2008
Location: Kansas City
Posts: 90
Thanks: 0
Thanked 2 times in 2 posts
Default still not working

Quote:
Originally Posted by NinjaTrader_Ben View Post
Hello,

Try commenting out lines of code and conditions until it progresses, then you will know what is the cause. You might want to start by commenting out this line since the Print() right above it was the last thing to work:
if (ToTime(Time[0]) >= MarketOpen && ToTime(Time[0]) < MarketClose)
I commented out the time filter code as you suggested, and it did place one order but then once that position was closed out, OnBarUpdate appeared to stop running again. Not sure why the time filter would have held it up in the first place because the Market Replay I was running was within an appropriate time frame (although I was testing outside of the timeframe).

I've attached the strategy. If someone could try it and see if they get the same results that would be helpful. First time trying to do a Multi-time frame strategy, so I'm sure I'm just missing something. Although, most of the code in the attached strategy was copied right out of NT reference samples. Thanks,

kc
Attached Files
File Type: zip TestBarsInProgress.zip (5.5 KB, 4 views)
kcsystemtrader is offline  
Reply With Quote
Old 02-09-2009, 12:15 AM   #4
NinjaTrader_Ben
NinjaTrader Customer Service
 
NinjaTrader_Ben's Avatar
 
Join Date: May 2008
Location: Denver, CO
Posts: 3,157
Thanks: 0
Thanked 3 times in 3 posts
Default

Hello,

Print() your MarketOpen and MarketClose and your ToTime() values just prior to the condition and see what values are being used.
NinjaTrader_Ben is offline  
Reply With Quote
Old 02-09-2009, 07:58 AM   #5
kcsystemtrader
Member
 
Join Date: Dec 2008
Location: Kansas City
Posts: 90
Thanks: 0
Thanked 2 times in 2 posts
Default

Quote:
Originally Posted by NinjaTrader_Ben View Post
Hello,

Print() your MarketOpen and MarketClose and your ToTime() values just prior to the condition and see what values are being used.
That is not the issue. I ran it this morning with real market data and it gets past that. It prints everything up to the line of code listed below, and then OnBarUpdate throws the error "Object reference not set to an instance of an object" in the Log.
Code:
 
if (myEntryOrder == null && Position.MarketPosition == MarketPosition.Flat)

Does it have something to do with == null?

Or perhaps something in the code under that line where I'm asking for information about a certain bar and I don't have that many bars yet or something?

Thanks,

kc
kcsystemtrader is offline  
Reply With Quote
Old 02-09-2009, 08:08 AM   #6
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 107 times in 70 posts
Default

Here is likely your problem.

if (CurrentBar > barNumberOfOrder + 2)
CancelOrder(myEntryOrder);

You can't call cancel order on a null IOrder object. You need to check for null before.
NinjaTrader_Josh is offline  
Reply With Quote
Old 02-09-2009, 08:32 AM   #7
kcsystemtrader
Member
 
Join Date: Dec 2008
Location: Kansas City
Posts: 90
Thanks: 0
Thanked 2 times in 2 posts
Default

Quote:
Originally Posted by NinjaTrader_Josh View Post
Here is likely your problem.

if (CurrentBar > barNumberOfOrder + 2)
CancelOrder(myEntryOrder);

You can't call cancel order on a null IOrder object. You need to check for null before.

That was it. I have my real strategy working now. As usual, thanks for the great support.
kcsystemtrader is offline  
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
...instance of an object error? Burga1 General Programming 12 05-05-2008 04:03 PM
Failed to call method "Initialize" for indicator 'Test': Object reference not set to clearpicks Indicator Development 3 04-23-2008 12:53 PM
Object reference... ATI user Strategy Development 3 04-08-2008 06:46 AM
Object not set to an instance of an object ATI user Historical NinjaTrader 6.5 Beta Threads 1 12-14-2007 03:35 AM
Error: Object reference not set to an instance of an object. Januson Market Analyzer 1 05-18-2007 12:12 AM


All times are GMT -6. The time now is 01:05 AM.