VWAP Mean Magnet v9(简单提醒)策略
该简化版本的 VWAP Mean Magnet 策略只使用 VWAP 和 RSI,不包含成交量过滤。当价格偏离 VWAP 且 RSI 达到极端水平时入场,价格回到 VWAP 时平仓。
详情
- 入场条件:
- 多头: 价格 < VWAP 且 RSI < 超卖区域。
- 空头: 价格 > VWAP 且 RSI > 超买区域。
- 多空方向: 双向。
- 出场条件:
- 当价格回到 VWAP 时平仓。
- 止损: 是,百分比止损。
- 默认值:
VWAP 长度= 60RSI 长度= 14RSI 超买= 65RSI 超卖= 25止损 %= 0.5
- 过滤器:
- 分类: 均值回归
- 方向: 双向
- 指标: 多个
- 止损: 是
- 复杂度: 简单
- 时间框架: 日内
using System;
using System.Linq;
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>
/// VWAP mean reversion with RSI.
/// </summary>
public class VwapMeanMagnetV9SimpleAlertStrategy : Strategy
{
private readonly StrategyParam<int> _vwapLength;
private readonly StrategyParam<int> _rsiLength;
private readonly StrategyParam<int> _rsiOverbought;
private readonly StrategyParam<int> _rsiOversold;
private readonly StrategyParam<decimal> _stopLossPercent;
private readonly StrategyParam<DataType> _candleType;
public int VwapLength { get => _vwapLength.Value; set => _vwapLength.Value = value; }
public int RsiLength { get => _rsiLength.Value; set => _rsiLength.Value = value; }
public int RsiOverbought { get => _rsiOverbought.Value; set => _rsiOverbought.Value = value; }
public int RsiOversold { get => _rsiOversold.Value; set => _rsiOversold.Value = value; }
public decimal StopLossPercent { get => _stopLossPercent.Value; set => _stopLossPercent.Value = value; }
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public VwapMeanMagnetV9SimpleAlertStrategy()
{
_vwapLength = Param(nameof(VwapLength), 20).SetDisplay("VWAP Length", "VWAP Length", "General");
_rsiLength = Param(nameof(RsiLength), 14).SetDisplay("RSI Length", "RSI Length", "General");
_rsiOverbought = Param(nameof(RsiOverbought), 65).SetDisplay("RSI Overbought", "RSI Overbought", "General");
_rsiOversold = Param(nameof(RsiOversold), 35).SetDisplay("RSI Oversold", "RSI Oversold", "General");
_stopLossPercent = Param(nameof(StopLossPercent), 0.5m).SetDisplay("Stop Loss %", "Stop Loss %", "General");
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame()).SetDisplay("Candle Type", "Candle Type", "General");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
=> [(Security, CandleType)];
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var vwap = new VolumeWeightedMovingAverage { Length = VwapLength };
var rsi = new RelativeStrengthIndex { Length = RsiLength };
var subscription = SubscribeCandles(CandleType);
subscription.Bind(vwap, rsi, ProcessCandle).Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, vwap);
DrawIndicator(area, rsi);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle, decimal vwapValue, decimal rsiValue)
{
if (candle.State != CandleStates.Finished)
return;
if (candle.ClosePrice < vwapValue && rsiValue < RsiOversold && Position <= 0)
BuyMarket();
else if (candle.ClosePrice > vwapValue && rsiValue > RsiOverbought && Position >= 0)
SellMarket();
if (Position > 0 && candle.ClosePrice >= vwapValue)
SellMarket();
else if (Position < 0 && candle.ClosePrice <= vwapValue)
BuyMarket();
}
}
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, VolumeWeightedMovingAverage
from StockSharp.Algo.Strategies import Strategy
class vwap_mean_magnet_v9_simple_alert_strategy(Strategy):
def __init__(self):
super(vwap_mean_magnet_v9_simple_alert_strategy, self).__init__()
self._vwap_length = self.Param("VwapLength", 20) \
.SetDisplay("VWAP Length", "VWAP Length", "General")
self._rsi_length = self.Param("RsiLength", 14) \
.SetDisplay("RSI Length", "RSI Length", "General")
self._rsi_overbought = self.Param("RsiOverbought", 65) \
.SetDisplay("RSI Overbought", "RSI Overbought", "General")
self._rsi_oversold = self.Param("RsiOversold", 35) \
.SetDisplay("RSI Oversold", "RSI Oversold", "General")
self._stop_loss_percent = self.Param("StopLossPercent", 0.5) \
.SetDisplay("Stop Loss %", "Stop Loss %", "General")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4))) \
.SetDisplay("Candle Type", "Candle Type", "General")
@property
def vwap_length(self):
return self._vwap_length.Value
@property
def rsi_length(self):
return self._rsi_length.Value
@property
def rsi_overbought(self):
return self._rsi_overbought.Value
@property
def rsi_oversold(self):
return self._rsi_oversold.Value
@property
def stop_loss_percent(self):
return self._stop_loss_percent.Value
@property
def candle_type(self):
return self._candle_type.Value
def OnStarted2(self, time):
super(vwap_mean_magnet_v9_simple_alert_strategy, self).OnStarted2(time)
vwap = VolumeWeightedMovingAverage()
vwap.Length = self.vwap_length
rsi = RelativeStrengthIndex()
rsi.Length = self.rsi_length
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(vwap, rsi, self.on_process).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawIndicator(area, vwap)
self.DrawIndicator(area, rsi)
self.DrawOwnTrades(area)
def on_process(self, candle, vwap_value, rsi_value):
if candle.State != CandleStates.Finished:
return
if candle.ClosePrice < vwap_value and rsi_value < self.rsi_oversold and self.Position <= 0:
self.BuyMarket()
elif candle.ClosePrice > vwap_value and rsi_value > self.rsi_overbought and self.Position >= 0:
self.SellMarket()
if self.Position > 0 and candle.ClosePrice >= vwap_value:
self.SellMarket()
elif self.Position < 0 and candle.ClosePrice <= vwap_value:
self.BuyMarket()
def CreateClone(self):
return vwap_mean_magnet_v9_simple_alert_strategy()