NullReferenceException

Created at 05 Apr 2014, 00:08
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!
AN

andrei_c

Joined 04.04.2014

NullReferenceException
05 Apr 2014, 00:08


Hello,

i am trying to make some updates to Sample Trend Bot that comes with cAlgo,

i have tried to get last GrossProfit and if its value is negative then instead of a Sell it will open a Buy Order,

defining variables as

var longGrossProfit = longPosition.GrossProfit;
var shortGrossProfit = shortPosition.GrossProfit;

It builds well, however at the backtest stage it returns this Exception

Crashed in OnTick with NullReferenceException: Object reference not set to an instance of an object.

i am attaching the code here as well

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

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class SampleTrendCBot : Robot
    {
        [Parameter("MA Type")]
        public MovingAverageType MAType { get; set; }

        [Parameter()]
        public DataSeries SourceSeries { get; set; }

        [Parameter("Slow Periods", DefaultValue = 10)]
        public int SlowPeriods { get; set; }

        [Parameter("Fast Periods", DefaultValue = 5)]
        public int FastPeriods { get; set; }

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

        [Parameter(DefaultValue = 150)]
        public int StopLoss { get; set; }

        [Parameter(DefaultValue = 300)]
        public int TakeProfit { get; set; }

        private MovingAverage slowMa;
        private MovingAverage fastMa;
        private const string label = "Sample Trend cBot";

        protected override void OnStart()
        {
            fastMa = Indicators.MovingAverage(SourceSeries, FastPeriods, MAType);
            slowMa = Indicators.MovingAverage(SourceSeries, SlowPeriods, MAType);
        }

        protected override void OnTick()
        {
            var longPosition = Positions.Find(label, Symbol, TradeType.Buy);
            var shortPosition = Positions.Find(label, Symbol, TradeType.Sell);

            var currentSlowMa = slowMa.Result.Last(0);
            var currentFastMa = fastMa.Result.Last(0);
            var previousSlowMa = slowMa.Result.Last(1);
            var previousFastMa = fastMa.Result.Last(1);

            var longProfit = longPosition.GrossProfit;
            var shortProfit = shortPosition.GrossProfit;

            if (previousSlowMa > previousFastMa && currentSlowMa <= currentFastMa && longPosition == null)
            {
                if (shortPosition != null)
                    ClosePosition(shortPosition);
                if (shortProfit > 0)
                {
                    ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, label, StopLoss, TakeProfit);
                }
                else
                {
                    ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, label, StopLoss, TakeProfit);
                }
            }
            else if (previousSlowMa < previousFastMa && currentSlowMa >= currentFastMa && shortPosition == null)
            {
                if (longPosition != null)
                    ClosePosition(longPosition);
                if (longProfit > 0)
                {
                    ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, label, StopLoss, TakeProfit);
                }
                else
                {
                    ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, label, StopLoss, TakeProfit);
                }
            }
        }

    }
}

Could anyone please help me to find what i am missing,

Thanks


@andrei_c
Replies

cAlgoFx
05 Apr 2014, 09:32

Use these linq statements for lines 54 && 55;

var longProfit = Positions.Where(pos => pos.Label == label && pos.SymbolCode == Symbol.Code && pos.TradeType == TradeType.Buy).Sum(pos => pos.GrossProfit);
var shortProfit = Positions.Where(pos => pos.Label == label && pos.SymbolCode == Symbol.Code && pos.TradeType == TradeType.Sell).Sum(pos => pos.GrossProfit);

Should get you sorted.

 

Happy hunting


@cAlgoFx

cAlgoFx
05 Apr 2014, 09:42

RE:

cAlgoFx said:

Use these linq statements for lines 54 && 55;

var longProfit = Positions.Where(pos => pos.Label == label && pos.SymbolCode == Symbol.Code && pos.TradeType == TradeType.Buy).Sum(pos => pos.GrossProfit);
var shortProfit = Positions.Where(pos => pos.Label == label && pos.SymbolCode == Symbol.Code && pos.TradeType == TradeType.Sell).Sum(pos => pos.GrossProfit);

Should get you sorted.

 

Happy hunting

Don't forget pos.Label==LABEL.


@cAlgoFx

andrei_c
05 Apr 2014, 23:21

Thanks a Lot,

that worked perfectly,

seems i have a lot to learn still :)


@andrei_c