所有输入都通过 StrategyParam<T> 创建,可在 StockSharp 前端直接修改或用于参数优化,完全对应原始 EA 的设置方式。
默认设置
趋势周期:6 小时。
信号周期:30 分钟。
移动平均类型:全部为简单移动平均 (SMA)。
移动平均长度:三段平滑分别为 4 / 13 / 13,信号线长度为 4(两个周期均相同)。
SignalBar:1(使用最近一根已收盘 K 线)。
TradeVolume:1 手。
所有权限开关:默认开启。
其他说明
策略不会自动下达止损或止盈单,需要时请结合独立的风险管理模块。
图表会绘制信号周期的 K 线、两条 WAMI 曲线以及交易记录;趋势周期显示在单独的图表区域,方便人工核对。
实现完全采用蜡烛订阅和 BindEx 回调,没有直接调用指标的 GetValue 等低级接口,满足项目的高层 API 要求。
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>
/// WAMI Cloud X2 strategy (simplified). Uses RSI momentum with EMA trend
/// filter as a proxy for the original multi-stage WAMI oscillator.
/// </summary>
public class WamiCloudX2Strategy : Strategy
{
private readonly StrategyParam<DataType> _candleType;
private readonly StrategyParam<int> _rsiLength;
private readonly StrategyParam<int> _emaLength;
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 WamiCloudX2Strategy()
{
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
.SetDisplay("Candle Type", "Candles", "General");
_rsiLength = Param(nameof(RsiLength), 14)
.SetGreaterThanZero()
.SetDisplay("RSI Length", "RSI period", "Indicators");
_emaLength = Param(nameof(EmaLength), 50)
.SetGreaterThanZero()
.SetDisplay("EMA Length", "Trend EMA", "Indicators");
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var rsi = new RelativeStrengthIndex { Length = RsiLength };
var ema = new ExponentialMovingAverage { Length = EmaLength };
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(rsi, ema, (ICandleMessage candle, decimal rsiValue, decimal emaValue) =>
{
if (candle.State != CandleStates.Finished)
return;
if (!IsFormedAndOnlineAndAllowTrading())
return;
var close = candle.ClosePrice;
// Price above EMA with RSI not overbought => buy
if (close > emaValue && rsiValue > 55m && Position <= 0)
BuyMarket();
// Price below EMA with RSI not oversold => sell
else if (close < emaValue && rsiValue < 45m && Position >= 0)
SellMarket();
})
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, ema);
DrawOwnTrades(area);
}
}
}
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, ExponentialMovingAverage
from StockSharp.Algo.Strategies import Strategy
class wami_cloud_x2_strategy(Strategy):
def __init__(self):
super(wami_cloud_x2_strategy, self).__init__()
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4))) \
.SetDisplay("Candle Type", "Candles", "General")
self._rsi_length = self.Param("RsiLength", 14) \
.SetDisplay("RSI Length", "RSI period", "Indicators")
self._ema_length = self.Param("EmaLength", 50) \
.SetDisplay("EMA Length", "Trend EMA", "Indicators")
@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
def OnStarted2(self, time):
super(wami_cloud_x2_strategy, self).OnStarted2(time)
rsi = RelativeStrengthIndex()
rsi.Length = self.RsiLength
ema = ExponentialMovingAverage()
ema.Length = self.EmaLength
subscription = self.SubscribeCandles(self.CandleType)
subscription.Bind(rsi, ema, self._on_process).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawIndicator(area, ema)
self.DrawOwnTrades(area)
def _on_process(self, candle, rsi_value, ema_value):
if candle.State != CandleStates.Finished:
return
close = float(candle.ClosePrice)
rv = float(rsi_value)
ev = float(ema_value)
if close > ev and rv > 55 and self.Position <= 0:
self.BuyMarket()
elif close < ev and rv < 45 and self.Position >= 0:
self.SellMarket()
def CreateClone(self):
return wami_cloud_x2_strategy()