Topics

Forum Topics not found

Replies

hannanmumtaz01
04 May 2024, 04:52 ( Updated at: 04 May 2024, 06:37 )

RE: Previous Days/Weeks/Months Highs and Lows Indicator

Didnt work for me at all, did some cleanup / bugfixes, seems to be working now.

 

 

using cAlgo.API;
using System;

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class PreviousDayHighLow : Indicator
    {

        [Parameter("Show High", Group = "Previous Day H/L", DefaultValue = true)]
        public bool PreDayHigh { get; set; }

        [Parameter("Show Low", Group = "Previous Day H/L", DefaultValue = true)]
        public bool PreDayLow { get; set; }

        [Parameter("Color", Group = "Previous Day H/L", DefaultValue = ColorSelector.Pink)]
        public ColorSelector PreDayLineColor { get; set; }

        [Parameter("Style", Group = "Previous Day H/L", DefaultValue = LineSelector.Dots)]
        public LineSelector PreDayLineStyle { get; set; }

        [Parameter("Thickness", Group = "Previous Day H/L", MinValue = 1, DefaultValue = 1)]
        public int PreDayThickness { get; set; }

        [Parameter("Show High", Group = "Previous Week H/L", DefaultValue = false)]
        public bool PreWeekHigh { get; set; }

        [Parameter("Show Low", Group = "Previous Week H/L", DefaultValue = false)]
        public bool PreWeekLow { get; set; }

        [Parameter("Color", Group = "Previous Week H/L", DefaultValue = ColorSelector.Yellow)]
        public ColorSelector PreWeekLineColor { get; set; }

        [Parameter("Style", Group = "Previous Week H/L", DefaultValue = LineSelector.LinesDots)]
        public LineSelector PreWeekLineStyle { get; set; }

        [Parameter("Thickness", Group = "Previous Week H/L", MinValue = 1, DefaultValue = 1)]
        public int PreWeekThickness { get; set; }

        [Parameter("Show High", Group = "Previous Month H/L", DefaultValue = false)]
        public bool PreMonthHigh { get; set; }

        [Parameter("Show Low", Group = "Previous Month H/L", DefaultValue = false)]
        public bool PreMonthLow { get; set; }

        [Parameter("Color", Group = "Previous Month H/L", DefaultValue = ColorSelector.Purple)]
        public ColorSelector PreMonthLineColor { get; set; }

        [Parameter("Style", Group = "Previous Month H/L", DefaultValue = LineSelector.Solid)]
        public LineSelector PreMonthLineStyle { get; set; }

        [Parameter("Thickness", Group = "Previous Month H/L", MinValue = 1, DefaultValue = 1)]
        public int PreMonthThickness { get; set; }

        [Parameter("Hue", Group = "Global", DefaultValue = HueSelector.Main)]
        public HueSelector PreHue { get; set; }

        [Parameter("Start Hour", Group = "Global", DefaultValue = 12, MinValue = 0, MaxValue = 23, Step = 1)]
        public int StartTimeHour { get; set; }

        [Parameter("UTC adjustment (-/+)", Group = "Global", DefaultValue = 8, MinValue = -12, MaxValue = 14, Step = 1)]
        public int Utc { get; set; }

        private Bars _week, _day, _month;
        private IndicatorDataSeries _indexM, _indexD, _indexW;

        public enum HueSelector
        {
            Main,
            Light,
            Dark
        }

        public enum LineSelector
        {
            Solid,
            Lines,
            LinesDots,
            Dots,
            DotsRare,
            DotsVeryRare
        }

        public enum ColorSelector
        {
            Default,
            White,
            Black,
            Gray,
            Blue,
            Cyan,
            Red,
            Pink,
            Green,
            Orange,
            Yellow,
            Purple
        }

        protected override void Initialize()
        {
            System.Diagnostics.Debugger.Launch();

            _week = MarketData.GetBars(TimeFrame.Weekly);
            _day = MarketData.GetBars(TimeFrame.Daily);
            _month = MarketData.GetBars(TimeFrame.Monthly);

            _indexW = CreateDataSeries();
            _indexD = CreateDataSeries();
            _indexM = CreateDataSeries();

        }

        public override void Calculate(int index)
        {
            var indexOpenTime = Bars.OpenTimes[index];

            CalculateDailyHighLows(index, indexOpenTime);
            CalculateWeeklyHighLows(index, indexOpenTime);
            CalculateMonthlyHighLows(index, indexOpenTime);

        }

        private void CalculateMonthlyHighLows(int index, DateTime indexOpenTime)
        {
            if (!PreMonthHigh && !PreMonthLow) return;

            var monthlyIndex = _month.OpenTimes.GetIndexByTime(indexOpenTime);
            _indexM[index] = monthlyIndex > 0 ? monthlyIndex : _indexM[index - 1];

            var nameHigh = "Month High" + _indexM[index];
            var nameLow = "Month Low" + _indexM[index];

            var indexStart = (int)_indexM[index];

            var start = _month.OpenTimes[indexStart - 1];
            var stop = Bars.OpenTimes[index];

            var high = _month.HighPrices[indexStart - 1];
            var low = _month.LowPrices[indexStart - 1];

            var clr = GetColor(PreMonthLineColor, PreHue);
            var lns = GetLineStyle(PreMonthLineStyle);

            if (double.IsNaN(high) || double.IsNaN(low)) return;

            if (PreMonthHigh)
                Chart.DrawTrendLine(nameHigh, start, high, stop, high, clr, PreMonthThickness, lns);
            if (PreMonthLow)
                Chart.DrawTrendLine(nameLow, start, low, stop, low, clr, PreMonthThickness, lns);
        }

        private void CalculateWeeklyHighLows(int index, DateTime indexOpenTime)
        {
            if (!PreWeekHigh && !PreWeekLow) return;

            var weeklyIndex = _week.OpenTimes.GetIndexByTime(indexOpenTime);
            _indexW[index] = weeklyIndex > 0 ? weeklyIndex : _indexW[index - 1];

            var nameHigh = "Week High" + _indexW[index];
            var nameLow = "Week Low" + _indexW[index];

            var indexStart = (int)_indexW[index];

            var start = _week.OpenTimes[indexStart - 1];
            var stop = Bars.OpenTimes[index];

            var high = _week.HighPrices[indexStart - 1];
            var low = _week.LowPrices[indexStart - 1];

            var clr = GetColor(PreWeekLineColor, PreHue);
            var lns = GetLineStyle(PreWeekLineStyle);

            if (double.IsNaN(high) || double.IsNaN(low)) return;

            if (PreWeekHigh)
                Chart.DrawTrendLine(nameHigh, start, high, stop, high, clr, PreWeekThickness, lns);
            if (PreWeekLow)
                Chart.DrawTrendLine(nameLow, start, low, stop, low, clr, PreWeekThickness, lns);
        }

        private void CalculateDailyHighLows(int index, DateTime indexOpenTime)
        {
            if (!PreDayHigh && !PreDayLow) return;

            var dailyIndex = _day.OpenTimes.GetIndexByTime(indexOpenTime);
            _indexD[index] = dailyIndex > 0 ? dailyIndex : _indexD[index - 1];

            var nameHigh = "Day High" + _indexD[index];
            var nameLow = "Day Low" + _indexD[index];

            var indexStart = (int)_indexD[index];

            var startHour = StartTimeHour - Utc;
            DateTime lineStart;

            switch (startHour)
            {
                case >= 24:
                    startHour -= 24;
                    lineStart = new DateTime(Bars.OpenTimes[index].Year, Bars.OpenTimes[index].Month, Bars.OpenTimes[index].Day, startHour, 0, 0);
                    break;
                case <= 0:
                    startHour += 24;
                    lineStart = new DateTime(Bars.OpenTimes[index].Year, Bars.OpenTimes[index].Month, Bars.OpenTimes[index].Day, startHour, 0, 0);
                    break;
                default:
                    lineStart = new DateTime(Bars.OpenTimes[index].Year, Bars.OpenTimes[index].Month, Bars.OpenTimes[index].Day, startHour, 0, 0);
                    break;
            }

            // line length
            var timeSpan = new TimeSpan(23, 59, 59);
            var lineStop = lineStart + timeSpan;

            // line height
            var dayHighPrice = _day.HighPrices[indexStart - 1];
            var dayLowPrice = _day.LowPrices[indexStart - 1];

            // line style
            var color = GetColor(PreDayLineColor, PreHue);
            var lineStyle = GetLineStyle(PreDayLineStyle);

            if (double.IsNaN(dayHighPrice) || double.IsNaN(dayLowPrice)) return;

            if (PreDayHigh)
                Chart.DrawTrendLine(nameHigh, lineStart, dayHighPrice, lineStop, dayHighPrice, color, PreDayThickness, lineStyle);
            if (PreDayLow)
                Chart.DrawTrendLine(nameLow, lineStart, dayLowPrice, lineStop, dayLowPrice, color, PreDayThickness, lineStyle);
        }

        private Color GetColor(ColorSelector color, HueSelector hue)
        {
            Color result;

            switch (color)
            {

                case ColorSelector.Black:
                    {
                        result = Color.Black;
                    }
                    break;
                case ColorSelector.White:
                    {
                        result = Color.White;
                    }
                    break;
                case ColorSelector.Gray:
                    {
                        if (hue.Equals(HueSelector.Dark))
                        {
                            result = Color.DarkGray;
                        }
                        else if (hue.Equals(HueSelector.Light))
                        {
                            result = Color.LightGray;
                        }
                        else
                        {
                            result = Color.Gray;
                        }
                    }
                    break;
                case ColorSelector.Blue:
                    {
                        if (hue.Equals(HueSelector.Dark))
                        {
                            result = Color.DarkBlue;
                        }
                        else if (hue.Equals(HueSelector.Light))
                        {
                            result = Color.LightBlue;
                        }
                        else
                        {
                            result = Color.Blue;
                        }
                    }
                    break;
                case ColorSelector.Cyan:
                    {
                        if (hue.Equals(HueSelector.Dark))
                        {
                            result = Color.DarkCyan;
                        }
                        else if (hue.Equals(HueSelector.Light))
                        {
                            result = Color.LightCyan;
                        }
                        else
                        {
                            result = Color.Cyan;
                        }
                    }
                    break;
                case ColorSelector.Red:
                    {
                        if (hue.Equals(HueSelector.Dark))
                        {
                            result = Color.DarkRed;
                        }
                        else if (hue.Equals(HueSelector.Light))
                        {
                            result = Color.OrangeRed;
                        }
                        else
                        {
                            result = Color.Red;
                        }
                    }
                    break;
                case ColorSelector.Pink:
                    {
                        if (hue.Equals(HueSelector.Dark))
                        {
                            result = Color.DarkMagenta;
                        }
                        else if (hue.Equals(HueSelector.Light))
                        {
                            result = Color.LightPink;
                        }
                        else
                        {
                            result = Color.Pink;
                        }
                    }
                    break;
                case ColorSelector.Green:
                    {
                        if (hue.Equals(HueSelector.Dark))
                        {
                            result = Color.DarkGreen;
                        }
                        else if (hue.Equals(HueSelector.Light))
                        {
                            result = Color.LightGreen;
                        }
                        else
                        {
                            result = Color.Green;
                        }
                    }
                    break;
                case ColorSelector.Orange:
                    {
                        if (hue.Equals(HueSelector.Dark))
                        {
                            result = Color.DarkOrange;
                        }
                        else if (hue.Equals(HueSelector.Light))
                        {
                            result = Color.Orange;
                        }
                        else
                        {
                            result = Color.Orange;
                        }
                    }
                    break;
                case ColorSelector.Yellow:
                    {
                        if (hue.Equals(HueSelector.Dark))
                        {
                            result = Color.Gold;
                        }
                        else if (hue.Equals(HueSelector.Light))
                        {
                            result = Color.LightYellow;
                        }
                        else
                        {
                            result = Color.Yellow;
                        }
                    }
                    break;
                case ColorSelector.Purple:
                    {
                        if (hue.Equals(HueSelector.Dark))
                        {
                            result = Color.DarkViolet;
                        }
                        else if (hue.Equals(HueSelector.Light))
                        {
                            result = Color.Violet;
                        }
                        else
                        {
                            result = Color.Purple;
                        }
                    }
                    break;
                case ColorSelector.Default:
                    {
                        result = Application.ColorTheme.Equals(ColorTheme.Dark) ? Color.White : Color.Black;
                    }
                    break;
                default:
                    {
                        result = Application.ColorTheme.Equals(ColorTheme.Dark) ? Color.White : Color.Black;
                    }
                    break;
            }

            return result;
        }

        private static LineStyle GetLineStyle(LineSelector line) =>
            line switch
            {
                LineSelector.Solid => LineStyle.Solid,
                LineSelector.Lines => LineStyle.Lines,
                LineSelector.LinesDots => LineStyle.LinesDots,
                LineSelector.Dots => LineStyle.Dots,
                LineSelector.DotsRare => LineStyle.DotsRare,
                LineSelector.DotsVeryRare => LineStyle.DotsVeryRare,
                _ => LineStyle.Solid
            };
    }
}

@hannanmumtaz01