OBV 背离策略
能量潮(OBV)通过累积成交量反映成交动能,理论上成交量领先价格。当价格创出新高但 OBV 未能确认,或反之,可能预示反转。本策略利用这种背离做反向交易,抓住难以持续的走势。
测试表明年均收益约为 112%,该策略在外汇市场表现最佳。
每根K线都会更新 OBV,并与之前读数比较。当价格创新低但 OBV 形成更高的低点时发出看涨信号;当价格上破新高但 OBV 滞后时则为看跌信号。移动平均线提供离场依据,百分比止损控制风险。
该方法试图在成交量耗尽后捕捉均值回归,通常持仓到价格重新穿过均线为止。
细节
- 入场条件:价格与 OBV 背离。
- 多/空:双向。
- 退出条件:价格穿越移动平均线或止损。
- 止损:是,按百分比。
- 默认值:
DivergencePeriod= 5MAPeriod= 20CandleType= 5 分钟StopLossPercent= 2
- 过滤条件:
- 类别: 背离
- 方向: 双向
- 指标: OBV, 均线
- 止损: 有
- 复杂度: 中等
- 时间框架: 日内
- 季节性: 无
- 神经网络: 无
- 背离: 是
- 风险级别: 中等
using System;
using System.Collections.Generic;
using System.Linq;
using Ecng.Common;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// OBV (On-Balance Volume) Divergence strategy.
/// Bullish divergence: the low falls below the lows of the previous DivergencePeriod candles while OBV stays above its value
/// at the earlier low. Bearish divergence: the high rises above their highs while OBV stays below its value at the earlier high.
/// A divergence opens a position while flat; it closes when the close crosses back over the moving average or at the percent stop.
/// </summary>
public class ObvDivergenceStrategy : Strategy
{
private readonly StrategyParam<int> _divergencePeriod;
private readonly StrategyParam<int> _maPeriod;
private readonly StrategyParam<decimal> _stopLossPercent;
private readonly StrategyParam<DataType> _candleType;
private readonly List<(decimal High, decimal Low, decimal Obv)> _history = [];
private decimal _obv;
private decimal? _prevClose;
/// <summary>
/// Number of previous candles the new extreme is compared with.
/// </summary>
public int DivergencePeriod
{
get => _divergencePeriod.Value;
set => _divergencePeriod.Value = value;
}
/// <summary>
/// MA Period.
/// </summary>
public int MAPeriod
{
get => _maPeriod.Value;
set => _maPeriod.Value = value;
}
/// <summary>
/// Stop-loss percentage.
/// </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 ObvDivergenceStrategy()
{
_divergencePeriod = Param(nameof(DivergencePeriod), 5)
.SetGreaterThanZero()
.SetDisplay("Divergence Period", "Previous candles the new extreme is compared with", "Indicators");
_maPeriod = Param(nameof(MAPeriod), 20)
.SetGreaterThanZero()
.SetDisplay("MA Period", "Period for SMA exit signal", "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();
_history.Clear();
_obv = 0;
_prevClose = null;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_history.Clear();
_obv = 0;
_prevClose = null;
var sma = new SimpleMovingAverage { Length = MAPeriod };
var subscription = SubscribeCandles(CandleType);
subscription
.BindEx(sma, 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);
DrawIndicator(area, sma);
DrawOwnTrades(area);
}
}
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 smaValue)
{
if (candle.State != CandleStates.Finished)
return;
var close = candle.ClosePrice;
if (_prevClose is decimal prevClose)
{
if (close > prevClose)
_obv += candle.TotalVolume;
else if (close < prevClose)
_obv -= candle.TotalVolume;
}
_prevClose = close;
// Compare this candle with the DivergencePeriod candles before it.
var previous = _history.ToArray();
_history.Add((candle.HighPrice, candle.LowPrice, _obv));
if (_history.Count > DivergencePeriod)
_history.RemoveAt(0);
if (previous.Length < DivergencePeriod || !smaValue.IsFormed)
return;
if (!IsFormedAndOnlineAndAllowTrading())
return;
var ma = smaValue.GetValue<decimal>();
if (Position > 0)
{
if (close > ma)
SellMarket(Position);
return;
}
if (Position < 0)
{
if (close < ma)
BuyMarket(-Position);
return;
}
var lowest = previous.MinBy(c => c.Low);
var highest = previous.MaxBy(c => c.High);
var bullish = candle.LowPrice < lowest.Low && _obv > lowest.Obv;
var bearish = candle.HighPrice > highest.High && _obv < highest.Obv;
if (bullish && !bearish)
BuyMarket(Volume);
else if (bearish && !bullish)
SellMarket(Volume);
}
}
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 SimpleMovingAverage
from StockSharp.Algo.Strategies import Strategy
class obv_divergence_strategy(Strategy):
"""
OBV (On-Balance Volume) Divergence strategy.
Bullish divergence: the low falls below the lows of the previous DivergencePeriod candles while OBV stays above its value
at the earlier low. Bearish divergence: the high rises above their highs while OBV stays below its value at the earlier high.
A divergence opens a position while flat; it closes when the close crosses back over the moving average or at the percent stop.
"""
def __init__(self):
super(obv_divergence_strategy, self).__init__()
self._divergence_period = self.Param("DivergencePeriod", 5).SetGreaterThanZero().SetDisplay("Divergence Period", "Previous candles the new extreme is compared with", "Indicators")
self._ma_period = self.Param("MAPeriod", 20).SetGreaterThanZero().SetDisplay("MA Period", "Period for SMA exit signal", "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._history = []
self._obv = Decimal(0)
self._prev_close = None
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(obv_divergence_strategy, self).OnReseted()
self._history = []
self._obv = Decimal(0)
self._prev_close = None
def OnStarted2(self, time):
super(obv_divergence_strategy, self).OnStarted2(time)
self._history = []
self._obv = Decimal(0)
self._prev_close = None
sma = SimpleMovingAverage()
sma.Length = self._ma_period.Value
subscription = self.SubscribeCandles(self.candle_type)
subscription.BindEx(sma, 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.DrawIndicator(area, sma)
self.DrawOwnTrades(area)
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, sma_value):
if candle.State != CandleStates.Finished:
return
close = candle.ClosePrice
if self._prev_close is not None:
if close > self._prev_close:
self._obv += candle.TotalVolume
elif close < self._prev_close:
self._obv -= candle.TotalVolume
self._prev_close = close
# Compare this candle with the DivergencePeriod candles before it.
period = self._divergence_period.Value
previous = list(self._history)
self._history.append((candle.HighPrice, candle.LowPrice, self._obv))
if len(self._history) > period:
self._history.pop(0)
if len(previous) < period or not sma_value.IsFormed:
return
if not self.IsFormedAndOnlineAndAllowTrading():
return
ma = sma_value.GetValue[Decimal](None)
if self.Position > 0:
if close > ma:
self.SellMarket(self.Position)
return
if self.Position < 0:
if close < ma:
self.BuyMarket(-self.Position)
return
lowest = min(previous, key=lambda c: c[1])
highest = max(previous, key=lambda c: c[0])
bullish = candle.LowPrice < lowest[1] and self._obv > lowest[2]
bearish = candle.HighPrice > highest[0] and self._obv < highest[2]
if bullish and not bearish:
self.BuyMarket(self.Volume)
elif bearish and not bullish:
self.SellMarket(self.Volume)
def CreateClone(self):
return obv_divergence_strategy()