Reference custom indicator to my Cbot result NaN

Created at 23 Jan 2015, 19:00
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!
AM

amirkhan

Joined 23.01.2015

Reference custom indicator to my Cbot result NaN
23 Jan 2015, 19:00


hello my custom indicator is this :

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

namespace cAlgo
{
    [Indicator()]
    public class HKA : Indicator
    {
        public IndicatorDataSeries _haOpen;
        public IndicatorDataSeries _haClose;

        public IndicatorDataSeries resultFA;

        public int CandleWidth = 5;


        public string UpColor = "Blue";

        public string DownColor = "Red";

        public Colors _upColor;
        public Colors _downColor;
        private bool _incorrectColors;
        private Random _random = new Random();
        private IndicatorDataSeries resta;
        protected override void Initialize()
        {
            _haOpen = CreateDataSeries();
            _haClose = CreateDataSeries();
            resta = CreateDataSeries();
            resultFA = CreateDataSeries();
            if (!Enum.TryParse<Colors>(UpColor, out _upColor) || !Enum.TryParse<Colors>(DownColor, out _downColor))
                _incorrectColors = true;
        }

        public override void Calculate(int index)
        {
            if (_incorrectColors)
            {
                var errorColor = _random.Next(2) == 0 ? Colors.Red : Colors.White;
                ChartObjects.DrawText("Error", "Incorrect colors", StaticPosition.Center, errorColor);
                return;
            }

            var open = MarketSeries.Open[index];
            var high = MarketSeries.High[index];
            var low = MarketSeries.Low[index];
            var close = MarketSeries.Close[index];

            var haClose = (open + high + low + close) / 4;
            double haOpen;
            if (index > 0)
                haOpen = (_haOpen[index - 1] + _haClose[index - 1]) / 2;
            else
                haOpen = (open + close) / 2;

            var haHigh = Math.Max(Math.Max(high, haOpen), haClose);
            var haLow = Math.Min(Math.Min(low, haOpen), haClose);

            var color = haOpen > haClose ? _downColor : _upColor;


            if (haOpen > haClose)
            {
                resta[index] = 2.01;
            }
            else
            {
                resta[index] = 1.01;
            }
            resultFA[index] = resta[index];
            ChartObjects.DrawLine("candle" + index, index, haOpen, index, haClose, color, CandleWidth, LineStyle.Solid);
            ChartObjects.DrawLine("line" + index, index, haHigh, index, haLow, color, 1, LineStyle.Solid);
            _haOpen[index] = haOpen;
            _haClose[index] = haClose;

        }
    }
}

and when I add this lines to my Cbot only  see NaN( I referenced this indicator by ctdn guide  section :

  protected override void OnTick()
        {


            Print(_HKA.resultFA.Last(1));

        }

after click to + Manage References and add my custom indicator .... using this lines to code for reference  :

 public class mycbot : Robot
    {
  private HKA _HKA;
  protected override void OnStart()
        {
       
            _HKA = Indicators.GetIndicator<HKA>();
         
        }

 


@amirkhan
Replies

Spotware
10 Feb 2015, 09:27

You need to mark resultFA with OutputAttribute.


@Spotware

AndreaDev
23 Feb 2019, 14:11

RE:

Spotware said:

You need to mark resultFA with OutputAttribute.

I do have the same issue but OutputAttribute didn't help this is my cutomIndicator :

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

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class CustomIndicator : Indicator
    {

        //--- Public
        [Parameter(DefaultValue = 1, MinValue = 1)]
        public int Periods { get; set; }
        [Parameter("MA Type", DefaultValue = MovingAverageType.Simple)]
        public MovingAverageType MAType { get; set; }
        [Output("NewOpen ", Color = Colors.Black)]
        public IndicatorDataSeries NewOpen { get; set; }
        //--- Private
        private MovingAverage maOpen;


        protected override void Initialize()
        {
            NewOpen = CreateDataSeries();
            maOpen = Indicators.MovingAverage(MarketSeries.Open, Periods, MAType);
        }

        public override void Calculate(int index)
        {         
            //--- My Calculate Value
           NewOpen[index] = MarketSeries.Open[index];
        }


    }
}

Then I'm trying to retrieve the value inside my CustomcBot(I add the indicator in the manager reference as per guide line) :

using System;
using System.Linq;
using System.Windows.Forms;
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 CustomcBot : Robot
    {
        //--- Public
        [Parameter(DefaultValue = 1, MinValue = 1)]
        public int Periods { get; set; }
        [Parameter("MA Type", DefaultValue = MovingAverageType.Simple)]
        public MovingAverageType MAType { get; set; }

        private CustomIndicator cIndicator;

        protected override void OnStart()
        {
            cIndicator = Indicators.GetIndicator<CustomIndicator>( Periods, MAType);
        }

        protected override void OnTick()
        {
            Print("Open {0}", cIndicator.NewOpen);
        }

        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}

In the log I keep receiving "Open IndicatorDataSeries (Count: 0, LastValue: NaN)", any idea why this is happening?

I'm on ICMarket cTrader v3.2(last one).


@AndreaDev

... Deleted by UFO ...

PanagiotisCharalampous
27 Feb 2019, 09:32

Hi AndreaDev,

Remove the following line from your indicator and it should work fine

NewOpen = CreateDataSeries();

Best Regards,

Panagiotis


@PanagiotisCharalampous

lec0456
27 Feb 2019, 10:26

And if it still doesn't work it looks like your namespace is wrong too.

It should be

namespace cAlgo.Indicators


@lec0456

... Deleted by UFO ...