Category Trend  Published on 27/02/2020

VWAP with Bands

Description

This is just a simple multitimeframe VWAP with deviation bands


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

namespace cAlgo
{
    [Indicator(IsOverlay = true, AutoRescale = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class VWAP : Indicator
    {
        [Parameter("TimeFrame")]
        public TimeFrame TF { get; set; }
        [Parameter("Show Interruptions", DefaultValue = false)]
        public bool ShowInterruptions { get; set; }

        [Parameter("Show Standard Deviations", Group = "Standard Deviations", DefaultValue = false)]
        public bool ShowSTDs { get; set; }
        [Parameter("First Multiplier", Group = "Standard Deviations", DefaultValue = 1)]
        public double STD1M { get; set; }
        [Parameter("Second Multiplier", Group = "Standard Deviations", DefaultValue = 2)]
        public double STD2M { get; set; }
        [Parameter("Third Multiplier", Group = "Standard Deviations", DefaultValue = 3)]
        public double STD3M { get; set; }

        [Output("Main", Color = Colors.Orange, Thickness = 2, PlotType = PlotType.DiscontinuousLine)]
        public IndicatorDataSeries Result { get; set; }

        [Output("Upper Deviation 1", Color = Colors.RoyalBlue, Thickness = 2, PlotType = PlotType.DiscontinuousLine)]
        public IndicatorDataSeries STD1U { get; set; }
        [Output("Lower Deviation 1", Color = Colors.RoyalBlue, Thickness = 2, PlotType = PlotType.DiscontinuousLine)]
        public IndicatorDataSeries STD1D { get; set; }
        [Output("Upper Deviation 2", Color = Colors.RoyalBlue, Thickness = 2, PlotType = PlotType.DiscontinuousLine)]
        public IndicatorDataSeries STD2U { get; set; }
        [Output("Lower Deviation 2", Color = Colors.RoyalBlue, Thickness = 2, PlotType = PlotType.DiscontinuousLine)]
        public IndicatorDataSeries STD2D { get; set; }
        [Output("Upper Deviation 3", Color = Colors.RoyalBlue, Thickness = 2, PlotType = PlotType.DiscontinuousLine)]
        public IndicatorDataSeries STD3U { get; set; }
        [Output("Lower Deviation 3", Color = Colors.RoyalBlue, Thickness = 2, PlotType = PlotType.DiscontinuousLine)]
        public IndicatorDataSeries STD3D { get; set; }

        [Parameter("Build Lines", Group = "Lines", DefaultValue = false)]
        public bool BuilLines { get; set; }

        public IndicatorDataSeries tpv;
        private MarketSeries Series;
        private int LastStartIndex = 0;

        protected override void Initialize()
        {
            Series = MarketData.GetSeries(TF);
            tpv = CreateDataSeries();
        }

        public override void Calculate(int index)
        {
            tpv[index] = MarketSeries.Close[index] * MarketSeries.TickVolume[index];

            int startIndex = MarketSeries.OpenTime.GetIndexByTime(Series.OpenTime[Series.OpenTime.GetIndexByTime(MarketSeries.OpenTime[index])]);

            if (startIndex > LastStartIndex)
            {
                if (ShowInterruptions)
                    Chart.DrawIcon("Interruption " + startIndex, ChartIconType.Diamond, startIndex, Result[startIndex - 1], Color.Yellow);
            }

            double cptv = 0, cv = 0;

            for (int i = index; i >= startIndex; --i)
            {
                cptv += tpv[i];
                cv += MarketSeries.TickVolume[i];
            }

            Result[index] = cptv / cv;

            //Standard deviation

            if (ShowSTDs)
            {
                double squaredErrors = 0;

                for (int i = index; i >= startIndex; --i)
                {
                    squaredErrors += Math.Pow(MarketSeries.Close[i] - Result[index], 2);
                }

                squaredErrors /= (index - startIndex + 1);
                squaredErrors = Math.Sqrt(squaredErrors);

                STD1U[index] = squaredErrors * STD1M + Result[index];
                STD1D[index] = Result[index] - squaredErrors * STD1M;
                STD2U[index] = squaredErrors * STD2M + Result[index];
                STD2D[index] = Result[index] - squaredErrors * STD2M;
                STD3U[index] = squaredErrors * STD3M + Result[index];
                STD3D[index] = Result[index] - squaredErrors * STD3M;
            }

            //Build lines

            if (BuilLines)
                Chart.DrawTrendLine("Fixed " + startIndex, startIndex, Result[startIndex - 1], index, Result[startIndex - 1], Color.Yellow);
        }
    }
}


CY
cysecsbin.01

Joined on 10.11.2018 Blocked

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: VWAP.algo
  • Rating: 5
  • Installs: 4681
Comments
Log in to add a comment.
shane.scott.pub's avatar
shane.scott.pub · 1 year ago

Thanks for the work put in and I will definitely give it a bash and see what you have created. Thank you for your contributions. :)

shane.scott.pub's avatar
shane.scott.pub · 1 year ago

@moiz.forex You're gonna have to be a bit more specific. An image link would be ideal. But the short answer is that this is not tradingview and the goal isn't to be tradingview. However if there is an element to the indicator on another platform that you would l;like to point out as a suggestion then pls include the name and author of the indicator, a link to an image marked up with suggested areas of interest as well as textual description in as much detail as possible as to the exact functionality that you are suggesting incorporating. Simply saying "its not like tradingview one" is not very useful. Thanks.

MO
moiz.forex · 1 year ago

Hi Bot guys, My question is why i dont see the upper and lower band in Vwap as i see that in trading view can someone tell me what i am doing wrong. this indicator shows everything and all bands but how can i use this indicator as what i see in trading view

JS
JS15 · 4 years ago

Hi there, a generous person on the cTrader Telegram who goes by the name /DarthMaulAtWork/ showed me what to do. Thanks again!

I copied and pasted the code below. It only highlights the 1sd but if you add:

[Cloud("Upper Deviation 2", "Lower Deviation 2")]

and/or

[Cloud("Upper Deviation 3", "Lower Deviation 3")]

below [Cloud("Upper Deviation 1", "Lower Deviation 1")]

you can shade the 2nd and 3rd Std Deviation bands too.

Thank you for the coding of this indicator, it is amazing!!!

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

namespace cAlgo
{
    [Cloud("Upper Deviation 1", "Lower Deviation 1")]
    [Indicator(IsOverlay = true, AutoRescale = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class VWAPCloud : Indicator
    {
        [Parameter("TimeFrame")]
        public TimeFrame TF { get; set; }
        [Parameter("Show Interruptions", DefaultValue = false)]
        public bool ShowInterruptions { get; set; }

        [Parameter("Show Standard Deviations", Group = "Standard Deviations", DefaultValue = false)]
        public bool ShowSTDs { get; set; }
        [Parameter("First Multiplier", Group = "Standard Deviations", DefaultValue = 1)]
        public double STD1M { get; set; }
        [Parameter("Second Multiplier", Group = "Standard Deviations", DefaultValue = 2)]
        public double STD2M { get; set; }
        [Parameter("Third Multiplier", Group = "Standard Deviations", DefaultValue = 3)]
        public double STD3M { get; set; }

        [Output("Main", Color = Colors.Orange, Thickness = 2, PlotType = PlotType.DiscontinuousLine)]
        public IndicatorDataSeries Result { get; set; }

        [Output("Upper Deviation 1", Color = Colors.RoyalBlue, Thickness = 2, PlotType = PlotType.DiscontinuousLine)]
        public IndicatorDataSeries STD1U { get; set; }
        [Output("Lower Deviation 1", Color = Colors.RoyalBlue, Thickness = 2, PlotType = PlotType.DiscontinuousLine)]
        public IndicatorDataSeries STD1D { get; set; }
        [Output("Upper Deviation 2", Color = Colors.RoyalBlue, Thickness = 2, PlotType = PlotType.DiscontinuousLine)]
        public IndicatorDataSeries STD2U { get; set; }
        [Output("Lower Deviation 2", Color = Colors.RoyalBlue, Thickness = 2, PlotType = PlotType.DiscontinuousLine)]
        public IndicatorDataSeries STD2D { get; set; }
        [Output("Upper Deviation 3", Color = Colors.RoyalBlue, Thickness = 2, PlotType = PlotType.DiscontinuousLine)]
        public IndicatorDataSeries STD3U { get; set; }
        [Output("Lower Deviation 3", Color = Colors.RoyalBlue, Thickness = 2, PlotType = PlotType.DiscontinuousLine)]
        public IndicatorDataSeries STD3D { get; set; }

        [Parameter("Build Lines", Group = "Lines", DefaultValue = false)]
        public bool BuilLines { get; set; }

        public IndicatorDataSeries tpv;
        private MarketSeries Series;
        private int LastStartIndex = 0;

        protected override void Initialize()
        {
            Series = MarketData.GetSeries(TF);
            tpv = CreateDataSeries();
        }

        public override void Calculate(int index)
        {
            tpv[index] = MarketSeries.Close[index] * MarketSeries.TickVolume[index];

            int startIndex = MarketSeries.OpenTime.GetIndexByTime(Series.OpenTime[Series.OpenTime.GetIndexByTime(MarketSeries.OpenTime[index])]);

            if (startIndex > LastStartIndex)
            {
                if (ShowInterruptions)
                    Chart.DrawIcon("Interruption " + startIndex, ChartIconType.Diamond, startIndex, Result[startIndex - 1], Color.Yellow);
            }

            double cptv = 0, cv = 0;

            for (int i = index; i >= startIndex; --i)
            {
                cptv += tpv[i];
                cv += MarketSeries.TickVolume[i];
            }

            Result[index] = cptv / cv;

            //Standard deviation

            if (ShowSTDs)
            {
                double squaredErrors = 0;

                for (int i = index; i >= startIndex; --i)
                {
                    squaredErrors += Math.Pow(MarketSeries.Close[i] - Result[index], 2);
                }

                squaredErrors /= (index - startIndex + 1);
                squaredErrors = Math.Sqrt(squaredErrors);

                STD1U[index] = squaredErrors * STD1M + Result[index];
                STD1D[index] = Result[index] - squaredErrors * STD1M;
                STD2U[index] = squaredErrors * STD2M + Result[index];
                STD2D[index] = Result[index] - squaredErrors * STD2M;
                STD3U[index] = squaredErrors * STD3M + Result[index];
                STD3D[index] = Result[index] - squaredErrors * STD3M;
            }

            //Build lines

            if (BuilLines)
                Chart.DrawTrendLine("Fixed " + startIndex, startIndex, Result[startIndex - 1], index, Result[startIndex - 1], Color.Yellow);
        }
    }
}

JS
JS15 · 4 years ago

Hi there,

Thank you for this awesome indicator!

Ctrader now enables you to draw semi-transparent clouds between two indicator lines. I was wondering if you could draw a cloud between the Upper and Lower 1SD VWAP Bands if possible.

Happy to pay for this to be done.

Please let me know.

Love your work.

Thank you

JS