Tiger.com Windows Wiki
English
English
  • 👋Welcome!
  • ⭐How to start?
    • Recommended system requirements
    • Register and setup
    • Licenses and login
  • ⚙️Settings
    • Workspace
    • Trading modes
    • Connections
      • Crypto exchanges
        • TigerX
        • Binance via Tiger.com Broker
        • Bybit via Tiger.com Broker
        • Binance
        • Bitfinex
        • BitMEX
        • Bybit
        • OKX
        • Gate.io
        • MEXC
      • Trading terminals
        • DataFeed
        • MetaTrader 5
        • OEC Trader (GAIN Capital)
        • Rithmic
        • QUIK
        • SmartCOM
        • Trader WorkStation (Interactive Brokers)
        • Transaq Connector
    • Basic setup
      • Selecting a symbol
      • Selecting a timeframe
      • Linking windows
      • Setting up exit strategies
      • Setting offsets
    • Terminal
      • Order volumes preset
      • Configuration
        • How to create public configuration
      • General settings
      • Email notifications
      • Telegram alerts
      • Hotkeys Manager
      • Sound alerts
      • Symbols manager
  • 🖥️Windows
    • Chart
      • Chart trading
      • Toolbar setup
      • Chart theme
      • Chart settings
      • Scaling and moving the chart
      • Cluster chart
      • Cluster presets
      • Graphical objects in Chart window
        • Text
        • Ruler
        • Volume profile
        • Trend angle
        • Elliott Correction Wave
        • Elliott Impulse Wave
        • Fibonacci Fan
        • Fibonacci Extensions
        • Fibonacci Retracement
        • Fibonacci Time Zones
        • Linear Regression
      • Main indicators in Chart window
        • Bar Search
        • Bar Timer
        • Bid Ask
        • Big Trades
        • BW MFI
        • Cluster Search
        • Cluster Statistic
        • Cumulative Delta
        • Delta
        • Depth Of Market
        • Dynamic Levels
        • Elders Force Index
        • External Chart
        • Gradient
        • Heatmap
        • High Low
        • Histogram
        • Ichimoku
        • Margin Zones
        • Maximum Levels
        • Open Interest
        • Price
        • Session Color
        • Trades Flow
        • Volume
        • Volume Profile
        • VWAP
        • Weis Wave Volume
        • ZigZag
    • DOM
      • Selecting a trading account
      • Ruler
      • Multiplier
      • DOM settings
        • Main
        • Cluster
        • Trading
      • Stop Loss
      • Take Profit
      • Trigger orders
    • Watchlist
      • How to filter watchlist
    • SmartTape
    • Statistics
    • Volume search
    • All prices
    • Options board
  • 📋Tables
    • Orders
    • Executions
    • Positions
    • XPositions
    • Accounts
    • Limits
    • Player
    • Signals
    • Log
  • 🔍Video Tutorials
    • Terminal Basics
    • Charts and technical analysis
    • Charts, Trade Tape and Player
    • Trading in Tiger.com Windows
  • 💡Platform Updates
    • Version 7.0 Beta
    • Version 6.9
    • Version 6.8
    • Version 6.7
    • Version 6.6
    • Version 6.5
    • Version 6.4
    • Version 6.3
    • Version 6.2
    • Version 6.1
    • Version 6.0.2
    • Version 6.0.1
    • Version 6.0.0
    • Version 5.0.7
    • Version 4.5.15
  • ⌨️Development for Tiger.com Windows
    • Indicator examples
      • DepthOfMarket
      • Trades Flow
      • Volume Profiles
      • Cluster Search
      • Bar Search
      • VWAP
      • Bar Timer
      • Volume
      • Trades
      • Session color
      • Open Interest
      • Dynamic Levels
      • Delta
      • Cumulative Delta
      • Cluster Statistic
      • Bid Ask
      • External Chart
      • High Low
      • Histogram
    • Source examples
      • Moving Average
      • Stock
    • Examples of graphical objects
      • Fibonacci Extensions
      • Fibonacci Retracement
      • Vertical Line
      • Horizontal Line
      • Volume Profile
      • Rectangle
  • ❓Frequently Asked Questions
    • Questions about licenses
    • Questions about indicators
    • Questions about connections
    • Questions about trading
    • Error "Connection lost" when launching the terminal
    • Why do I need to set commission in the terminal?
    • How does automatic account selection work?
    • How do I optimize the terminal to improve performance?
    • How to use the Crypto license to trade on Binance
    • Which order types are available in Tiger.com?
    • How to set up chart auto-refresh for QUIK connection?
    • How do I enable Take Profit orders on Binance?
    • How does autoselection of data type work?
    • Troubles installing the terminal
    • What are the system requirements for the terminal?
    • How to provide Tiger.com Windows app logs for investigation?
    • Fixing Network Issues: How to Adjust DNS Settings
  • 📬Technical support
  • 💭Suggest an improvement
Powered by GitBook
On this page
  1. Development for Tiger.com Windows
  2. Indicator examples

VWAP

//------------------------------------------------------------------------------
//
// Indicator VWAP. Copyright (c) 2023 Tiger Trade Capital AG. All rights reserved.
//
//------------------------------------------------------------------------------
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.Serialization;
using System.Windows.Media;
using TigerTrade.Chart.Base;
using TigerTrade.Chart.Base.Enums;
using TigerTrade.Chart.Indicators.Common;
using TigerTrade.Chart.Indicators.Drawings;
using TigerTrade.Chart.Indicators.Drawings.Enums;
using TigerTrade.Chart.Indicators.Enums;
using TigerTrade.Core.Utils.Time;
 
namespace TigerTrade.Chart.Indicators.Custom
{
    [DataContract(Name = "VWAPIndicator", Namespace = "http://schemas.datacontract.org/2004/07/TigerTrade.Chart.Indicators.Custom")]
    [Indicator("X_VWAP", "*VWAP", true, Type = typeof(VWAPIndicator))]
    internal sealed class VWAPIndicator : IndicatorBase
    {
        private IndicatorPeriodType _period;
 
        [DataMember(Name = "Period")]
        [Category("Параметры"), DisplayName("Период")]
        public IndicatorPeriodType Period
        {
            get => _period;
            set
            {
                if (value == _period)
                {
                    return;
                }
 
                _period = value;
 
                StdDevCache.Clear();
 
                OnPropertyChanged();
            }
        }
 
        private TimeSpan? _startTime;
 
        [DataMember(Name = "StartTime")]
        [Category("Параметры"), DisplayName("Время")]
        public TimeSpan? StartTime
        {
            get => _startTime;
            set
            {
                if (value.Equals(_startTime))
                {
                    return;
                }
 
                _startTime = value;
 
                StdDevCache.Clear();
 
                OnPropertyChanged();
            }
        }
 
        private IndicatorPriceType _priceType;
 
        [DataMember(Name = "PriceType")]
        [Category("Параметры"), DisplayName("Цена")]
        public IndicatorPriceType PriceType
        {
            get => _priceType;
            set
            {
                if (value == _priceType)
                {
                    return;
                }
 
                _priceType = value;
 
                StdDevCache.Clear();
 
                OnPropertyChanged();
            }
        }
 
        private List<decimal> _stdDevs;
 
        [DataMember(Name = "StdDevs")]
        [Category("Параметры"), DisplayName("StdDevs")]
        public List<decimal> StdDevs
        {
            get => _stdDevs ?? (StdDevs = new List<decimal>());
            set
            {
                if (Equals(value, _stdDevs))
                {
                    return;
                }
 
                _stdDevs = value;
             
                OnPropertyChanged();
            }
        }
 
        private ChartSeries _vwapSeries;
 
        [DataMember(Name = "VWAP")]
        [Category("ChartIndicatorsCharts"), DisplayName("VWAP")]
        public ChartSeries VWAPSeries
        {
            get => _vwapSeries ?? (_vwapSeries = new ChartSeries(ChartSeriesType.Line, Colors.Blue));
            private set
            {
                if (Equals(value, _vwapSeries))
                {
                    return;
                }
 
                _vwapSeries = value;
 
                OnPropertyChanged();
            }
        }
 
        private ChartSeries _stdDevSeries;
     
        [DataMember(Name = "StdDev")]
        [Category("ChartIndicatorsCharts"), DisplayName("StdDev")]
        public ChartSeries StdDevSeries
        {
            get => _stdDevSeries ?? (_stdDevSeries = new ChartSeries(ChartSeriesType.Line, Colors.Red));
            private set
            {
                if (Equals(value, _stdDevSeries))
                {
                    return;
                }
 
                _stdDevSeries = value;
 
                OnPropertyChanged();
            }
        }
 
        public VWAPIndicator()
        {
            Period = IndicatorPeriodType.Day;
 
            StartTime = null;
 
            PriceType = IndicatorPriceType.Median;
 
            StdDevs.Add(1);
            StdDevs.Add(2);
        }
 
        private List<double> _stdDevCache;
     
        private List<double> StdDevCache => _stdDevCache ?? (_stdDevCache = new List<double>(1000));
 
        protected override void Execute()
        {
            var date = Helper.Date;
            var price = Helper.Price(_priceType);
            var vol = Helper.Volume;
 
            var vwap = new double[date.Length];
            var stdDev = new double[date.Length];
            var splits = new bool[date.Length];
 
            var calcStdDevs = StdDevs.Count > 0;
 
            if (date.Length < StdDevCache.Count)
            {
                StdDevCache.Clear();
            }
 
            var lastSequence = -1;
 
            var cumVolumePrice = 0.0;
            var cumVolume = 0.0;
 
            var newDayIndex = 0;
 
            double timeOffset;
 
            if (StartTime.HasValue)
            {
                var oaBaseDate = DateTime.FromOADate(0);
 
                timeOffset = -oaBaseDate.Add(StartTime.Value).ToOADate();
            }
            else
            {
                timeOffset = TimeHelper.GetSessionOffset(DataProvider.Symbol.Exchange);
            }
 
            for (var i = 0; i < date.Length; i++)
            {
                var sequence = -1;
 
                switch (Period)
                {
                    case IndicatorPeriodType.Day:
 
                        sequence = DataProvider.Period.GetSequence(ChartPeriodType.Day, 1, date[i], timeOffset);
 
                        break;
 
                    case IndicatorPeriodType.Week:
 
                        sequence = DataProvider.Period.GetSequence(ChartPeriodType.Week, 1, date[i], timeOffset);
 
                        break;
 
                    case IndicatorPeriodType.Month:
 
                        sequence = DataProvider.Period.GetSequence(ChartPeriodType.Month, 1, date[i], timeOffset);
 
                        break;
                }
 
                if (sequence != lastSequence)
                {
                    lastSequence = sequence;
 
                    newDayIndex = i;
 
                    cumVolumePrice = 0.0;
                    cumVolume = 0.0;
 
                    splits[i] = true;
                }
 
                cumVolumePrice += price[i] * vol[i];
                cumVolume += vol[i];
 
                if (cumVolume == 0)
                {
                    continue;
                }
 
                vwap[i] = cumVolumePrice / cumVolume;
 
                if (calcStdDevs)
                {
                    if (i < StdDevCache.Count)
                    {
                        stdDev[i] = StdDevCache[i];
                    }
                    else
                    {
                        var variance = 0.0;
 
                        for (var j = newDayIndex; j < i; j++)
                        {
                            variance += (vol[j]/cumVolume)*(price[j] - vwap[i])*(price[j] - vwap[i]);
                        }
 
                        stdDev[i] = Math.Sqrt(variance);
 
                        StdDevCache.Add(stdDev[i]);
                    }
                }
            }
 
            var vwapSeries = new IndicatorSeriesData(vwap, VWAPSeries)
            {
                Style =
                {
                    DisableMinMax = true
                }
            };
 
            Series.Add(vwapSeries);
 
            if (calcStdDevs)
            {
                foreach (var dev in StdDevs)
                {
                    if (dev <= 0)
                    {
                        continue;
                    }
 
                    var stdDevPos = new double[date.Length];
                    var stdDevNeg = new double[date.Length];
 
                    var d = (double) dev;
 
                    for (var i = 0; i < date.Length; i++)
                    {
                        stdDevPos[i] = vwap[i] + stdDev[i] * d;
                        stdDevNeg[i] = vwap[i] - stdDev[i] * d;
                    }
 
                    var stdDevPosSeries = new IndicatorSeriesData(stdDevPos, StdDevSeries)
                    {
                        Style = {DisableMinMax = true}
                    };
 
                    var stdDevNegSeries = new IndicatorSeriesData(stdDevNeg, StdDevSeries)
                    {
                        Style = {DisableMinMax = true}
                    };
 
                    Series.Add(stdDevPosSeries, stdDevNegSeries);
                }
            }
 
            foreach (var series in Series)
            {
                series.UserData["S"] = splits;
                series.UserData["SE"] = false;
            }
        }
 
        public override void ApplyColors(IChartTheme theme)
        {
            VWAPSeries.AllColors = theme.GetNextColor();
            StdDevSeries.AllColors = theme.GetNextColor();
 
            base.ApplyColors(theme);
        }
 
        public override void CopyTemplate(IndicatorBase indicator, bool style)
        {
            var i = (VWAPIndicator)indicator;
 
                       Period = i.Period;
            PriceType = i.PriceType;
 
            StdDevs = i.StdDevs.ToList();
 
            VWAPSeries.CopyTheme(i.VWAPSeries);
            StdDevSeries.CopyTheme(i.StdDevSeries);
 
            base.CopyTemplate(indicator, style);
        }
    }
}
PreviousBar SearchNextBar Timer

Last updated 2 years ago

⌨️