Vertical grid lines which represent a start of a new day

Created at 26 Mar 2021, 11: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!
EY

eynt

Joined 08.05.2020

Vertical grid lines which represent a start of a new day
26 Mar 2021, 11:23


Is there a way to view on my chart vertical grid lines which represent a start of a new day?

 

Thanks


@eynt
Replies

amusleh
26 Mar 2021, 17:55

Hi,

You can use the cTrader Period Separator, it will draw a line at the beginning of each day and it works for time frames lower than hourly.

If that's not enough for you then you can use this sample indicator:

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

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class DailySeparatorLineSample : Indicator
    {
        private const string ChartObjectNamesSuffix = "DailySeparatorLineSample";

        private TimeSpan _dayTime, _dayTimeUserOffset;

        private Color _linesColor;

        [Parameter("Day Time", DefaultValue = "00:00:00")]
        public string DayTime { get; set; }

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

        [Parameter("Color Alpha", DefaultValue = 200, MaxValue = 255, MinValue = 0)]
        public int LinesColorAlpha { get; set; }

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

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

        protected override void Initialize()
        {
            if (TimeSpan.TryParse(DayTime, CultureInfo.InvariantCulture, out _dayTime))
            {
                _dayTimeUserOffset = _dayTime.Add(-Application.UserTimeOffset);
            }

            _linesColor = ParseColor(LinesColor, LinesColorAlpha);

            Application.UserTimeOffsetChanged += Application_UserTimeOffsetChanged;

            DrawLines();
        }

        public override void Calculate(int index)
        {
        }

        private void Application_UserTimeOffsetChanged(UserTimeOffsetChangedEventArgs obj)
        {
            _dayTimeUserOffset = _dayTime.Add(-Application.UserTimeOffset);

            DrawLines();
        }

        private void DrawLines()
        {
            RemoveLines();

            var endDate = Bars.OpenTimes.LastValue.Date.AddDays(100);

            for (var date = Bars.OpenTimes[0].Date; date <= endDate; date = date.AddDays(1))
            {
                var lineTime = date.Add(_dayTimeUserOffset);
                var lineName = string.Format("{0}_{1}", ChartObjectNamesSuffix, date);

                Chart.DrawVerticalLine(lineName, lineTime, _linesColor, LinesThickness, LinesStyle);
            }
        }

        private void RemoveLines()
        {
            var chartObjects = Chart.Objects.ToArray();

            foreach (var chartObject in chartObjects)
            {
                if (!chartObject.Name.StartsWith(ChartObjectNamesSuffix, StringComparison.OrdinalIgnoreCase)) continue;

                Chart.RemoveObject(chartObject.Name);
            }
        }

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

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

 


@amusleh