Strategy vault / Trend following
Volume Weighted Average Price Strategy
The Volume Weighted Average Price (VWAP) strategy is a strategy that tracks the average price of a stock over a specified time. The strategy uses VWAP as a benchmark and takes long or short positions when the price…

Overview
The Volume Weighted Average Price (VWAP) strategy is a strategy that tracks the average price of a stock over a specified time. The strategy uses VWAP as a benchmark and takes long or short positions when the price crosses above or below VWAP. It also sets stop loss and take profit conditions to manage trades.
Strategy Logic
The strategy first calculates the typical price (average of high, low and close prices) multiplied by volume, and the sum of volume. Then VWAP is calculated by dividing the sum of typical price-volume product by the sum of volume. When price crosses over VWAP, go long. When price crosses below, go short.
The profit taking condition for long positions is to close when price rises 3% above the entry price. The stop loss condition is when price drops 1% below entry price. Similar conditions apply for short positions.
Advantage Analysis
The main advantages of the VWAP strategy are:
Uses the well-recognized VWAP statistic as benchmark for trade signals, making the strategy more effective.
Utilizes both vwap signals and stop loss/profit taking, able to profit from trends and limit losses.
Simple and clear logic, easy to understand and implement.
Risk Analysis
There are also some risks with this strategy:
VWAP cannot predict future prices, so signals may lag.
Stop loss may be too wide, increasing potential loss.
Longer backtests means more signals, actual performance may differ.
These risks may be reduced through parameter tuning, optimizing stop loss algorithms etc.
Optimization Directions
Some ways to optimize the strategy include:
Optimize VWAP parameters to find best calculation period.
Test other tracking stop algorithms e.g. moving average stop, parabolic SAR.
Combine other indicators to filter VWAP signals, e.g. volume, Bollinger Bands.
Conclusion
In summary, the VWAP strategy utilizes the predictive power of this important statistic, with stop loss/profit taking to achieve long-term positive expectancy. But further optimizations and combination with other strategies are needed to reduce market fluctuation risks for larger profitability.
Parameters
| Name | Default | What it controls |
|---|---|---|
| v_input_1 | 14 | Period |
Source Pine Script
/*backtest
start: 2023-11-28 00:00:00
end: 2023-12-28 00:00:00
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=4
strategy("VWAP Strategy by Royce Mars", overlay=true)
cumulativePeriod = input(14, "Period")
var float cumulativeTypicalPriceVolume = 0.0
var float cumulativeVolume = 0.0
typicalPrice = (high + low + close) / 3
typicalPriceVolume = typicalPrice * volume
cumulativeTypicalPriceVolume := cumulativeTypicalPriceVolume + typicalPriceVolume
cumulativeVolume := cumulativeVolume + volume
vwapValue = cumulativeTypicalPriceVolume / cumulativeVolume
// Buy condition: Price crosses over VWAP
buyCondition = crossover(close, vwapValue)
// Short condition: Price crosses below VWAP
shortCondition = crossunder(close, vwapValue)
// Profit-taking condition for long positions: Sell long position when profit reaches 3%
profitTakingLongCondition = close / strategy.position_avg_price >= 1.03
// Profit-taking condition for short positions: Cover short position when profit reaches 3%
profitTakingShortCondition = close / strategy.position_avg_price <= 0.97
// Stop loss condition for long positions: Sell long position when loss reaches 1%
stopLossLongCondition = close / strategy.position_avg_price <= 0.99
// Stop loss condition for short positions: Cover short position when loss reaches 1%
stopLossShortCondition = close / strategy.position_avg_price >= 1.01
// Strategy Execution
strategy.entry("Buy", strategy.long, when=buyCondition)
strategy.close("Buy", when=shortCondition or profitTakingLongCondition or stopLossLongCondition)
strategy.entry("Short", strategy.short, when=shortCondition)
strategy.close("Short", when=buyCondition or profitTakingShortCondition or stopLossShortCondition)
// Plot VWAP on the chart
plot(vwapValue, color=color.blue, title="VWAP")
Published by ChaoZhang on the FMZ strategy square · last modified 2023-12-29. Collected in the Quant Trading Vault. Unvalidated research: re-test with your own data and costs before trusting it.
Build this with AgenKit
A spec is not a trading system. Paste this into your coding agent and AgenKit runs a specialist team — architecture, test-first build, risk gates, review — with paper trading on by default.
/agenkit build a production-grade trading bot with a backtest engine, risk controls and paper trading by default from strategies/Volume-Weighted-Average-Price-Strategy.md- npx agenkit install engineering-kit in your project
- Paste the command into your agent and approve each phase
- Get tests, backtest, risk controls and a paper-trading switch
Understand it first
Before you run this, read the lesson behind the idea and how to backtest it honestly. Specs like this one are the survivors of an unknown number of trials; deflate their results accordingly.
Related specs
- Indicator Verification Automated Trading StrategyPine Script · Trend following · 10m
- Double Moving Average Crossover Strategy 436797Pine Script · Trend following · 1h
- Dual Laser Trend seeking StrategyPine Script · Trend following · 1h
- An Optimization of Dual Moving Average Trend Following Strategy Based on Indicators CombinationPine Script · Trend following · 4h
- Enhanced Moving Average Crossover Sakkoulas Trading StrategyPine Script · Trend following · 1h
- Trend Following Strategy Based on Time Series Decomposition and Volume Weighted Bollinger BandsPine Script · Trend following · 1h