Category Trend  Published on 05/03/2020

VWAP Oscillator

Description

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

namespace cAlgo
{
    [Levels(0, 1, 2, 3, -1, -2, -3)]
    [Indicator(IsOverlay = false, ScalePrecision = 1, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class VWAPOscilattor : Indicator
    {
        [Parameter(DefaultValue = 5)]
        public int Thickness { get; set; }
        [Parameter(DefaultValue = 11)]
        public TimeFrame TF { get; set; }

        [Output("Open", PlotType = PlotType.Points, LineColor = "Black")]
        public IndicatorDataSeries Open { get; set; }
        [Output("High", PlotType = PlotType.Points, LineColor = "Black")]
        public IndicatorDataSeries High { get; set; }
        [Output("Loe", PlotType = PlotType.Points, LineColor = "Black")]
        public IndicatorDataSeries Low { get; set; }
        [Output("Close", PlotType = PlotType.Points, LineColor = "Black")]
        public IndicatorDataSeries Close { get; set; }

        [Output("0", LineColor = "Gray")]
        public IndicatorDataSeries ZeroLine { get; set; }
        [Output("1", LineColor = "FFFF999A")]
        public IndicatorDataSeries FirstUpperBand { get; set; }
        [Output("2", LineColor = "FFFF5B62")]
        public IndicatorDataSeries SecondUpperBand { get; set; }
        [Output("3", LineColor = "FFFF0000")]
        public IndicatorDataSeries ThirdUpperBand { get; set; }
        [Output("-1", LineColor = "FF9CFF9C")]
        public IndicatorDataSeries FirstLowerBand { get; set; }
        [Output("-2", LineColor = "FF4EFF4E")]
        public IndicatorDataSeries SecondLowerBand { get; set; }
        [Output("-3", LineColor = "FF00FF00")]
        public IndicatorDataSeries ThirdLowerBand { get; set; }

        Bars Series;

        protected override void Initialize()
        {
            Series = MarketData.GetBars(TF);
        }

        public override void Calculate(int index)
        {
            double vwapValue = 0, volumesSum = 0;
            int startIndex = Bars.OpenTimes.GetIndexByTime(Series.OpenTimes[Series.OpenTimes.GetIndexByTime(Bars.OpenTimes[index])]);

            for (int i = index; i >= startIndex; i--)
            {
                vwapValue += Bars[i].Close * Bars[i].TickVolume;
                volumesSum += Bars[i].TickVolume;
            }

            vwapValue /= volumesSum;

            double vwapSTD = 0;

            for (int i = index; i >= startIndex; i--)
            {
                vwapSTD += Math.Pow(Bars[i].Close - vwapValue, 2);
            }
            vwapSTD = Math.Sqrt(vwapSTD / (index - startIndex + 1));

            Close[index] = (Bars[index].Close - vwapValue) / vwapSTD;
            Open[index] = (Bars[index].Open - vwapValue) / vwapSTD;
            High[index] = (Bars[index].High - vwapValue) / vwapSTD;
            Low[index] = (Bars[index].Low - vwapValue) / vwapSTD;

            Close[index] = double.IsInfinity(Close[index]) ? 0 : Close[index];
            Open[index] = double.IsInfinity(Open[index]) ? 0 : Open[index];
            High[index] = double.IsInfinity(High[index]) ? 0 : High[index];
            Low[index] = double.IsInfinity(Low[index]) ? 0 : Low[index];

            Close[index] = Close[index] > 5 ? 5 : Close[index] < -5 ? -5 : Close[index];
            Open[index] = Open[index] > 5 ? 5 : Open[index] < -5 ? -5 : Open[index];
            High[index] = High[index] > 5 ? 5 : High[index] < -5 ? -5 : High[index];
            Low[index] = Low[index] > 5 ? 5 : Low[index] < -5 ? -5 : Low[index];

            IndicatorArea.DrawTrendLine("HL " + index, index, High[index], index, Low[index], Close[index] > Open[index] ? Color.Green : Color.Red);
            IndicatorArea.DrawTrendLine("Body " + index, index, Close[index], index, Open[index], Close[index] > Open[index] ? Color.Green : Color.Red, Thickness);

            ZeroLine[index] = 0;
            FirstLowerBand[index] = -1;
            FirstUpperBand[index] = 1;
            SecondLowerBand[index] = -2;
            SecondUpperBand[index] = 2;
            ThirdLowerBand[index] = -3;
            ThirdUpperBand[index] = 3;
        }
    }
}


CY
cysecsbin.01

Joined on 10.11.2018 Blocked

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