Indicator not working when called from cBot

08 Jan 2019, 23:21Indicator not working when called from cBot#1
ulises.guerreroposts: 5since: 23 Nov 2018

Hello, 

I have a multi-timeframe indicator that works fine on its own, but when I call it from a cBot, it always returns the same value (wrong value). Please help me to understand why it doesn't give the proper values when called from the Cbot. Here's the code of the indicator and Cbot:

 

**************************************************************

**************************************************************

**************************************************************

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

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None, AutoRescale = false)]
    public class h4TriggerLines : Indicator
    {
        [Output("h4 TriggerLevel", LineColor = "Purple")]
        public IndicatorDataSeries h4TriggerLevel { get; set; }

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

        private MarketSeries h4series;
        private ExponentialMovingAverage emah4;
        private int indexh4;
        private int indexh4previous;

        protected override void Initialize()
        {
            h4series = MarketData.GetSeries(TimeFrame.Hour4);
            emah4 = Indicators.ExponentialMovingAverage(h4series.Close, 15);
        }

        public override void Calculate(int index)
        {
            h4TriggerLevel[index] = h4TriggerLevel[index - 1];
            h4Direction[index] = h4Direction[index - 1];

            indexh4previous = indexh4;
            indexh4 = GetIndexByDate(h4series, MarketSeries.OpenTime[index]);

            if (indexh4 != -1)
            {
                if (indexh4previous != indexh4)
                {
                    if (h4series.Close[indexh4] > emah4.Result[indexh4] && h4series.Close[indexh4 - 1] < emah4.Result[indexh4 - 1])
                    {
                        h4TriggerLevel[index] = h4series.High[indexh4];
                        h4Direction[index] = 1;
                        ChartObjects.DrawText(String.Format("ArrowH4{0}", indexh4), "▲", index, h4series.High[indexh4], VerticalAlignment.Top, HorizontalAlignment.Center, Colors.Purple);
                    }

                    if (h4series.Close[indexh4] < emah4.Result[indexh4] && h4series.Close[indexh4 - 1] > emah4.Result[indexh4 - 1])
                    {
                        h4TriggerLevel[index] = h4series.Low[indexh4];
                        h4Direction[index] = -1;
                        ChartObjects.DrawText(String.Format("ArrowH4{0}", indexh4), "▼", index, h4series.Low[indexh4], VerticalAlignment.Bottom, HorizontalAlignment.Center, Colors.Purple);
                    }
                }
            }
        }


        private int GetIndexByDate(MarketSeries series, DateTime time)
        {
            for (int i = series.Close.Count - 1; i > 0; i--)
            {
                if (time == series.OpenTime[i])
                {
                    return i;
                }
                else if (series.OpenTime[i] < time)
                {
                    return i;
                    //return last value prev. to desired
                }
            }
            return -1;
        }
    }
}

 

 

**************************************************************

**************************************************************

**************************************************************

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 H1EntryBotH4 : Robot
    {

        private MovingAverage h1ema15;
        private h4TriggerLines TriggerLines;
        private MarketSeries h4series;

        protected override void OnStart()
        {
            h4series = MarketData.GetSeries(TimeFrame.Hour4);

            h1ema15 = Indicators.SimpleMovingAverage(MarketSeries.Close, 15);
            TriggerLines = Indicators.GetIndicator<h4TriggerLines>();
        }

        protected override void OnBar()
        {
            Print(TriggerLines.h4TriggerLevel.Last(1));
            Print(TriggerLines.h4Direction.Last(1));

            if (MarketSeries.Close.Last(1) > h1ema15.Result.Last(1))
            {
                PlaceStopOrder(TradeType.Buy, Symbol, 1000, MarketSeries.High.Last(1), "entry", 10, 10);
            }

        }

        protected override void OnStop()
        {

        }
    }
}

11 Jan 2019, 02:29#2
ulises.guerreroposts: 5since: 23 Nov 2018

Please help, I'm stuck with development. Why does the cBot load a constant value rather than the proper indicator values?

11 Jan 2019, 14:07#3
Panagiotis Charalampousposts: 1938since: 13 Jan 2017

Hi ulises.guerrero,

We are having a look at this. I will come back to you soon.

Best Regards,

Panagiotis


Head of Community Management at cTrader