Adaptive RSI Strategy
The Adaptive RSI strategy derives a smoothing coefficient from the Relative Strength Index. When RSI deviates from the neutral 50 level, the coefficient increases, making the adaptive RSI follow price more closely. Near 50, the coefficient shrinks and the curve smooths. A long position is opened when the adaptive RSI turns up, while a short position is opened when it turns down.
Details
- Entry Criteria:
- Adaptive RSI crosses above its previous value.
- Adaptive RSI crosses below its previous value.
- Long/Short: Both long and short trades.
- Exit Criteria:
- Opposite signal.
- Stops: None.
- Default Values:
Length= 14
- Filters:
- Category: Momentum
- Direction: Both
- Indicators: RSI
- Stops: No
- Complexity: Low
- Timeframe: Any
- Seasonality: No
- Neural networks: No
- Divergence: No
- Risk level: Low
namespace StockSharp.Samples.Strategies;
using System;
using System.Collections.Generic;
using Ecng.Common;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
/// <summary>
/// Adaptive RSI Strategy - uses RSI to compute adaptive smoothing factor,
/// trades on turns (local min/max) of the adaptive RSI line.
/// </summary>
public class AdaptiveRsiStrategy : Strategy
{
private readonly StrategyParam<DataType> _candleType;
private readonly StrategyParam<int> _length;
private readonly StrategyParam<int> _cooldownBars;
private decimal? _arsiPrev;
private decimal? _arsiPrevPrev;
private int _cooldownRemaining;
public AdaptiveRsiStrategy()
{
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(30).TimeFrame())
.SetDisplay("Candle type", "Candle type for strategy calculation.", "General");
_length = Param(nameof(Length), 14)
.SetGreaterThanZero()
.SetDisplay("RSI Length", "RSI period", "Parameters");
_cooldownBars = Param(nameof(CooldownBars), 15)
.SetDisplay("Cooldown Bars", "Bars between trades", "Risk");
}
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
public int Length
{
get => _length.Value;
set => _length.Value = value;
}
public int CooldownBars
{
get => _cooldownBars.Value;
set => _cooldownBars.Value = value;
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
=> [(Security, CandleType)];
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_arsiPrev = null;
_arsiPrevPrev = null;
_cooldownRemaining = 0;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var rsi = new RelativeStrengthIndex { Length = Length };
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(rsi, ProcessCandle)
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle, decimal rsiValue)
{
if (candle.State != CandleStates.Finished)
return;
if (!IsFormedAndOnlineAndAllowTrading())
return;
var alpha = 2m * Math.Abs(rsiValue / 100m - 0.5m);
var src = candle.ClosePrice;
var prev = _arsiPrev ?? src;
var arsi = alpha * src + (1 - alpha) * prev;
if (_arsiPrevPrev is not null)
{
if (_cooldownRemaining > 0)
{
_cooldownRemaining--;
_arsiPrevPrev = _arsiPrev;
_arsiPrev = arsi;
return;
}
var longCondition = _arsiPrev <= _arsiPrevPrev && arsi > _arsiPrev;
var shortCondition = _arsiPrev >= _arsiPrevPrev && arsi < _arsiPrev;
if (longCondition && Position <= 0)
{
if (Position < 0)
BuyMarket(Math.Abs(Position));
BuyMarket(Volume);
_cooldownRemaining = CooldownBars;
}
else if (shortCondition && Position >= 0)
{
if (Position > 0)
SellMarket(Math.Abs(Position));
SellMarket(Volume);
_cooldownRemaining = CooldownBars;
}
}
_arsiPrevPrev = _arsiPrev;
_arsiPrev = arsi;
}
}
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 RelativeStrengthIndex
from StockSharp.Algo.Strategies import Strategy
class adaptive_rsi_strategy(Strategy):
"""Adaptive RSI Strategy."""
def __init__(self):
super(adaptive_rsi_strategy, self).__init__()
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(30))) \
.SetDisplay("Candle type", "Candle type for strategy calculation.", "General")
self._length = self.Param("Length", 14) \
.SetDisplay("RSI Length", "RSI period", "Parameters")
self._cooldown_bars = self.Param("CooldownBars", 15) \
.SetDisplay("Cooldown Bars", "Bars between trades", "Risk")
self._arsi_prev = None
self._arsi_prev_prev = None
self._cooldown_remaining = 0
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(adaptive_rsi_strategy, self).OnReseted()
self._arsi_prev = None
self._arsi_prev_prev = None
self._cooldown_remaining = 0
def OnStarted2(self, time):
super(adaptive_rsi_strategy, self).OnStarted2(time)
rsi = RelativeStrengthIndex()
rsi.Length = int(self._length.Value)
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(rsi, self._on_process).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawOwnTrades(area)
def _on_process(self, candle, rsi_value):
if candle.State != CandleStates.Finished:
return
if not self.IsFormedAndOnlineAndAllowTrading():
return
rsi_v = float(rsi_value)
alpha = 2.0 * abs(rsi_v / 100.0 - 0.5)
src = float(candle.ClosePrice)
prev = self._arsi_prev if self._arsi_prev is not None else src
arsi = alpha * src + (1 - alpha) * prev
cooldown = int(self._cooldown_bars.Value)
if self._arsi_prev_prev is not None:
if self._cooldown_remaining > 0:
self._cooldown_remaining -= 1
self._arsi_prev_prev = self._arsi_prev
self._arsi_prev = arsi
return
long_condition = self._arsi_prev <= self._arsi_prev_prev and arsi > self._arsi_prev
short_condition = self._arsi_prev >= self._arsi_prev_prev and arsi < self._arsi_prev
if long_condition and self.Position <= 0:
if self.Position < 0:
self.BuyMarket(Math.Abs(self.Position))
self.BuyMarket(self.Volume)
self._cooldown_remaining = cooldown
elif short_condition and self.Position >= 0:
if self.Position > 0:
self.SellMarket(Math.Abs(self.Position))
self.SellMarket(self.Volume)
self._cooldown_remaining = cooldown
self._arsi_prev_prev = self._arsi_prev
self._arsi_prev = arsi
def CreateClone(self):
return adaptive_rsi_strategy()