Draw object from one timeframe on another timeframe

Created at 14 Aug 2021, 15: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!
CT

ctid3999979

Joined 05.07.2021

Draw object from one timeframe on another timeframe
14 Aug 2021, 15:00


Hi

I'm starting work on an indicator to draw rectangles at supply and demand zones and I'd like to be able to draw these rectangles from different timeframes regardless of the time frame I'm currently displaying. For example, I may be on the 15min timeframe, but I want it to display the zones using data from the hourly candles.

I'm just starting out and so just using horizontal lines before updating it to rectangles etc. The code below draws a horizontal line at the high of each hourly candle as expected however, I was expecting it also draw the horizontal line at the hourly high if I was on a different timeframe. Currently, it doesn't draw a line unless I'm also viewing the hourly chart.

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 Test : Indicator

    {

        [Parameter("1Hr", DefaultValue = true)]

        public bool oneHr { get; set; }

 

        private Bars oneHrBars;

 

 

        protected override void Initialize()

        {

            // Initialize and create nested indicators

            oneHrBars = MarketData.GetBars(TimeFrame.Hour);

        }

 

        

        public override void Calculate(int index)

        {

            // Calculate value at specified index

            // Result[index] = ...

 

            if (oneHr == true)

            {

                Chart.DrawHorizontalLine("Hourly High", oneHrBars.HighPrices.Last(1), Color.Cyan);

            }

        }

    }

}

 


@ctid3999979
Replies

amusleh
16 Aug 2021, 15:33

Hi,

You can use any other time frame data for drawing objects on a chart, the code you have posted draws an horizontal line on latest closed hourly chart bar high, try this:

using cAlgo.API;
using cAlgo.API.Internals;

namespace cAlgo

{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class Test : Indicator
    {
        private Bars oneHrBars;

        protected override void Initialize()
        {
            oneHrBars = MarketData.GetBars(TimeFrame.Hour);
        }

        public override void Calculate(int index)
        {
            if (Chart.TimeFrame != TimeFrame.Hour)
            {
                var a = Chart.DrawHorizontalLine("Hourly High", oneHrBars.HighPrices.Last(1), Color.Cyan);

                a.IsInteractive = true;
            }
        }
    }
}

Change your time frame to a chart other than hourly chart, and you will see that indicator will draw an horizontal line, then switch back to hourly chart and you will see that the horizontal line is on last closed candle high price.

For matching one time data to another time frame you can use GetIndexByTime method of Bars.OpenTimes.


@amusleh