Zig Dan Zag Inversión a Largo Plazo Definitiva
Estrategia de inversión a largo plazo que combina pivotes ZigZag con un filtro de tendencia SMA lento. Se abre una posición cuando se forma un nuevo mínimo ZigZag por encima de la SMA, mientras que las salidas ocurren en pivotes opuestos por debajo de la SMA.
Detalles
- Criterios de entrada: Nuevo mínimo ZigZag por encima de la SMA.
- Largo/Corto: Solo largos.
- Criterios de salida: Máximo ZigZag por debajo de la SMA.
- Stops: No.
- Valores predeterminados:
ZigzagDepth= 12SmaLength= 200CandleType= TimeSpan.FromHours(1)
- Filtros:
- Categoría: Tendencia
- Dirección: Solo largos
- Indicadores: Highest, Lowest, SimpleMovingAverage
- Stops: No
- Complejidad: Intermedio
- Marco temporal: Largo plazo
- Estacionalidad: No
- Redes neuronales: No
- Divergencia: No
- Nivel de riesgo: Medio
using System;
using System.Linq;
using System.Collections.Generic;
using Ecng.Common;
using Ecng.Collections;
using Ecng.Serialization;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// Long-term ZigZag investment strategy with SMA trend filter.
/// </summary>
public class ZigDanZagUltimateInvestmentLongTermStrategy : Strategy
{
private readonly StrategyParam<int> _zigzagDepth;
private readonly StrategyParam<int> _smaLength;
private readonly StrategyParam<DataType> _candleType;
private decimal _lastZigzag;
private decimal _lastZigzagHigh;
private decimal _lastZigzagLow;
private int _direction;
private decimal _sma;
public int ZigzagDepth
{
get => _zigzagDepth.Value;
set => _zigzagDepth.Value = value;
}
public int SmaLength
{
get => _smaLength.Value;
set => _smaLength.Value = value;
}
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
public ZigDanZagUltimateInvestmentLongTermStrategy()
{
_zigzagDepth = Param(nameof(ZigzagDepth), 12)
.SetDisplay("ZigZag Depth", "Pivot search depth", "ZigZag");
_smaLength = Param(nameof(SmaLength), 50)
.SetDisplay("SMA Length", "Long-term trend filter", "Trend");
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
.SetDisplay("Candle Type", "Type of candles to use", "General");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
=> [(Security, CandleType)];
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_lastZigzag = 0m;
_lastZigzagHigh = 0m;
_lastZigzagLow = 0m;
_direction = 0;
_sma = 0m;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var highest = new Highest { Length = ZigzagDepth };
var lowest = new Lowest { Length = ZigzagDepth };
var sma = new SimpleMovingAverage { Length = SmaLength };
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(highest, lowest, sma, ProcessCandle)
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, sma);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle, decimal highest, decimal lowest, decimal smaValue)
{
if (candle.State != CandleStates.Finished)
return;
_sma = smaValue;
// update last ZigZag pivot
if (candle.HighPrice >= highest && _direction != 1)
{
_lastZigzag = candle.HighPrice;
_lastZigzagHigh = candle.HighPrice;
_direction = 1;
}
else if (candle.LowPrice <= lowest && _direction != -1)
{
_lastZigzag = candle.LowPrice;
_lastZigzagLow = candle.LowPrice;
_direction = -1;
}
// long-only logic using SMA as trend filter
if (_lastZigzag == _lastZigzagLow && candle.ClosePrice > _sma && Position <= 0)
BuyMarket();
else if (_lastZigzag == _lastZigzagHigh && candle.ClosePrice < _sma && 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 SimpleMovingAverage, Highest, Lowest
from StockSharp.Algo.Strategies import Strategy
class zig_dan_zag_ultimate_investment_long_term_strategy(Strategy):
def __init__(self):
super(zig_dan_zag_ultimate_investment_long_term_strategy, self).__init__()
self._zigzag_depth = self.Param("ZigzagDepth", 12) \
.SetDisplay("ZigZag Depth", "Pivot search depth", "ZigZag")
self._sma_length = self.Param("SmaLength", 50) \
.SetDisplay("SMA Length", "Long-term trend filter", "Trend")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4))) \
.SetDisplay("Candle Type", "Type of candles to use", "General")
self._last_zigzag = 0.0
self._last_zigzag_high = 0.0
self._last_zigzag_low = 0.0
self._direction = 0
self._sma = 0.0
@property
def zigzag_depth(self):
return self._zigzag_depth.Value
@property
def sma_length(self):
return self._sma_length.Value
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(zig_dan_zag_ultimate_investment_long_term_strategy, self).OnReseted()
self._last_zigzag = 0.0
self._last_zigzag_high = 0.0
self._last_zigzag_low = 0.0
self._direction = 0
self._sma = 0.0
def OnStarted2(self, time):
super(zig_dan_zag_ultimate_investment_long_term_strategy, self).OnStarted2(time)
highest = Highest()
highest.Length = self.zigzag_depth
lowest = Lowest()
lowest.Length = self.zigzag_depth
sma = SimpleMovingAverage()
sma.Length = self.sma_length
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(highest, lowest, sma, self.on_process).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawIndicator(area, sma)
self.DrawOwnTrades(area)
def on_process(self, candle, highest, lowest, sma_value):
if candle.State != CandleStates.Finished:
return
self._sma = sma_value
# update last ZigZag pivot
if candle.HighPrice >= highest and self._direction != 1:
self._last_zigzag = candle.HighPrice
self._last_zigzag_high = candle.HighPrice
self._direction = 1
elif candle.LowPrice <= lowest and self._direction != -1:
self._last_zigzag = candle.LowPrice
self._last_zigzag_low = candle.LowPrice
self._direction = -1
# long-only logic using SMA as trend filter
if self._last_zigzag == self._last_zigzag_low and candle.ClosePrice > self._sma and self.Position <= 0:
self.BuyMarket()
elif self._last_zigzag == self._last_zigzag_high and candle.ClosePrice < self._sma and self.Position > 0:
self.SellMarket()
def CreateClone(self):
return zig_dan_zag_ultimate_investment_long_term_strategy()