Custom supertrend problem

Created at 05 Jun 2024, 22: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!
ZE

Zed.Mad

Joined 16.05.2024

Custom supertrend problem
05 Jun 2024, 22:50


Hi everyone,

I'm encountering an issue with accessing data from a Supertrend indicator, specifically the "direction" property. For some reason, I keep getting the following error message: "Crashed in Initialize with NullReferenceException: Object reference not set to an instance of an object." What's puzzling is that there are times when I can access the data without any problems, but other times, even without making any changes to the indicator, I encounter this error. Could someone please assist me with this?

Below is the code I'm using:

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

namespace cAlgo
{
    [Cloud("Close Up", "Bull Line", Opacity = 0.2, FirstColor = "FF7CFC00")]
    [Cloud("Bear Line", "Close Down", Opacity = 0.2, FirstColor = "Red")]
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class SuperTrendTest : Indicator
    {
        [Parameter("ATR Period", DefaultValue = 13)]
        public int Period { get; set; }

        [Parameter("ATR Multiplier", DefaultValue = 2)]
        public double atrMulti { get; set; }

        [Parameter("ATR Source", DefaultValue = MovingAverageType.Exponential)]
        public MovingAverageType AtrSource { get; set; }

        [Output("Bear Line", LineColor = "A8FF0000", PlotType = PlotType.DiscontinuousLine, LineStyle = LineStyle.Solid, Thickness = 1)]
        public IndicatorDataSeries HighLine { get; set; }

        [Output("Bull Line", LineColor = "877CFC00", PlotType = PlotType.DiscontinuousLine, LineStyle = LineStyle.Solid, Thickness = 1)]
        public IndicatorDataSeries LowLine { get; set; }

        [Output("Close Up", PlotType = PlotType.DiscontinuousLine, LineColor = "Transparent")]
        public IndicatorDataSeries UpClose { get; set; }

        [Output("Close Down", PlotType = PlotType.DiscontinuousLine, LineColor = "Transparent")]
        public IndicatorDataSeries DownClose { get; set; }

        [Output("Direction")]
        public IndicatorDataSeries Direction { get; set; }

        private AverageTrueRange atr;
        private double AtrDistance;

        protected override void Initialize()
        {
            var result = System.Diagnostics.Debugger.Launch();

            // Debug prints to check initialization
            Print("Initializing indicator...");

            atr = Indicators.AverageTrueRange(Period, AtrSource);
            Print("ATR initialized.");

            Direction = CreateDataSeries();
            Print("Direction data series created.");

            Print("Initialization complete.");
        }

        public override void Calculate(int index)
        {
            AtrDistance = atr.Result[index] * atrMulti;

            if (index == 0)
            {
                Direction[index] = Bars.ClosePrices[index] >= Bars.OpenPrices[index] ? 1 : -1;
            }
            else
            {
                if (Direction[index - 1] == 1)
                {
                    if (Bars.ClosePrices[index] < LowLine[index - 1])
                    {
                        Direction[index] = -1;
                    }
                    else
                    {
                        Direction[index] = 1;
                    }
                }
                else if (Direction[index - 1] == -1)
                {
                    if (Bars.ClosePrices[index] > HighLine[index - 1])
                    {
                        Direction[index] = 1;
                    }
                    else
                    {
                        Direction[index] = -1;
                    }
                }
            }

            if (Direction[index] == 1)
            {
                if (LowLine[index - 1] > Bars.LowPrices[index] - AtrDistance)
                {
                    LowLine[index] = LowLine[index - 1];
                }
                else
                {
                    LowLine[index] = Bars.LowPrices[index] - AtrDistance;
                }
                HighLine[index] = double.NaN;
                UpClose[index] = Bars.ClosePrices[index];
                DownClose[index] = double.NaN;
            }
            else if (Direction[index] == -1)
            {
                if (HighLine[index - 1] < Bars.HighPrices[index] + AtrDistance)
                {
                    HighLine[index] = HighLine[index - 1];
                }
                else
                {
                    HighLine[index] = Bars.HighPrices[index] + AtrDistance;
                }
                LowLine[index] = double.NaN;
                DownClose[index] = Bars.ClosePrices[index];
                UpClose[index] = double.NaN;
            }

            // Add debug output
            Print($"Index: {index}, ClosePrice: {Bars.ClosePrices[index]}, Direction: {Direction[index]}, LowLine: {LowLine[index]}, HighLine: {HighLine[index]}, AtrDistance: {AtrDistance}");
        }
    }
}

I've also created a simple cBot just to print the property, but it returns NaN. Here's the cBot code:

 

using cAlgo.API;
using cAlgo.API.Indicators;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None, AddIndicators =true)]
    public class MyBot : Robot
    {
        private SuperTrendTest superTrend;
        protected override void OnStart()
        {
            var result = System.Diagnostics.Debugger.Launch();

            if (result is false)
            {
                Print("Debugger launch failed");
            }
            superTrend = Indicators.GetIndicator<SuperTrendTest>(13, 2, MovingAverageType.Exponential);
       
        }

        protected override void OnTick()
        {
            Print(superTrend.Direction.LastValue);
            
            
        }
    }
}

Any help or guidance on resolving this issue would be greatly appreciated.

Thank you in advance!


@Zed.Mad
Replies

PanagiotisCharalampous
06 Jun 2024, 10:49

Hi there,

I tried the indicator but it runs fine for me. Any specific steps to reproduce the problem?

Best regards,

Panagiotis


@PanagiotisCharalampous

Zed.Mad
06 Jun 2024, 17:08

RE: Custom supertrend problem

PanagiotisCharalampous said: 

Hi there,

I tried the indicator but it runs fine for me. Any specific steps to reproduce the problem?

Best regards,

Panagiotis

Hey, thanks for the reply. It started working after close and open ctrader… Thanks anyway for the help.


@Zed.Mad