Strategy vault / Trend following
VWAP Moving Average Crossover with Dynamic ATR Stop Loss and Take Profit Strategy
This strategy trades based on the crossover relationship between the VWAP (Volume Weighted Average Price) indicator and price. It opens a long position when the price crosses above the VWAP and a short position when…

Overview
This strategy trades based on the crossover relationship between the VWAP (Volume Weighted Average Price) indicator and price. It opens a long position when the price crosses above the VWAP and a short position when the price crosses below the VWAP. Meanwhile, it utilizes the ATR (Average True Range) indicator to calculate dynamic stop loss and take profit levels to control risk and lock in profits.
Strategy Principles
- Calculate the VWAP value over a given period as a reference for the average market cost.
- Determine the crossover situation between the price and VWAP: a long signal is triggered when the closing price crosses above the VWAP, and a short signal is triggered when it crosses below the VWAP.
- Use the ATR indicator to calculate the current market volatility range and set dynamic stop loss and take profit levels based on the ATR value and given multiplier factors.
- Once a position is opened, exit the trade when the price reaches the stop loss or take profit level.
Advantage Analysis
- VWAP can effectively reflect the average market cost. Combined with price, it can better judge the trend strength and potential support/resistance levels.
- Dynamic stop loss and take profit based on the ATR indicator can adapt to the volatility range under different market conditions, controlling risk while considering profit potential.
- Parameters are adjustable, such as the calculation periods for VWAP and ATR, stop loss and take profit multipliers, etc., which can be flexibly set according to different market characteristics and risk preferences.
Risk Analysis
- As a trend indicator, VWAP has a certain lag and may perform poorly in choppy markets, generating more false signals.
- Fixed ATR multiplier stop loss and take profit may not fully adapt to rapidly changing market sentiment, leading to premature stop losses or insufficient profit room.
- The strategy does not consider price gaps, where the opening price directly jumps over the stop loss or take profit levels, exposing certain risks.
Optimization Directions
- Combine other trend indicators or volatility indicators on top of VWAP to assist in judgment, such as MA, EMA, etc., to improve signal reliability.
- Optimize the ATR multiplier factor by introducing an adaptive dynamic adjustment mechanism to dynamically adjust the multiplier size based on recent price volatility characteristics.
- Add price gap handling in the stop loss and take profit logic, such as direct stop loss or take profit at the opening price, pending orders, and other coping mechanisms.
- Consider introducing position management and money management strategies, such as fixed ratio, fixed risk, and other fund allocation methods to improve the overall return-to-risk ratio.
Summary
This strategy focuses on VWAP, generating trading signals through crossovers with price while combining ATR for dynamic stop loss and take profit to control drawdown risk while capturing trends. The overall idea is simple and easy to understand. However, there is further room for optimization. By introducing auxiliary indicators, optimizing stop loss and take profit logic, adding money management, etc., the strategy can better adapt to changing market environments and improve its robustness and profitability.
Parameters
| Name | Default | What it controls |
|---|---|---|
| v_input_1 | 40 | VWAP Period |
| v_input_2 | 14 | ATR Period |
| v_input_3 | 1.5 | ATR Multiplier for Stop Loss |
| v_input_4 | 3 | ATR Multiplier for Take Profit |
Source Pine Script
/*backtest
start: 2023-03-26 00:00:00
end: 2024-03-31 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=4
strategy("Hannah Strategy Stop Loss and Take Profit", overlay=true)
// Inputs
cumulativePeriod = input(40, "VWAP Period")
atrPeriod = input(14, "ATR Period")
multiplier = input(1.5, "ATR Multiplier for Stop Loss")
targetMultiplier = input(3, "ATR Multiplier for Take Profit")
// Calculations for VWAP
typicalPrice = (high + low + close) / 3
typicalPriceVolume = typicalPrice * volume
cumulativeTypicalPriceVolume = sum(typicalPriceVolume, cumulativePeriod)
cumulativeVolume = sum(volume, cumulativePeriod)
vwapValue = cumulativeTypicalPriceVolume / cumulativeVolume
// Plot VWAP on the chart
plot(vwapValue, color=color.blue, title="VWAP")
// Entry Conditions based on price crossing over/under VWAP
longCondition = crossover(close, vwapValue)
shortCondition = crossunder(close, vwapValue)
// ATR Calculation for setting dynamic stop loss and take profit
atr = atr(atrPeriod)
// Execute Trades with Dynamic Stop Loss and Take Profit based on ATR
if (longCondition)
strategy.entry("Long", strategy.long)
// Setting stop loss and take profit for long positions
strategy.exit("Long Exit", "Long", stop=close - atr * multiplier, limit=close + atr * targetMultiplier)
if (shortCondition)
strategy.entry("Short", strategy.short)
// Setting stop loss and take profit for short positions
strategy.exit("Short Exit", "Short", stop=close + atr * multiplier, limit=close - atr * targetMultiplier)
Published by ChaoZhang on the FMZ strategy square · last modified 2024-04-01. 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/VWAP-Moving-Average-Crossover-with-Dynamic-ATR-Stop-Loss-and-Take-Profit-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
- Fixed Range Volume Profile And Anchored VWAP Trend Identification Strategy With Dynamic Stop LossPine Script · Trend following · 1h
- Multi Period Trend Confirmation Dynamic Risk Control Quantitative Trading StrategyPine Script · Trend following · 1h
- Multi Dimensional Technical Indicator Composite Trend Tracking Quantitative Trading StrategyPine Script · Trend following · 5m
- Super Trend V StrategyPine Script · Trend following · 1d
- Trend Confirmation Tracking StrategyPine Script · Trend following · 1h
- Multi Factor Trend Price Action Strategy with Dynamic Risk Management SystemPine Script · Trend following · 2h