Information

Username: emeeder1
Member since: 04 Feb 2016
Last login: 23 Nov 2023
Status: Active

Activity

Where Created Comments
Algorithms 0 3
Forum Topics 6 9
Jobs 0 0

Last Algorithm Comments

EM
emeeder1 · 4 years ago

The above has a couple corrections to the one EARL posted. It works.

But it could be cleaned up a bit and updated to newer API.

also, both high and low show in pink because red and green dont show well with my chart colors. You have to change colurs in the code with whatever color you prefer.

Maybe someone else want to modify that so color con be selected in settings.  The current selection for color in setting does not work.

But at least now it shows both highs and lows correctly

EM
emeeder1 · 4 years ago

using System;
using cAlgo.API;
using cAlgo.API.Indicators;

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = true, AutoRescale = false, AccessRights = AccessRights.None)]
    public class SwingHighLowText : Indicator
    {
        [Parameter("Source")]
        public DataSeries Source { get; set; }

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

        [Parameter("Scale Precision", DefaultValue = 5)]
        public int ScalePrecision { get; set; }

        [Parameter("Text Color", DefaultValue = "Pink")]
        public string TextColor { get; set; }



        private Colors color = Colors.White;
        private Colors colorSell = Colors.Pink;
        private Colors colorBuy = Colors.Pink;
        private string format;

        protected override void Initialize()
        {
            // Parse color from string, e.g. "Yellow", "Green", "Red". string must start with large letter, "Red" is valid, "red" - not.
            Enum.TryParse(TextColor, out color);

            // create string format based on scale precision, e.g "0.000" for scale precision = 3
            format = "0." + new string('0', ScalePrecision);

            UpdateLabels(true);
        }

        public override void Calculate(int index)
        {
            if (IsRealTime)
                UpdateLabels(false);
        }

        private void UpdateLabels(bool fastUpdate)
        {
            ChartObjects.RemoveAllObjects();

            int startIndex = fastUpdate ? Source.Count - 350 : 0;
            int index;
            int index2;

            index = Source.Count - 2;

            while (index >= startIndex)
            {
                if (IsLocalExtremum(index, true))
                {
                    ChartObjects.DrawText("max_" + index, Source[index].ToString(format), index, Source[index], VerticalAlignment.Top, HorizontalAlignment.Center, colorSell);
                    index = index - Period;
                }
                else
                    index--;
            }
            index2 = Source.Count - 2;
            while (index2 >= startIndex)
            {
                if (IsLocalExtremumM(index2, true))
                {
                    ChartObjects.DrawText("min_" + index2, Source[index2].ToString(format), index2, Source[index2], VerticalAlignment.Bottom, HorizontalAlignment.Center, colorBuy);
                    index2 = index2 - Period;
                }
                else

                    index2--;
            }
            var lastIndex = Source.Count - 1;

        }

        private bool IsLocalExtremum(int index, bool findMax)
        {
            int end = Math.Min(index + Period, Source.Count - 1);
            int start = Math.Max(index - Period, 0);

            double value = Source[index];

            for (int i = start; i <= end; i++)
            {
                if (findMax && value < Source[i])
                    return false;

                if (!findMax && value > Source[i])
                    return false;
            }
            return true;
        }
        private bool IsLocalExtremumM(int index2, bool findMin)
        {
            int end = Math.Min(index2 + Period, Source.Count - 1);
            int start = Math.Max(index2 - Period, 0);

            double value = Source[index2];

            for (int i = start; i <= end; i++)
            {
                if (findMin && value > Source[i])
                    return false;

                if (!findMin && value < Source[i])
                    return false;
            }
            return true;
        }
    }
}