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

Created at 30 Sep 2020, 10:58
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!
xabbu's avatar

xabbu

Joined 20.07.2020

while referencing an indicator: Crashed in OnTick with NullReferenceException: Object reference not set to an instance of an object.
30 Sep 2020, 10:58


Dear all,

 

I got an behavior I can't explain:

I'm referencing an indicator. Referenced indicator works without compiling or runtime error. If I run the referecing indicator, the correct values from the referenced indicator are displayed (good!) BUT the log throughs out "Crashed in ... with NullReferenceException: Object reference not set to an instance of an object." on every tick (?).

I have no idea why this happens. If I reference another indicator, everything works fine without any error messages. Both indicators are referenced in the "manage Reference" section. Both indicators work fine without any errors for itself.

Any idea where to look or what might going wrong?

 

Kindest regards,

 

here the referencing (receiving) indicator:

 

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

namespace cAlgo
{

    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AutoRescale = false, AccessRights = AccessRights.None)]
    public class SampleReferenceincludecustomIndicatorVolume : Indicator
    {
        [Parameter("Source")]
        public DataSeries DataSource { get; set; }

        [Output("Referenced DOSC Output", LineColor = "Red", PlotType = PlotType.Points)]
        public IndicatorDataSeries refDOSC { get; set; }

        [Output("ZeroLine", LineColor = "White")]
        public IndicatorDataSeries zeroline { get; set; }



        public DOSC _isTradeAllowed;
        public EXITRVINNFX _isTradeAllowed1;


        protected override void Initialize()
        {

            //_isTradeAllowed = Indicators.GetIndicator<DOSC>(DataSource, 7, 1);
            _isTradeAllowed1 = Indicators.GetIndicator<EXITRVINNFX>(DataSource, 10);
        }

        public override void Calculate(int index)
        {
            //refDOSC[index] = _isTradeAllowed.Result[index];
            refDOSC[index] = _isTradeAllowed1._rviBool[index];
            zeroline[index] = 0;


        }
    }
}

 

and here the referenced (sending) indicator:

 

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

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = false, ScalePrecision = 5, AccessRights = AccessRights.None)]
    public class EXITRVINNFX : Indicator
    {
        [Parameter("Source")]
        public DataSeries Source { get; set; }

        [Parameter(DefaultValue = 10)]
        public int Period { get; set; }

        [Output("RVI", LineColor = "Green")]
        public IndicatorDataSeries _rviResult { get; set; }

        [Output("Signal", LineColor = "Red")]
        public IndicatorDataSeries _rviSignal { get; set; }

        [Output("Bool", LineColor = "Red", PlotType = PlotType.Points)]
        public IndicatorDataSeries _rviBool { get; set; }


        private IndicatorDataSeries _value1;
        private IndicatorDataSeries _value2;
        private string _text;
        private Color _color;



        protected override void Initialize()
        {
            _value1 = CreateDataSeries();
            _value2 = CreateDataSeries();
            _text = "";
            _color = Color.Red;
        }


        public override void Calculate(int index)
        {
            _value1[index] = ((Bars.ClosePrices[index] - Bars.OpenPrices[index]) + 2 * (Bars.ClosePrices[index - 1] - Bars.OpenPrices[index - 1]) + 2 * (Bars.ClosePrices[index - 2] - Bars.OpenPrices[index - 2]) + (Bars.ClosePrices[index - 3] - Bars.OpenPrices[index - 3])) / 6;

            _value2[index] = ((Bars.HighPrices[index] - Bars.LowPrices[index]) + 2 * (Bars.HighPrices[index - 1] - Bars.LowPrices[index - 1]) + 2 * (Bars.HighPrices[index - 2] - Bars.LowPrices[index - 2]) + (Bars.HighPrices[index - 3] - Bars.LowPrices[index - 3])) / 6;

            double num = 0;
            double denum = 0;
            for (int i = 0; i < Period; i++)
            {
                num += _value1[index - i];
                denum += _value2[index - i];
            }

            _rviResult[index] = num / denum;

            _rviSignal[index] = (_rviResult[index] + 2 * _rviResult[index - 1] + 2 * _rviResult[index - 2] + _rviResult[index - 3]) / 6;




            if (_rviSignal.HasCrossedAbove(_rviResult, 0))
            {

                _text = "EXIT long" + _rviBool.LastValue;
                _color = Color.Red;
                _rviBool[index] = 1;

            }
            else
            {

                if (_rviResult.HasCrossedAbove(_rviSignal, 0))
                {
                    _text = "EXIT short" + _rviBool.LastValue;
                    _color = Color.Red;
                    _rviBool[index] = -1;
                }
                else
                {
                    _text = "";
                    _rviBool[index] = 0;
                }
            }
            IndicatorArea.DrawStaticText("Symbols", _text, VerticalAlignment.Top, HorizontalAlignment.Right, _color);

        }

    }
}

 


@xabbu
Replies

PanagiotisCharalampous
30 Sep 2020, 11:11

Hi xabbu,

You problem is here

IndicatorArea.DrawStaticText("Symbols", _text, VerticalAlignment.Top, HorizontalAlignment.Right, _color);

When the indicator is referenced, the indicator area is not initialized. You can fix it by adding this condition

            if (IndicatorArea != null)
                IndicatorArea.DrawStaticText("Symbols", _text, VerticalAlignment.Top, HorizontalAlignment.Right, _color);

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous

xabbu
30 Sep 2020, 11:26

Dear Panagiotis,

thank you very much for your fast support. I added the line of code and it works!!!

Without your knowledge I would have NEVER found this solution.

Kindest regards to Cyprus and you & your team


@xabbu