波动率风险溢价
该策略通过卖出期权获取波动率风险溢价,假设隐含波动率平均高于实现波动率。通过 delta 对冲以分离溢价。
空头期权头寸在严格的风险控制和定期再对冲下管理。
细节
- 数据:期权隐含波动率和实现波动率。
- 入场:当隐含 > 实现时卖出价外期权。
- 出场:到期回购或波动率飙升时平仓。
- 工具:指数或外汇期权。
- 风险:delta 对冲和 vega 止损。
// VolatilityRiskPremiumStrategy.cs
// -----------------------------------------------------------------------------
// Volatility risk premium strategy.
// Compares realized volatility (StdDev) to ATR as a proxy for vol premium.
// When realized vol is low relative to ATR (vol premium is high), sells vol
// by going long. When realized vol exceeds ATR, exits to flat.
// Uses Bollinger Bands width as an alternative volatility measure.
// -----------------------------------------------------------------------------
// Date: 2 Aug 2025
// -----------------------------------------------------------------------------
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>
/// Volatility risk premium strategy using realized vs implied volatility proxy.
/// </summary>
public class VolatilityRiskPremiumStrategy : Strategy
{
private readonly StrategyParam<int> _stdDevPeriod;
private readonly StrategyParam<int> _atrPeriod;
private readonly StrategyParam<decimal> _volRatioThreshold;
private readonly StrategyParam<int> _cooldownBars;
private readonly StrategyParam<DataType> _candleType;
/// <summary>
/// Standard deviation period for realized volatility.
/// </summary>
public int StdDevPeriod
{
get => _stdDevPeriod.Value;
set => _stdDevPeriod.Value = value;
}
/// <summary>
/// ATR period for implied movement proxy.
/// </summary>
public int AtrPeriod
{
get => _atrPeriod.Value;
set => _atrPeriod.Value = value;
}
/// <summary>
/// Ratio threshold for vol premium signal.
/// </summary>
public decimal VolRatioThreshold
{
get => _volRatioThreshold.Value;
set => _volRatioThreshold.Value = value;
}
/// <summary>
/// Cooldown bars between trades.
/// </summary>
public int CooldownBars
{
get => _cooldownBars.Value;
set => _cooldownBars.Value = value;
}
/// <summary>
/// The type of candles to use for strategy calculation.
/// </summary>
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
private StandardDeviation _stdDev;
private AverageTrueRange _atr;
private int _cooldownRemaining;
public VolatilityRiskPremiumStrategy()
{
_stdDevPeriod = Param(nameof(StdDevPeriod), 20)
.SetDisplay("StdDev Period", "Period for realized volatility", "Parameters");
_atrPeriod = Param(nameof(AtrPeriod), 14)
.SetDisplay("ATR Period", "Period for ATR calculation", "Parameters");
_volRatioThreshold = Param(nameof(VolRatioThreshold), 1.0m)
.SetDisplay("Vol Ratio Threshold", "StdDev/ATR ratio threshold", "Parameters");
_cooldownBars = Param(nameof(CooldownBars), 20)
.SetDisplay("Cooldown Bars", "Bars to wait between trades", "Risk");
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(15).TimeFrame())
.SetDisplay("Candle Type", "Type of candles to use", "General");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
if (Security != null)
yield return (Security, CandleType);
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_stdDev = null;
_atr = null;
_cooldownRemaining = 0;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_stdDev = new StandardDeviation { Length = StdDevPeriod };
_atr = new AverageTrueRange { Length = AtrPeriod };
SubscribeCandles(CandleType)
.Bind(_stdDev, _atr, ProcessCandle)
.Start();
}
private void ProcessCandle(ICandleMessage candle, decimal stdDevValue, decimal atrValue)
{
if (candle.State != CandleStates.Finished)
return;
if (!_stdDev.IsFormed || !_atr.IsFormed)
return;
if (!IsFormedAndOnlineAndAllowTrading())
return;
if (_cooldownRemaining > 0)
{
_cooldownRemaining--;
return;
}
if (atrValue <= 0)
return;
var volRatio = stdDevValue / atrValue;
// Low realized vol relative to ATR -> vol premium is high -> sell vol (go long)
if (volRatio < VolRatioThreshold && Position <= 0)
{
if (Position < 0)
BuyMarket(Math.Abs(Position));
BuyMarket(Volume);
_cooldownRemaining = CooldownBars;
}
// High realized vol relative to ATR -> vol premium collapsed -> exit or go short
else if (volRatio > VolRatioThreshold * 1.5m && Position >= 0)
{
if (Position > 0)
SellMarket(Math.Abs(Position));
SellMarket(Volume);
_cooldownRemaining = CooldownBars;
}
}
}
import clr
clr.AddReference("StockSharp.Messages")
clr.AddReference("StockSharp.Algo")
clr.AddReference("StockSharp.Algo.Indicators")
clr.AddReference("StockSharp.Algo.Strategies")
from System import TimeSpan, Math
from StockSharp.Messages import DataType, CandleStates
from StockSharp.Algo.Indicators import StandardDeviation, AverageTrueRange
from StockSharp.Algo.Strategies import Strategy
class volatility_risk_premium_strategy(Strategy):
"""Volatility risk premium strategy using realized vs implied volatility proxy."""
def __init__(self):
super(volatility_risk_premium_strategy, self).__init__()
self._stddev_period = self.Param("StdDevPeriod", 20) \
.SetDisplay("StdDev Period", "Period for realized volatility", "Parameters")
self._atr_period = self.Param("AtrPeriod", 14) \
.SetDisplay("ATR Period", "Period for ATR calculation", "Parameters")
self._vol_ratio_threshold = self.Param("VolRatioThreshold", 1.0) \
.SetDisplay("Vol Ratio Threshold", "StdDev/ATR ratio threshold", "Parameters")
self._cooldown_bars = self.Param("CooldownBars", 20) \
.SetDisplay("Cooldown Bars", "Bars to wait between trades", "Risk")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(15))) \
.SetDisplay("Candle Type", "Type of candles to use", "General")
self._stddev = None
self._atr = None
self._cooldown_remaining = 0
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(volatility_risk_premium_strategy, self).OnReseted()
self._stddev = None
self._atr = None
self._cooldown_remaining = 0
def OnStarted2(self, time):
super(volatility_risk_premium_strategy, self).OnStarted2(time)
self._stddev = StandardDeviation()
self._stddev.Length = int(self._stddev_period.Value)
self._atr = AverageTrueRange()
self._atr.Length = int(self._atr_period.Value)
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(self._stddev, self._atr, self._process_candle).Start()
def _process_candle(self, candle, stddev_val, atr_val):
if candle.State != CandleStates.Finished:
return
if not self._stddev.IsFormed or not self._atr.IsFormed:
return
if not self.IsFormedAndOnlineAndAllowTrading():
return
if self._cooldown_remaining > 0:
self._cooldown_remaining -= 1
return
av = float(atr_val)
if av <= 0:
return
vol_ratio = float(stddev_val) / av
vol_thresh = float(self._vol_ratio_threshold.Value)
cooldown = int(self._cooldown_bars.Value)
if vol_ratio < vol_thresh and self.Position <= 0:
if self.Position < 0:
self.BuyMarket(Math.Abs(self.Position))
self.BuyMarket(self.Volume)
self._cooldown_remaining = cooldown
elif vol_ratio > vol_thresh * 1.5 and self.Position >= 0:
if self.Position > 0:
self.SellMarket(Math.Abs(self.Position))
self.SellMarket(self.Volume)
self._cooldown_remaining = cooldown
def CreateClone(self):
return volatility_risk_premium_strategy()