在 GitHub 上查看
Easiest RSI 策略(ID 4204)
源自 MetaTrader 4 专家顾问 “Easiest RSI”(MQL/9827/Easiest_RSI.mq4)。
概览
原版 EA 利用 RSI 穿越超卖/超买区间时开仓,并在行情持续向持仓方向发展时最多再加码两次。所有订单使用相同手数、固定止损,并在浮盈扩大时按小步长移动跟踪止损。
移植到 StockSharp 后的策略保留了上述思路:
- 在选定的 K 线序列上计算 14 周期 RSI 作为信号源。
- 当 RSI 自下而上突破超卖阈值时做多,自上而下跌破超买阈值时做空。
- 通过
StepPips 控制的价格增量复制 MT4 的加仓逻辑,每次追加下单仍使用 LotSize,总订单数受 MaxEntries 限制。
- 止损与跟踪止损均按点值(pip)计算,并自动适配 4/5 位报价品种。
- 所有运行状态(RSI 历史值、最近成交价、止损位置等)都保存在基本字段中,符合框架约束。
参数
| 名称 |
默认值 |
说明 |
LotSize |
1 |
每笔市价单的交易量。 |
StopLossPips |
50 |
初始止损距离(pip)。设为 0 可关闭固定止损。 |
TrailingStopPips |
50 |
跟踪止损距离(pip)。设为 0 关闭跟踪。 |
StepPips |
20 |
追加加仓所需的最小顺势幅度(pip)。 |
RsiPeriod |
14 |
RSI 周期。 |
OversoldLevel |
30 |
触发做多信号的超卖阈值。 |
OverboughtLevel |
70 |
触发做空信号的超买阈值。 |
MaxEntries |
3 |
同方向最多允许的累计下单次数(与 MT4 原策略一致)。 |
CandleType |
TimeFrame(5m) |
用于计算 RSI 的 K 线类型/周期。 |
所有基于 pip 的参数都会结合品种的 Step(最小跳动)转换为绝对价格。对于 5 位外汇报价,会自动将步长扩大 10 倍,使 50 仍代表 5.0 pip,符合原作者的说明。
交易逻辑
- 信号判定:仅处理收盘 K 线,保存最近两次 RSI 值,模拟 MT4 中
iRSI(...,1) 与 iRSI(...,2) 的调用。当 RSI 穿越阈值时产生信号。
- 初始建仓:空仓状态下,出现看涨穿越则买入,看跌穿越则卖出。
- 加码:持仓期间,比较最新收盘价及当根最高/最低价与“最近一次成交价”的差值。若顺势幅度达到
StepPips,且尚未达到 MaxEntries,则再下同样手数的市价单。
- 止损:每次成交后,按照当前持仓均价±
StopLossPips 重新计算整体止损,取最“远”的价格以保护整个仓位。
- 跟踪止损:持仓盈利后,参照 K 线最高价(多头)或最低价(空头)将止损向收益方向推进。额外保留相当于 5 个最小跳动的缓冲,以还原 MT4 中
OrderStopLoss() + 5*Point 的条件。
- 离场:价格触及管理中的止损价位时,通过市价单平掉全部仓位。策略不设固定止盈,仅依赖跟踪止损锁定利润。
实现细节
- 通过高层 API:
SubscribeCandles().Bind(...) 订阅数据,BuyMarket / SellMarket 发送市价单。
_longOrderPending、_shortOrderPending 以及对应的退出标志用于避免在订单等待成交期间重复下单。
- 未调用
StartProtection(),所有风控逻辑均手工实现,以贴近原始 EA。
- 由于 StockSharp 采用净持仓模式,跟踪止损作用于整体仓位。当存在多笔加仓时,一旦综合止损被击中,将同时平掉所有手数。原 EA 分别管理每笔订单的止损,本移植版本在文档中注明了这一差异。
使用建议
- 连接到目标券商/行情源后,为策略指定交易标的,并设置
CandleType 与 MT4 中使用的周期一致(例如 EURUSD 5 分钟)。
- 根据标的波动调整各项 pip 参数。若希望沿用原 EA “五位数需额外加零”的设定,可直接在参数中乘以 10。
- 可通过调整
MaxEntries 与 StepPips 控制加仓频率和激进程度。
- 推荐先在仿真/回测环境验证点值转换与跟踪止损行为,再投入真实交易。
文件列表
CS/EasiestRsiStrategy.cs:策略实现。
README.md:英文说明。
README_zh.md:本文档。
README_ru.md:俄文说明。
根据需求未提供 Python 版本。
using System;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// Easiest RSI: RSI reversal with EMA filter and ATR stops.
/// </summary>
public class EasiestRsiStrategy : Strategy
{
private readonly StrategyParam<DataType> _candleType;
private readonly StrategyParam<int> _rsiLength;
private readonly StrategyParam<int> _emaLength;
private readonly StrategyParam<int> _atrLength;
private decimal _prevRsi;
private decimal _entryPrice;
public EasiestRsiStrategy()
{
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(8).TimeFrame())
.SetDisplay("Candle Type", "Timeframe.", "General");
_rsiLength = Param(nameof(RsiLength), 14)
.SetDisplay("RSI Length", "RSI period.", "Indicators");
_emaLength = Param(nameof(EmaLength), 20)
.SetDisplay("EMA Length", "Trend filter.", "Indicators");
_atrLength = Param(nameof(AtrLength), 14)
.SetDisplay("ATR Length", "ATR period.", "Indicators");
}
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public int RsiLength { get => _rsiLength.Value; set => _rsiLength.Value = value; }
public int EmaLength { get => _emaLength.Value; set => _emaLength.Value = value; }
public int AtrLength { get => _atrLength.Value; set => _atrLength.Value = value; }
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_prevRsi = 0; _entryPrice = 0;
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_prevRsi = 0; _entryPrice = 0;
var rsi = new RelativeStrengthIndex { Length = RsiLength };
var ema = new ExponentialMovingAverage { Length = EmaLength };
var atr = new AverageTrueRange { Length = AtrLength };
var subscription = SubscribeCandles(CandleType);
subscription.Bind(rsi, ema, atr, ProcessCandle).Start();
var area = CreateChartArea();
if (area != null) { DrawCandles(area, subscription); DrawIndicator(area, ema); DrawOwnTrades(area); }
}
private void ProcessCandle(ICandleMessage candle, decimal rsiVal, decimal emaVal, decimal atrVal)
{
if (candle.State != CandleStates.Finished) return;
if (_prevRsi == 0 || atrVal <= 0) { _prevRsi = rsiVal; return; }
var close = candle.ClosePrice;
if (Position > 0)
{
if (close >= _entryPrice + atrVal * 2.5m || close <= _entryPrice - atrVal * 1.5m || rsiVal > 70) { SellMarket(); _entryPrice = 0; }
}
else if (Position < 0)
{
if (close <= _entryPrice - atrVal * 2.5m || close >= _entryPrice + atrVal * 1.5m || rsiVal < 30) { BuyMarket(); _entryPrice = 0; }
}
if (Position == 0)
{
if (rsiVal > 55 && _prevRsi <= 55 && close > emaVal) { _entryPrice = close; BuyMarket(); }
else if (rsiVal < 45 && _prevRsi >= 45 && close < emaVal) { _entryPrice = close; 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.Strategies import Strategy
from StockSharp.Algo.Indicators import RelativeStrengthIndex, ExponentialMovingAverage, AverageTrueRange
class easiest_rsi_strategy(Strategy):
def __init__(self):
super(easiest_rsi_strategy, self).__init__()
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(8))) \
.SetDisplay("Candle Type", "Timeframe.", "General")
self._rsi_length = self.Param("RsiLength", 14) \
.SetDisplay("RSI Length", "RSI period.", "Indicators")
self._ema_length = self.Param("EmaLength", 20) \
.SetDisplay("EMA Length", "Trend filter.", "Indicators")
self._atr_length = self.Param("AtrLength", 14) \
.SetDisplay("ATR Length", "ATR period.", "Indicators")
self._prev_rsi = 0.0
self._entry_price = 0.0
@property
def CandleType(self):
return self._candle_type.Value
@property
def RsiLength(self):
return self._rsi_length.Value
@property
def EmaLength(self):
return self._ema_length.Value
@property
def AtrLength(self):
return self._atr_length.Value
def OnStarted2(self, time):
super(easiest_rsi_strategy, self).OnStarted2(time)
self._prev_rsi = 0.0
self._entry_price = 0.0
self._rsi = RelativeStrengthIndex()
self._rsi.Length = self.RsiLength
self._ema = ExponentialMovingAverage()
self._ema.Length = self.EmaLength
self._atr = AverageTrueRange()
self._atr.Length = self.AtrLength
subscription = self.SubscribeCandles(self.CandleType)
subscription.Bind(self._rsi, self._ema, self._atr, self.ProcessCandle).Start()
def ProcessCandle(self, candle, rsi_val, ema_val, atr_val):
if candle.State != CandleStates.Finished:
return
rv = float(rsi_val)
ev = float(ema_val)
av = float(atr_val)
if self._prev_rsi == 0 or av <= 0:
self._prev_rsi = rv
return
close = float(candle.ClosePrice)
if self.Position > 0:
if close >= self._entry_price + av * 2.5 or close <= self._entry_price - av * 1.5 or rv > 70:
self.SellMarket()
self._entry_price = 0.0
elif self.Position < 0:
if close <= self._entry_price - av * 2.5 or close >= self._entry_price + av * 1.5 or rv < 30:
self.BuyMarket()
self._entry_price = 0.0
if not self.IsFormedAndOnlineAndAllowTrading():
self._prev_rsi = rv
return
if self.Position == 0:
if rv > 55 and self._prev_rsi <= 55 and close > ev:
self._entry_price = close
self.BuyMarket()
elif rv < 45 and self._prev_rsi >= 45 and close < ev:
self._entry_price = close
self.SellMarket()
self._prev_rsi = rv
def OnReseted(self):
super(easiest_rsi_strategy, self).OnReseted()
self._prev_rsi = 0.0
self._entry_price = 0.0
def CreateClone(self):
return easiest_rsi_strategy()