Category Trend  Published on 12/06/2023

Three in a Row

Description



Want to know when three consecutive bullish or bearish candles happen? 
This indicator will signal the next opening candle when the above condition is met.

USE IT AS A cBOT TOO!


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AutoRescale = false, AccessRights = AccessRights.None)]
    public class ConsecutiveCandlesIndicator : Indicator
    {
        [Output("Bullish Signal", LineColor = "Green", PlotType = PlotType.Histogram)]
        public IndicatorDataSeries BullishSignal { get; set; }

        [Output("Bearish Signal", LineColor = "Red", PlotType = PlotType.Points)]
        public IndicatorDataSeries BearishSignal { get; set; }

        protected override void Initialize()
        {
        }

        public override void Calculate(int index)
        {
            if (index >= 3)
            {
                bool isBullishSignal = IsConsecutiveBullishCandles(index);
                bool isBearishSignal = IsConsecutiveBearishCandles(index);

                if (isBullishSignal)
                {
                    BullishSignal[index] = Bars.OpenPrices[index];
                }
                else if (isBearishSignal)
                {
                    BearishSignal[index] = Bars.OpenPrices[index];
                }
                else
                {
                    BullishSignal[index] = double.NaN;
                    BearishSignal[index] = double.NaN;
                }
            }
            else
            {
                BullishSignal[index] = double.NaN;
                BearishSignal[index] = double.NaN;
            }
        }

        private bool IsConsecutiveBullishCandles(int index)
        {
            return IsBullishCandle(index - 1) && IsBullishCandle(index - 2) && IsBullishCandle(index - 3);
        }

        private bool IsConsecutiveBearishCandles(int index)
        {
            return IsBearishCandle(index - 1) && IsBearishCandle(index - 2) && IsBearishCandle(index - 3);
        }

        private bool IsBullishCandle(int index)
        {
            return Bars.ClosePrices[index] > Bars.OpenPrices[index];
        }

        private bool IsBearishCandle(int index)
        {
            return Bars.ClosePrices[index] < Bars.OpenPrices[index];
        }
    }
}

is.villaescusa's avatar
is.villaescusa

Joined on 23.02.2023

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: Three consecutive.algo
  • Rating: 0
  • Installs: 875
  • Modified: 12/06/2023 08:37
Comments
Log in to add a comment.
JI
jim.tollan · 1 year ago

nice starting point for comparing different candle patterns. one little nice change might be to define how many candles in a row, rather than a static 3. this code might work:

        [Parameter("Number in a row", DefaultValue = 3, MinValue = 1, MaxValue = 6, Group = "Standard")]
        public int ConsecutiveCount
        {
            get;
            set;
        }

        private bool IsConsecutiveBullishCandles(int index)
        {
            bool isConsecutive = true;
            
            for (int i = 1; i <= ConsecutiveCount; i++)
            {
                isConsecutive = isConsecutive && IsBullishCandle(index - i);
            }
            return isConsecutive;
        }

        private bool IsConsecutiveBearishCandles(int index)
        {
            bool isConsecutive = true;
            
            for (int i = 1; i <= ConsecutiveCount; i++)
            {
                isConsecutive = isConsecutive && IsBearishCandle(index - i);
            }
            return isConsecutive;
        }

just a thought.. cheers...