VWAP (Volume Weighted Average Price)

by ctid418503 in category Trend at 05/05/2018
Description

I couldn't find a VWAP indicator for Ctrader so programmed one and am sharing it with the community.  There is lots of information about this indicator on-line. VWAP indicator not to be confused with VWAP execution.

Parameters deserve some explanation:

  • One day only?  If this is set to true/yes then the VWAP is only calculated and shown on the chart for the current day.  I would suggest using this setting if you are using this indicator in a cbot because performance (speed) will be improved in backtesting and optimisation.  Default: false.  When set to false you will see the VWAP day by day.  The calculation resets each day.
  • Periods.  Default: 0.  If you set a value other than 0 for this parameter it completely overrides the day or 'day by day' calculation and calculates the VWAP on a rolling basis for the the number of periods you choose.

I'll try to respond to any comments.  Also please share any good strategies you have using this indicator.

Donations welcome to:
BTC: 33gjtYhKVqFxmcbcko63WnwiVJvew3PauQ
ETH: 0xb54dF35117D94a43Ca25A3A348Ac20DF7F667F7b
LTC: M8YRuyH5USv2MvJyyF55U5ik1yMfm6TtMH

Cheers,

Zaknafein Z.

Download
784 downloads
How to install
Notification Publishing copyrighted material is strictly prohibited. If you believe there is copyrighted material in this section you may use the Copyright Infringement Notification form to submit a claim.
Formula / Source Code
Language: C#
Trading Platform: cAlgocTrader
// -------------------------------------------------------------------------------------------------
//
//    VWAP (Volume Weighted Average Price) = Cumulative(Typical Price x Volume) / Cumulative(Volume)
//    by Zaknafein Z
//
//    Donations welcome to:
//    
//    BTC: 33gjtYhKVqFxmcbcko63WnwiVJvew3PauQ
//    ETH: 0xb54dF35117D94a43Ca25A3A348Ac20DF7F667F7b
//    LTC: M8YRuyH5USv2MvJyyF55U5ik1yMfm6TtMH
//
//    v1.0 Inital version 04/05/18
// -------------------------------------------------------------------------------------------------

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

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.EasternStandardTime, AutoRescale = false, AccessRights = AccessRights.None)]
    public class VWAP : Indicator
    {
        [Parameter(DefaultValue = 0)]
        public int Periods { get; set; }

        [Parameter("One day only?", DefaultValue = false)]
        public bool Odo { get; set; }

        [Output("Main", Color = Colors.DarkOrchid)]
        public IndicatorDataSeries Result { get; set; }

        public override void Calculate(int index)
        {
            int ii = index;
            double CumTypPrice = 0;
            double CumVol = 0;

            if (Periods == 0)
            {
                while (MarketSeries.OpenTime[ii] >= (Odo == false ? MarketSeries.OpenTime[ii].Date : DateTime.Now.Date) && ii != 0)
                {
                    CumTypPrice += MarketSeries.Typical[ii] * MarketSeries.TickVolume[ii];
                    CumVol += MarketSeries.TickVolume[ii];
                    ii--;
                    if (MarketSeries.OpenTime[ii].Hour == 0 && MarketSeries.OpenTime[ii].Minute == 0)
                        break;
                }
            }
            else
            {
                for (; ii >= MarketSeries.OpenTime.Count - Periods; ii--)
                {
                    CumTypPrice += MarketSeries.Typical[ii] * MarketSeries.TickVolume[ii];
                    CumVol += MarketSeries.TickVolume[ii];
                }
            }

            Result[index] = CumTypPrice / CumVol;

        }

        protected override void Initialize()
        {
            Print("VWAP indicator started...");
        }
    }
}
Comments

ctid418503 - June 28, 2018 @ 21:25

I just noticed that there is a commercial (with free trial) VWAP indicator available here https://ctdn.com/algos/indicators/show/1572 and I was interested to see if this gives the same results.  I was going to install the trial version but it asked for full access rights for no reason so I didn't go ahead.

However I can see from the screenshot posted there that my free VWAP indicator is not giving the same results.   This is because  of the bit in my code:

TimeZones.UTC

which means it is considering the start of the day (and therefore when to reset and start calculating the values) as 00:00 CET.  If you just change this to  "TimeZones.EasternStandardTime" it gives the same results from what I can see comparing it to the picture.  You can also influence it by changing the line:

if (MarketSeries.OpenTime[ii].Hour == 0 && MarketSeries.OpenTime[ii].Minute == 0)

to say some other value than 0, i.e. if you change both 0s to 5s then that means it will reset and restart calulating at the beginning of the 5th hour of the day according to the timezone specified.

I am not actually sure what the 'standard' is for VWAP (whether CET or EST or whatever) but it can make a massive difference in the results so you have to be aware of it.

 

ctid418503 - June 28, 2018 @ 21:36

Actually I updated it to EST because that's probably more standard.

ctid418503 - June 28, 2018 @ 21:44

Doh, when I said "it is considering the start of the day (and therefore when to reset and start calculating the values) as 00:00 CET", I meant to say "UTC".  Anyhow I've reuploaded it now and it defaults to EST which is probably more normal, but set it how you want.  

ctid418503 - June 28, 2018 @ 21:47

PS. I'm working on a cBot using this indicator and getting quite positive results already in backtesting.  I'll probably upload it here when I've fettled with it a bit more.

0