Loading multiple timeframes on indicator and painting

Created at 13 Aug 2021, 02:14
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!
HE

herculesone

Joined 02.11.2020

Loading multiple timeframes on indicator and painting
13 Aug 2021, 02:14


Hello there! 

I would like some help to make an indicator, which is fairly simple but does not show as I want it.

I would like to show the closing prices of different timeframes on the same chart. However, I am unable to make the chart match the output I want to show.

Here is the 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 Closes_4hr_D_W_M_Q_Y : Indicator
    {

        [Output("4 Hour", LineColor = "Green", PlotType = PlotType.Points, Thickness = 2)]
        public IndicatorDataSeries Hour4 { get; set; }

        [Output("Daily", LineColor = "Black", PlotType = PlotType.Line, LineStyle = LineStyle.Solid, Thickness = 2)]
        public IndicatorDataSeries Daily { get; set; }

        [Output("Weekly", LineColor = "Red", PlotType = PlotType.Line, LineStyle = LineStyle.Solid, Thickness = 2)]
        public IndicatorDataSeries Weekly { get; set; }

        [Output("Monthly", LineColor = "Navy", PlotType = PlotType.Line, LineStyle = LineStyle.Solid, Thickness = 2)]
        public IndicatorDataSeries Monthly { get; set; }

        public Bars _daily, _4h, _weekly, _month;

        protected override void Initialize()
        {
            _4h = MarketData.GetBars(TimeFrame.Hour4);
            _daily = MarketData.GetBars(TimeFrame.Daily);
            _weekly = MarketData.GetBars(TimeFrame.Weekly);
            _month = MarketData.GetBars(TimeFrame.Monthly);
        }

        public override void Calculate(int index)
        {            
            Hour4[index] = _4h.ClosePrices[index];
            Daily[index] = _daily.ClosePrices[index];
            Weekly[index] = _weekly.ClosePrices[index];
            Monthly[index] = _month.ClosePrices[index];
        }
    }
}

 

I would expect that on a 1 hour chart, the Daily output would show as a straight line until the next day, when there would be another straight line etc. Instead, the indicators paint further back as if they are attached to another chart.

It is like the "index" applies to the chart I am calling and not the "index" of the applicable chart, which does make sense in a way but I want to work around it and cannot figure out how.

In essence when I call "index" of a 1 hour chart, the price on the daily chart of the same index should be the same until all the hours are complete. 

Any help is appreciated, thank you!

 


@herculesone
Replies

firemyst
29 Aug 2021, 12:21

RE:

herculesone said:

Hello there! 

I would like some help to make an indicator, which is fairly simple but does not show as I want it.

I would like to show the closing prices of different timeframes on the same chart. However, I am unable to make the chart match the output I want to show.

Here is the 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 Closes_4hr_D_W_M_Q_Y : Indicator
    {

        [Output("4 Hour", LineColor = "Green", PlotType = PlotType.Points, Thickness = 2)]
        public IndicatorDataSeries Hour4 { get; set; }

        [Output("Daily", LineColor = "Black", PlotType = PlotType.Line, LineStyle = LineStyle.Solid, Thickness = 2)]
        public IndicatorDataSeries Daily { get; set; }

        [Output("Weekly", LineColor = "Red", PlotType = PlotType.Line, LineStyle = LineStyle.Solid, Thickness = 2)]
        public IndicatorDataSeries Weekly { get; set; }

        [Output("Monthly", LineColor = "Navy", PlotType = PlotType.Line, LineStyle = LineStyle.Solid, Thickness = 2)]
        public IndicatorDataSeries Monthly { get; set; }

        public Bars _daily, _4h, _weekly, _month;

        protected override void Initialize()
        {
            _4h = MarketData.GetBars(TimeFrame.Hour4);
            _daily = MarketData.GetBars(TimeFrame.Daily);
            _weekly = MarketData.GetBars(TimeFrame.Weekly);
            _month = MarketData.GetBars(TimeFrame.Monthly);
        }

        public override void Calculate(int index)
        {            
            Hour4[index] = _4h.ClosePrices[index];
            Daily[index] = _daily.ClosePrices[index];
            Weekly[index] = _weekly.ClosePrices[index];
            Monthly[index] = _month.ClosePrices[index];
        }
    }
}

 

I would expect that on a 1 hour chart, the Daily output would show as a straight line until the next day, when there would be another straight line etc. Instead, the indicators paint further back as if they are attached to another chart.

It is like the "index" applies to the chart I am calling and not the "index" of the applicable chart, which does make sense in a way but I want to work around it and cannot figure out how.

In essence when I call "index" of a 1 hour chart, the price on the daily chart of the same index should be the same until all the hours are complete. 

Any help is appreciated, thank you!

 

The index of the current chart won't be the same as the index on the other timeframe charts.That's why you're having the issue. For all the timeframes, you're getting the current index on the chart you're viewing. Since there's significantly more bars on the H4 chart than there are in the D1 chart for the day, you're going to have multiple incorrect readings.

For instance, when viewing the H4 chart, you need to get the corresponding index on the D, W, and M charts.

//Example code to get the correct Daily index on the H4 chart in the Calculate method. 
//This code assumes you'll always be looking at the H4 chart.

int altIndexD1 = _daily.OpenTimes.GetIndexByTime(Bars.OpenTimes[index]);

 


@firemyst

herculesone
11 Sep 2021, 18:18

RE: RE:

Thank you very much for your response, I indeed discovered this property and it helped get the time I need still the readings are way off sometimes, I made another post about this today with some example code here:

cTDN Forum - Multi time frame data (ctrader.com)

 

Thank you again for your help!

 


@herculesone