Crashed with null reference exeception??

Created at 20 Oct 2014, 00:15
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!
97

9718853

Joined 14.10.2014

Crashed with null reference exeception??
20 Oct 2014, 00:15




using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class HedgeBot : Robot
    {


        [Parameter("Initial Volume", DefaultValue = 10000, MinValue = 0)]
        public int InitialVolume { get; set; }

        [Parameter("Take Profit", DefaultValue = 30)]
        public int TakeProfit { get; set; }

        [Parameter("Stop Loss", DefaultValue = 20)]
        public int StopLoss { get; set; }

        private const string label = "HedgeBot";

        protected override void OnStart()
        {

            ExecuteMarketOrder(TradeType.Buy, Symbol, InitialVolume, label, StopLoss, TakeProfit);
            ExecuteMarketOrder(TradeType.Sell, Symbol, InitialVolume, label, StopLoss, TakeProfit);

        }
        protected override void OnTick()
        {
            var longPosition = Positions.Find(label, Symbol, TradeType.Buy);
            var shortPosition = Positions.Find(label, Symbol, TradeType.Sell);
            {
                if (longPosition.GrossProfit > 10)
                {
                    ExecuteMarketOrder(TradeType.Buy, Symbol, InitialVolume * 2, label, StopLoss, TakeProfit);
                }
                else if (shortPosition.GrossProfit > 10)
                {
                    ExecuteMarketOrder(TradeType.Sell, Symbol, InitialVolume * 2, label, StopLoss, TakeProfit);
                }
                {
                    return;
                }
            }
        }
    }
}

Can anyone please point out the error here?

The full error message is: Crashed in OnTick with NullReferenceException: Object reference not set to an instance of an object.

 


@9718853
Replies

Invalid
20 Oct 2014, 09:58

RE:

Most probably longPosition or shortPosition is null. Make sure both of them were opened successfully.

 

 

Ian_Drummond said:



using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class HedgeBot : Robot
    {


        [Parameter("Initial Volume", DefaultValue = 10000, MinValue = 0)]
        public int InitialVolume { get; set; }

        [Parameter("Take Profit", DefaultValue = 30)]
        public int TakeProfit { get; set; }

        [Parameter("Stop Loss", DefaultValue = 20)]
        public int StopLoss { get; set; }

        private const string label = "HedgeBot";

        protected override void OnStart()
        {

            ExecuteMarketOrder(TradeType.Buy, Symbol, InitialVolume, label, StopLoss, TakeProfit);
            ExecuteMarketOrder(TradeType.Sell, Symbol, InitialVolume, label, StopLoss, TakeProfit);

        }
        protected override void OnTick()
        {
            var longPosition = Positions.Find(label, Symbol, TradeType.Buy);
            var shortPosition = Positions.Find(label, Symbol, TradeType.Sell);
            {
                if (longPosition.GrossProfit > 10)
                {
                    ExecuteMarketOrder(TradeType.Buy, Symbol, InitialVolume * 2, label, StopLoss, TakeProfit);
                }
                else if (shortPosition.GrossProfit > 10)
                {
                    ExecuteMarketOrder(TradeType.Sell, Symbol, InitialVolume * 2, label, StopLoss, TakeProfit);
                }
                {
                    return;
                }
            }
        }
    }
}

Can anyone please point out the error here?

The full error message is: Crashed in OnTick with NullReferenceException: Object reference not set to an instance of an object.

 

 


@Invalid