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 05-08-2009, 04:56 PM   #1
kcsystemtrader
Member
 
Join Date: Dec 2008
Location: Kansas City
Posts: 90
Thanks: 0
Thanked 2 times in 2 posts
Default Error calling 'OnExecution' - object reference not set to an instance of an object

I'm trying to load a strategy in Market Replay and it gets hung up doing all the calculations before kicking off (the calcs it does to try to determine current strategy position). I get the error trying to call OnExecution in the log.

The line of code I've isolated is

Code:
 
Print("Test1");
if (EntryOrder1 != null && EntryOrder1.Token == execution.Order.Token)
{
Print("Test2");
if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))...
I'm checking for a null reference which is usually the reason for this error.

Here is the output just prior to the error. Test1 is right before the line of code above, and Print("test2"); is right after the line of code above, but as you can see Test2 does not print.
Code:
 
5/7/2009 1:14:27 PM Cancelled pending exit order, since associated position is closed: Order='NT-00002/Replay101' ...
 
OnOrderUpdate(): 5/7/2009 1:14:27 PM
 
OnOrderUpdate(): 5/7/2009 1:14:27 PM
 
OnOrderUpdate(): 5/7/2009 1:14:27 PM
 
OnExecution(): 5/7/2009 1:14:27 PM
Test1
Any ideas? Thanks,

kc
kcsystemtrader is offline  
Reply With Quote
Old 05-09-2009, 02:06 PM   #2
NinjaTrader_Ray
Administrator
 
NinjaTrader_Ray's Avatar
 
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,164
Thanks: 6
Thanked 46 times in 32 posts
Default

Can you check if execution.Order is null?
NinjaTrader_Ray is offline  
Reply With Quote
Old 05-09-2009, 04:47 PM   #3
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_Ray View Post
Can you check if execution.Order is null?
I'll try that, but you would think if OnExecution is being called, then by default, execution.Order.Token should have some value assigned to it.
kcsystemtrader is offline  
Reply With Quote
Old 05-11-2009, 07:55 AM   #4
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

The OnExecution may have been triggering off of a different execution. What were the results from your check?
NinjaTrader_Josh is offline  
Reply With Quote
Old 05-11-2009, 01:20 PM   #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_Josh View Post
The OnExecution may have been triggering off of a different execution. What were the results from your check?

I put
Code:
&& execution.Order != null
in all the places in OnExecution and the error still generates.

It is getting hung up on the historical data calcs --- almost like there is a historical database problem from 5/7 or something. Any other ideas?
kcsystemtrader is offline  
Reply With Quote
Old 05-11-2009, 01:25 PM   #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

If you feel there is a database concern, please try resetting your database. Tools->Options->Misc->Reset DB.

Please in OnExecution() just print the execution directly without accessing any properties.
NinjaTrader_Josh is offline  
Reply With Quote
Old 05-11-2009, 02:11 PM   #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
If you feel there is a database concern, please try resetting your database. Tools->Options->Misc->Reset DB.

Please in OnExecution() just print the execution directly without accessing any properties.
It probably is just a database concern because when I load on a different chart (i load this strategy typically on Nasdaq index, and if I change to QQQQ (which is generates very close to the same trade results), it works).

However I just added
Code:
Print("OnExecution triggered by " + execution.ToString());
and here is the output:
HTML Code:
OnExecution(): 5/7/2009 1:30:43 PM
OnExecution triggered by Execution='NT-00003' Instrument='AAPL' Account='Replay101' Name='Exit' Exchange=Default Price=128.603937442173 Quantity=100 Market position=Short Commission=1 Order='NT-00005' Time='5/7/2009 2:13:59 PM'
Looks like it called OnExecution at 1:30 but is referencing an execution at 2:13. Probably a non-issue and I know how to work-around in the future, just thought I'd share. I don't want to reset my db but that would probably work. Is there a way just to reset one instrument? I already tried reloading historical data from the chart but that didn't seem to fix it.
kcsystemtrader is offline  
Reply With Quote
Old 05-11-2009, 02:41 PM   #8
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

Unfortunately not. You could try closing down the market replay connection and open up anew to see if that helps.
NinjaTrader_Josh is offline  
Reply With Quote
Old 05-11-2009, 03:04 PM   #9
kcsystemtrader
Member
 
Join Date: Dec 2008
Location: Kansas City
Posts: 90
Thanks: 0
Thanked 2 times in 2 posts
Default CancelOrder was the culprit

Just figured out what was going on. There was a pause in market data for some reason, or rather a large step change in last traded price as opposed to the normal market movement the strategy normally sees. This caused a profit order to be filled. The profit order execution attempts to cancel a StopOrder --- in this case the StopOrder it expected to be active never got set because the price didn't transition through its trigger point (the step change went well through it and the profit order price). So, I'm now checking all potential stop orders to be != null when the Profit Order gets filled, and this appears to have fixed my issue.

Now, the question is, why is my NT data lagging what I'm seeing in TWS? I noticed this today and was going to post another thread regarding it. Here is what I'm seeing...

In NT's data window on the top left of the control center, I see the bid, bid volume, ask, ask volume, and last volume all updating a few times per second. However, the 'last' price becomes frozen for seconds to sometimes minutes at a time (this is what caused my above issue...nevertheless I needed to fix this in case a true market step change occurs).

I visually confirmed in TWS at the very same time that the last price WAS changing here. It was always at or in between the bid and ask. However, in NT, the bid and ask would start moving up or down together and the last price just stays frozen. Once the last price data starts filtering in again, my OnBarUpdate begins running again and then has to react to what it sees as somewhat of a price shock when in reality there was none.

Do I need to use OnMarketData? Is this just a market data throttling issue? Seems to me if TWS has the updated last trade then NT should be getting the data too. Thanks,

kc
kcsystemtrader is offline  
Reply With Quote
Old 05-11-2009, 03:11 PM   #10
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

If NT receives a last update event then it will display it. If not then it will not. What you may want to consider is checking with no strategies or charts open. Just one instrument in the Control Center readout.
NinjaTrader_Josh 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
Error on running optimizer. Object reference not set to an instance of an object. vasily20011 Strategy Analyzer 5 03-09-2009 08:09 AM
Object reference not set to an instance of an object kcsystemtrader Strategy Development 6 02-09-2009 08:32 AM
Error on calling the 'OnOrderUpdate' method for strategy 'maslope': Object reference scriabinop23 General Programming 4 01-24-2008 02:20 PM
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 11:45 PM.