Category Trend  Published on 27/06/2023

BollingerBands Trend Flat indicator

Description

This custom indicator is derived from the Bollinger Bands indicator by normalizing the deviation bands and comparing them with the original deviation bands. Use this indicator to identify the trend and flat market price conditions.

For a bullish trend sentiment, the indicator (green component) shows a +1 value, and for a bearish trend sentiment, the indicator (red component) shows a -1 value. In a flat market sentiment, both the bullish and bearish components have a value of zero.


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

namespace cAlgo
{
    [Levels(0)]
    [Indicator(IsOverlay = false, AccessRights = AccessRights.None)]
    public class mBBtrendflat : Indicator
    {
        [Parameter("Periods (20)", DefaultValue = 20)]
        public int inpPeriod { get; set; }
        [Parameter("Standard Deviation (2.0)", DefaultValue = 2)]
        public double inpDeviation { get; set; }
        [Parameter("Smooth Type (sma)", DefaultValue = MovingAverageType.Simple)]
        public MovingAverageType inpSmoothType { get; set; }
        [Parameter("Use FIR filter (no)", DefaultValue = false)]
        public bool inpUseFIRfilter { get; set; }

        [Output("Trend Up", LineColor = "Green", PlotType = PlotType.Histogram, LineStyle = LineStyle.Solid, Thickness = 3)]
        public IndicatorDataSeries outUp { get; set; }
        [Output("Trend Down", LineColor = "Red", PlotType = PlotType.Histogram, LineStyle = LineStyle.Solid, Thickness = 3)]
        public IndicatorDataSeries outDown { get; set; }

        private BollingerBands _bbands;
        private IndicatorDataSeries _price, _min, _max;

        protected override void Initialize()
        {
            _price = CreateDataSeries();
            _min = CreateDataSeries();
            _max = CreateDataSeries();
            _bbands = Indicators.BollingerBands(_price, inpPeriod, inpDeviation, inpSmoothType);
        }

        public override void Calculate(int i)
        {
            _price[i] = i>3 && inpUseFIRfilter == true ? (Bars.ClosePrices[i] + 2.0 * Bars.ClosePrices[i-1] + 2.0 * Bars.ClosePrices[i-2] + Bars.ClosePrices[i-3]) / 6.0 : Bars.ClosePrices[i];
            _max[i] = i>inpPeriod ? Math.Max(_bbands.Top.Maximum(inpPeriod), _bbands.Bottom.Maximum(inpPeriod)) : _bbands.Top[i];
            _min[i] = i>inpPeriod ? Math.Min(_bbands.Top.Minimum(inpPeriod), _bbands.Bottom.Minimum(inpPeriod)) : _bbands.Bottom[i];
            
            outUp[i] = _bbands.Top[i] == _max[i] && Bars.ClosePrices[i] > _bbands.Main[i] ? +1 : 0;
            outDown[i] = _bbands.Bottom[i] == _min[i] && Bars.ClosePrices[i] < _bbands.Main[i] ? -1 : 0;
        }
    }
}


mfejza's avatar
mfejza

Joined on 25.01.2022

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: mBBtrendflat.algo
  • Rating: 5
  • Installs: 676
  • Modified: 27/06/2023 11:32
Comments
Log in to add a comment.
No comments found.