Draw a ChartVerticalLine at 22:00 every day

Created at 30 Dec 2021, 04:23
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!
Capt.Z-Fort.Builder's avatar

Capt.Z-Fort.Builder

Joined 03.06.2020

Draw a ChartVerticalLine at 22:00 every day
30 Dec 2021, 04:23


Hello,

As the subject stated, how can I "Draw a ChartVerticalLine at 22:00 every day", though cTrader has a function to show 'Period Seperators', but it only separate at GMT 00:00 each day. I want the separators all to sit at 22:00GMT the market closing time.

Thanks.

Here is the reference link: 

 


@Capt.Z-Fort.Builder
Replies

amusleh
30 Dec 2021, 08:36

Hi,

You can draw a vertical line on any time you want to, you just have to pass the time.

Use a loop and draw a line every day at 22 GMT, here is an example that might help you: 

 


@amusleh

Capt.Z-Fort.Builder
30 Dec 2021, 12:29

RE:

Hi Amusleh,

Thanks for your reply. It's perfect fit my requirement. :)

 

amusleh said:

Hi,

You can draw a vertical line on any time you want to, you just have to pass the time.

Use a loop and draw a line every day at 22 GMT, here is an example that might help you: 

 

 


@Capt.Z-Fort.Builder

Capt.Z-Fort.Builder
31 Dec 2021, 17:40

RE:

Hi Amusleh,

This tool is great for me, and I find a tiny little thing you might help me make it be much fit to my requirement.

I want it only to apply to the current day, however, I can't set the 'Past Days' and 'Future Days' to 0. Keep both with value 1, it will display the highlights for the past and next days.

Can you possible revise it and notify me when it is available.

Thanks very much.

amusleh said:

Hi,

You can draw a vertical line on any time you want to, you just have to pass the time.

Use a loop and draw a line every day at 22 GMT, here is an example that might help you: 

 

 


@Capt.Z-Fort.Builder

amusleh
03 Jan 2022, 09:21

Hi,

Try this:

using cAlgo.API;
using System;
using System.Globalization;

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class TradingTimePeriods : Indicator
    {
        private readonly string _name = "Trading Time Periods";

        private TimeSpan _startTime, _endTime;

        private Color _backgroundColor;

        private bool _isLinesPlotted;

        [Parameter("Start", DefaultValue = "08:00:00", Group = "Time")]
        public string StartTime { get; set; }

        [Parameter("End", DefaultValue = "16:30:00", Group = "Time")]
        public string EndTime { get; set; }

        [Parameter("Background", DefaultValue = "Blue", Group = "Color")]
        public string BackgroundColor { get; set; }

        [Parameter("Transparency", DefaultValue = 50, MaxValue = 255, MinValue = 0, Group = "Color")]
        public int Transparency { get; set; }

        [Parameter("Thickness", DefaultValue = 1, Group = "Lines")]
        public int LinesThickness { get; set; }

        [Parameter("Style", DefaultValue = LineStyle.DotsVeryRare, Group = "Lines")]
        public LineStyle LinesStyle { get; set; }

        [Parameter("Enable", DefaultValue = true, Group = "Max Time Frame")]
        public bool EnableMaxTimeFrame { get; set; }

        [Parameter("Time Frame", DefaultValue = "Hour4", Group = "Max Time Frame")]
        public TimeFrame MaxTimeFrame { get; set; }

        [Parameter("Interactive", DefaultValue = false, Group = "Appearance")]
        public bool Interactive { get; set; }

        [Parameter("Filled", DefaultValue = true, Group = "Appearance")]
        public bool Filled { get; set; }

        [Parameter("Past Days #", DefaultValue = 5, MinValue = 0, Group = "Others")]
        public int PastDaysNumber { get; set; }

        [Parameter("Future Days #", DefaultValue = 5, MinValue = 0, Group = "Others")]
        public int FutureDaysNumber { get; set; }

        [Parameter("Skip Weekends", DefaultValue = true, Group = "Others")]
        public bool SkipWeekends { get; set; }

        public ChartArea Area
        {
            get
            {
                return IndicatorArea ?? (ChartArea)Chart;
            }
        }

        protected override void Initialize()
        {
            _backgroundColor = GetColor(BackgroundColor, Transparency);

            ParseTimes();

            Application.UserTimeOffsetChanged += Application_UserTimeOffsetChanged;
        }

        public override void Calculate(int index)
        {
            if (!IsLastBar || _isLinesPlotted || (EnableMaxTimeFrame && TimeFrame > MaxTimeFrame))
            {
                return;
            }

            _isLinesPlotted = true;

            DateTime firstBarTime = AddDays(Bars.OpenTimes[index], -PastDaysNumber).Date;
            DateTime lastDayTime = AddDays(Bars.OpenTimes[index], FutureDaysNumber).Date;

            double maxPriceLevel = Bars.HighPrices.Maximum(Bars.HighPrices.Count) * 3;

            for (DateTime currentTime = firstBarTime; currentTime <= lastDayTime; currentTime = AddDays(currentTime, 1))
            {
                string objName = string.Format("{0} {1}", _name, currentTime);

                DateTime startTime = currentTime.Add(_startTime);
                DateTime endTime = currentTime.Add(_endTime);

                ChartRectangle rectangle = Chart.DrawRectangle(objName, startTime, 0, endTime, maxPriceLevel, _backgroundColor, LinesThickness, LinesStyle);

                rectangle.IsFilled = Filled;
                rectangle.IsInteractive = Interactive;
            }
        }

        private void Application_UserTimeOffsetChanged(UserTimeOffsetChangedEventArgs obj)
        {
            ParseTimes();

            _isLinesPlotted = false;
        }

        private DateTime AddDays(DateTime dateTime, int numberOfDays)
        {
            DateTime result = dateTime;

            for (int i = 1; i <= Math.Abs(numberOfDays); i++)
            {
                result = result.AddDays(numberOfDays > 0 ? 1 : -1);

                if (SkipWeekends && (result.DayOfWeek == DayOfWeek.Saturday || result.DayOfWeek == DayOfWeek.Sunday))
                {
                    result = result.DayOfWeek == DayOfWeek.Saturday ? result.AddDays(numberOfDays > 0 ? 2 : -2) : result.AddDays(numberOfDays > 0 ? 1 : -1);
                }
            }

            return result;
        }

        private void ParseTimes()
        {
            if (TimeSpan.TryParse(StartTime, CultureInfo.InvariantCulture, out _startTime))
            {
                _startTime = _startTime.Add(-Application.UserTimeOffset);
            }

            if (TimeSpan.TryParse(EndTime, CultureInfo.InvariantCulture, out _endTime))
            {
                _endTime = _endTime.Add(-Application.UserTimeOffset);
            }
        }

        private Color GetColor(string colorString, int alpha = 255)
        {
            var color = colorString[0] == '#' ? Color.FromHex(colorString) : Color.FromName(colorString);

            return Color.FromArgb(alpha, color);
        }
    }
}

 


@amusleh

Capt.Z-Fort.Builder
03 Jan 2022, 15:15 ( Updated at: 03 Jan 2022, 15:16 )

RE:

Thank you Amushleh, very kind of you. And it's perfect!

amusleh said:

Hi,

Try this:

using cAlgo.API;
using System;
using System.Globalization;

namespace cAlgo
{
   ...
}

 

 


@Capt.Z-Fort.Builder