Runtime error: Object reference not set to an instance of an object
Author: lookingbackon
Creation Date: 4/5/2011 10:18 AM
profile picture

lookingbackon

#1
Hello,
I just fine-tuned a strategy and the strategy can properly run basing on the existing symbol. But when I click other symbols for test, it can't work with the error remarks as below:

QUOTE:
Runtime error: Object reference not set to an instance of an object
At WealthLab.Strategies.BollingerBuyer.Execute()


What could be the problem?
Thanks
profile picture

Cone

#2
Much too hard to guess without see the code.
profile picture

lookingbackon

#3
So I need to put the strategy code here or just apply for a support ticket to send the code to you?
Thanks
profile picture

Eugene

#4
BollingerBuyer, a pre-built strategy that came with WL, doesn't seem to be super secret code. It's best to start from Errors | Strategy > "Object reference not set to an instance of an object". Most likely cause is described in paragraph #2:
QUOTE:
If "Benchmark Buy & Hold" symbol is defined and enabled in the Wealth-Lab's Preferences dialog, Backtest Settings, you need to make sure that the symbol name is valid and could be found in your DataSets. Entering a symbol that does not exist is a popular reason for getting this error message in backtests.
profile picture

lookingbackon

#5
It's not the cause of 2nd paragragh.

The draft code is as below. Would appreciate if you can help to address the problem.
Thanks
CODE:
Please log in to see this code.
profile picture

Eugene

#6
Easy one:
CODE:
Please log in to see this code.


That happens when if (!IsLastPositionActive) i.e. you have no open positions, so you're checking the entry price for a Position that does not exist yet.
profile picture

lookingbackon

#7
Thanks.
Yes, it's the cause.
So,is it possible to conduct the comparison for:
the "close(bar)" and the "entry price"(or the close price of the BAR at which last position buy/short) for the last postion which was sold/covered already?
profile picture

Eugene

#8
Yes, by including an extra check for Positions.Count > 0 and at the same time making sure that the Position's Active property returns false (e.g. if(!LastPosition.Active) ).
profile picture

lookingbackon

#9
It works now.Thanks a lot.
profile picture

tomphm

#10
Hello,

I got the following error message when running the code below. The compilation is okay. My understanding is that the error is due to using the 'buy/sell' statements within a function beside that of 'Execute ()' function. Please let me know what can be done to resolve this. Thanks for any help.




CODE:
Please log in to see this code.
profile picture

Eugene

#11
Strategies aren't meant to be self-referenced or become recursive. If you have a good reason for doing what you're trying to do, feel free to ask in a new thread - because being the most unusual cause of typical, FAQ-like "Object reference not set to an instance of an object" error I've ever seen, it doesn't really belong here. Otherwise the solution is to not break what's working.
profile picture

tomphm

#12
Thanks Eugene, I am not sure what you mean by 'self-reference' or 'recursive' in this case. The reason I use this approach is to make the program more modular and its flow more easier to understand. The function that open & close positions is part of a larger program and will be call many times depends on a number of conditions. Having it resides on a function beside the Main or Execute () seems to be cleaner and makes more sense. Thanks.
profile picture

Eugene

#13
Thanks for the clarification Tom. In fact, your program is trying to be exactly what you're not sure about -- i.e. be recursive or reference itself. The concept of recursion is outside C# 101 but still you might want to google it. You're invited to discuss the modular approach to building WealthScript programs in a new (or more appropriate) forum thread. This is a topic of its own.

profile picture

WEALTHPRO25

#14
i have the following code and it compiles but i get runtime error: object reference not set to an instance of an object, please help

CODE:
Please log in to see this code.
profile picture

Eugene

#15
The DataSet parameter of GetExternalSymbol is context sensitive:
CODE:
Please log in to see this code.


Or simply don't use it (just like you do for "Bars b1"):
CODE:
Please log in to see this code.
profile picture

WEALTHPRO25

#16
i have been using this code with no issue and suddenly i have runtime error using symbol ^GSPC with yahoo provider, please help

CODE:
Please log in to see this code.


the run time error is index is out of range must be non negative and less than size of collection,
also daily, weekly, quaterly and yearly chart work but monthly chart gives an error
profile picture

Eugene

#17
For possible reasons see Errors > Index was out of range. Most likely you have a broken chart drawing object on Monthly ^GSPC as the code runs for me just fine.
profile picture

WEALTHPRO25

#18
yes i was playing with the chart drawing objects, how do i fix it
profile picture

WEALTHPRO25

#19
i fixed it, i erased the objects and now it works thanks
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).