Topics
26 Aug 2015, 12:26
 2453
 6
Replies

Yaseen19
26 Aug 2015, 13:58

RE:

Spotware said:

Dear Trader,

Please take a look at the following article regarding NullReferenceExceptions: https://msdn.microsoft.com/en-us/library/system.nullreferenceexception(v=vs.110).aspx

This happens if you still try to access the position after it is closed. A simple solution to avoid such errors is to check if the position is not null prior trying to access it.

...

if (pos != null)
	ClosePosition(pos);

Thank you once again for the quick responce. The only problem I have is that im working with a trailing stop loss and im not sure how to access the position once it has closed.

 


@Yaseen19

Yaseen19
26 Aug 2015, 13:02

RE:

Spotware said:

Dear Trader,

Please have a look at the HistoricalTrade section of our API reference and at the following code snippet.

        Position pos;
        HistoricalTrade histrade;

        protected override void OnStart()
        {

            ExecuteMarketOrder(TradeType.Buy, Symbol, 10000, "label");
            pos = Positions.Find("label");
            ClosePosition(pos);

            histrade = History.FindLast("label");
            Print("Closing Price ", histrade.ClosingPrice);

        }

Thank you for the quick response. However i recieve this error "Crashed in OnTick with NullReferenceException: Object reference not set to an instance of an object." Any idea on why this happens? 

 


@Yaseen19