在 GitHub 上查看
No Nonsense Tester 策略
概述
No Nonsense Tester 策略 是 MQL4 "NoNonsenseTester" 智能交易系统在 StockSharp 平台上的移植版本。实现遵循 NNFX 的核心流程:先确认趋势基准线,再等待两个确认指标,使用 ATR 检查波动性,并通过严格的退出规则管理头寸。策略全部关键阈值都通过 StrategyParam 参数公开,方便在 StockSharp 中进行批量优化测试。
交易逻辑
- 基准线过滤:可配置周期的 EMA 定义主要趋势,仅在收盘价穿越基准线时考虑入场。
- 确认指标一:RSI 必须位于多头阈值之上或空头互补阈值之下,用于验证基准线突破。
- 确认指标二:CCI 需与趋势方向一致,并超过设定的绝对幅度,以过滤掉噪音信号。
- 波动性过滤:ATR 必须高于
AtrMinimum,确保仅在具备足够波动的市场环境中开仓。
- 开仓:当基准线穿越、两项确认和波动性条件全部满足时,策略按突破方向建仓。
AtrEntryMultiplier 参数允许按 ATR 规模调整下单量。
- 止盈止损:建仓后立刻计算 ATR 倍数的止损与止盈。若启用 ATR 拖尾,则在行情向有利方向发展时持续上调保护止损。
- 退出覆盖:额外的短周期 RSI 监控持仓,一旦多头跌破下轨或空头突破上轨,即使价格尚未触及保护位也会提前离场。
参数说明
| 参数 |
说明 |
BaselineLength |
EMA 基准线周期。 |
ConfirmationRsiLength |
RSI 确认指标周期。 |
ConfirmationRsiThreshold |
RSI 多空分界阈值。 |
ConfirmationCciLength |
CCI 确认指标周期。 |
ConfirmationCciThreshold |
接受信号所需的 CCI 绝对幅度。 |
AtrPeriod |
ATR 计算周期。 |
AtrEntryMultiplier |
按 ATR 调整下单量的倍数。 |
AtrTakeProfitMultiplier |
止盈使用的 ATR 倍数。 |
AtrStopLossMultiplier |
止损使用的 ATR 倍数。 |
AtrTrailingMultiplier |
拖尾止损的 ATR 倍数,设为 0 表示关闭。 |
AtrMinimum |
开仓所需的最小 ATR。 |
ExitRsiLength |
退出 RSI 的周期。 |
ExitRsiUpperLevel |
触发空头离场的 RSI 上轨。 |
ExitRsiLowerLevel |
触发多头离场的 RSI 下轨。 |
CandleType |
计算使用的蜡烛类型(时间框架)。 |
图表元素
策略会自动绘制:
优化建议
所有核心 StrategyParam 参数都设置了优化范围,延续原版测试器的可调性。可通过 StockSharp 优化工具扫描不同的基准线周期、确认阈值以及风险设置,复现 MQL 版本的参数网格测试。
使用提示
- 根据个人 NNFX 指标组合调整阈值,快速验证自定义模板。
- 合理设置
AtrMinimum,避免在低波动区间频繁交易。
- 若要测试续仓策略,可将
AtrTrailingMultiplier 设为大于零,让盈利头寸在保护止损的同时继续扩展。
namespace StockSharp.Samples.Strategies;
using System;
using Ecng.Common;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.Messages;
/// <summary>
/// No Nonsense Tester strategy: EMA baseline + RSI confirmation + CCI confirmation.
/// Buys when price above EMA, RSI above 50, CCI above 0.
/// Sells when price below EMA, RSI below 50, CCI below 0.
/// </summary>
public class NoNonsenseTesterStrategy : Strategy
{
private readonly StrategyParam<DataType> _candleType;
private readonly StrategyParam<int> _emaPeriod;
private readonly StrategyParam<int> _rsiPeriod;
private readonly StrategyParam<int> _cciPeriod;
private bool _wasBullish;
private bool _hasPrevSignal;
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public int EmaPeriod { get => _emaPeriod.Value; set => _emaPeriod.Value = value; }
public int RsiPeriod { get => _rsiPeriod.Value; set => _rsiPeriod.Value = value; }
public int CciPeriod { get => _cciPeriod.Value; set => _cciPeriod.Value = value; }
public NoNonsenseTesterStrategy()
{
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(15).TimeFrame())
.SetDisplay("Candle Type", "Candle timeframe", "General");
_emaPeriod = Param(nameof(EmaPeriod), 25)
.SetGreaterThanZero()
.SetDisplay("EMA Period", "EMA baseline period", "Indicators");
_rsiPeriod = Param(nameof(RsiPeriod), 14)
.SetGreaterThanZero()
.SetDisplay("RSI Period", "RSI confirmation period", "Indicators");
_cciPeriod = Param(nameof(CciPeriod), 14)
.SetGreaterThanZero()
.SetDisplay("CCI Period", "CCI confirmation period", "Indicators");
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_wasBullish = false;
_hasPrevSignal = false;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_hasPrevSignal = false;
var ema = new ExponentialMovingAverage { Length = EmaPeriod };
var rsi = new RelativeStrengthIndex { Length = RsiPeriod };
var cci = new CommodityChannelIndex { Length = CciPeriod };
var subscription = SubscribeCandles(CandleType);
subscription.Bind(ema, rsi, cci, ProcessCandle).Start();
}
private void ProcessCandle(ICandleMessage candle, decimal ema, decimal rsi, decimal cci)
{
if (candle.State != CandleStates.Finished) return;
var close = candle.ClosePrice;
var isBullish = close > ema && rsi > 50 && cci > 0;
if (_hasPrevSignal && isBullish != _wasBullish)
{
if (isBullish && Position <= 0)
BuyMarket();
else if (!isBullish && close < ema && rsi < 50 && cci < 0 && Position >= 0)
SellMarket();
}
_wasBullish = isBullish;
_hasPrevSignal = true;
}
}
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 ExponentialMovingAverage, RelativeStrengthIndex, CommodityChannelIndex
from StockSharp.Algo.Strategies import Strategy
class no_nonsense_tester_strategy(Strategy):
def __init__(self):
super(no_nonsense_tester_strategy, self).__init__()
self._ema_period = self.Param("EmaPeriod", 25) \
.SetDisplay("EMA Period", "EMA baseline period", "Indicators")
self._rsi_period = self.Param("RsiPeriod", 14) \
.SetDisplay("RSI Period", "RSI confirmation period", "Indicators")
self._cci_period = self.Param("CciPeriod", 14) \
.SetDisplay("CCI Period", "CCI confirmation period", "Indicators")
self._ema = None
self._rsi = None
self._cci = None
self._was_bullish = False
self._has_prev_signal = False
@property
def ema_period(self):
return self._ema_period.Value
@property
def rsi_period(self):
return self._rsi_period.Value
@property
def cci_period(self):
return self._cci_period.Value
def OnReseted(self):
super(no_nonsense_tester_strategy, self).OnReseted()
self._ema = None
self._rsi = None
self._cci = None
self._was_bullish = False
self._has_prev_signal = False
def OnStarted2(self, time):
super(no_nonsense_tester_strategy, self).OnStarted2(time)
self._ema = ExponentialMovingAverage()
self._ema.Length = self.ema_period
self._rsi = RelativeStrengthIndex()
self._rsi.Length = self.rsi_period
self._cci = CommodityChannelIndex()
self._cci.Length = self.cci_period
self._has_prev_signal = False
subscription = self.SubscribeCandles(DataType.TimeFrame(TimeSpan.FromMinutes(15)))
subscription.Bind(self._ema, self._rsi, self._cci, self._process_candle)
subscription.Start()
def _process_candle(self, candle, ema_value, rsi_value, cci_value):
if candle.State != CandleStates.Finished:
return
if not self._ema.IsFormed or not self._rsi.IsFormed or not self._cci.IsFormed:
return
close = float(candle.ClosePrice)
ema_val = float(ema_value)
rsi_val = float(rsi_value)
cci_val = float(cci_value)
is_bullish = close > ema_val and rsi_val > 50.0 and cci_val > 0.0
if self._has_prev_signal and is_bullish != self._was_bullish:
if is_bullish and self.Position <= 0:
self.BuyMarket()
elif not is_bullish and close < ema_val and rsi_val < 50.0 and cci_val < 0.0 and self.Position >= 0:
self.SellMarket()
self._was_bullish = is_bullish
self._has_prev_signal = True
def CreateClone(self):
return no_nonsense_tester_strategy()