Momentum Divergence Strategy
The Momentum Divergence strategy compares momentum readings with price direction to spot early signs of a reversal. Divergences occur when price makes a new extreme yet the momentum indicator fails to confirm, hinting at weakening strength.
Testing indicates an average annual return of about 106%. It performs best in the stocks market.
A bullish setup happens when price records a lower low while the momentum oscillator prints a higher low. A bearish setup forms when price pushes to a higher high but momentum fails to follow. Positions are closed when momentum crosses back through zero or the divergence is invalidated.
This approach appeals to traders looking to anticipate turning points rather than chase trends. Stops are used to control risk in case the market continues to move against the divergence signal.
Details
- Entry Criteria:
- Long: Price makes lower low && Momentum shows higher low
- Short: Price makes higher high && Momentum shows lower high
- Long/Short: Both sides.
- Exit Criteria:
- Long: Exit when momentum crosses below zero
- Short: Exit when momentum crosses above zero
- Stops: Yes, fixed stop-loss.
- Default Values:
MomentumPeriod = 14
StopLossPercent = 2
A lower low is a close below the previous close while momentum, the close minus the close MomentumPeriod candles earlier, rises above its previous value; a higher high is the mirror. The zero-cross exits wait for momentum to pass through zero after the entry. The fixed stop is StopLossPercent of the entry price, watched between candles as well; 0 disables it. No rule uses a moving average, so MaPeriod is gone. An entry signal against an open position reverses it.
CandleType = TimeSpan.FromMinutes(5)
- Filters:
- Category: Reversal
- Direction: Both
- Indicators: Momentum
- Stops: Yes
- Complexity: Intermediate
- Timeframe: Intraday
- Seasonality: No
- Neural networks: No
- Divergence: Yes
- Risk Level: Medium
using System;
using System.Collections.Generic;
using Ecng.Common;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// Momentum Divergence strategy.
/// A close below the previous close while MomentumPeriod momentum rises above its previous value is a bullish divergence and goes long;
/// a close above the previous close while momentum falls is a bearish one and goes short, reversing an opposite position. A long closes
/// when momentum crosses below zero and a short when it crosses above zero, and a percent stop limits the loss.
/// </summary>
public class MomentumDivergenceStrategy : Strategy
{
private readonly StrategyParam<int> _momentumPeriod;
private readonly StrategyParam<decimal> _stopLossPercent;
private readonly StrategyParam<DataType> _candleType;
private decimal? _prevClose;
private decimal? _prevMomentum;
/// <summary>
/// Period of the momentum indicator.
/// </summary>
public int MomentumPeriod
{
get => _momentumPeriod.Value;
set => _momentumPeriod.Value = value;
}
/// <summary>
/// Stop loss percentage from entry price.
/// </summary>
public decimal StopLossPercent
{
get => _stopLossPercent.Value;
set => _stopLossPercent.Value = value;
}
/// <summary>
/// Candle type.
/// </summary>
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
/// <summary>
/// Constructor.
/// </summary>
public MomentumDivergenceStrategy()
{
_momentumPeriod = Param(nameof(MomentumPeriod), 14)
.SetGreaterThanZero()
.SetDisplay("Momentum Period", "Period of the momentum indicator", "Indicators");
_stopLossPercent = Param(nameof(StopLossPercent), 2m)
.SetNotNegative()
.SetDisplay("Stop Loss %", "Stop loss percentage from entry price", "Risk");
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
.SetDisplay("Candle Type", "Type of candles to use", "General");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_prevClose = null;
_prevMomentum = null;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_prevClose = null;
_prevMomentum = null;
var momentum = new Momentum { Length = MomentumPeriod };
var subscription = SubscribeCandles(CandleType);
subscription
.BindEx(momentum, ProcessCandle)
.Start();
StartProtection(new Unit(), new Unit(StopLossPercent, UnitTypes.Percent), useMarketOrders: true, isLocalStop: true);
// The stop has to see prices between candles, not only at their close.
foreach (var field in new[] { Level1Fields.BestBidPrice, Level1Fields.BestAskPrice })
{
var quotes = new Subscription(DataType.Level1, Security);
quotes.MarketData.BuildField = field;
SubscribeLevel1(quotes).Bind(ObserveProtectionQuote).Start();
}
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawOwnTrades(area);
var oscillators = CreateChartArea();
if (oscillators != null)
{
DrawIndicator(oscillators, momentum);
}
}
}
private void ObserveProtectionQuote(Level1ChangeMessage quote)
{
// The high-level handler activates native protection before this callback, also between signal bars.
}
private void ProcessCandle(ICandleMessage candle, IIndicatorValue momentumValue)
{
if (candle.State != CandleStates.Finished)
return;
if (!momentumValue.IsFormed)
return;
var close = candle.ClosePrice;
var value = momentumValue.GetValue<decimal>();
var prevClose = _prevClose;
var prevMomentum = _prevMomentum;
_prevClose = close;
_prevMomentum = value;
if (prevClose is not decimal lastClose || prevMomentum is not decimal lastMomentum)
return;
if (!IsFormedAndOnlineAndAllowTrading())
return;
var bullish = close < lastClose && value > lastMomentum;
var bearish = close > lastClose && value < lastMomentum;
if (bullish && Position <= 0)
BuyMarket(Volume + Math.Abs(Position));
else if (bearish && Position >= 0)
SellMarket(Volume + Math.Abs(Position));
else if (Position > 0 && lastMomentum >= 0 && value < 0)
SellMarket(Position);
else if (Position < 0 && lastMomentum <= 0 && value > 0)
BuyMarket(-Position);
}
}
import clr
clr.AddReference("StockSharp.Messages")
clr.AddReference("StockSharp.Algo")
clr.AddReference("StockSharp.Algo.Indicators")
clr.AddReference("StockSharp.Algo.Strategies")
clr.AddReference("StockSharp.BusinessEntities")
from System import TimeSpan, Decimal
from StockSharp.Messages import DataType, CandleStates, Unit, UnitTypes, Level1Fields
from StockSharp.BusinessEntities import Subscription
from StockSharp.Algo.Indicators import Momentum
from StockSharp.Algo.Strategies import Strategy
class momentum_divergence_strategy(Strategy):
"""
Momentum Divergence strategy.
A close below the previous close while MomentumPeriod momentum rises above its previous value is a bullish divergence and goes long;
a close above the previous close while momentum falls is a bearish one and goes short, reversing an opposite position. A long closes
when momentum crosses below zero and a short when it crosses above zero, and a percent stop limits the loss.
"""
def __init__(self):
super(momentum_divergence_strategy, self).__init__()
self._momentum_period = self.Param("MomentumPeriod", 14).SetGreaterThanZero().SetDisplay("Momentum Period", "Period of the momentum indicator", "Indicators")
self._stop_loss_percent = self.Param("StopLossPercent", 2.0).SetNotNegative().SetDisplay("Stop Loss %", "Stop loss percentage from entry price", "Risk")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(5))).SetDisplay("Candle Type", "Type of candles to use", "General")
self._reset_state()
@property
def candle_type(self):
return self._candle_type.Value
def _reset_state(self):
self._prev_close = None
self._prev_momentum = None
def OnReseted(self):
super(momentum_divergence_strategy, self).OnReseted()
self._reset_state()
def OnStarted2(self, time):
super(momentum_divergence_strategy, self).OnStarted2(time)
self._reset_state()
momentum = Momentum()
momentum.Length = self._momentum_period.Value
subscription = self.SubscribeCandles(self.candle_type)
subscription.BindEx(momentum, self._process_candle).Start()
self.StartProtection(Unit(), Unit(Decimal(self._stop_loss_percent.Value), UnitTypes.Percent), useMarketOrders=True, isLocalStop=True)
# The stop has to see prices between candles, not only at their close.
for field in (Level1Fields.BestBidPrice, Level1Fields.BestAskPrice):
quotes = Subscription(DataType.Level1, self.Security)
quotes.MarketData.BuildField = field
self.SubscribeLevel1(quotes).Bind(self._observe_protection_quote).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawOwnTrades(area)
oscillators = self.CreateChartArea()
if oscillators is not None:
self.DrawIndicator(oscillators, momentum)
def _observe_protection_quote(self, quote):
# The high-level handler activates native protection before the callback, including between bars.
pass
def _process_candle(self, candle, momentum_value):
if candle.State != CandleStates.Finished:
return
if not momentum_value.IsFormed:
return
close = candle.ClosePrice
value = momentum_value.GetValue[Decimal](None)
last_close = self._prev_close
last_momentum = self._prev_momentum
self._prev_close = close
self._prev_momentum = value
if last_close is None or last_momentum is None:
return
if not self.IsFormedAndOnlineAndAllowTrading():
return
bullish = close < last_close and value > last_momentum
bearish = close > last_close and value < last_momentum
if bullish and self.Position <= 0:
self.BuyMarket(self.Volume + abs(self.Position))
elif bearish and self.Position >= 0:
self.SellMarket(self.Volume + abs(self.Position))
elif self.Position > 0 and last_momentum >= 0 and value < 0:
self.SellMarket(self.Position)
elif self.Position < 0 and last_momentum <= 0 and value > 0:
self.BuyMarket(-self.Position)
def CreateClone(self):
return momentum_divergence_strategy()