0
Answered

SetPointOfEndLine method example

Jose Manuel Benitez 2 years ago updated 2 years ago 2

I would like to plot a DataSerie not continuos i.e. without plotting values in all bars. For example to plot Europe Session Highs and Lows but these lines should start and finish every day but without value during Asia Session.

Please would you mind give an example how to use SetPointOfEndLine method in a ValueDataSeries?

Thanks

Answered

Here is an example of "Open line"indicator. It uses SetPointOfEndLine method

public class OpenLine : Indicator
    {
        private bool _customSessionStart;
        private TimeSpan _startDate = new TimeSpan(9, 0, 0);
        private ValueDataSeries _line;
        private string _openCandleText = "Open Line";
        private int _fontSize = 10;
        private decimal _openValue = decimal.Zero;

        [Category("Session time")]
        [DisplayName("Custom session start")]
        [PropertyOrder(10)]
        public bool CustomSessionStart
        {
            get => _customSessionStart;
            set
            {
                _customSessionStart = value;
                RecalculateValues();
            }
        }

        [Category("Session time")]
        [DisplayName("Start time(GMT)")]
        [PropertyOrder(20)]
        public TimeSpan StartDate
        {
            get => _startDate;
            set
            {
                _startDate = value;
                RecalculateValues();
            }
        }

        [Category("Text settings")]
        [DisplayName("Text")]
        public string OpenCandleText
        {
            get => _openCandleText;
            set
            {
                _openCandleText = value;
                RecalculateValues();
            }
        }

        [Category("Text settings")]
        [DisplayName("Text size")]
        public int FontSize
        {
            get => _fontSize;
            set
            {
                _fontSize = value;
                RecalculateValues();
            }
        }

        private int _offset;

        [Category("Text settings")]
        [DisplayName("Y offset")]
        public int Offset
        {
            get => _offset;
            set { _offset = value; RecalculateValues(); }
        }


        public OpenLine():base(true)
        {
            _line = (ValueDataSeries)(DataSeries[0]);
            _line.VisualType=VisualMode.Square;
            _line.Color = Colors.SkyBlue;
            _line.Width = 2;
            DenyToChangePanel = true;
        }

        protected override void OnCalculate(int bar, decimal value)
        {
            if (bar == 0)
            {
                _openValue = decimal.Zero;
                return;
            }

            var candle = GetCandle(bar);
            bool isStart = _customSessionStart ? (candle.Time.TimeOfDay >= _startDate && GetCandle(bar - 1).Time.TimeOfDay < _startDate) : IsNewSession(bar);
            
            if (isStart)
            {
                _openValue = candle.Open;
                _line.SetPointOfEndLine(bar-1);
            }

            if (_openValue != decimal.Zero)
            {
                _line[bar] = _openValue;
                var penColor = System.Drawing.Color.FromArgb(_line.Color.A,_line.Color.R,_line.Color.G,_line.Color.B);

                AddText("Open Line", _openCandleText, true, CurrentBar, _openValue, -_offset, 0, penColor
                        , System.Drawing.Color.Transparent, System.Drawing.Color.Transparent, _fontSize,
                        DrawingText.TextAlign.Left);
            }
        }
    }