Category Trend  Published on 05/07/2022

VWAP (Volume Weighted Average Price)

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,

David Wilson-Parr.


// -------------------------------------------------------------------------------------------------
//
//    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...");
        }
    }
}


CT
ctid418503

Joined on 04.05.2018

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: VWAP.algo
  • Rating: 5
  • Installs: 17074
  • Modified: 13/10/2021 09:54
Comments
Log in to add a comment.
TH
thibauldr · 2 years ago

Be very careful, this indicator is everything but not a VWAP and calculation is wrong! 

S.
s.singh.au · 2 years ago

Hi, thanks for this indicator, only one issue, the indicator does keep current.  That is, it get frozen at a particular 'time' & only I can it up-to-date is to click on another time frame & back to my original time frame.

CR
crobbeandersson · 4 years ago

This indicator needs to be redone to calculate the whole forexsession, starting from Sydney open to NY close, that is why it shows different then for example Trading View and MT4.

RO
robert.vanderlaan · 5 years ago

Not VWAP..

- Virtually identical to identical to a MA of the same period
- When period set to "0", gives very different results than on any other platform,
like Metatrader or Tradingview.


(example: look at GBP/JPY during that huge up-swing, creating a strange hard corner
in this VWAP, vs on TradingView)

ON
oneb700d@gmail.com · 5 years ago

Do you still update this indicator?

It seems to stop working when set to ODO. Works fine when set to normal.

CT
ctid418503 · 6 years ago

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.

CT
ctid418503 · 6 years ago

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.  

CT
ctid418503 · 6 years ago

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

CT
ctid418503 · 6 years ago

I just noticed that there is a commercial (with free trial) VWAP indicator available here https://ctrader.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.