Exp QQE Cloud 策略
该策略在平滑 RSI 上应用 QQE 指标,仅在预设的交易时段开始时开仓, 并在出现相反信号或到达结束时间时平仓。
细节
- 入场条件:
- 做多:在
StartHour:StartMinute时,QQE 趋势转向上。 - 做空:在
StartHour:StartMinute时,QQE 趋势转向下。
- 做多:在
- 出场条件:
- 相反的 QQE 趋势信号。
- 时间超过
StopHour:StopMinute。
- 指标:
- RSI(周期
RsiPeriod,平滑RsiSmoothing) - QQE 带宽,系数
QqeFactor
- RSI(周期
- 止损:默认无。
- 默认值:
CandleType= 1 分钟RsiPeriod= 14RsiSmoothing= 5QqeFactor= 4.236StartHour= 0,StartMinute= 0StopHour= 23,StopMinute= 59
- 过滤:
- 进出场时间窗口
- 趋势跟随,单一时间框架
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>
/// QQE Cloud strategy.
/// Uses RSI with EMA smoothing and volatility-based bands for trend detection.
/// Buys when smoothed RSI crosses above upper band, sells when it crosses below lower band.
/// </summary>
public class ExpQqeCloudStrategy : Strategy
{
private readonly StrategyParam<DataType> _candleType;
private readonly StrategyParam<int> _rsiPeriod;
private readonly StrategyParam<int> _smoothPeriod;
private readonly StrategyParam<decimal> _qqeFactor;
private int _barCount;
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public int RsiPeriod { get => _rsiPeriod.Value; set => _rsiPeriod.Value = value; }
public int SmoothPeriod { get => _smoothPeriod.Value; set => _smoothPeriod.Value = value; }
public decimal QqeFactor { get => _qqeFactor.Value; set => _qqeFactor.Value = value; }
public ExpQqeCloudStrategy()
{
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
.SetDisplay("Candle type", "Candle type for strategy calculation.", "General");
_rsiPeriod = Param(nameof(RsiPeriod), 14)
.SetDisplay("RSI Length", "RSI period", "QQE");
_smoothPeriod = Param(nameof(SmoothPeriod), 5)
.SetDisplay("Smooth Period", "EMA smoothing period for RSI", "QQE");
_qqeFactor = Param(nameof(QqeFactor), 4.236m)
.SetDisplay("QQE Factor", "QQE volatility factor", "QQE");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_barCount = 0;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_barCount = 0;
var rsi = new RelativeStrengthIndex { Length = RsiPeriod };
var ema = new ExponentialMovingAverage { Length = SmoothPeriod };
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(rsi, ema, ProcessCandle)
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, rsi);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle, decimal rsiValue, decimal emaValue)
{
if (candle.State != CandleStates.Finished)
return;
_barCount++;
// Use EMA of price as trend filter, RSI for signals
// QQE-style: when RSI is strong (>60) and price above EMA -> buy
// when RSI is weak (<40) and price below EMA -> sell
if (_barCount < 3)
return;
var price = candle.ClosePrice;
if (!IsFormedAndOnlineAndAllowTrading())
return;
if (rsiValue > 65 && price > emaValue && Position <= 0)
BuyMarket();
else if (rsiValue < 35 && price < emaValue && 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 RelativeStrengthIndex, ExponentialMovingAverage
from StockSharp.Algo.Strategies import Strategy
class exp_qqe_cloud_strategy(Strategy):
def __init__(self):
super(exp_qqe_cloud_strategy, self).__init__()
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4))) \
.SetDisplay("Candle type", "Candle type for strategy calculation.", "General")
self._rsi_period = self.Param("RsiPeriod", 14) \
.SetDisplay("RSI Length", "RSI period", "QQE")
self._smooth_period = self.Param("SmoothPeriod", 5) \
.SetDisplay("Smooth Period", "EMA smoothing period for RSI", "QQE")
self._qqe_factor = self.Param("QqeFactor", 4.236) \
.SetDisplay("QQE Factor", "QQE volatility factor", "QQE")
self._bar_count = 0
@property
def candle_type(self):
return self._candle_type.Value
@property
def rsi_period(self):
return self._rsi_period.Value
@property
def smooth_period(self):
return self._smooth_period.Value
@property
def qqe_factor(self):
return self._qqe_factor.Value
def OnReseted(self):
super(exp_qqe_cloud_strategy, self).OnReseted()
self._bar_count = 0
def OnStarted2(self, time):
super(exp_qqe_cloud_strategy, self).OnStarted2(time)
self._bar_count = 0
rsi = RelativeStrengthIndex()
rsi.Length = int(self.rsi_period)
ema = ExponentialMovingAverage()
ema.Length = int(self.smooth_period)
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(rsi, ema, self.process_candle).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawIndicator(area, rsi)
self.DrawOwnTrades(area)
def process_candle(self, candle, rsi_value, ema_value):
if candle.State != CandleStates.Finished:
return
self._bar_count += 1
if self._bar_count < 3:
return
rsi_value = float(rsi_value)
ema_value = float(ema_value)
price = float(candle.ClosePrice)
if rsi_value > 65 and price > ema_value and self.Position <= 0:
self.BuyMarket()
elif rsi_value < 35 and price < ema_value and self.Position >= 0:
self.SellMarket()
def CreateClone(self):
return exp_qqe_cloud_strategy()