Information

Username: if.mihai@gmail.com
Member since: 16 Oct 2017
Last login: 16 Oct 2017
Status: Active

Activity

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

Last Algorithm Comments

IF
if.mihai@gmail.com · 7 years ago

smaller code working with any number of breakout bars (not only 8)

 

```

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

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = true, AccessRights = AccessRights.None)]
    public class BreakOutCandles : Indicator
    {

        [Parameter("How Many Break Out Bars", DefaultValue = 2)]
        public int HowMany { get; set; }

        [Parameter("Candle width", DefaultValue = 5)]
        public int CandleWidth { get; set; }

        [Parameter("Wick width", DefaultValue = 1)]
        public int WickWidth { get; set; }

        [Parameter("Breakout up color", DefaultValue = "LimeGreen")]
        public string UpColor { get; set; }

        [Parameter("Breakout down color", DefaultValue = "Red")]
        public string DownColor { get; set; }

        [Parameter("Range up color", DefaultValue = "LightGray")]
        public string RangeUpColor { get; set; }

        [Parameter("Rangedown color", DefaultValue = "LightGray")]
        public string RangeDownColor { get; set; }


        private Colors _UpColor;
        private Colors _DownColor;
        private Colors _RangeUpColor;
        private Colors _RangeDownColor;
        private Colors color;


        private bool _incorrectColors;
        private Random _random = new Random();

        protected override void Initialize()
        {
            if (!Enum.TryParse<Colors>(UpColor, out _UpColor) || !Enum.TryParse<Colors>(DownColor, out _DownColor) || !Enum.TryParse<Colors>(RangeUpColor, out _RangeUpColor) || !Enum.TryParse<Colors>(RangeDownColor, out _RangeDownColor))
                _incorrectColors = true;
        }

        public override void Calculate(int index)
        {
            if (_incorrectColors)
            {
                var errorColor = _random.Next(2) == 0 ? Colors.Red : Colors.White;
                ChartObjects.DrawText("Error", "Incorrect colors", StaticPosition.Center, errorColor);
                return;
            }

            var open = MarketSeries.Open[index];
            var high = MarketSeries.High[index];
            var low = MarketSeries.Low[index];
            var close = MarketSeries.Close[index];

            var highest = MarketSeries.High[index - 1];
            var lowest = MarketSeries.Low[index - 1];
            for (int i = 2; i <= HowMany; i++)
            {
                if (highest < MarketSeries.High[index - i])
                    highest = MarketSeries.High[index - i];
                if (lowest > MarketSeries.Low[index - i])
                    lowest = MarketSeries.Low[index - i];
            }

            if (close > highest)
                color = _UpColor;
            else if (close < lowest)
                color = _DownColor;
            else if (close > open)
                color = _RangeUpColor;
            else if (close <= open)
                color = _RangeDownColor;

            ChartObjects.DrawLine("candle" + index, index, open, index, close, color, CandleWidth, LineStyle.Solid);
            ChartObjects.DrawLine("line" + index, index, high, index, low, color, WickWidth, LineStyle.Solid);

        }
    }
}

```