Category Trend  Published on 20/09/2021

Braid Filter Mod v1.1

Description

The histogram is based on the difference between the highest and lowest moving average out of the 3. The line is a level based on ATR that filters trading during sideways markets.

This indicator is a modified version of the one coded by kaneida84https://ctrader.com/algos/indicators/show/2717

The original indicator was coded in MQL4 by Robert Hill after attending a workshop that traded the 15 minutes timeframe.

 

Log:

Mon Sep 20, 2021 - Fixed the Arrows Yes/No parameter.

Braid Filter Mod v1

 

Braid Filter Mod v1 Parameters


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

namespace cAlgo
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class BraidFilterModv1 : Indicator
    {
        [Parameter("MA1 Period", DefaultValue = 3)]
        public int ma1Per { get; set; }

        [Parameter("MA2 Period", DefaultValue = 7)]
        public int ma2Per { get; set; }

        [Parameter("MA3 Period", DefaultValue = 14)]
        public int ma3Per { get; set; }

        [Parameter("ATR Period", DefaultValue = 14)]
        public int atrPer { get; set; }

        [Parameter("PipsMinSepPercent", DefaultValue = 40)]
        public int pipPerc { get; set; }

        [Parameter("MA Type", DefaultValue = MovingAverageType.Simple)]
        public MovingAverageType maType { get; set; }

        [Parameter("Arrows", DefaultValue = true)]
        public bool arrows { get; set; }

        [Parameter("Arrow Distance", DefaultValue = 1)]
        public double arrowDistance { get; set; }

        [Parameter("MA Crossing Signal", DefaultValue = true)]
        public bool maCrossingArrow { get; set; }

        [Parameter("Filter Crossing Signal", DefaultValue = true)]
        public bool filterCrossingArrow { get; set; }


        [Output("Up Histogram 1", LineColor = "FF008080", PlotType = PlotType.Histogram, Thickness = 4)]
        public IndicatorDataSeries up1 { get; set; }

        [Output("Up Histogram 2", LineColor = "FF77B0B2", PlotType = PlotType.Histogram, Thickness = 4)]
        public IndicatorDataSeries up2 { get; set; }

        [Output("Down Histogram 1", LineColor = "FFA52A2A", PlotType = PlotType.Histogram, Thickness = 4)]
        public IndicatorDataSeries down1 { get; set; }

        [Output("Down Histogram 2", LineColor = "FFC4898A", PlotType = PlotType.Histogram, Thickness = 4)]
        public IndicatorDataSeries down2 { get; set; }

        [Output("Neutral Histogram", LineColor = "FFC0C0C0", PlotType = PlotType.Histogram, Thickness = 4)]
        public IndicatorDataSeries neutral { get; set; }

        [Output("Filter", LineColor = "FFC0C0C0", LineStyle = LineStyle.Solid, Thickness = 2)]
        public IndicatorDataSeries filter { get; set; }


        private IndicatorDataSeries max, min, dif;
        private MovingAverage ma1, ma2, ma3;
        private AverageTrueRange atr;


        protected override void Initialize()
        {
            ma1 = Indicators.MovingAverage(Bars.ClosePrices, ma1Per, maType);
            ma2 = Indicators.MovingAverage(Bars.OpenPrices, ma2Per, maType);
            ma3 = Indicators.MovingAverage(Bars.ClosePrices, ma3Per, maType);
            atr = Indicators.AverageTrueRange(atrPer * 2 - 1, MovingAverageType.Simple);
            max = CreateDataSeries();
            min = CreateDataSeries();
            dif = CreateDataSeries();
        }

        public override void Calculate(int index)
        {

            max[index] = Math.Max(Math.Max(ma1.Result[index], ma2.Result[index]), ma3.Result[index]);
            min[index] = Math.Min(Math.Min(ma1.Result[index], ma2.Result[index]), ma3.Result[index]);
            dif[index] = max[index] - min[index];
            filter[index] = atr.Result[index] * pipPerc / 100;

            var aboveFilter = dif[index] > filter[index];
            var hasCrossedFilter = aboveFilter && dif[index - 1] < filter[index - 1];
            var upTrend = ma1.Result[index] > ma2.Result[index];
            var downTrend = ma1.Result[index] < ma2.Result[index];
            var upTrendCrossing = aboveFilter && upTrend && ma1.Result[index - 1] < ma2.Result[index - 1];
            var downTrendCrossing = aboveFilter && downTrend && ma1.Result[index - 1] > ma2.Result[index - 1];
            var upTrendFilterCrossing = hasCrossedFilter && upTrend;
            var downTrendFilterCrossing = hasCrossedFilter && downTrend;

            // Indicator
            if (aboveFilter)
            {
                if (upTrend)
                {
                    if (dif[index] >= dif[index - 1])
                    {
                        up1[index] = dif[index];
                        up2[index] = double.NaN;
                        down1[index] = double.NaN;
                        down2[index] = double.NaN;
                        neutral[index] = double.NaN;
                    }
                    else
                    {
                        if (ma1.Result[index - 1] < ma2.Result[index - 1])
                        {
                            up1[index] = dif[index];
                            up2[index] = double.NaN;
                            down1[index] = double.NaN;
                            down2[index] = double.NaN;
                            neutral[index] = double.NaN;
                        }
                        else
                        {
                            up2[index] = dif[index];
                            up1[index] = double.NaN;
                            down1[index] = double.NaN;
                            down2[index] = double.NaN;
                            neutral[index] = double.NaN;
                        }
                    }
                }
                else if (downTrend)
                {
                    if (dif[index] >= dif[index - 1])
                    {
                        down1[index] = dif[index];
                        down2[index] = double.NaN;
                        up1[index] = double.NaN;
                        up2[index] = double.NaN;
                        neutral[index] = double.NaN;
                    }
                    else
                    {
                        if (ma1.Result[index - 1] > ma2.Result[index - 1])
                        {
                            down1[index] = dif[index];
                            down2[index] = double.NaN;
                            up1[index] = double.NaN;
                            up2[index] = double.NaN;
                            neutral[index] = double.NaN;
                        }
                        else
                        {
                            down2[index] = dif[index];
                            down1[index] = double.NaN;
                            up1[index] = double.NaN;
                            up2[index] = double.NaN;
                            neutral[index] = double.NaN;
                        }
                    }
                }
            }
            else
            {
                neutral[index] = dif[index];
                up1[index] = double.NaN;
                up2[index] = double.NaN;
                down1[index] = double.NaN;
                down2[index] = double.NaN;
            }

            // Arrows
            if (arrows)
            {
                if ((filterCrossingArrow && upTrendFilterCrossing) || (maCrossingArrow && upTrendCrossing))
                {
                    Chart.DrawIcon("BraidFilterUp" + Bars.OpenTimes.Last(0).ToString(), ChartIconType.UpArrow, Bars.OpenTimes.Last(0), Bars.LowPrices.Last(0) - (arrowDistance * atr.Result.Last(1)), "Teal");
                }
                else
                {
                    Chart.RemoveObject("BraidFilterUp" + Bars.OpenTimes.Last(0).ToString());
                }
                if ((filterCrossingArrow && downTrendFilterCrossing) || (maCrossingArrow && downTrendCrossing))
                {
                    Chart.DrawIcon("BraidFilterDown" + Bars.OpenTimes.Last(0).ToString(), ChartIconType.DownArrow, Bars.OpenTimes.Last(0), Bars.HighPrices.Last(0) + (arrowDistance * atr.Result.Last(1)), "Brown");
                }
                else
                {
                    Chart.RemoveObject("BraidFilterDown" + Bars.OpenTimes.Last(0).ToString());
                }
            }
        }
    }
}


danieljclsilva's avatar
danieljclsilva

Joined on 15.03.2019

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: Braid Filter Mod v1.1.algo
  • Rating: 0
  • Installs: 1536
  • Modified: 13/10/2021 09:54
Comments
Log in to add a comment.
No comments found.