Crashed in OnTick with NullReferenceException

Created at 20 Apr 2014, 22:03
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
OL

Old Account

Joined 14.10.2013

Crashed in OnTick with NullReferenceException
20 Apr 2014, 22:03


Hi

Can someone tell me what's wrong whit this code?

 

   protected override void OnStart()
        {
            ExecuteMarketOrder(TradeType.Buy, Symbol, 1000, "Test");
        }

        protected override void OnTick()
        {
            var position = Positions.Find("Test");

            ClosePosition(position);

        }

 

Thanks


@Old Account
Replies

Researcher
22 Apr 2014, 00:18

If there is no such position on tick the Find() method returns null. Try this code:

     protected override void OnTick()
     {
         var position = Positions.Find("Test"); 
         if (position != null)
             ClosePosition(position);
     }

 


@Researcher

Old Account
22 Apr 2014, 23:08

RE:

Researcher said:

If there is no such position on tick the Find() method returns null. Try this code:

     protected override void OnTick()
     {
         var position = Positions.Find("Test"); 
         if (position != null)
             ClosePosition(position);
     }

 

Thank you !!!


@Old Account