VIX 波动率飙升策略
当 VIX 指数高于其均值加上若干倍标准差时买入,并在固定数量的柱后平仓。
详情
- 入场条件: VIX > 均值 + StdDevMultiplier × 标准差。
- 多空方向: 仅做多。
- 出场条件:
ExitPeriods根柱后平仓。 - 止损: 有。
- 默认值:
StdDevLength= 15StdDevMultiplier= 2ExitPeriods= 10CandleType= TimeSpan.FromMinutes(5)VixSecurity= "CBOE:VIX"
- 过滤器:
- 类别: Volatility
- 方向: Long
- 指标: SMA, StdDev
- 止损: Yes
- 复杂度: Beginner
- 时间框架: Intraday
- 季节性: No
- 神经网络: No
- 背离: No
- 风险等级: Medium
using System;
using System.Collections.Generic;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// VIX Spike Strategy.
/// Uses Bollinger Bands to detect price spikes and trades mean reversion.
/// </summary>
public class VixSpikeStrategy : Strategy
{
private readonly StrategyParam<int> _bbLength;
private readonly StrategyParam<decimal> _bbWidth;
private readonly StrategyParam<int> _exitPeriods;
private readonly StrategyParam<DataType> _candleType;
private int _cooldown;
public int BbLength { get => _bbLength.Value; set => _bbLength.Value = value; }
public decimal BbWidth { get => _bbWidth.Value; set => _bbWidth.Value = value; }
public int ExitPeriods { get => _exitPeriods.Value; set => _exitPeriods.Value = value; }
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public VixSpikeStrategy()
{
_bbLength = Param(nameof(BbLength), 20)
.SetGreaterThanZero()
.SetDisplay("BB Length", "Bollinger Bands length", "Parameters");
_bbWidth = Param(nameof(BbWidth), 2m)
.SetGreaterThanZero()
.SetDisplay("BB Width", "Bollinger Bands width multiplier", "Parameters");
_exitPeriods = Param(nameof(ExitPeriods), 15)
.SetGreaterThanZero()
.SetDisplay("Exit Bars", "Bars to hold position", "Parameters");
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
.SetDisplay("Candle Type", "Type of candles", "Parameters");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_cooldown = 0;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var bb = new BollingerBands
{
Length = BbLength,
Width = BbWidth
};
_cooldown = 0;
var subscription = SubscribeCandles(CandleType);
subscription
.BindEx(bb, ProcessCandle)
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, bb);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle, IIndicatorValue value)
{
if (candle.State != CandleStates.Finished)
return;
var bb = value as BollingerBandsValue;
if (bb == null)
return;
if (bb.UpBand is not decimal upper ||
bb.LowBand is not decimal lower)
return;
if (_cooldown > 0)
{
_cooldown--;
return;
}
// Spike down below lower band - buy (mean reversion)
if (candle.ClosePrice < lower && Position <= 0)
{
BuyMarket();
_cooldown = 60;
}
// Spike up above upper band - sell (mean reversion)
else if (candle.ClosePrice > upper && Position >= 0)
{
SellMarket();
_cooldown = 60;
}
}
}
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 BollingerBands
from StockSharp.Algo.Strategies import Strategy
class vix_spike_strategy(Strategy):
def __init__(self):
super(vix_spike_strategy, self).__init__()
self._bb_length = self.Param("BbLength", 20) \
.SetDisplay("BB Length", "Bollinger Bands length", "Parameters")
self._bb_width = self.Param("BbWidth", 2.0) \
.SetDisplay("BB Width", "Bollinger Bands width multiplier", "Parameters")
self._exit_periods = self.Param("ExitPeriods", 15) \
.SetDisplay("Exit Bars", "Bars to hold position", "Parameters")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(5))) \
.SetDisplay("Candle Type", "Type of candles", "Parameters")
self._cooldown = 0
@property
def bb_length(self):
return self._bb_length.Value
@property
def bb_width(self):
return self._bb_width.Value
@property
def exit_periods(self):
return self._exit_periods.Value
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(vix_spike_strategy, self).OnReseted()
self._cooldown = 0
def OnStarted2(self, time):
super(vix_spike_strategy, self).OnStarted2(time)
bb = BollingerBands()
bb.Length = self.bb_length
bb.Width = self.bb_width
self._cooldown = 0
subscription = self.SubscribeCandles(self.candle_type)
subscription.BindEx(bb, self.on_process).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawIndicator(area, bb)
self.DrawOwnTrades(area)
def on_process(self, candle, value):
if candle.State != CandleStates.Finished:
return
upper = float(value.UpBand)
lower = float(value.LowBand)
if upper == 0 or lower == 0:
return
if self._cooldown > 0:
self._cooldown -= 1
return
close = float(candle.ClosePrice)
if close < lower and self.Position <= 0:
self.BuyMarket()
self._cooldown = 60
elif close > upper and self.Position >= 0:
self.SellMarket()
self._cooldown = 60
def CreateClone(self):
return vix_spike_strategy()