Binary Wave StdDev 策略
该策略对 MA、MACD、CCI、动量、RSI 和 ADX 的信号按权重求和。 当标准差表示的波动率超过阈值时,按累计得分的方向开仓。 可选的止损和止盈以点为单位。
细节
- 入场条件:
- 多头:得分 > 0 且 StdDev >= EntryVolatility
- 空头:得分 < 0 且 StdDev >= EntryVolatility
- 出场条件:
- 波动率下降到 ExitVolatility 以下
- 止损/止盈:通过
UseStopLoss和UseTakeProfit可选 - 默认值:
WeightMa= 1WeightMacd= 1WeightCci= 1WeightMomentum= 1WeightRsi= 1WeightAdx= 1MaPeriod= 13FastMacd= 12SlowMacd= 26SignalMacd= 9CciPeriod= 14MomentumPeriod= 14RsiPeriod= 14AdxPeriod= 14StdDevPeriod= 9EntryVolatility= 1.5ExitVolatility= 1StopLossPoints= 1000TakeProfitPoints= 2000UseStopLoss= falseUseTakeProfit= falseCandleType= TimeSpan.FromHours(4).TimeFrame()
- 过滤:
- 类别:趋势跟随
- 方向:双向
- 指标:MA、MACD、CCI、Momentum、RSI、ADX、StandardDeviation
- 止损:可选
- 复杂度:中
- 时间框架:中期
- 季节性:无
- 神经网络:无
- 背离:无
- 风险等级:中
using System;
using System.Collections.Generic;
using Ecng.Common;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
using StockSharp.Algo;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// Binary Wave Standard Deviation strategy.
/// Combines multiple indicators with weights and uses volatility filter based on standard deviation.
/// </summary>
public class BinaryWaveStdDevStrategy : Strategy
{
private readonly StrategyParam<decimal> _weightMa;
private readonly StrategyParam<decimal> _weightCci;
private readonly StrategyParam<decimal> _weightRsi;
private readonly StrategyParam<int> _maPeriod;
private readonly StrategyParam<int> _cciPeriod;
private readonly StrategyParam<int> _rsiPeriod;
private readonly StrategyParam<int> _stdDevPeriod;
private readonly StrategyParam<DataType> _candleType;
public decimal WeightMa { get => _weightMa.Value; set => _weightMa.Value = value; }
public decimal WeightCci { get => _weightCci.Value; set => _weightCci.Value = value; }
public decimal WeightRsi { get => _weightRsi.Value; set => _weightRsi.Value = value; }
public int MaPeriod { get => _maPeriod.Value; set => _maPeriod.Value = value; }
public int CciPeriod { get => _cciPeriod.Value; set => _cciPeriod.Value = value; }
public int RsiPeriod { get => _rsiPeriod.Value; set => _rsiPeriod.Value = value; }
public int StdDevPeriod { get => _stdDevPeriod.Value; set => _stdDevPeriod.Value = value; }
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public BinaryWaveStdDevStrategy()
{
_weightMa = Param(nameof(WeightMa), 1m)
.SetDisplay("MA Weight", "Weight for moving average direction", "Weights");
_weightCci = Param(nameof(WeightCci), 1m)
.SetDisplay("CCI Weight", "Weight for CCI direction", "Weights");
_weightRsi = Param(nameof(WeightRsi), 1m)
.SetDisplay("RSI Weight", "Weight for RSI", "Weights");
_maPeriod = Param(nameof(MaPeriod), 13)
.SetDisplay("MA Period", "Moving average period", "Indicators");
_cciPeriod = Param(nameof(CciPeriod), 14)
.SetDisplay("CCI Period", "Lookback period for CCI", "Indicators");
_rsiPeriod = Param(nameof(RsiPeriod), 14)
.SetDisplay("RSI Period", "Lookback for RSI", "Indicators");
_stdDevPeriod = Param(nameof(StdDevPeriod), 9)
.SetDisplay("StdDev Period", "Length of standard deviation", "Indicators");
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
.SetDisplay("Candle Type", "Timeframe for candles", "General");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var ema = new ExponentialMovingAverage { Length = MaPeriod };
var cci = new CommodityChannelIndex { Length = CciPeriod };
var rsi = new RelativeStrengthIndex { Length = RsiPeriod };
var stdDev = new StandardDeviation { Length = StdDevPeriod };
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(ema, cci, rsi, stdDev, ProcessCandle)
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, ema);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle, decimal emaValue, decimal cciValue, decimal rsiValue, decimal stdDevValue)
{
if (candle.State != CandleStates.Finished)
return;
if (!IsFormedAndOnlineAndAllowTrading())
return;
var score = 0m;
score += candle.ClosePrice > emaValue ? WeightMa : -WeightMa;
score += cciValue > 0 ? WeightCci : -WeightCci;
score += rsiValue > 50 ? WeightRsi : -WeightRsi;
if (score > 0 && Position <= 0)
BuyMarket();
else if (score < 0 && Position >= 0)
SellMarket();
}
}
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, CommodityChannelIndex, RelativeStrengthIndex, StandardDeviation
from StockSharp.Algo.Strategies import Strategy
class binary_wave_std_dev_strategy(Strategy):
def __init__(self):
super(binary_wave_std_dev_strategy, self).__init__()
self._weight_ma = self.Param("WeightMa", 1.0) \
.SetDisplay("MA Weight", "Weight for moving average direction", "Weights")
self._weight_cci = self.Param("WeightCci", 1.0) \
.SetDisplay("CCI Weight", "Weight for CCI direction", "Weights")
self._weight_rsi = self.Param("WeightRsi", 1.0) \
.SetDisplay("RSI Weight", "Weight for RSI", "Weights")
self._ma_period = self.Param("MaPeriod", 13) \
.SetDisplay("MA Period", "Moving average period", "Indicators")
self._cci_period = self.Param("CciPeriod", 14) \
.SetDisplay("CCI Period", "Lookback period for CCI", "Indicators")
self._rsi_period = self.Param("RsiPeriod", 14) \
.SetDisplay("RSI Period", "Lookback for RSI", "Indicators")
self._std_dev_period = self.Param("StdDevPeriod", 9) \
.SetDisplay("StdDev Period", "Length of standard deviation", "Indicators")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4))) \
.SetDisplay("Candle Type", "Timeframe for candles", "General")
@property
def weight_ma(self):
return self._weight_ma.Value
@property
def weight_cci(self):
return self._weight_cci.Value
@property
def weight_rsi(self):
return self._weight_rsi.Value
@property
def ma_period(self):
return self._ma_period.Value
@property
def cci_period(self):
return self._cci_period.Value
@property
def rsi_period(self):
return self._rsi_period.Value
@property
def std_dev_period(self):
return self._std_dev_period.Value
@property
def candle_type(self):
return self._candle_type.Value
def OnStarted2(self, time):
super(binary_wave_std_dev_strategy, self).OnStarted2(time)
ema = ExponentialMovingAverage()
ema.Length = self.ma_period
cci = CommodityChannelIndex()
cci.Length = self.cci_period
rsi = RelativeStrengthIndex()
rsi.Length = self.rsi_period
std_dev = StandardDeviation()
std_dev.Length = self.std_dev_period
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(ema, cci, rsi, std_dev, self.process_candle).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawIndicator(area, ema)
self.DrawOwnTrades(area)
def process_candle(self, candle, ema_value, cci_value, rsi_value, std_dev_value):
if candle.State != CandleStates.Finished:
return
ema_value = float(ema_value)
cci_value = float(cci_value)
rsi_value = float(rsi_value)
close_price = float(candle.ClosePrice)
w_ma = float(self.weight_ma)
w_cci = float(self.weight_cci)
w_rsi = float(self.weight_rsi)
score = 0.0
score += w_ma if close_price > ema_value else -w_ma
score += w_cci if cci_value > 0 else -w_cci
score += w_rsi if rsi_value > 50 else -w_rsi
if score > 0 and self.Position <= 0:
self.BuyMarket()
elif score < 0 and self.Position >= 0:
self.SellMarket()
def CreateClone(self):
return binary_wave_std_dev_strategy()