Estrategia de Expansión de Volatilidad Parabolic SAR
La estrategia Parabolic SAR Volatility Expansion está construida en torno al Parabolic SAR con detección de expansión de volatilidad.
Las pruebas indican un rendimiento anual promedio de aproximadamente 49%. Funciona mejor en el mercado de criptomonedas.
Las señales se activan cuando Parabolic confirma cambios de tendencia en datos intradía (5m). Esto hace que el método sea adecuado para traders activos.
Los stops dependen de múltiplos de ATR y factores como SarAf, SarMaxAf. Ajuste estos valores predeterminados para equilibrar el riesgo y la recompensa.
Detalles
- Criterios de entrada: ver implementación para condiciones de indicadores.
- Largo/Corto: Ambos.
- Criterios de salida: señal opuesta o lógica de stops.
- Stops: Sí, usando cálculos basados en indicadores.
- Valores predeterminados:
SarAf = 0.02mSarMaxAf = 0.2mAtrPeriod = 14VolatilityExpansionFactor = 2.0mCandleType = TimeSpan.FromMinutes(5).TimeFrame()
- Filtros:
- Categoría: Seguimiento de tendencia
- Dirección: Ambos
- Indicadores: Parabolic SAR
- Stops: Sí
- Complejidad: Intermedio
- Marco temporal: Intradía (5m)
- Estacionalidad: No
- Redes neuronales: No
- Divergencia: No
- Nivel de riesgo: Medio
using System;
using System.Collections.Generic;
using Ecng.Common;
using StockSharp.Algo;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// Trend-following strategy that activates Parabolic SAR signals only when ATR expands above its recent regime.
/// </summary>
public class ParabolicSarWithVolatilityExpansionStrategy : Strategy
{
private readonly StrategyParam<decimal> _sarAf;
private readonly StrategyParam<decimal> _sarMaxAf;
private readonly StrategyParam<int> _atrPeriod;
private readonly StrategyParam<decimal> _volatilityExpansionFactor;
private readonly StrategyParam<int> _cooldownBars;
private readonly StrategyParam<decimal> _stopLossPercent;
private readonly StrategyParam<DataType> _candleType;
private ParabolicSar _parabolicSar;
private AverageTrueRange _atr;
private SimpleMovingAverage _atrSma;
private StandardDeviation _atrStdDev;
private int _cooldown;
/// <summary>
/// Parabolic SAR acceleration factor.
/// </summary>
public decimal SarAf
{
get => _sarAf.Value;
set => _sarAf.Value = value;
}
/// <summary>
/// Parabolic SAR maximum acceleration factor.
/// </summary>
public decimal SarMaxAf
{
get => _sarMaxAf.Value;
set => _sarMaxAf.Value = value;
}
/// <summary>
/// ATR period.
/// </summary>
public int AtrPeriod
{
get => _atrPeriod.Value;
set => _atrPeriod.Value = value;
}
/// <summary>
/// Multiplier for volatility expansion detection.
/// </summary>
public decimal VolatilityExpansionFactor
{
get => _volatilityExpansionFactor.Value;
set => _volatilityExpansionFactor.Value = value;
}
/// <summary>
/// Bars to wait after each order.
/// </summary>
public int CooldownBars
{
get => _cooldownBars.Value;
set => _cooldownBars.Value = value;
}
/// <summary>
/// Stop loss percentage.
/// </summary>
public decimal StopLossPercent
{
get => _stopLossPercent.Value;
set => _stopLossPercent.Value = value;
}
/// <summary>
/// Candle type.
/// </summary>
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
/// <summary>
/// Initializes strategy parameters.
/// </summary>
public ParabolicSarWithVolatilityExpansionStrategy()
{
_sarAf = Param(nameof(SarAf), 0.02m)
.SetRange(0.001m, 1m)
.SetDisplay("SAR AF", "Parabolic SAR acceleration factor", "Indicators");
_sarMaxAf = Param(nameof(SarMaxAf), 0.2m)
.SetRange(0.01m, 2m)
.SetDisplay("SAR Max AF", "Parabolic SAR maximum acceleration factor", "Indicators");
_atrPeriod = Param(nameof(AtrPeriod), 14)
.SetRange(2, 100)
.SetDisplay("ATR Period", "Period for ATR calculation", "Indicators");
_volatilityExpansionFactor = Param(nameof(VolatilityExpansionFactor), 1.6m)
.SetRange(0.1m, 10m)
.SetDisplay("Volatility Expansion Factor", "Factor for volatility expansion detection", "Signals");
_cooldownBars = Param(nameof(CooldownBars), 84)
.SetRange(1, 500)
.SetDisplay("Cooldown Bars", "Bars to wait after each order", "Risk");
_stopLossPercent = Param(nameof(StopLossPercent), 2m)
.SetRange(0.5m, 10m)
.SetDisplay("Stop Loss %", "Stop loss percentage", "Risk");
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).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();
_parabolicSar = null;
_atr = null;
_atrSma = null;
_atrStdDev = null;
_cooldown = 0;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
if (Security == null)
throw new InvalidOperationException("Security is not specified.");
_parabolicSar = new ParabolicSar
{
Acceleration = SarAf,
AccelerationMax = SarMaxAf,
};
_atr = new AverageTrueRange { Length = AtrPeriod };
_atrSma = new SimpleMovingAverage { Length = AtrPeriod };
_atrStdDev = new StandardDeviation { Length = AtrPeriod };
_cooldown = 0;
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(_parabolicSar, ProcessCandle)
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, _parabolicSar);
DrawIndicator(area, _atr);
DrawOwnTrades(area);
}
StartProtection(new Unit(0, UnitTypes.Absolute), new Unit(StopLossPercent, UnitTypes.Percent), false);
}
private void ProcessCandle(ICandleMessage candle, decimal sarValue)
{
if (candle.State != CandleStates.Finished)
return;
var atrValue = _atr.Process(candle).ToDecimal();
var atrSmaValue = _atrSma.Process(atrValue, candle.OpenTime, true).ToDecimal();
var atrStdDevValue = _atrStdDev.Process(atrValue, candle.OpenTime, true).ToDecimal();
if (!_parabolicSar.IsFormed || !_atr.IsFormed || !_atrSma.IsFormed || !_atrStdDev.IsFormed)
return;
if (ProcessState != ProcessStates.Started)
return;
if (_cooldown > 0)
{
_cooldown--;
return;
}
var volatilityThreshold = atrSmaValue + VolatilityExpansionFactor * atrStdDevValue;
var isVolatilityExpanding = atrValue >= volatilityThreshold;
var isAboveSar = candle.ClosePrice > sarValue;
var isBelowSar = candle.ClosePrice < sarValue;
if (Position == 0)
{
if (isVolatilityExpanding && isAboveSar)
{
BuyMarket();
_cooldown = CooldownBars;
}
else if (isVolatilityExpanding && isBelowSar)
{
SellMarket();
_cooldown = CooldownBars;
}
return;
}
if (Position > 0 && (!isAboveSar || !isVolatilityExpanding))
{
SellMarket(Math.Abs(Position));
_cooldown = CooldownBars;
}
else if (Position < 0 && (!isBelowSar || !isVolatilityExpanding))
{
BuyMarket(Math.Abs(Position));
_cooldown = 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, Decimal
from StockSharp.Messages import DataType, CandleStates, Unit, UnitTypes
from StockSharp.Algo.Indicators import ParabolicSar, AverageTrueRange, SimpleMovingAverage, StandardDeviation, CandleIndicatorValue
from StockSharp.Algo.Strategies import Strategy
from indicator_extensions import *
class parabolic_sar_volatility_expansion_strategy(Strategy):
"""
Trend-following strategy that activates Parabolic SAR signals only when ATR expands above its recent regime.
"""
def __init__(self):
super(parabolic_sar_volatility_expansion_strategy, self).__init__()
self._sar_af = self.Param("SarAf", 0.02) \
.SetRange(0.001, 1.0) \
.SetDisplay("SAR AF", "Parabolic SAR acceleration factor", "Indicators")
self._sar_max_af = self.Param("SarMaxAf", 0.2) \
.SetRange(0.01, 2.0) \
.SetDisplay("SAR Max AF", "Parabolic SAR maximum acceleration factor", "Indicators")
self._atr_period = self.Param("AtrPeriod", 14) \
.SetRange(2, 100) \
.SetDisplay("ATR Period", "Period for ATR calculation", "Indicators")
self._volatility_expansion_factor = self.Param("VolatilityExpansionFactor", 1.6) \
.SetRange(0.1, 10.0) \
.SetDisplay("Volatility Expansion Factor", "Factor for volatility expansion detection", "Signals")
self._cooldown_bars = self.Param("CooldownBars", 84) \
.SetRange(1, 500) \
.SetDisplay("Cooldown Bars", "Bars to wait after each order", "Risk")
self._stop_loss_percent = self.Param("StopLossPercent", 2.0) \
.SetRange(0.5, 10.0) \
.SetDisplay("Stop Loss %", "Stop loss percentage", "Risk")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(5))) \
.SetDisplay("Candle Type", "Type of candles to use", "General")
self._cooldown = 0
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(parabolic_sar_volatility_expansion_strategy, self).OnReseted()
self._cooldown = 0
def OnStarted2(self, time):
super(parabolic_sar_volatility_expansion_strategy, self).OnStarted2(time)
parabolic_sar = ParabolicSar()
parabolic_sar.Acceleration = Decimal(self._sar_af.Value)
parabolic_sar.AccelerationMax = Decimal(self._sar_max_af.Value)
atr_period = int(self._atr_period.Value)
self._atr = AverageTrueRange()
self._atr.Length = atr_period
self._atr_sma = SimpleMovingAverage()
self._atr_sma.Length = atr_period
self._atr_std_dev = StandardDeviation()
self._atr_std_dev.Length = atr_period
self._cooldown = 0
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(parabolic_sar, self._process_candle).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawIndicator(area, parabolic_sar)
self.DrawIndicator(area, self._atr)
self.DrawOwnTrades(area)
self.StartProtection(
Unit(0, UnitTypes.Absolute),
Unit(self._stop_loss_percent.Value, UnitTypes.Percent),
False
)
def _process_candle(self, candle, sar_value):
if candle.State != CandleStates.Finished:
return
atr_result = self._atr.Process(CandleIndicatorValue(self._atr, candle))
atr_value = float(atr_result)
atr_sma_value = float(process_float(self._atr_sma, Decimal(atr_value), candle.OpenTime, True))
atr_std_dev_value = float(process_float(self._atr_std_dev, Decimal(atr_value), candle.OpenTime, True))
if not self.IsFormedAndOnlineAndAllowTrading():
return
if not self._atr.IsFormed or not self._atr_sma.IsFormed or not self._atr_std_dev.IsFormed:
return
if self._cooldown > 0:
self._cooldown -= 1
return
vef = float(self._volatility_expansion_factor.Value)
volatility_threshold = atr_sma_value + vef * atr_std_dev_value
is_volatility_expanding = atr_value >= volatility_threshold
price = float(candle.ClosePrice)
sar_val = float(sar_value)
is_above_sar = price > sar_val
is_below_sar = price < sar_val
cd = int(self._cooldown_bars.Value)
if self.Position == 0:
if is_volatility_expanding and is_above_sar:
self.BuyMarket()
self._cooldown = cd
elif is_volatility_expanding and is_below_sar:
self.SellMarket()
self._cooldown = cd
return
if self.Position > 0 and (not is_above_sar or not is_volatility_expanding):
self.SellMarket(Math.Abs(self.Position))
self._cooldown = cd
elif self.Position < 0 and (not is_below_sar or not is_volatility_expanding):
self.BuyMarket(Math.Abs(self.Position))
self._cooldown = cd
def CreateClone(self):
return parabolic_sar_volatility_expansion_strategy()