NullReferenceException

Created at 19 Sep 2020, 07:50
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!
RI

ricardo.ballinas

Joined 19.09.2020

NullReferenceException
19 Sep 2020, 07:50


hi! can you please help me? I'm new to this of the Calgo and I can't find my mistake... here is the code...

I know that there is something in the protected override void OnTick(), because when I move the code to the protected override void OnBar() the error disapears...

Thanks in forehand

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 ATR : Robot
    {
        [Parameter("MA Method", DefaultValue = MovingAverageType.Simple)]
        public MovingAverageType MaType { get; set; }

        [Parameter("Period", DefaultValue = 15, MinValue = 2, MaxValue = 50)]
        public int Period { get; set; }

        [Parameter("Weight", DefaultValue = 3.0, MinValue = 0.1, MaxValue = 4.0)]
        public double Weight { get; set; }

        [Parameter("True:High_Low False:Close", DefaultValue = true)]
        public bool UseHighAndLow { get; set; }

        [Parameter("Trending MA", DefaultValue = 96)]
        public int TrendMA { get; set; }

        [Parameter("Signal MA", DefaultValue = 4)]
        public int SignalMA { get; set; }

        [Parameter("Source: ")]
        public DataSeries SourceMA { get; set; }

        [Parameter("Volume: ", DefaultValue = 1000)]
        public double Volume { get; set; }

        [Parameter("Secure Take Profit on Trail: ", DefaultValue = 5)]
        public double takeProfit { get; set; }

        [Parameter("Secure Take Profit on Pushback: ", DefaultValue = 5)]
        public double takeProfit2 { get; set; }

        [Parameter("The Ballinas Cerdio Number", DefaultValue = 0.003)]
        public double ballinascerdio { get; set; }

        [Parameter("Include Trailing Stop", DefaultValue = false)]
        public bool IncludeTrailingStop { get; set; }

        [Parameter("Trailing Stop Trigger (pips)", DefaultValue = 30)]
        public int TrailingStopTrigger { get; set; }

        [Parameter("Trailing Stop Step (pips)", DefaultValue = 0.5)]
        public double TrailingStopStep { get; set; }

        [Output("Main")]
        public IndicatorDataSeries Result { get; set; }
        private ATRStops ATRST;
        private MovingAverage TMA;
        private MovingAverage SMA;
        double PriceSlope;
        double PriceSlope2;
        double PriceSlope3;
        double PriceSlope4;



        protected override void OnBar()
        {
            ATRST = Indicators.GetIndicator<ATRStops>(MaType, Period, Weight, UseHighAndLow);
            TMA = Indicators.MovingAverage(SourceMA, TrendMA, MovingAverageType.Simple);
            SMA = Indicators.MovingAverage(SourceMA, SignalMA, MovingAverageType.Simple);

            var symbolPositionsBuy = Positions.FindAll("Trend Buy", SymbolName);
            var symbolPositionsSell = Positions.FindAll("Trend Sell", SymbolName);
            Print(Positions.FindAll("Trend Buy", SymbolName));
            Print("# of buying positions " + symbolPositionsBuy.Length);
            Print("# of selling positions " + symbolPositionsSell.Length);

            PriceSlope = SMA.Result.Last(1) - SMA.Result.Last(2);
            PriceSlope2 = SMA.Result.Last(1) - SMA.Result.Last(3);
            PriceSlope3 = SMA.Result.Last(1) - SMA.Result.Last(4);
            PriceSlope4 = SMA.Result.Last(1) - SMA.Result.Last(5);
            Print("The price Slope is :" + PriceSlope);




            if (Bars.ClosePrices.Last(1) > ATRST.Result.Last(1) && TMA.Result.Last(1) < Bars.ClosePrices.Last(1) && symbolPositionsBuy.Length == 0 && PriceSlope > 0.004)
            {
                Print("Sending buy order with the trend");
                ExecuteMarketOrder(TradeType.Buy, SymbolName, Volume, "Trend Buy", null, null, "Trending buy op");
                //remember to correct the Take Profit
            }


            if (Bars.ClosePrices.Last(1) < ATRST.Result.Last(1) && TMA.Result.Last(1) > Bars.ClosePrices.Last(1) && symbolPositionsSell.Length == 0 && PriceSlope < -0.004)
            {
                Print("Sending sell order with the trend");
                ExecuteMarketOrder(TradeType.Sell, SymbolName, Volume, "Trend Sell", null, null, "Trending op sell");
            }



        }

        protected override void OnTick()
        {
            PriceSlope = SMA.Result.Last(1) - SMA.Result.Last(2);
            PriceSlope2 = SMA.Result.Last(1) - SMA.Result.Last(3);
            PriceSlope3 = SMA.Result.Last(1) - SMA.Result.Last(4);
            PriceSlope4 = SMA.Result.Last(1) - SMA.Result.Last(5);
            Print("The price Slope is :" + PriceSlope);

            foreach (Position position in Positions)
            {
                if (position.Label.Equals("Trend Buy"))
                {

                    if (PriceSlope <= -0.003 || PriceSlope2 <= -0.003 || PriceSlope3 <= -0.003 || PriceSlope4 <= -0.003 || LastResult.Position.GrossProfit <= -10)
                    {
                        ClosePosition(position);
                    }
                }
                else if (position.Label.Equals("Trend Sell"))
                {

                    if (PriceSlope >= 0.003 || PriceSlope2 >= 0.003 || PriceSlope3 >= 0.003 || PriceSlope3 >= 0.003 || LastResult.Position.GrossProfit <= -10)
                    {
                        ClosePosition(position);
                    }
                }

                if (IncludeTrailingStop)
                {
                    SetTrailingStop();
                }

            }

        }

        private void SetTrailingStop()
        {
            var sellPositions = Positions.FindAll("Trend Sell", SymbolName, TradeType.Sell);

            foreach (Position position in sellPositions)
            {
                double distance = position.EntryPrice - Symbol.Bid;

                if (distance < TrailingStopTrigger * Symbol.PipSize)
                    continue;

                double newStopLossPrice = Symbol.Ask + TrailingStopStep * Symbol.PipSize;

                if (position.StopLoss == null || newStopLossPrice < position.StopLoss)
                {
                    ModifyPosition(position, newStopLossPrice, position.TakeProfit);
                }
            }

            var buyPositions = Positions.FindAll("Trend Buy", SymbolName, TradeType.Buy);

            foreach (Position position in buyPositions)
            {
                double distance = Symbol.Ask - position.EntryPrice;

                if (distance < TrailingStopTrigger * Symbol.PipSize)
                    continue;

                double newStopLossPrice = Symbol.Bid - TrailingStopStep * Symbol.PipSize;
                if (position.StopLoss == null || newStopLossPrice > position.StopLoss)
                {
                    ModifyPosition(position, newStopLossPrice, position.TakeProfit);
                }
            }
        }

    }

}


@ricardo.ballinas
Replies

firemyst
19 Sep 2020, 08:19

An issue you have is with the statements:

LastResult.Position.GrossProfit

 

Let's say you have 3 positions #1, #2, #3.

the LastResult is Position #3.

However, your system could then close position #3.

so the next OnTick it checks the LastResult.Position, it no longer exists (since it was closed), thus you have a null reference.

 


@firemyst

ricardo.ballinas
19 Sep 2020, 08:50

RE:

firemyst said:

An issue you have is with the statements:

LastResult.Position.GrossProfit

 

Let's say you have 3 positions #1, #2, #3.

the LastResult is Position #3.

However, your system could then close position #3.

so the next OnTick it checks the LastResult.Position, it no longer exists (since it was closed), thus you have a null reference.

 

Thanks a lot bro!!! it worked!!!


@ricardo.ballinas