Главная
/
Примеры стратегий
Открыть на GitHub
Стратегия RSI Slowdown
Стратегия RSI Slowdown реагирует на экстремальные значения индикатора относительной силы, когда замедляется изменение между барами. Если RSI достигает зон перекупленности или перепроданности и его прирост становится меньше одного пункта, система предполагает разворот тренда.
Длинная позиция открывается при достижении верхнего уровня, если рост RSI замедляется. Короткая позиция открывается при падении RSI к нижнему уровню при аналогичном замедлении. Противоположная позиция закрывается перед входом в новую сделку.
По умолчанию используется 6-часовой таймфрейм и RSI с периодом 2 и уровнями 90 и 10, как в исходной версии MetaTrader.
Детали
Условия входа :
Long : RSI >= LevelMax и |RSI - пред. RSI| < 1 (если включено замедление)
Short : RSI <= LevelMin и |RSI - пред. RSI| < 1 (если включено замедление)
Long/Short : Оба направления.
Условия выхода :
Long : Противоположный сигнал или вход в Short.
Short : Противоположный сигнал или вход в Long.
Стопы : Автоматические стопы не используются.
Параметры по умолчанию :
RsiPeriod = 2
LevelMax = 90
LevelMin = 10
SeekSlowdown = true
CandleType = TimeSpan.FromHours(6)
Фильтры :
Категория: Разворот
Направление: Оба
Индикаторы: RSI
Стопы: Нет
Сложность: Базовая
Таймфрейм: Внутридневной и свинговый
Сезонность: Нет
Нейросети: Нет
Дивергенция: Да (замедление)
Уровень риска: Средний
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>
/// RSI slowdown strategy.
/// Opens long when RSI reaches the upper level and slows down.
/// Opens short when RSI reaches the lower level and slows down.
/// </summary>
public class RsiSlowdownStrategy : Strategy
{
private readonly StrategyParam<int> _rsiPeriod;
private readonly StrategyParam<decimal> _levelMax;
private readonly StrategyParam<decimal> _levelMin;
private readonly StrategyParam<bool> _seekSlowdown;
private readonly StrategyParam<DataType> _candleType;
private decimal _previousRsi;
/// <summary>
/// RSI period length.
/// </summary>
public int RsiPeriod
{
get => _rsiPeriod.Value;
set => _rsiPeriod.Value = value;
}
/// <summary>
/// Upper RSI level.
/// </summary>
public decimal LevelMax
{
get => _levelMax.Value;
set => _levelMax.Value = value;
}
/// <summary>
/// Lower RSI level.
/// </summary>
public decimal LevelMin
{
get => _levelMin.Value;
set => _levelMin.Value = value;
}
/// <summary>
/// Enable slowdown condition.
/// </summary>
public bool SeekSlowdown
{
get => _seekSlowdown.Value;
set => _seekSlowdown.Value = value;
}
/// <summary>
/// The type of candles to use for calculations.
/// </summary>
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
/// <summary>
/// Constructor.
/// </summary>
public RsiSlowdownStrategy()
{
_rsiPeriod = Param(nameof(RsiPeriod), 2)
.SetGreaterThanZero()
.SetDisplay("RSI Period", "RSI calculation period", "RSI")
.SetOptimize(2, 14, 1);
_levelMax = Param(nameof(LevelMax), 90m)
.SetDisplay("Upper Level", "Overbought RSI level", "RSI")
.SetOptimize(50m, 100m, 5m);
_levelMin = Param(nameof(LevelMin), 10m)
.SetDisplay("Lower Level", "Oversold RSI level", "RSI")
.SetOptimize(0m, 50m, 5m);
_seekSlowdown = Param(nameof(SeekSlowdown), true)
.SetDisplay("Seek Slowdown", "Check RSI change below 1", "RSI");
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(6).TimeFrame())
.SetDisplay("Candle Type", "Type of candles for the strategy", "General");
_previousRsi = decimal.MinValue;
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_previousRsi = decimal.MinValue;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_previousRsi = decimal.MinValue;
var rsi = new RelativeStrengthIndex
{
Length = RsiPeriod
};
var subscription = SubscribeCandles(CandleType);
subscription.Bind(rsi, ProcessCandle).Start();
}
private void ProcessCandle(ICandleMessage candle, decimal rsiValue)
{
if (candle.State != CandleStates.Finished)
return;
if (_previousRsi == decimal.MinValue)
{
_previousRsi = rsiValue;
return;
}
if (!IsFormedAndOnlineAndAllowTrading())
{
_previousRsi = rsiValue;
return;
}
var isSlowdown = !SeekSlowdown || Math.Abs(_previousRsi - rsiValue) < 1m;
if (isSlowdown)
{
if (rsiValue >= LevelMax && Position <= 0)
{
BuyMarket();
}
else if (rsiValue <= LevelMin && Position >= 0)
{
SellMarket();
}
}
_previousRsi = rsiValue;
}
}
import clr
clr.AddReference("StockSharp.Messages")
clr.AddReference("StockSharp.Algo")
clr.AddReference("StockSharp.Algo.Indicators")
clr.AddReference("StockSharp.Algo.Strategies")
from System import TimeSpan
from StockSharp.Messages import DataType, CandleStates
from StockSharp.Algo.Indicators import RelativeStrengthIndex
from StockSharp.Algo.Strategies import Strategy
class rsi_slowdown_strategy(Strategy):
def __init__(self):
super(rsi_slowdown_strategy, self).__init__()
self._rsi_period = self.Param("RsiPeriod", 2) \
.SetDisplay("RSI Period", "RSI calculation period", "RSI")
self._level_max = self.Param("LevelMax", 90.0) \
.SetDisplay("Upper Level", "Overbought RSI level", "RSI")
self._level_min = self.Param("LevelMin", 10.0) \
.SetDisplay("Lower Level", "Oversold RSI level", "RSI")
self._seek_slowdown = self.Param("SeekSlowdown", True) \
.SetDisplay("Seek Slowdown", "Check RSI change below 1", "RSI")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(6))) \
.SetDisplay("Candle Type", "Type of candles for the strategy", "General")
self._previous_rsi = None
@property
def rsi_period(self):
return self._rsi_period.Value
@property
def level_max(self):
return self._level_max.Value
@property
def level_min(self):
return self._level_min.Value
@property
def seek_slowdown(self):
return self._seek_slowdown.Value
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(rsi_slowdown_strategy, self).OnReseted()
self._previous_rsi = None
def OnStarted2(self, time):
super(rsi_slowdown_strategy, self).OnStarted2(time)
self._previous_rsi = None
rsi = RelativeStrengthIndex()
rsi.Length = int(self.rsi_period)
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(rsi, self.process_candle).Start()
def process_candle(self, candle, rsi_value):
if candle.State != CandleStates.Finished:
return
rsi_value = float(rsi_value)
if self._previous_rsi is None:
self._previous_rsi = rsi_value
return
is_slowdown = not self.seek_slowdown or abs(self._previous_rsi - rsi_value) < 1.0
lmax = float(self.level_max)
lmin = float(self.level_min)
if is_slowdown:
if rsi_value >= lmax and self.Position <= 0:
self.BuyMarket()
elif rsi_value <= lmin and self.Position >= 0:
self.SellMarket()
self._previous_rsi = rsi_value
def CreateClone(self):
return rsi_slowdown_strategy()