// Downloaded From https://www.WiseStockTrader.com
#region Using declarations
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Xml.Serialization;
using NinjaTrader.Cbi;
using NinjaTrader.Data;
using NinjaTrader.Gui.Chart;
#endregion

// This namespace holds all indicators and is required. Do not change it.
namespace NinjaTrader.Indicator
{
    /// <summary>
    /// Enter the description of your new custom indicator here
    /// </summary>
    [Description("Enter the description of your new custom indicator here")]
    public class BollingerBandWidthPercent : Indicator
    {
        #region Variables
        // Wizard generated variables
            private int period = 14; // Default setting for Period
            private double stdDev = 2; // Default setting for StdDev
        // User defined variables (add any user defined variables below)
        #endregion

        /// <summary>
        /// This method is used to configure the indicator and is called once before any bar data is loaded.
        /// </summary>
        protected override void Initialize()
        {
            Add(new Plot(Color.FromKnownColor(KnownColor.Lime), PlotStyle.Line, "BBWidthPercent"));
            CalculateOnBarClose	= true;
            Overlay				= false;
            PriceTypeSupported	= true;
        }

        /// <summary>
        /// Called on each bar update event (incoming tick)
        /// </summary>
        protected override void OnBarUpdate()
        {
			double upperValue= Bollinger(stdDev,period).Upper[0];
			double lowerValue= Bollinger(stdDev,period).Lower[0];
			double middleValue= Bollinger(stdDev,period).Middle[0];
			double bbwp= ((upperValue/lowerValue)-1)*100;
			BBWidthPercent.Set(bbwp);
        }

        #region Properties
        [Browsable(false)]	// this line prevents the data series from being displayed in the indicator properties dialog, do not remove
        [XmlIgnore()]		// this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
        public DataSeries BBWidthPercent
        {
            get { return Values[0]; }
        }

        [Description("")]
        [Category("Parameters")]
        public int Period
        {
            get { return period; }
            set { period = Math.Max(1, value); }
        }

        [Description("")]
        [Category("Parameters")]
        public double StdDev
        {
            get { return stdDev; }
            set { stdDev = Math.Max(1, value); }
        }
        #endregion
    }
}

#region NinjaScript generated code. Neither change nor remove.
// This namespace holds all indicators and is required. Do not change it.
namespace NinjaTrader.Indicator
{
    public partial class Indicator : IndicatorBase
    {
        private BollingerBandWidthPercent[] cacheBollingerBandWidthPercent = null;

        private static BollingerBandWidthPercent checkBollingerBandWidthPercent = new BollingerBandWidthPercent();

        /// <summary>
        /// Enter the description of your new custom indicator here
        /// </summary>
        /// <returns></returns>
        public BollingerBandWidthPercent BollingerBandWidthPercent(int period, double stdDev)
        {
            return BollingerBandWidthPercent(Input, period, stdDev);
        }

        /// <summary>
        /// Enter the description of your new custom indicator here
        /// </summary>
        /// <returns></returns>
        public BollingerBandWidthPercent BollingerBandWidthPercent(Data.IDataSeries input, int period, double stdDev)
        {
            checkBollingerBandWidthPercent.Period = period;
            period = checkBollingerBandWidthPercent.Period;
            checkBollingerBandWidthPercent.StdDev = stdDev;
            stdDev = checkBollingerBandWidthPercent.StdDev;

            if (cacheBollingerBandWidthPercent != null)
                for (int idx = 0; idx < cacheBollingerBandWidthPercent.Length; idx++)
                    if (cacheBollingerBandWidthPercent[idx].Period == period && Math.Abs(cacheBollingerBandWidthPercent[idx].StdDev - stdDev) <= double.Epsilon && cacheBollingerBandWidthPercent[idx].EqualsInput(input))
                        return cacheBollingerBandWidthPercent[idx];

            BollingerBandWidthPercent indicator = new BollingerBandWidthPercent();
            indicator.BarsRequired = BarsRequired;
            indicator.CalculateOnBarClose = CalculateOnBarClose;
            indicator.Input = input;
            indicator.Period = period;
            indicator.StdDev = stdDev;
            indicator.SetUp();

            BollingerBandWidthPercent[] tmp = new BollingerBandWidthPercent[cacheBollingerBandWidthPercent == null ? 1 : cacheBollingerBandWidthPercent.Length + 1];
            if (cacheBollingerBandWidthPercent != null)
                cacheBollingerBandWidthPercent.CopyTo(tmp, 0);
            tmp[tmp.Length - 1] = indicator;
            cacheBollingerBandWidthPercent = tmp;
            Indicators.Add(indicator);

            return indicator;
        }

    }
}

// This namespace holds all market analyzer column definitions and is required. Do not change it.
namespace NinjaTrader.MarketAnalyzer
{
    public partial class Column : ColumnBase
    {
        /// <summary>
        /// Enter the description of your new custom indicator here
        /// </summary>
        /// <returns></returns>
        [Gui.Design.WizardCondition("Indicator")]
        public Indicator.BollingerBandWidthPercent BollingerBandWidthPercent(int period, double stdDev)
        {
            return _indicator.BollingerBandWidthPercent(Input, period, stdDev);
        }

        /// <summary>
        /// Enter the description of your new custom indicator here
        /// </summary>
        /// <returns></returns>
        public Indicator.BollingerBandWidthPercent BollingerBandWidthPercent(Data.IDataSeries input, int period, double stdDev)
        {
            return _indicator.BollingerBandWidthPercent(input, period, stdDev);
        }

    }
}

// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{
    public partial class Strategy : StrategyBase
    {
        /// <summary>
        /// Enter the description of your new custom indicator here
        /// </summary>
        /// <returns></returns>
        [Gui.Design.WizardCondition("Indicator")]
        public Indicator.BollingerBandWidthPercent BollingerBandWidthPercent(int period, double stdDev)
        {
            return _indicator.BollingerBandWidthPercent(Input, period, stdDev);
        }

        /// <summary>
        /// Enter the description of your new custom indicator here
        /// </summary>
        /// <returns></returns>
        public Indicator.BollingerBandWidthPercent BollingerBandWidthPercent(Data.IDataSeries input, int period, double stdDev)
        {
            if (InInitialize && input == null)
                throw new ArgumentException("You only can access an indicator with the default input/bar series from within the 'Initialize()' method");

            return _indicator.BollingerBandWidthPercent(input, period, stdDev);
        }

    }
}
#endregion