cBot working not correctly

Created at 03 Sep 2014, 16:44
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!
OB

obial

Joined 03.09.2014

cBot working not correctly
03 Sep 2014, 16:44


I have the following code onTick and it throws some errors, but it works fine when there is an opened order with SL and TP set:

Crashed in OnTick with NullReferenceException: Object reference not set to an instance of an object.
Crashed in OnTick with InvalidOperationException: Nullable object must have a value.

 

           var PendingOrdersStrategy1 = 0;

            foreach (var order in PendingOrders)
            {
                if (order.Label == Strategy1)
                {
                    PendingOrdersStrategy1 = PendingOrdersStrategy1 + 1;
                }
            }

            var position = Positions.Find("", Symbol);

            if (position != null && position.StopLoss != 0 && position.TakeProfit != 0 && position.Comment == Strategy1)
            {

                var TargetVolume = Symbol.NormalizeVolume(position.Volume * Multiplier, RoundingMode.Up);
                var TargetSLSS = position.EntryPrice;

                var expirationTime = Server.Time.AddSeconds(SecondsTimeout);

                sellOrderTargetPrice = Math.Round(position.StopLoss.Value - PipsAway * Symbol.PipSize, Symbol.Digits);
                buyOrderTargetPrice = Math.Round(position.StopLoss.Value + PipsAway * Symbol.PipSize, Symbol.Digits);

                if (position.TradeType == TradeType.Buy && PendingOrdersStrategy1 <= 1)
                {
                    PlaceStopOrder(TradeType.Sell, Symbol, TargetVolume, sellOrderTargetPrice, Strategy1, TargetSLSS, position.EntryPrice, expirationTime, Strategy1);
                }

                if (position.TradeType == TradeType.Sell && PendingOrdersStrategy1 <= 1)
                {
                    PlaceStopOrder(TradeType.Buy, Symbol, TargetVolume, buyOrderTargetPrice, Strategy1, TargetSLSS, position.EntryPrice, expirationTime, Strategy1);
                }

            }

            if (History.FindLast(Strategy1, Symbol).NetProfit > 0)
            {
                foreach (var order in PendingOrders)
                {
                    if (order.Label == Strategy1 && order.Comment == Strategy1)
                    {
                        CancelPendingOrder(order);
                    }
                }


@obial
Replies

Spotware
03 Sep 2014, 16:48

History.FindLast method could return null if there is no such historical trades. You need to check it for null.


@Spotware

obial
03 Sep 2014, 16:54

I tried it by removing the below part and it is still the same errors:

            if (History.FindLast(Strategy1, Symbol).NetProfit > 0)
            {
                foreach (var order in PendingOrders)
                {
                    if (order.Label == Strategy1 && order.Comment == Strategy1)
                    {
                        CancelPendingOrder(order);
                    }
                }

 

looks like there needs to be smth defined for the below, because it works fine when there is an opened order with set SL and TP, but I cannot find anything into the documentation or smth to debug !

             var TargetVolume = Symbol.NormalizeVolume(position.Volume * Multiplier, RoundingMode.Up);
                var TargetSLSS = position.EntryPrice;

                var expirationTime = Server.Time.AddSeconds(SecondsTimeout);

                sellOrderTargetPrice = Math.Round(position.StopLoss.Value - PipsAway * Symbol.PipSize, Symbol.Digits);
                buyOrderTargetPrice = Math.Round(position.StopLoss.Value + PipsAway * Symbol.PipSize, Symbol.Digits);


@obial

Spotware
03 Sep 2014, 17:07

  if (position != null && position.StopLoss != 0 && position.TakeProfit != 0 && position.Comment == Strategy1)

You need to check SL and TP for null instead of comparing it with 0.

You can also use Visual Studio to debug your algorithms: http://help.spotware.com/calgo/visual-studio/debug-cbots


@Spotware

obial
03 Sep 2014, 17:41

That worked fine, now the cBot isn't crashing any more.

 

Thnx


@obial