Information

Username: Augustinas_Mk
Member since: 30 Mar 2016
Last login: 25 Jun 2024
Status: Active

Activity

Where Created Comments
Algorithms 0 1
Forum Topics 0 0
Jobs 0 0

Last Algorithm Comments

AU
Augustinas_Mk · 8 years ago

This SHOWS HIGHER AND LOWER MAX POSITIONS

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

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = true, AutoRescale = false, AccessRights = AccessRights.None)]
    public class SwingHigh : 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.Red;
        private Colors colorBuy = Colors.Green;
        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 start = Math.Min(index2 + Period, Source.Count - 1);
            int end = 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;
        }
    }
}