Category Other  Published on 12/04/2020

Candle Formation 1.4

Description

Change log : 
1.4 : Add option to pick which pattern to show


This indicator shows your 4 types of Candle Formation : 

H : Hammer, The numbers tells you how long is the wick compared to the body.

T: Shooting Star, The numbers tells you how long is the wick compared to the body.

U : Bullish Engulfing
E : Bearish Engulfing


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

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class PAx : Indicator
    {
        [Parameter("Look Back Period ", DefaultValue = 100)]
        public int lookback { get; set; }

        [Parameter("Use Bullish Engulfing ?  ", DefaultValue = true)]
        public bool useBU { get; set; }
        [Parameter("Use Bearish Engulfing ?  ", DefaultValue = true)]
        public bool useBE { get; set; }
        [Parameter("Use Hammer  ?  ", DefaultValue = true)]
        public bool useHammer { get; set; }
        [Parameter("Use Shooting Star ?  ", DefaultValue = true)]
        public bool useSS { get; set; }
        [Parameter("Use Morning Star ?  ", DefaultValue = true)]
        public bool useMS { get; set; }




        private List<DateTime> hammer = new List<DateTime> 
        {
                    };
        private List<DateTime> shootingstar = new List<DateTime> 
        {
                    };

        private List<DateTime> BEEB = new List<DateTime> 
        {
                    };


        private List<DateTime> BUEB = new List<DateTime> 
        {
                    };

        private List<DateTime> MS = new List<DateTime> 
        {
                    };

        private List<DateTime> ES = new List<DateTime> 
        {
                    };

        IndicatorDataSeries ema20;


        protected override void Initialize()
        {
            // Initialize and create nested indicators

            OnTimer();

        }

        protected override void OnTimer()
        {
            // Initialize and create nested indicators

            ema20 = Indicators.ExponentialMovingAverage(MarketSeries.Close, 20).Result;

            hammermethod();
            shootingstarmethod();
            BEEBmethod();
            BUEBmethod();
            MSmethod();
            ESmethod();



        }

        public override void Calculate(int index)
        {
            // Calculate value at specified index
            // Result[index] = ...
        }


        //==================

        private void hammermethod()
        {

            if (!useHammer)
                return;

            for (var i = 1; i <= lookback; i++)
            {
                var c = MarketSeries.Close.Last(i);
                var o = MarketSeries.Open.Last(i);
                var h = MarketSeries.High.Last(i);
                var l = MarketSeries.Low.Last(i);
                var lt = MarketSeries.Low.Last(i + 1);
                if (l > lt)
                    continue;

                var body = Math.Abs(c - o);
                var shadow = Math.Abs((c > o) ? o - l : c - l);
                var wick = (c > o) ? h - c : h - o;


                if (shadow / body < 3)
                    continue;
                if (wick / shadow > 0.3)
                    continue;

                hammer.Add(MarketSeries.OpenTime.Last(i));
                var text = Chart.DrawText("hammer" + hammer[hammer.Count - 1], Math.Floor(shadow / body) + "H", MarketSeries.OpenTime.Last(i), l - 0.02 * Chart.Height * Symbol.PipSize, Color.Green);
                text.HorizontalAlignment = HorizontalAlignment.Center;



            }

        }


        private void shootingstarmethod()
        {

            if (!useSS)
                return;

            for (var i = 1; i <= lookback; i++)
            {
                var c = MarketSeries.Close.Last(i);
                var o = MarketSeries.Open.Last(i);
                var h = MarketSeries.High.Last(i);
                var l = MarketSeries.Low.Last(i);

                var ht = MarketSeries.High.Last(i + 1);

                if (h < ht)
                    continue;

                var body = Math.Abs(c - o);
                var wick = Math.Abs((c > o) ? o - l : c - l);
                var shadow = (c > o) ? h - c : h - o;



                if (shadow / body < 3)
                    continue;
                if (wick / shadow > 0.3)
                    continue;

                shootingstar.Add(MarketSeries.OpenTime.Last(i));
                var text = Chart.DrawText("star" + shootingstar[shootingstar.Count - 1], Math.Floor(shadow / body) + "T", MarketSeries.OpenTime.Last(i), h + 0.02 * Chart.Height * Symbol.PipSize, Color.Red);
                text.HorizontalAlignment = HorizontalAlignment.Center;
            }



        }

        private void BEEBmethod()
        {

            if (!useBE)
                return;

            for (var i = 1; i <= lookback; i++)
            {
                var c = MarketSeries.Close.Last(i);
                var o = MarketSeries.Open.Last(i);
                var h = MarketSeries.High.Last(i);
                var l = MarketSeries.Low.Last(i);

                var ct = MarketSeries.Close.Last(i + 1);
                var ot = MarketSeries.Open.Last(i + 1);
                var ht = MarketSeries.High.Last(i + 1);
                var lt = MarketSeries.Low.Last(i + 1);



                if (ct < ot || c > o)
                    continue;



                if (c < ot)
                {
                    //if (o > ema20.Last(i))
                    //continue;

                    BEEB.Add(MarketSeries.OpenTime.Last(i));
                    var text = Chart.DrawText("BEEB" + BEEB[BEEB.Count - 1], "E", MarketSeries.OpenTime.Last(i), h + 0.03 * Chart.Height * Symbol.PipSize, Color.OrangeRed);

                    text.HorizontalAlignment = HorizontalAlignment.Center;
                }

            }
        }
        private void MSmethod()
        {
            if (!useMS)
                return;


            for (var i = 1; i <= lookback; i++)
            {
                var c = MarketSeries.Close.Last(i);
                var o = MarketSeries.Open.Last(i);
                var h = MarketSeries.High.Last(i);
                var l = MarketSeries.Low.Last(i);

                var ct = MarketSeries.Close.Last(i + 1);
                var ot = MarketSeries.Open.Last(i + 1);
                var ht = MarketSeries.High.Last(i + 1);
                var lt = MarketSeries.Low.Last(i + 1);

                var cz = MarketSeries.Close.Last(i + 2);
                var oz = MarketSeries.Open.Last(i + 2);
                var hz = MarketSeries.High.Last(i + 2);
                var lz = MarketSeries.Low.Last(i + 2);



                if (c < o || cz > oz)
                    continue;




                if (ht < cz + (oz - cz) * 0.5 && c > cz + (oz - cz) * 0.5 && Math.Abs(oz - cz) > 3 * Math.Abs(ct - ot))
                {
                    //if (o < ema20.Last(i))
                    //continue;

                    MS.Add(MarketSeries.OpenTime.Last(i + 1));

                    if (MS.Count > 0)
                    {
                        var text = Chart.DrawText("MS" + MS[MS.Count - 1], "MS", MarketSeries.OpenTime.Last(i + 1), lt - 0.05 * Chart.Height * Symbol.PipSize, Color.Violet);
                        text.HorizontalAlignment = HorizontalAlignment.Center;
                    }


                }

            }



        }

        private void ESmethod()
        {


            for (var i = 1; i <= lookback; i++)
            {
                var c = MarketSeries.Close.Last(i);
                var o = MarketSeries.Open.Last(i);
                var h = MarketSeries.High.Last(i);
                var l = MarketSeries.Low.Last(i);

                var ct = MarketSeries.Close.Last(i + 1);
                var ot = MarketSeries.Open.Last(i + 1);
                var ht = MarketSeries.High.Last(i + 1);
                var lt = MarketSeries.Low.Last(i + 1);

                var cz = MarketSeries.Close.Last(i + 2);
                var oz = MarketSeries.Open.Last(i + 2);
                var hz = MarketSeries.High.Last(i + 2);
                var lz = MarketSeries.Low.Last(i + 2);



                if (c > o || cz < oz)
                    continue;




                if (lt > cz - Math.Abs(oz - cz) * 0.5 && c < cz - Math.Abs(oz - cz) * 0.5 && Math.Abs(oz - cz) > 3 * Math.Abs(ct - ot))
                {
                    //if (o < ema20.Last(i))
                    //continue;

                    ES.Add(MarketSeries.OpenTime.Last(i + 1));

                    if (ES.Count > 0)
                    {
                        var text = Chart.DrawText("ES" + ES[ES.Count - 1], "ES", MarketSeries.OpenTime.Last(i + 1), ht + 0.05 * Chart.Height * Symbol.PipSize, Color.Violet);
                        text.HorizontalAlignment = HorizontalAlignment.Center;
                    }


                }

            }



        }


        private void BUEBmethod()
        {

            if (useBU)
                for (var i = 1; i <= lookback; i++)
                {
                    var c = MarketSeries.Close.Last(i);
                    var o = MarketSeries.Open.Last(i);
                    var h = MarketSeries.High.Last(i);
                    var l = MarketSeries.Low.Last(i);

                    var ct = MarketSeries.Close.Last(i + 1);
                    var ot = MarketSeries.Open.Last(i + 1);
                    var ht = MarketSeries.High.Last(i + 1);
                    var lt = MarketSeries.Low.Last(i + 1);



                    if (ct > ot || c < o)
                        continue;



                    if (c > ot)
                    {
                        //if (o < ema20.Last(i))
                        //continue;

                        BUEB.Add(MarketSeries.OpenTime.Last(i));
                        var text = Chart.DrawText("BUEB" + BUEB[BUEB.Count - 1], "U", MarketSeries.OpenTime.Last(i), l - 0.03 * Chart.Height * Symbol.PipSize, Color.GreenYellow);

                        text.HorizontalAlignment = HorizontalAlignment.Center;



                    }

                }



        }

        //=====
    }
}


fundspreader's avatar
fundspreader

Joined on 30.01.2017

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: Candle Formation 1.4.algo
  • Rating: 5
  • Installs: 2340
  • Modified: 13/10/2021 09:54
Comments
Log in to add a comment.
No comments found.