Preface

In financial trading, having reliable benchmarks and strategies is essential for success. One such crucial benchmark is the Volume Weighted Average Price (VWAP). This post will explore how to detect outlier using Inter quartile Range (IQR) Method.

What is VWAP?

VWAP (Volume Weighted Average Price) is a trading benchmark used by traders and analysts that gives the average price traded at throughout the day, based on both volume and price. VWAP provides a more accurate measure of the average price of a security than a simple average price calculation, as it weights the price by the volume traded at each price level. vwap-outlier-dection-formula

Calculation of VWAP

The VWAP is calculated by taking the total value of trades (price multiplied by the volume) and dividing it by the total volume of trades over a specific period. The formula can be expressed as:

Steps to Calculate VWAP

  • Collect Trade Data: Gather the price and volume of each trade over the desired time period.
  • Compute Cumulative Values:
    • Cumulative total value: Sum of the products of price and volume for each trade.
    • Cumulative total volume: Sum of the volumes for each trade.
  • Apply the Formula: Divide the cumulative total value by the cumulative total volume to get the VWAP.

Example

Suppose you have the following trades: vwap-outlier-detection-calculation

Interquartile Range (IQR) Method

Extract Trade Prices

  • Extract Trade Prices: First, the function extracts the prices from the Trade objects into a slice of floats.
  • Sort Prices: The prices are sorted in ascending order. Sorting is necessary to calculate the quartiles correctly.

Calculate Q1, Q3, and IQR

  • Q1: (first quartile) is the median of the lower half of the sorted prices.
  • Q3: (third quartile) is the median of the upper half of the sorted prices.
  • IQR (interquartile range): is calculated as ( Q3 - Q1 ).

Filter Out Outliers

Trades are considered outliers if their prices are below ( Q1 - 1.5 \times IQR ) or above ( Q3 + 1.5 \times IQR ). The function loops through the trades and filters out the ones that fall outside this range.

Applications of VWAP

  • Trading Strategy: Traders use VWAP to ensure they execute trades in line with the average market price, aiming to buy below the VWAP and sell above it.
  • Performance Benchmark: VWAP is used to assess the quality of trade executions. If the execution price is better than the VWAP, it indicates a good trade.
  • Institutional Trading: Large institutional traders use VWAP to minimize market impact when executing large orders.
Example using Golang - VWAP Outlier Detection
import (
	"github.com/mdshahjahanmiah/vwap-outlier-detection/pkg/trade"
	"log/slog"
	"time"
)

func main() {
	// Initialize trade windows for BTC/USD and ETH/USD pairs
	btcWindow := trade.NewTradeWindow()
	ethWindow := trade.NewTradeWindow()

	// Simulate adding trades (this would be replaced with actual trade data in a real application)
	trades := []trade.Trade{
		{Timestamp: time.Now().Add(-1 * time.Minute), Pair: "BTC/USD", Price: 50000, Volume: 1},
		{Timestamp: time.Now().Add(-1 * time.Minute), Pair: "BTC/USD", Price: 51000, Volume: 1.5},
		{Timestamp: time.Now().Add(-1 * time.Minute), Pair: "BTC/USD", Price: 52000, Volume: 2},
		{Timestamp: time.Now().Add(-1 * time.Minute), Pair: "BTC/USD", Price: 53000, Volume: 2.5},
		{Timestamp: time.Now().Add(-1 * time.Minute), Pair: "BTC/USD", Price: 54000, Volume: 3},
		{Timestamp: time.Now().Add(-1 * time.Minute), Pair: "ETH/USD", Price: 3000, Volume: 10},
		{Timestamp: time.Now().Add(-1 * time.Minute), Pair: "ETH/USD", Price: 3100, Volume: 15},
		{Timestamp: time.Now().Add(-1 * time.Minute), Pair: "ETH/USD", Price: 3200, Volume: 20},
		{Timestamp: time.Now().Add(-1 * time.Minute), Pair: "ETH/USD", Price: 3300, Volume: 25},
		{Timestamp: time.Now().Add(-1 * time.Minute), Pair: "ETH/USD", Price: 3400, Volume: 30},
	}

	// Separate trades by pairs
	btcTrades, ethTrades := trade.ProcessTradesByPair(trades)

	// Channels to receive the Volume Weighted Average Price results
	btcWeightedAveragePriceChan := make(chan float64)
	ethWeightedAveragePriceChan := make(chan float64)

	// Go routine to process BTC/USD trades
	go func() {
		btcWindow.AddTrades(btcTrades)
		btcValidTrades := btcWindow.GetValidTrades()
		btcWeightedAveragePrice := trade.CalculateVolumeWeightedAveragePrice(btcValidTrades)
		btcWeightedAveragePriceChan <- btcWeightedAveragePrice
	}()

	// Go routine to process ETH/USD trades
	go func() {
		ethWindow.AddTrades(ethTrades)
		ethValidTrades := ethWindow.GetValidTrades()
		ethWeightedAveragePrice := trade.CalculateVolumeWeightedAveragePrice(ethValidTrades)
		ethWeightedAveragePriceChan <- ethWeightedAveragePrice
	}()

	// Receive and print the Volume Weighted Average Price results
	btcWeightedAveragePrice := <-btcWeightedAveragePriceChan
	ethWeightedAveragePrice := <-ethWeightedAveragePriceChan

	slog.Info("volume weighted average price", "BTC/USD", btcWeightedAveragePrice)
	slog.Info("volume weighted average price", "ETH/USD", ethWeightedAveragePrice)
}

For the full code, please visit gitHub repository, Trading Outlier Detector

Limitations of VWAP

  • Lagging Indicator: VWAP is a lagging indicator, meaning it reflects past prices and volumes. It does not predict future price movements.
  • Intraday Use: VWAP is typically used for intraday analysis. Its relevance decreases over longer periods.
  • Sensitivity to Outliers: VWAP can be influenced by large trades (outliers), which might distort the average price.

Conclusion

Volume Weighted Average Price (VWAP) is more than just a trading benchmark. It is a strategic tool that aids traders in minimizing market impact and making informed trading decisions.

NOTE: I'm constantly delighted to receive feedback. Whether you spot an error, have a suggestion for improvement, or just want to share your thoughts, please don't hesitate to comment/reach out. I truly value connecting with readers!