VWAP EMA ATR Pullback
Trend-following strategy using EMAs, VWAP, and ATR.
Testing indicates an average annual return of about 55%. It performs best in the futures market.
The approach identifies strong trends via fast and slow EMAs separated by an ATR-based distance. Entries occur when price pulls back to VWAP, aiming to join the trend. A take-profit is placed at the VWAP plus or minus the ATR multiple.
Details
- Entry Criteria:
- Long: uptrend and close < VWAP.
- Short: downtrend and close > VWAP.
- Long/Short: Both sides.
- Exit Criteria: Target at VWAP ± ATR * multiplier.
- Stops: No.
- Default Values:
FastEmaLength= 30SlowEmaLength= 200AtrLength= 14AtrMultiplier= 1.5CandleType= TimeSpan.FromMinutes(5)
- Filters:
- Category: Trend
- Direction: Both
- Indicators: EMA, ATR, VWAP
- Stops: No
- Complexity: Moderate
- Timeframe: Intraday
- Seasonality: No
- Neural networks: No
- Divergence: No
- Risk level: Medium
using System;
using System.Linq;
using System.Collections.Generic;
using Ecng.Common;
using Ecng.Collections;
using Ecng.Serialization;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// Combines fast and slow EMAs with VWAP pullback and ATR filter.
/// Enters in trend direction when price pulls back to VWAP.
/// Takes profit at VWAP plus/minus ATR multiplier.
/// </summary>
public class VwapEmaAtrPullbackStrategy : Strategy
{
private readonly StrategyParam<int> _fastEmaLength;
private readonly StrategyParam<int> _slowEmaLength;
private readonly StrategyParam<int> _atrLength;
private readonly StrategyParam<decimal> _atrMultiplier;
private readonly StrategyParam<DataType> _candleType;
/// <summary>
/// Fast EMA period.
/// </summary>
public int FastEmaLength
{
get => _fastEmaLength.Value;
set => _fastEmaLength.Value = value;
}
/// <summary>
/// Slow EMA period.
/// </summary>
public int SlowEmaLength
{
get => _slowEmaLength.Value;
set => _slowEmaLength.Value = value;
}
/// <summary>
/// ATR period length.
/// </summary>
public int AtrLength
{
get => _atrLength.Value;
set => _atrLength.Value = value;
}
/// <summary>
/// ATR multiplier for trend filter and exits.
/// </summary>
public decimal AtrMultiplier
{
get => _atrMultiplier.Value;
set => _atrMultiplier.Value = value;
}
/// <summary>
/// Candle type for calculations.
/// </summary>
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
/// <summary>
/// Initializes a new instance of <see cref="VwapEmaAtrPullbackStrategy"/>.
/// </summary>
public VwapEmaAtrPullbackStrategy()
{
_fastEmaLength = Param(nameof(FastEmaLength), 30)
.SetGreaterThanZero()
.SetDisplay("Fast EMA", "Fast EMA length", "Trend")
.SetOptimize(10, 60, 5);
_slowEmaLength = Param(nameof(SlowEmaLength), 200)
.SetGreaterThanZero()
.SetDisplay("Slow EMA", "Slow EMA length", "Trend")
.SetOptimize(100, 300, 20);
_atrLength = Param(nameof(AtrLength), 14)
.SetGreaterThanZero()
.SetDisplay("ATR Length", "ATR period", "Volatility")
.SetOptimize(5, 30, 1);
_atrMultiplier = Param(nameof(AtrMultiplier), 1.5m)
.SetGreaterThanZero()
.SetDisplay("ATR Mult", "ATR multiplier", "Volatility")
.SetOptimize(1m, 3m, 0.5m);
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(2).TimeFrame())
.SetDisplay("Candle Type", "Type of candles", "General");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var emaFast = new EMA { Length = FastEmaLength };
var emaSlow = new EMA { Length = SlowEmaLength };
var atr = new AverageTrueRange { Length = AtrLength };
var vwap = new VolumeWeightedMovingAverage();
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(vwap, emaFast, emaSlow, atr, ProcessCandle)
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, vwap);
DrawIndicator(area, emaFast);
DrawIndicator(area, emaSlow);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle, decimal vwapValue, decimal emaFastValue, decimal emaSlowValue, decimal atrValue)
{
if (candle.State != CandleStates.Finished)
return;
var uptrend = emaFastValue > emaSlowValue && (emaFastValue - emaSlowValue) > atrValue * AtrMultiplier;
var downtrend = emaFastValue < emaSlowValue && (emaSlowValue - emaFastValue) > atrValue * AtrMultiplier;
var longEntry = uptrend && candle.ClosePrice < vwapValue;
var shortEntry = downtrend && candle.ClosePrice > vwapValue;
if (longEntry && Position <= 0)
{
BuyMarket(Volume + Math.Abs(Position));
}
else if (shortEntry && Position >= 0)
{
SellMarket(Volume + Math.Abs(Position));
}
var longTarget = vwapValue + atrValue * AtrMultiplier;
var shortTarget = vwapValue - atrValue * AtrMultiplier;
if (Position > 0 && candle.ClosePrice >= longTarget)
SellMarket(Math.Abs(Position));
else if (Position < 0 && candle.ClosePrice <= shortTarget)
BuyMarket(Math.Abs(Position));
}
}
import clr
clr.AddReference("StockSharp.Messages")
clr.AddReference("StockSharp.Algo")
clr.AddReference("StockSharp.Algo.Indicators")
clr.AddReference("StockSharp.Algo.Strategies")
from System import TimeSpan, Math
from StockSharp.Messages import DataType, CandleStates
from StockSharp.Algo.Indicators import AverageTrueRange, ExponentialMovingAverage, VolumeWeightedMovingAverage
from StockSharp.Algo.Strategies import Strategy
class vwap_ema_atr_pullback_strategy(Strategy):
def __init__(self):
super(vwap_ema_atr_pullback_strategy, self).__init__()
self._fast_ema_length = self.Param("FastEmaLength", 30) \
.SetDisplay("Fast EMA", "Fast EMA length", "Trend")
self._slow_ema_length = self.Param("SlowEmaLength", 200) \
.SetDisplay("Slow EMA", "Slow EMA length", "Trend")
self._atr_length = self.Param("AtrLength", 14) \
.SetDisplay("ATR Length", "ATR period", "Volatility")
self._atr_multiplier = self.Param("AtrMultiplier", 1.5) \
.SetDisplay("ATR Mult", "ATR multiplier", "Volatility")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(2))) \
.SetDisplay("Candle Type", "Type of candles", "General")
@property
def fast_ema_length(self):
return self._fast_ema_length.Value
@property
def slow_ema_length(self):
return self._slow_ema_length.Value
@property
def atr_length(self):
return self._atr_length.Value
@property
def atr_multiplier(self):
return self._atr_multiplier.Value
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(vwap_ema_atr_pullback_strategy, self).OnReseted()
def OnStarted2(self, time):
super(vwap_ema_atr_pullback_strategy, self).OnStarted2(time)
ema_fast = ExponentialMovingAverage()
ema_fast.Length = self.fast_ema_length
ema_slow = ExponentialMovingAverage()
ema_slow.Length = self.slow_ema_length
atr = AverageTrueRange()
atr.Length = self.atr_length
vwap = VolumeWeightedMovingAverage()
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(vwap, ema_fast, ema_slow, atr, self.on_process).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawIndicator(area, vwap)
self.DrawIndicator(area, ema_fast)
self.DrawIndicator(area, ema_slow)
self.DrawOwnTrades(area)
def on_process(self, candle, vwap_value, ema_fast_value, ema_slow_value, atr_value):
if candle.State != CandleStates.Finished:
return
uptrend = ema_fast_value > ema_slow_value and (ema_fast_value - ema_slow_value) > atr_value * self.atr_multiplier
downtrend = ema_fast_value < ema_slow_value and (ema_slow_value - ema_fast_value) > atr_value * self.atr_multiplier
long_entry = uptrend and candle.ClosePrice < vwap_value
short_entry = downtrend and candle.ClosePrice > vwap_value
if long_entry and self.Position <= 0:
self.BuyMarket()
elif short_entry and self.Position >= 0:
self.SellMarket()
long_target = vwap_value + atr_value * self.atr_multiplier
short_target = vwap_value - atr_value * self.atr_multiplier
if self.Position > 0 and candle.ClosePrice >= long_target:
self.SellMarket()
elif self.Position < 0 and candle.ClosePrice <= short_target:
self.BuyMarket()
def CreateClone(self):
return vwap_ema_atr_pullback_strategy()