RSI Value
基于相对强弱指数(RSI)中线交叉的交易策略。
当RSI从下向上穿越设定水平(默认50)时开多单;当RSI从上向下穿越该水平时开空单。相反的交叉用于平仓。可选的止损、止盈和追踪止损用于风险控制。
细节
- 入场条件:RSI上穿水平买入,下穿卖出。
- 多空方向:双向。
- 出场条件:反向交叉或触发追踪止损。
- 止损:可选固定止损、止盈和追踪止损。
- 默认值:
RsiPeriod= 14RsiLevel= 50StopLoss= 100TakeProfit= 200TrailingStop= 0CandleType= TimeSpan.FromMinutes(5)
- 过滤器:
- 分类:振荡器
- 方向:双向
- 指标:RSI
- 止损:有
- 复杂度:基础
- 时间框架:日内 (5分钟)
- 季节性:无
- 神经网络:无
- 背离:无
- 风险级别:中等
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 level crossing strategy.
/// </summary>
public class RsiValueStrategy : Strategy
{
private readonly StrategyParam<int> _rsiPeriod;
private readonly StrategyParam<decimal> _rsiLevel;
private readonly StrategyParam<DataType> _candleType;
private decimal _prevRsi;
private bool _hasPrev;
public int RsiPeriod { get => _rsiPeriod.Value; set => _rsiPeriod.Value = value; }
public decimal RsiLevel { get => _rsiLevel.Value; set => _rsiLevel.Value = value; }
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public RsiValueStrategy()
{
_rsiPeriod = Param(nameof(RsiPeriod), 14)
.SetGreaterThanZero()
.SetDisplay("RSI Period", "RSI period", "Indicators");
_rsiLevel = Param(nameof(RsiLevel), 50m)
.SetDisplay("RSI Level", "RSI crossing level", "Indicators");
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
.SetDisplay("Candle Type", "Candle type", "General");
}
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
=> [(Security, CandleType)];
protected override void OnReseted()
{
base.OnReseted();
_prevRsi = 0;
_hasPrev = false;
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var rsi = new RelativeStrengthIndex { Length = RsiPeriod };
SubscribeCandles(CandleType)
.Bind(rsi, ProcessCandle)
.Start();
}
private void ProcessCandle(ICandleMessage candle, decimal rsiVal)
{
if (candle.State != CandleStates.Finished) return;
if (!_hasPrev)
{
_prevRsi = rsiVal;
_hasPrev = true;
return;
}
var crossUp = _prevRsi <= RsiLevel && rsiVal > RsiLevel;
var crossDown = _prevRsi >= RsiLevel && rsiVal < RsiLevel;
if (crossUp && Position <= 0)
{
if (Position < 0) BuyMarket();
BuyMarket();
}
else if (crossDown && Position >= 0)
{
if (Position > 0) SellMarket();
SellMarket();
}
_prevRsi = rsiVal;
}
}
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_value_strategy(Strategy):
def __init__(self):
super(rsi_value_strategy, self).__init__()
self._rsi_period = self.Param("RsiPeriod", 14) \
.SetDisplay("RSI Period", "RSI period", "Indicators")
self._rsi_level = self.Param("RsiLevel", 50.0) \
.SetDisplay("RSI Level", "RSI crossing level", "Indicators")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4))) \
.SetDisplay("Candle Type", "Candle type", "General")
self._prev_rsi = 0.0
self._has_prev = False
@property
def rsi_period(self):
return self._rsi_period.Value
@property
def rsi_level(self):
return self._rsi_level.Value
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(rsi_value_strategy, self).OnReseted()
self._prev_rsi = 0.0
self._has_prev = False
def OnStarted2(self, time):
super(rsi_value_strategy, self).OnStarted2(time)
rsi = RelativeStrengthIndex()
rsi.Length = self.rsi_period
self.SubscribeCandles(self.candle_type) \
.Bind(rsi, self.process_candle) \
.Start()
def process_candle(self, candle, rsi_val):
if candle.State != CandleStates.Finished:
return
rsi_val = float(rsi_val)
if not self._has_prev:
self._prev_rsi = rsi_val
self._has_prev = True
return
level = float(self.rsi_level)
cross_up = self._prev_rsi <= level and rsi_val > level
cross_down = self._prev_rsi >= level and rsi_val < level
if cross_up and self.Position <= 0:
if self.Position < 0:
self.BuyMarket()
self.BuyMarket()
elif cross_down and self.Position >= 0:
if self.Position > 0:
self.SellMarket()
self.SellMarket()
self._prev_rsi = rsi_val
def CreateClone(self):
return rsi_value_strategy()