certain timeframe bars are not loading up to the current index

Created at 29 Jan 2021, 16:32
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!
hrivven's avatar

hrivven

Joined 29.12.2020

certain timeframe bars are not loading up to the current index
29 Jan 2021, 16:32


This program overlays an alternative timeframe bars source onto the chart, the problem is only a certain number of selected timeframes are working correctly i.e, are being drawn up to the last index in the series like this : (chart timeframe = 1h , alternate timeframe  =  6h)

 

But say for (chart timeframe = 1h, alternate timeframe = 4h)  it shows up like this:

only when I manually zoom out the chart and more bar history is loaded, the drawings are correct like this :

why is this occurring ? 

here's my code:

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 TimeFrame_Candles : Indicator
    {
        [Parameter("Time Frame", DefaultValue = "TimeFrame.Hour")]
        public TimeFrame time_frame { get; set; }

        Bars bars_source;
        int last_index;

        DateTime open_time;
        DateTime close_time;

        double open;
        double close;
        double high;
        double low;
        string color;
        int type;

        protected override void Initialize()
        {
            bars_source = MarketData.GetBars(time_frame, SymbolName);
            last_index = 0;
        }

        public override void Calculate(int index)
        {
            if (index > last_index)
            {
                var x = bars_source.OpenTimes.GetIndexByTime(bars_source[last_index].OpenTime);
                bool index_in_range = indexinrange(x);

                if (index_in_range && x != -1)
                    on_candle_close(x);
                else
                    return;
                last_index = index;
            }
        }

        private void on_candle_close(int x)
        {
            open_time = bars_source[x].OpenTime;
            close_time = bars_source[x + 1].OpenTime;

            open = bars_source[x].Open;
            close = bars_source[x].Close;
            high = bars_source[x].High;
            low = bars_source[x].Low;

            type = close > open ? 1 : close < open ? 0 : 2;

            color = type == 1 ? "Green" : type == 0 ? "Red" : "DimGray";

            var rect = Chart.DrawRectangle("draw_no" + x.ToString(), open_time, open, close_time, close, color, 3);
            rect.IsFilled = false;
            Chart.DrawText("text" + x.ToString(), x.ToString(), bars_source[x].OpenTime, bars_source[x].Close, "Black");

        }


        private bool indexinrange(int index)
        {
            var last_index = bars_source.Count - 1;
            if (index < last_index)
                return true;
            else if (index >= last_index)
                return false;
            return false;

        }
    }
}

thank you


@hrivven
Replies

PanagiotisCharalampous
01 Feb 2021, 07:52

Hi hrivven,

Which broker do you use? I tried to reproduce this but the indicator works fine on any timeframe I tried.

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous