midline doesn't move

Created at 19 Jun 2022, 11:21
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!
IR

IRCtrader

Joined 17.06.2021

midline doesn't move
19 Jun 2022, 11:21


in this below code i have a box with mid line.

when i move vertical line box move but midline disapper.

i know that problem is in Chart_ObjectsUpdated part. but i coudln't fix that.

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

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class AWBoxOffline2 : Indicator
    {
        private ChartVerticalLine _vl;

        private ChartRectangle _box;
        
        private ChartTrendLine _midline;

        [Parameter("Period", DefaultValue = 60, MinValue = 1)]
        public int Period { get; set; }

        [Parameter("Line Color", DefaultValue = "Red", Group = "Line")]
        public string LineColor { get; set; }

        [Parameter("LineStyle", DefaultValue = LineStyle.Solid, Group = "Line")]
        public LineStyle ls { get; set; }

        [Parameter("Thickness", DefaultValue = 2, Group = "Line")]
        public int Thickness { get; set; }
        
        [Parameter("Mid Line", DefaultValue = true, Group = "Mid Line")]
        public bool Midline { get; set; }
        
        [Parameter("Mid Line Color", DefaultValue = "Red", Group = "Mid Line")]
        public string Color2 { get; set; }
        [Parameter("Mid Line Style", DefaultValue = LineStyle.Solid, Group = "Mid Line")]
        public LineStyle ls2 { get; set; }
        [Parameter("Mid Line Thickness", DefaultValue = 2, Group = "Mid Line")]
        public int Thickness2 { get; set; }

        protected override void Initialize()
        {
            _vl = Chart.DrawVerticalLine("scrollLine", Chart.LastVisibleBarIndex, Color.FromName(LineColor), Thickness, ls);
            _vl.IsInteractive = true;
            var verticalLineBarIndex = Bars.OpenTimes.GetIndexByTime(_vl.Time);
            
             if (Midline)
            {
                _midline = Chart.DrawTrendLine("trendLine",verticalLineBarIndex , (Bars.LowPrices.Minimum(Period) + Bars.HighPrices.Maximum(Period)) / 2, verticalLineBarIndex-Period, (Bars.LowPrices.Minimum(Period) + Bars.HighPrices.Maximum(Period)) / 2,Color2,Thickness2,ls2);
            }


            _box = Chart.DrawRectangle("rectangle_sample", verticalLineBarIndex, Bars.LowPrices.Minimum(Period), verticalLineBarIndex - Period, Bars.HighPrices.Maximum(Period), Color.FromArgb(100, Color.Red));
            _box.IsFilled = true;
            _box.IsInteractive = false;
            _box.IsFilled = false;
            _box.Thickness = Thickness;
            _box.Color = LineColor;
            _box.LineStyle = ls;

            Chart.ObjectsUpdated += Chart_ObjectsUpdated;
        }

        private void Chart_ObjectsUpdated(ChartObjectsUpdatedEventArgs obj)
        {
            if (!obj.ChartObjects.Contains(_vl))
                return;

            var verticalLineBarIndex = Bars.OpenTimes.GetIndexByTime(_vl.Time);

            _box.Time1 = Bars.OpenTimes[verticalLineBarIndex];
            _box.Time2 = Bars.OpenTimes[verticalLineBarIndex - Period];

            _box.Y1 = GetMinimum(Bars.LowPrices, verticalLineBarIndex, Period);
            _box.Y2 = GetMaximum(Bars.HighPrices, verticalLineBarIndex, Period);
            
            _midline.Time1=Bars.OpenTimes[verticalLineBarIndex];
            _midline.Time2 = Bars.OpenTimes[verticalLineBarIndex - Period];

            _midline.Y1=GetMinimum(Bars.LowPrices, verticalLineBarIndex, Period)+GetMaximum(Bars.HighPrices, verticalLineBarIndex, Period)/2;
            _midline.Y2 = GetMinimum(Bars.LowPrices, verticalLineBarIndex, Period)+GetMaximum(Bars.HighPrices, verticalLineBarIndex, Period)/2;
        }

        private double GetMinimum(DataSeries source, int index, int periods)
        {
            double min = double.MaxValue;

            var lastBarIndex = index - periods;

            for (int i = index; i > lastBarIndex; i--)
            {
                min = Math.Min(min, source[i]);
            }

            return min;
        }

        private double GetMaximum(DataSeries source, int index, int periods)
        {
            double max = double.MinValue;

            var lastBarIndex = index - periods;

            for (int i = index; i > lastBarIndex; i--)
            {
                max = Math.Max(max, source[i]);
            }

            return max;
        }

        public override void Calculate(int index)
        {
        }
    }
}

 


@IRCtrader
Replies

amusleh
20 Jun 2022, 09:53

Hi,

Try this:

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

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class AWBoxOffline2 : Indicator
    {
        private ChartVerticalLine _vl;

        private ChartRectangle _box;
        
        private ChartTrendLine _midline;

        [Parameter("Period", DefaultValue = 60, MinValue = 1)]
        public int Period { get; set; }

        [Parameter("Line Color", DefaultValue = "Red", Group = "Line")]
        public string LineColor { get; set; }

        [Parameter("LineStyle", DefaultValue = LineStyle.Solid, Group = "Line")]
        public LineStyle ls { get; set; }

        [Parameter("Thickness", DefaultValue = 2, Group = "Line")]
        public int Thickness { get; set; }
        
        [Parameter("Mid Line", DefaultValue = true, Group = "Mid Line")]
        public bool Midline { get; set; }
        
        [Parameter("Mid Line Color", DefaultValue = "Red", Group = "Mid Line")]
        public string Color2 { get; set; }
        [Parameter("Mid Line Style", DefaultValue = LineStyle.Solid, Group = "Mid Line")]
        public LineStyle ls2 { get; set; }
        [Parameter("Mid Line Thickness", DefaultValue = 2, Group = "Mid Line")]
        public int Thickness2 { get; set; }

        protected override void Initialize()
        {
            _vl = Chart.DrawVerticalLine("scrollLine", Chart.LastVisibleBarIndex, Color.FromName(LineColor), Thickness, ls);
            _vl.IsInteractive = true;
            var verticalLineBarIndex = Bars.OpenTimes.GetIndexByTime(_vl.Time);
            
             if (Midline)
            {
                _midline = Chart.DrawTrendLine("trendLine",verticalLineBarIndex , (Bars.LowPrices.Minimum(Period) + Bars.HighPrices.Maximum(Period)) / 2, verticalLineBarIndex-Period, (Bars.LowPrices.Minimum(Period) + Bars.HighPrices.Maximum(Period)) / 2,Color2,Thickness2,ls2);
            }


            _box = Chart.DrawRectangle("rectangle_sample", verticalLineBarIndex, Bars.LowPrices.Minimum(Period), verticalLineBarIndex - Period, Bars.HighPrices.Maximum(Period), Color.FromArgb(100, Color.Red));
            _box.IsFilled = true;
            _box.IsInteractive = false;
            _box.IsFilled = false;
            _box.Thickness = Thickness;
            _box.Color = LineColor;
            _box.LineStyle = ls;

            Chart.ObjectsUpdated += Chart_ObjectsUpdated;
        }

        private void Chart_ObjectsUpdated(ChartObjectsUpdatedEventArgs obj)
        {
            if (!obj.ChartObjects.Contains(_vl))
                return;

            var verticalLineBarIndex = Bars.OpenTimes.GetIndexByTime(_vl.Time);

            _box.Time1 = Bars.OpenTimes[verticalLineBarIndex];
            _box.Time2 = Bars.OpenTimes[verticalLineBarIndex - Period];

            _box.Y1 = GetMinimum(Bars.LowPrices, verticalLineBarIndex, Period);
            _box.Y2 = GetMaximum(Bars.HighPrices, verticalLineBarIndex, Period);
            
            _midline.Time1= _box.Time1;
            _midline.Time2 = _box.Time2;

            _midline.Y1= _box.Y1 + ((_box.Y2 - _box.Y1) / 2);
            _midline.Y2 = _midline.Y1;
        }

        private double GetMinimum(DataSeries source, int index, int periods)
        {
            double min = double.MaxValue;

            var lastBarIndex = index - periods;

            for (int i = index; i > lastBarIndex; i--)
            {
                min = Math.Min(min, source[i]);
            }

            return min;
        }

        private double GetMaximum(DataSeries source, int index, int periods)
        {
            double max = double.MinValue;

            var lastBarIndex = index - periods;

            for (int i = index; i > lastBarIndex; i--)
            {
                max = Math.Max(max, source[i]);
            }

            return max;
        }

        public override void Calculate(int index)
        {
        }
    }
}

 


@amusleh