Fractal Modified Indicator (BWFMI) free

by Kbee in category Other at 29/06/2020
Description

01/07/2020 - Last update: fixed a bug

Bill Williams Fractal Indicator
is used to identify potential supports and resistances in the past prices, so the signals are very much delay and the purpose of this modification is to reduce noise from the given signals.

Let me know if there's any bug. Cheers

Standard N = 2

N = 5

Notification Publishing copyrighted material is strictly prohibited. If you believe there is copyrighted material in this section you may use the Copyright Infringement Notification form to submit a claim.
Formula / Source Code
Language: C#
Trading Platform: cAlgocTrader
using cAlgo.API;
using System.Collections.Generic;

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class BWFractalModifiedIndicator : Indicator
    {
        [Parameter("N", Group = "Bars [-N] ➞ Bars [Cur] ➞ Bars [+N]", DefaultValue = 2, MinValue = 1, MaxValue = 100)]
        public int N { get; set; }

        [Parameter("On Chart", Group = "Signals", DefaultValue = 200, MinValue = 0)]
        public int OnChart { get; set; }

        [Parameter("Signal Range", Group = "Signals", DefaultValue = 5, MinValue = 0, MaxValue = 100)]
        public double Range { get; set; }

        [Parameter("Bullish Icon", Group = "Signals", DefaultValue = ChartIconType.UpTriangle)]
        public ChartIconType BullishIcon { get; set; }

        [Parameter("Bearish Icon", Group = "Signals", DefaultValue = ChartIconType.DownTriangle)]
        public ChartIconType BearishIcon { get; set; }

        [Parameter("Bullish Color", Group = "Signals", DefaultValue = "FF7CFC00")]
        public string BullishColor { get; set; }

        [Parameter("Bearish Color", Group = "Signals", DefaultValue = "FFFF4500")]
        public string BearishColor { get; set; }

        [Output("Color Sample", LineColor = " LawnGreen")]
        public IndicatorDataSeries IDS { get; set; }

        double _Range;
        double LastIndex;

        public class Signal
        {
            public int Index { get; set; }
            public string Name { get; set; }
            public bool Bullish { get; set; }
            public double Position { get; set; }
            public ChartIcon ChartIcon { get; set; }
        }

        List<Signal> Signals { get; set; }

        protected override void Initialize()
        {
            _Range = (Chart.TopY - Chart.BottomY) * (Range/100);

            Signals = new List<Signal>();
            Chart.ZoomChanged += OnZoomChanged;
        }

        public override void Calculate(int _index)
        {
            var index = (_index - 1) - N;

            if (index < N || LastIndex == index)
            { return; }
            LastIndex = index;

            var lown = Bars[index].Low;
            var highn = Bars[index].High;

            var bullish = true;
            var bearish = true;

            for (int i = index - N; i <= index + N; i++)
            {
                if (highn < Bars[i].High)
                { bullish = false; }

                if (lown > Bars[i].Low)
                { bearish = false; }
            }

            if (bullish)
            { AddSignal(true, "Bullish: " + index.ToString(), index, highn); }

            if (bearish)
            { AddSignal(false, "Bearish: " + index.ToString(), index, lown); }

            /*
            var low2n = Bars[index - 2].Low;
            var low1n = Bars[index - 1].Low;
            var lown1 = Bars[index + 1].Low;
            var lown2 = Bars[index + 2].Low;

            var high2n = Bars[index - 2].High;
            var high1n = Bars[index - 1].High;
            var highn1 = Bars[index + 1].High;
            var highn2 = Bars[index + 2].High;

            //Bullish Fractal
            if(lown <= low2n && lown <= low1n && lown <= lown1 && lown <= lown2)
            { AddSignal(false, index, lown); }

            //Bearish Fractal
            if (highn >= high2n && highn >= high1n && highn >= highn1 && highn >= highn2)
            { AddSignal(true, index, highn); }
            */
        }

        void AddSignal (bool bullish, string name, int index, double position)
        {
            var pos = bullish ? position + _Range : position - _Range;
            var color = bullish ? BullishColor: BearishColor;
            ChartIconType cit = bullish ? BullishIcon : BearishIcon;

            Signals.Add(new Signal
            {
                Name = name,
                Index = index,
                Bullish = bullish,
                Position = position,
                ChartIcon = Chart.DrawIcon(name, cit, index, pos, color)
            });

            for (int i = 2; i <= 4; i++)
            {
                if (Signals.Count < i)
                { break; }
                
                var lastnname = Signals[Signals.Count - i].Name;
                var lastnindex = Signals[Signals.Count - i].Index;
                
                var bothbull = lastnname.Contains("Bullish") && name.Contains("Bullish") ? true : false;
                var bothbear = lastnname.Contains("Bearish") && name.Contains("Bearish") ? true : false;
                
                if (lastnindex + 1 == index && (bothbull || bothbear))
                { RemoveSignal(Signals.Count - i); }
            }

            if (Signals.Count > OnChart)
            { RemoveSignal(0); }
        }

        void RemoveSignal (int num)
        {
            var name = Signals[num].Name;
            Chart.RemoveObject(name);
            Signals.RemoveAt(num);
        }

        void OnZoomChanged(ChartZoomEventArgs e)
        {
            _Range = (Chart.TopY - Chart.BottomY) * (Range / 100);

            foreach (var signal in Signals)
            {
                var pos = signal.Bullish ? signal.Position + _Range : signal.Position - _Range;
                signal.ChartIcon.Y = pos;
            }
        }
    }
}
Comments
0