Object reference not set to an instance of an object
Author: Christos
Creation Date: 6/22/2012 6:57 AM
profile picture

Christos

#1
Please se below the piece of code which compiles succesfully but gives the error in the Topic name

CODE:
Please log in to see this code.


Apparently the problem is in the "LastPosition.NetProfit".

Strangely enough (for my understanding) if I change the BuyAtLimit order with BuyAtMarket(bar) the code runs with no problems...

Since my intention is to be able to extract the strategy returns would it be posible to help me overcome that problem and give me some insight with respect to its causes?
profile picture

Eugene

#2
Tenth "Object reference not set to an instance of an object" forum thread :)

The problem is both in trying to exit a Position that may not exist yet (i.e. BuyAtLimit didn't trigger):
CODE:
Please log in to see this code.

AND
in accessing a property of the Position that has already been exited:
CODE:
Please log in to see this code.

This is why there is a default single-position strategy template that protects from most coding errors of this kind.
profile picture

Eugene

#3
A Strategy isn't the proper to place to extract the Strategy returns like this. Please re-read the Remarks section of the QuickRef entry for Position Object > NetProfitPercent.
profile picture

Christos

#4
Eugene,

First of all many thanks for the instant reply.

As per your last post I am fully aware of the "issues" with extracting strategy returns from a Strategy (but thanks anyway for the "warning")

I think that the first comment in your first reply helped me solve the problem. I used the following code

CODE:
Please log in to see this code.


Don't you think this is a non - problematic solution?

As per your second comment I really don't see that creating any problem most probably since this is not the last active position but the last position.
profile picture

Eugene

#5
QUOTE:
As per your second comment I really don't see that creating any problem most probably since this is not the last active position but the last position.

It's not about Position order. The thing is, you were accessing an .NET object that could have already been disposed, causing unhandled exception. In your code that followed, this is not an issue.
profile picture

Christos

#6
I see your point.
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).