Прорыв наклона OBV
Стратегия OBV Slope Breakout отслеживает скорость изменения индикатора OBV. Сильный рост наклона подсказывает возможное формирование нового тренда.
Тестирование показывает среднегодичную доходность около 154%. Стратегию лучше запускать на фондовом рынке.
Сделка открывается, когда наклон превышает свой обычный уровень на несколько стандартных отклонений. Позиция открывается в направлении ускорения со стопом.
Она подходит активным трейдерам, стремящимся рано войти в тренд. Позиции закрываются, когда наклон возвращается к нормальным значениям. Значение по умолчанию LookbackPeriod = 20.
Подробности
- Условие входа: Индикатор превышает среднее значение на величину коэффициента отклонения.
- Лонг/Шорт: Оба направления.
- Условие выхода: Индикатор возвращается к среднему.
- Стопы: Да.
- Значения по умолчанию:
LookbackPeriod= 20SlopeLength= 5Multiplier= 2mStopLoss= 2mCandleType= TimeSpan.FromMinutes(5)
- Фильтры:
- Категория: Прорыв
- Направление: Оба
- Индикаторы: OBV
- Стопы: Да
- Сложность: Средняя
- Таймфрейм: Краткосрочный
- Сезонность: Нет
- Нейронные сети: Нет
- Дивергенция: Нет
- Уровень риска: Средний
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>
/// Strategy based on OBV slope breakout with EMA direction filter.
/// Opens positions when OBV slope deviates from its recent average and price confirms the direction relative to EMA.
/// </summary>
public class ObvSlopeBreakoutStrategy : Strategy
{
private readonly StrategyParam<int> _lookbackPeriod;
private readonly StrategyParam<decimal> _multiplier;
private readonly StrategyParam<decimal> _stopLoss;
private readonly StrategyParam<DataType> _candleType;
private readonly StrategyParam<int> _emaPeriod;
private readonly StrategyParam<int> _cooldownBars;
private OnBalanceVolume _obv;
private ExponentialMovingAverage _ema;
private decimal _prevObvValue;
private decimal _currentSlope;
private decimal _avgSlope;
private decimal _stdDevSlope;
private decimal[] _slopes;
private int _currentIndex;
private int _filledCount;
private int _cooldown;
private bool _isInitialized;
/// <summary>
/// Lookback period for slope statistics calculation.
/// </summary>
public int LookbackPeriod
{
get => _lookbackPeriod.Value;
set => _lookbackPeriod.Value = value;
}
/// <summary>
/// Standard deviation multiplier for breakout detection.
/// </summary>
public decimal Multiplier
{
get => _multiplier.Value;
set => _multiplier.Value = value;
}
/// <summary>
/// Stop-loss as a percentage of entry price.
/// </summary>
public decimal StopLoss
{
get => _stopLoss.Value;
set => _stopLoss.Value = value;
}
/// <summary>
/// Type of candles to use in the strategy.
/// </summary>
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
/// <summary>
/// EMA period for trend confirmation.
/// </summary>
public int EmaPeriod
{
get => _emaPeriod.Value;
set => _emaPeriod.Value = value;
}
/// <summary>
/// Cooldown bars between orders.
/// </summary>
public int CooldownBars
{
get => _cooldownBars.Value;
set => _cooldownBars.Value = value;
}
/// <summary>
/// Initializes a new instance of <see cref="ObvSlopeBreakoutStrategy"/>.
/// </summary>
public ObvSlopeBreakoutStrategy()
{
_lookbackPeriod = Param(nameof(LookbackPeriod), 20)
.SetGreaterThanZero()
.SetDisplay("Lookback Period", "Period for calculating average and standard deviation of OBV slope", "Strategy Parameters")
.SetOptimize(10, 50, 5);
_multiplier = Param(nameof(Multiplier), 1.5m)
.SetGreaterThanZero()
.SetDisplay("Std Dev Multiplier", "Multiplier for standard deviation to determine breakout threshold", "Strategy Parameters")
.SetOptimize(1m, 3m, 0.5m);
_stopLoss = Param(nameof(StopLoss), 2m)
.SetGreaterThanZero()
.SetDisplay("Stop Loss %", "Stop-loss as a percentage of entry price", "Risk Management");
_emaPeriod = Param(nameof(EmaPeriod), 20)
.SetGreaterThanZero()
.SetDisplay("EMA Period", "Period for EMA trend confirmation", "Indicator Parameters");
_cooldownBars = Param(nameof(CooldownBars), 1200)
.SetRange(1, 5000)
.SetDisplay("Cooldown Bars", "Bars to wait between orders", "Risk Management");
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
.SetDisplay("Candle Type", "Type of candles to use in the strategy", "General");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_obv = null;
_ema = null;
_prevObvValue = default;
_currentSlope = default;
_avgSlope = default;
_stdDevSlope = default;
_currentIndex = default;
_filledCount = default;
_cooldown = default;
_isInitialized = default;
_slopes = new decimal[LookbackPeriod];
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_obv = new OnBalanceVolume();
_ema = new ExponentialMovingAverage { Length = EmaPeriod };
_slopes = new decimal[LookbackPeriod];
_cooldown = 0;
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(_obv, _ema, ProcessObv)
.Start();
StartProtection(new(), new Unit(StopLoss, UnitTypes.Percent));
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, _ema);
DrawIndicator(area, _obv);
DrawOwnTrades(area);
}
}
private void ProcessObv(ICandleMessage candle, decimal obvValue, decimal emaValue)
{
if (candle.State != CandleStates.Finished)
return;
if (!_obv.IsFormed || !_ema.IsFormed)
return;
if (!_isInitialized)
{
_prevObvValue = obvValue;
_isInitialized = true;
return;
}
_currentSlope = obvValue - _prevObvValue;
_prevObvValue = obvValue;
_slopes[_currentIndex] = _currentSlope;
_currentIndex = (_currentIndex + 1) % LookbackPeriod;
if (_filledCount < LookbackPeriod)
_filledCount++;
if (_filledCount < LookbackPeriod)
return;
CalculateStatistics();
if (!IsFormedAndOnlineAndAllowTrading())
return;
if (_stdDevSlope <= 0)
return;
if (_cooldown > 0)
{
_cooldown--;
return;
}
var upperThreshold = _avgSlope + Multiplier * _stdDevSlope;
var lowerThreshold = _avgSlope - Multiplier * _stdDevSlope;
var closePrice = candle.ClosePrice;
var priceAboveEma = closePrice > emaValue;
var priceBelowEma = closePrice < emaValue;
if (Position == 0)
{
if (_currentSlope > upperThreshold && priceAboveEma)
{
BuyMarket();
_cooldown = CooldownBars;
}
else if (_currentSlope < lowerThreshold && priceBelowEma)
{
SellMarket();
_cooldown = CooldownBars;
}
}
else if (Position > 0)
{
if (_currentSlope <= _avgSlope || priceBelowEma)
{
SellMarket(Math.Abs(Position));
_cooldown = CooldownBars;
}
}
else if (Position < 0)
{
if (_currentSlope >= _avgSlope || priceAboveEma)
{
BuyMarket(Math.Abs(Position));
_cooldown = CooldownBars;
}
}
}
private void CalculateStatistics()
{
_avgSlope = 0;
var sumSquaredDiffs = 0m;
for (var i = 0; i < LookbackPeriod; i++)
_avgSlope += _slopes[i];
_avgSlope /= LookbackPeriod;
for (var i = 0; i < LookbackPeriod; i++)
{
var diff = _slopes[i] - _avgSlope;
sumSquaredDiffs += diff * diff;
}
_stdDevSlope = (decimal)Math.Sqrt((double)(sumSquaredDiffs / LookbackPeriod));
}
}
import clr
clr.AddReference("StockSharp.Messages")
clr.AddReference("StockSharp.Algo")
clr.AddReference("StockSharp.Algo.Indicators")
clr.AddReference("StockSharp.Algo.Strategies")
import math
from System import TimeSpan, Math
from StockSharp.Messages import DataType, Unit, UnitTypes, CandleStates
from StockSharp.Algo.Indicators import OnBalanceVolume, ExponentialMovingAverage
from StockSharp.Algo.Strategies import Strategy
class obv_slope_breakout_strategy(Strategy):
"""
Strategy based on OBV slope breakout with EMA direction filter.
Opens positions when OBV slope deviates from its recent average and price confirms direction relative to EMA.
"""
def __init__(self):
super(obv_slope_breakout_strategy, self).__init__()
self._lookback_period = self.Param("LookbackPeriod", 20) \
.SetGreaterThanZero() \
.SetDisplay("Lookback Period", "Period for calculating average and standard deviation of OBV slope", "Strategy Parameters") \
.SetOptimize(10, 50, 5)
self._multiplier = self.Param("Multiplier", 1.5) \
.SetGreaterThanZero() \
.SetDisplay("Std Dev Multiplier", "Multiplier for standard deviation to determine breakout threshold", "Strategy Parameters") \
.SetOptimize(1.0, 3.0, 0.5)
self._stop_loss = self.Param("StopLoss", 2.0) \
.SetGreaterThanZero() \
.SetDisplay("Stop Loss %", "Stop-loss as a percentage of entry price", "Risk Management")
self._ema_period = self.Param("EmaPeriod", 20) \
.SetGreaterThanZero() \
.SetDisplay("EMA Period", "Period for EMA trend confirmation", "Indicator Parameters")
self._cooldown_bars = self.Param("CooldownBars", 1200) \
.SetDisplay("Cooldown Bars", "Bars to wait between orders", "Risk Management")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(5))) \
.SetDisplay("Candle Type", "Type of candles to use in the strategy", "General")
self._obv = None
self._ema = None
self._prev_obv = 0.0
self._current_slope = 0.0
self._avg_slope = 0.0
self._std_dev_slope = 0.0
self._slopes = None
self._current_index = 0
self._filled_count = 0
self._cooldown = 0
self._is_initialized = False
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(obv_slope_breakout_strategy, self).OnReseted()
self._obv = None
self._ema = None
self._prev_obv = 0.0
self._current_slope = 0.0
self._avg_slope = 0.0
self._std_dev_slope = 0.0
lb = int(self._lookback_period.Value)
self._slopes = [0.0] * lb
self._current_index = 0
self._filled_count = 0
self._cooldown = 0
self._is_initialized = False
def OnStarted2(self, time):
super(obv_slope_breakout_strategy, self).OnStarted2(time)
lb = int(self._lookback_period.Value)
self._slopes = [0.0] * lb
self._cooldown = 0
self._filled_count = 0
self._current_index = 0
self._obv = OnBalanceVolume()
self._ema = ExponentialMovingAverage()
self._ema.Length = int(self._ema_period.Value)
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(self._obv, self._ema, self._process_obv).Start()
self.StartProtection(Unit(), Unit(self._stop_loss.Value, UnitTypes.Percent))
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawIndicator(area, self._ema)
self.DrawIndicator(area, self._obv)
self.DrawOwnTrades(area)
def _process_obv(self, candle, obv_value, ema_value):
if candle.State != CandleStates.Finished:
return
if not self._obv.IsFormed or not self._ema.IsFormed:
return
obv_val = float(obv_value)
ema_val = float(ema_value)
if not self._is_initialized:
self._prev_obv = obv_val
self._is_initialized = True
return
self._current_slope = obv_val - self._prev_obv
self._prev_obv = obv_val
lb = int(self._lookback_period.Value)
self._slopes[self._current_index] = self._current_slope
self._current_index = (self._current_index + 1) % lb
if self._filled_count < lb:
self._filled_count += 1
if self._filled_count < lb:
return
self._calculate_statistics()
if not self.IsFormedAndOnlineAndAllowTrading():
return
if self._std_dev_slope <= 0:
return
if self._cooldown > 0:
self._cooldown -= 1
return
mult = float(self._multiplier.Value)
upper_threshold = self._avg_slope + mult * self._std_dev_slope
lower_threshold = self._avg_slope - mult * self._std_dev_slope
close_price = float(candle.ClosePrice)
price_above_ema = close_price > ema_val
price_below_ema = close_price < ema_val
if self.Position == 0:
if self._current_slope > upper_threshold and price_above_ema:
self.BuyMarket()
self._cooldown = int(self._cooldown_bars.Value)
elif self._current_slope < lower_threshold and price_below_ema:
self.SellMarket()
self._cooldown = int(self._cooldown_bars.Value)
elif self.Position > 0:
if self._current_slope <= self._avg_slope or price_below_ema:
self.SellMarket(Math.Abs(self.Position))
self._cooldown = int(self._cooldown_bars.Value)
elif self.Position < 0:
if self._current_slope >= self._avg_slope or price_above_ema:
self.BuyMarket(Math.Abs(self.Position))
self._cooldown = int(self._cooldown_bars.Value)
def _calculate_statistics(self):
lb = int(self._lookback_period.Value)
self._avg_slope = 0.0
sum_sq = 0.0
for i in range(lb):
self._avg_slope += self._slopes[i]
self._avg_slope /= float(lb)
for i in range(lb):
diff = self._slopes[i] - self._avg_slope
sum_sq += diff * diff
self._std_dev_slope = math.sqrt(sum_sq / float(lb))
def CreateClone(self):
return obv_slope_breakout_strategy()