Can't catch custom indicator event in CBot

Created at 21 Mar 2017, 14:04
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!

Can't catch custom indicator event in CBot
21 Mar 2017, 14:04


Hi All!

I have my own Indicator that Draw Bars in custom collors and generate my custom Event when bar is change collor. In my cbot I want catch this event and use it. But problem is that event is never generates.

Here is my Indicator code:

using System;

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

namespace ImpulseSystem
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class ImpulseSystem : Indicator
    {
        private MovingAverage ema;

        private MacdCrossOver macdCrossOver;

        public event EventHandler<ImpulseSystemChangeStateEventArgs> StateChanged;

        [Parameter(DefaultValue = 13)]
        public int EmaPeriod { get; set; }

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

        public override void Calculate(int index)
        {
            this.Ema[index] = this.ema.Result[index];

            if (this.ema.Result.IsRising() && this.macdCrossOver.Histogram.IsRising())
            {
                this.DrawBar(index, Colors.Green);
                this.GenerateEvent(StateType.Green);
            }
            else if (this.ema.Result.IsFalling() && this.macdCrossOver.Histogram.IsFalling())
            {
                this.DrawBar(index, Colors.Red);
                this.GenerateEvent(StateType.Red);
            }
            else
            {
                this.DrawBar(index, Colors.Aqua);
                this.GenerateEvent(StateType.Neutral);
            }
        }

        /// <inheritdoc />
        protected override void Initialize()
        {
            this.ema = Indicators.MovingAverage(this.MarketSeries.Close, this.EmaPeriod, MovingAverageType.Exponential);
            this.macdCrossOver = Indicators.MacdCrossOver(this.MarketSeries.Close, 26, 12, 9);
        }

        private void DrawBar(int index, Colors color)
        {
            this.ChartObjects.DrawLine("candle" + index, index, MarketSeries.Open[index], index, MarketSeries.Close[index], color, 5);
            this.ChartObjects.DrawLine("wick" + index, index, MarketSeries.High[index], index, MarketSeries.Low[index], color);
        }

        private void GenerateEvent(StateType newState)
        {
            EventHandler<ImpulseSystemChangeStateEventArgs> handler = this.StateChanged;

            if (handler != null)
            {
                handler(this, new ImpulseSystemChangeStateEventArgs { StateType = newState });
            }
        }
    }
}

 

Here is my CBot code:

using cAlgo.API;

using ImpulseSystem;

namespace ImpulseSystemStateChangeNotifier
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class ImpulseSystemStateChangeNotifier : Robot
    {
        private ImpulseSystem.ImpulseSystem impulseSystem;

        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }

        protected override void OnStart()
        {
            this.impulseSystem = Indicators.GetIndicator<ImpulseSystem.ImpulseSystem>(this.MarketData.GetSeries(this.Symbol, this.TimeFrame), 13);

            this.impulseSystem.StateChanged += ImpulseSystemOnStateChanged;
        }

        private static void ImpulseSystemOnStateChanged(object sender, ImpulseSystemChangeStateEventArgs impulseSystemChangeStateEventArgs)
        {
            StateType newState = impulseSystemChangeStateEventArgs.StateType;
        }
    }
}

Method ImpulseSystemOnStateChanged is never executed. Why?

Thanks!


@Максим_Коваленко
Replies

Jiri
21 Mar 2017, 16:11

Hi, you need to invoke Calculate() method in the referenced indicator. Add this into your cBot.

protected override void OnTick()
{
    double a = impulseSystem.Ema.LastValue;
}

 


@Jiri

Максим_Коваленко
21 Mar 2017, 16:30

RE:

tmc. said:

Hi, you need to invoke Calculate() method in the referenced indicator. Add this into your cBot.

protected override void OnTick()
{
    double a = impulseSystem.Ema.LastValue;
}

 

Thanks!


@Максим_Коваленко