Object reference not set to an instance of an object in backtest
Author: wbzhang
Creation Date: 12/7/2010 12:58 PM
profile picture

wbzhang

#1
I encountered problem "Object reference not set to an instance of an object" while backtesting.

My data set contains all S&P 500 symbols. But not all symbols have this error, only few of them, for example:

QUOTE:
Error processing symbol AON Object reference not set to an instance of an object.
Error processing symbol APOL Object reference not set to an instance of an object.
Error processing symbol BMY Object reference not set to an instance of an object.
Error processing symbol LO Object reference not set to an instance of an object.
Error processing symbol UPS Object reference not set to an instance of an object.
Error processing symbol YHOO Object reference not set to an instance of an object.


I checked the price data, nothing special with them...

Then I checked my code:

QUOTE:
Position curPos = BuyAtMarket( bar+1);
curPos.Priority = 1.0;


If I remove the line
QUOTE:
curPos.Priority = 1.0;

Everything will be OK. Any idea?

Actually the "object reference not set.." error happens from time to time. Sometime it is my code problem, sometimes probably not. For this case, i cannot understand why "curPos.Priority = 1.0" will cause this error...

The interesting thing is, even with this error, it seems the priority is set to the correct value and the code continuously to run until the end. Any comments?
profile picture

Eugene

#2
There's actually nothing strange about this problem, everything is expected. This run-time error happens when WL skips - from time to time - some trades due to lack of capital in portfolio simulation mode. The trade is skipped but you're assigning a priority to the Priority property of a Position that doesn't exist. There's no object to assign anything to, like the Wiki FAQ I quoted earlier today is saying.

For an example of a proper way of assigning a priority, check out Position.Priority in the QuickRef.
profile picture

wbzhang

#3
Thanks. But I looked at "Trades" log, these trades still filled even while returning a null pointer of curPos, not sure why.

But you are right, I should check the position before setting priority..
profile picture

Eugene

#4
Did those few symbols have an Alert?
profile picture

wbzhang

#5
right. The few symbols have Alerts. I guess that's the problem..
This website uses cookies to improve your experience. We'll assume you're ok with that, but you can opt-out if you wish (Read more).