Strategy vault / Trend following
VWMA and ATR Trend Following Strategy
This strategy uses VWMA to determine the trend direction and sets stop loss with ATR to follow the trend. It is suitable for markets with obvious trends.

Overview
This strategy uses VWMA to determine the trend direction and sets stop loss with ATR to follow the trend. It is suitable for markets with obvious trends.
Strategy Logic
Use VWMA to determine the trend direction. Go long when price is above VWMA, go short when price is below VWMA.
Add RSI oscillator filter to avoid false breakout signals. Only take long signal when RSI is above 30.
Use ATR to calculate the stop loss line. ATR length is set to be the same as VWMA, multiplier is 3.5. Stop loss line updates in real time.
The ATR multiplier controls the tightness of the stop loss line. Larger multiplier means less frequent update, which is better for following the trend.
Position size is calculated based on account equity and stop loss percentage.
Exit long position when price breaks below the stop loss line.
Advantages
Using VWMA to determine trend catches trend opportunities persistently.
RSI filter avoids some false breakout signals.
ATR trailing stop follows the trend and avoids being stopped out by reversals.
Position sizing based on account equity and stop loss favors risk management.
Risks
Potential loss at trend turning points. Should reduce position size to limit losses.
Improper ATR parameter setting leads to too tight or loose stop loss line. Parameters should be tested.
Fast trend reversal may cause stop loss update to lag, increasing losses.
In low volatility environments, reduce position size and increase stop loss update frequency.
Enhancement
Test different VWMA parameter combinations to find optimal signal parameters.
Test other RSI settings like overbought/oversold lines.
Test ATR multiplier to find optimal balance between drawdown and tracking ability.
Add other filters like MACD, KD to improve signal quality.
Optimize position sizing and stop loss percentage based on market volatility.
Summary
The strategy has an overall trend-following bias and catches obvious price trends well. It has advantages in trend determination, signal filtering, stop loss trailing etc. It also has risks in trend reversal. Fine tuning parameters and position sizing can lead to better performance.
Parameters
| Name | Default | What it controls |
|---|---|---|
| v_input_1 | 33 | VWMA Length |
| v_input_2 | 33 | ATR length |
| v_input_3 | 3.5 | ATR Multiplier |
| v_input_4 | 14 | RSI of VWMA Length |
| v_input_5 | 10 | Risk % of capital |
| v_input_6 | 5 | Stop Loss |
Source Pine Script
/*backtest
start: 2023-10-07 00:00:00
end: 2023-10-13 00:00:00
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © mohanee
//@version=4
//strategy("", overlay=true)
strategy(title="VWMA_withATRstops_strategy V2", overlay=true, pyramiding=1, default_qty_type=strategy.percent_of_equity, default_qty_value=20, initial_capital=10000, currency=currency.USD) //default_qty_value=10, default_qty_type=strategy.fixed,
float xATRTrailingStop=na
int pos=na
vwmalength = input(33, title="VWMA Length", minval=1, maxval=365)
//vwmalength2 = input(9, title="VWAM Short Term Length", minval=1, maxval=365)
nATRPeriod = input(33, title="ATR length", minval=1, maxval=365)
nATRMultip = input(3.5, title="ATR Multiplier")
rsiofVwmaLength=input(14, title="RSI of VWMA Length")
riskCapital = input(title="Risk % of capital", defval=10, minval=1)
stopLoss=input(5,title="Stop Loss",minval=1)
vwmaVal=vwma(close, vwmalength)
//vwmaVal2=vwma(close, vwmalength2)
//maVal=sma(close, vwmalength)
plot(vwmaVal, color=color.orange, linewidth=2, title="VWMA")
//plot(vwmaVal2, color=color.blue, title="VWMA Short Term")
//plot(maVal, color=color.blue, title="MA")
//rsi of vwma Longterm
rsiofVwmaVal=rsi(vwmaVal,rsiofVwmaLength)
xATR = atr(nATRPeriod)
nLoss = nATRMultip * xATR
xATRTrailingStop:= iff(close > nz(xATRTrailingStop[1], 0) and close[1] > nz(xATRTrailingStop[1], 0), max(nz(xATRTrailingStop[1]), close - nLoss), iff(close < nz(xATRTrailingStop[1], 0) and close[1] < nz(xATRTrailingStop[1], 0), min(nz(xATRTrailingStop[1]), close + nLoss), iff(close > nz(xATRTrailingStop[1], 0), close - nLoss, close + nLoss)))
pos:= iff(close[1] < nz(xATRTrailingStop[1], 0) and close > nz(xATRTrailingStop[1], 0), 1, iff(close[1] > nz(xATRTrailingStop[1], 0) and close < nz(xATRTrailingStop[1], 0), -1, nz(pos[1], 0)))
color1 = pos == -1 ? color.red: pos == 1 ? color.green : color.blue
//plot(xATRTrailingStop, color=color1, title="ATR Trailing Stop")
//Entry--
//Echeck how many units can be purchased based on risk manage ment and stop loss
qty1 = (strategy.equity * riskCapital / 100 ) / (close*stopLoss/100)
//check if cash is sufficient to buy qty1 , if capital not available use the available capital only
qty1:= (qty1 * close >= strategy.equity ) ? (strategy.equity / close) : qty1
//Long Entry
//strategy.entry(id="VWMA LE", long=true, qty=qty1, when= close >vwmaVal and open>vwmaVal and close>open and close > xATRTrailingStop and xATRTrailingStop> vwmaVal)
strategy.entry(id="VWMA LE", long=true, qty=qty1, when= rsiofVwmaVal>=30 and close>open and close>vwmaVal and pos == 1 ) ///pos == 1 means ATRStop line is green
//vwmaVal2>vwmaVal and
plot(strategy.position_size>=1 ? xATRTrailingStop : na, color=color1, style=plot.style_linebr, title="ATR Trailing Stop")
bgcolor(strategy.position_size>=1 ? color.blue : na )
//Exit
strategy.close(id="VWMA LE", when= strategy.position_size>=1 and crossunder(close, xATRTrailingStop) )
//strategy.close(id="VWMA LE", when= strategy.position_size>=1 and close<vwmaVal and open<vwmaVal and close<open )Published by ChaoZhang on the FMZ strategy square · last modified 2023-11-07. 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/VWMA-and-ATR-Trend-Following-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
- Bollinger Bands and RSI Trend Following StrategyPine Script · Trend following · 1h
- Dual ATR Channel Trend Following StrategyPine Script · Trend following · 15m
- Dynamic Dual EMA Crossover Strategy with Adaptive Profit Loss ControlPine Script · Trend following · 1d
- EMA and RSI Trend Trading StrategyPine Script · Trend following · 1d
- Fixed Range Volume Profile And Anchored VWAP Trend Identification Strategy With Dynamic Stop LossPine Script · Trend following · 1h
- Multi Dimensional Short Term Trend Predictor Technical Analysis and Volatility Adaptive Trading StrategyPine Script · Trend following · 1h