VWAP Bounce — Pine Script v5 strategy
The script computes VWAP from the hlc3 price and compares the current close with that value. It opens one long position when the close crosses above VWAP and closes it when the close crosses below.
Full code (free to copy)
//@version=5
strategy("VWAP Bounce", overlay=true)
vwapVal = ta.vwap(hlc3)
if (close > vwapVal and close[1] < vwapVal[1])
strategy.entry("Long", strategy.long)
if (close < vwapVal and close[1] > vwapVal[1])
strategy.close("Long")How the script works
The script starts with a version directive set to 5, then declares a strategy named VWAP Bounce with overlay set to true so it is drawn on the price chart. The next line stores ta.vwap of the hlc3 series in the variable vwapVal, which gives one VWAP value per bar, recalculated from the session start.
The entry block tests two comparisons joined by and: the current close must be above vwapVal, and the previous close must be below the previous VWAP value. When both are true on the same bar, the close has crossed back above VWAP and strategy.entry is called with the identifier Long and the direction strategy.long, which sends a market buy order.
The exit block mirrors the entry: it checks that the close is below vwapVal while the previous close was above the previous VWAP value. If that is true, strategy.close is called with the same identifier Long and the open long position is closed. The code holds no stop loss, take profit, short entry or filter on trend or volume.
Parameters in the code
| Parameter | Value | Role |
|---|---|---|
Strategy name | VWAP Bounce | First argument of the strategy declaration. |
Overlay | true | Set in the strategy declaration, so the script is drawn on the price chart. |
VWAP source | hlc3 | Series passed to ta.vwap, the average of high, low and close. |
Entry identifier | Long | Label given to the position opened by strategy.entry. |
Entry direction | strategy.long | Direction argument of strategy.entry. |
Exit identifier | Long | Position closed by strategy.close. |
Pine version | 5 | Value of the version directive on the first line. |
Known limits of this rule
- VWAP is anchored to the session, so the line restarts from the price at the beginning of each session and the first cross after that reset can be misleading.
- In a sideways market the close crosses the line repeatedly, so the rule can open and close positions on small moves around the average rather than on a real bounce.
- The script sets no commission, slippage or order size, so the strategy tester applies its defaults and real trading costs are not modelled.
Before you trust it: test it on data it never saw
A backtest in TradingView's Strategy Tester shows one past path with the settings you picked. Strategy Arena Lab (free, Windows and Linux) replays this Pine on your PC, compares its trades one by one with TradingView's for supported Pine, counts the variants you try, then judges the rule on a period kept sealed during the search: holds, mirage or inconclusive.
FAQ
Does it work on crypto?
The script only reads hlc3 and calls ta.vwap, so it can run on any symbol the platform offers, crypto pairs included. VWAP is anchored per session, which fits markets that trade around the clock less naturally. You can replay it on a period you have never seen in Strategy Arena Lab, which is free.
How do I add a stop loss?
Add a strategy.exit call with a stop price and the same Long identifier, or pass a stop argument to strategy.entry. The current code contains neither.
Other free Pine Script v5 strategies
More TradingView paths
Educational code on past data. No investment advice; no result is promised.