Patrón de Doble Techo (Double Top Pattern)
El Doble Techo identifica dos picos separados por un número de barras con precios similares. Después de formarse el segundo pico, una vela bajista confirma la reversión.
Las pruebas indican un rendimiento anual promedio de aproximadamente el 58%. Funciona mejor en el mercado de acciones.
La estrategia vende en corto tras la confirmación con un stop por encima de los máximos del patrón, con el objetivo de obtener beneficios de un cambio de tendencia tras el agotamiento de los compradores.
Las posiciones se cierran mediante stop-loss o objetivos discrecionales.
Detalles
- Criterios de entrada: Dos techos dentro de
SimilarityPercentdespués deDistancebarras. - Largo/Corto: Solo cortos.
- Criterios de salida: El precio repunta o stop-loss.
- Stops: Sí.
- Valores predeterminados:
Distance= 5SimilarityPercent= 2.0mCandleType= TimeSpan.FromMinutes(15)StopLossPercent= 1.0m
- Filtros:
- Categoría: Patrón
- Dirección: Solo cortos
- Indicadores: Price Action
- Stops: Sí
- Complejidad: Intermedio
- Marco temporal: Intradía
- Estacionalidad: No
- Redes neuronales: No
- Divergencia: Sí
- Nivel de riesgo: Medio
using System;
using System.Collections.Generic;
using Ecng.Common;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// Short-only double top: two confirmed pivot highs followed by a bearish candle.
/// A pattern-high stop is checked against executable best asks and finished-bar highs.
/// </summary>
public class DoubleTopStrategy : Strategy
{
private readonly StrategyParam<int> _distance;
private readonly StrategyParam<decimal> _similarityPercent;
private readonly StrategyParam<decimal> _stopLossPercent;
private readonly StrategyParam<DataType> _candleType;
private ICandleMessage _twoBack;
private ICandleMessage _previous;
private (int Bar, decimal High)? _lastPivot;
private decimal? _candidateHigh;
private int _candidateExpires;
private decimal? _patternStop;
private int _bar;
private Order _entryOrder;
private Order _exitOrder;
public int Distance { get => _distance.Value; set => _distance.Value = value; }
public decimal SimilarityPercent { get => _similarityPercent.Value; set => _similarityPercent.Value = value; }
public decimal StopLossPercent { get => _stopLossPercent.Value; set => _stopLossPercent.Value = value; }
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public DoubleTopStrategy()
{
_distance = Param(nameof(Distance), 5).SetRange(3, 100)
.SetDisplay("Distance", "Minimum bars between confirmed pivot highs", "Pattern");
_similarityPercent = Param(nameof(SimilarityPercent), 2m).SetRange(0.1m, 5m)
.SetDisplay("Similarity %", "Maximum relative difference between pivot highs", "Pattern");
_stopLossPercent = Param(nameof(StopLossPercent), 1m).SetRange(0m, 99m)
.SetDisplay("Stop above highs (%)", "Percentage buffer above the higher pattern high; zero places it at the high.", "Protection");
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(15).TimeFrame())
.SetDisplay("Candle Type", "Timeframe for pivot highs and bearish confirmation", "General");
}
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
=> [(Security, CandleType), (Security, DataType.Level1)];
protected override void OnReseted()
{
base.OnReseted();
ClearState();
}
private void ClearState()
{
_twoBack = _previous = null;
_lastPivot = null;
_candidateHigh = null;
_candidateExpires = 0;
_patternStop = null;
_bar = 0;
_entryOrder = _exitOrder = null;
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
ClearState();
var asks = new Subscription(DataType.Level1, Security);
asks.MarketData.BuildField = Level1Fields.BestAskPrice;
SubscribeLevel1(asks).Bind(ProcessAsk).Start();
var candles = SubscribeCandles(CandleType);
candles.Bind(ProcessCandle).Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, candles);
DrawOwnTrades(area);
}
}
private static bool IsPending(Order order)
=> order is not null && order.State is not (OrderStates.Done or OrderStates.Failed);
private void ProcessAsk(Level1ChangeMessage message)
{
if (message.TryGetDecimal(Level1Fields.BestAskPrice) is decimal ask && ask > 0m)
CheckStop(ask);
}
private void CheckStop(decimal executableAsk)
{
if (Position < 0m && _patternStop is decimal stop && executableAsk >= stop && !IsPending(_exitOrder))
{
_exitOrder = BuyMarket(Math.Abs(Position));
// An exited pattern cannot be reused to open another position.
_twoBack = _previous = null;
_lastPivot = null;
_candidateHigh = null;
}
}
private void ProcessCandle(ICandleMessage candle)
{
if (candle.State != CandleStates.Finished)
return;
_bar++;
var older = _twoBack;
var middle = _previous;
_twoBack = middle;
_previous = candle;
if (Position < 0m)
{
// Covers missing/stale quotes or a gap; the next market order may fill beyond the stop.
CheckStop(candle.HighPrice);
return;
}
if (IsPending(_entryOrder) || IsPending(_exitOrder))
return;
if (older is not null && middle is not null &&
middle.HighPrice > older.HighPrice && middle.HighPrice >= candle.HighPrice)
{
var pivot = (Bar: _bar - 1, High: middle.HighPrice);
if (_lastPivot is { } first && pivot.Bar - first.Bar >= Distance &&
Math.Abs(pivot.High - first.High) * 100m <= first.High * SimilarityPercent)
{
_candidateHigh = Math.Max(first.High, pivot.High);
_candidateExpires = _bar + Distance;
}
_lastPivot = pivot;
}
if (_candidateHigh is not decimal high)
return;
if (_bar > _candidateExpires || candle.HighPrice > high)
{
_candidateHigh = null;
return;
}
if (candle.ClosePrice >= candle.OpenPrice || !IsFormedAndOnlineAndAllowTrading())
return;
_patternStop = high * (1m + StopLossPercent / 100m);
_entryOrder = SellMarket(Volume);
_candidateHigh = null;
_lastPivot = null;
}
}
import clr
clr.AddReference("StockSharp.Messages")
clr.AddReference("StockSharp.Algo")
clr.AddReference("StockSharp.Algo.Strategies")
clr.AddReference("StockSharp.BusinessEntities")
from System import TimeSpan, Decimal, Math
from StockSharp.Messages import DataType, CandleStates, Level1Fields, OrderStates
from StockSharp.BusinessEntities import Subscription
from StockSharp.Algo.Strategies import Strategy
class double_top_strategy(Strategy):
"""
Short-only double top: two confirmed pivot highs followed by a bearish candle.
A pattern-high stop watches executable best asks and finished-bar highs.
"""
def __init__(self):
super(double_top_strategy, self).__init__()
self._distance = self.Param("Distance", 5).SetRange(3, 100).SetDisplay("Distance", "Minimum bars between confirmed pivot highs", "Pattern")
self._similarity_percent = self.Param("SimilarityPercent", 2.0).SetRange(0.1, 5.0).SetDisplay("Similarity %", "Maximum relative difference between pivot highs", "Pattern")
self._stop_loss_percent = self.Param("StopLossPercent", 1.0).SetRange(0.0, 99.0).SetDisplay("Stop above highs (%)", "Percentage buffer above the higher pattern high; zero places it at the high.", "Protection")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(15))).SetDisplay("Candle Type", "Timeframe for pivot highs and bearish confirmation", "General")
self._clear_state()
@property
def candle_type(self):
return self._candle_type.Value
def GetWorkingSecurities(self):
return [(self.Security, self.candle_type), (self.Security, DataType.Level1)]
def _clear_state(self):
self._two_back = None
self._previous = None
self._last_pivot = None
self._candidate_high = None
self._candidate_expires = 0
self._pattern_stop = None
self._bar = 0
self._entry_order = None
self._exit_order = None
def OnReseted(self):
super(double_top_strategy, self).OnReseted()
self._clear_state()
def OnStarted2(self, time):
super(double_top_strategy, self).OnStarted2(time)
self._clear_state()
asks = Subscription(DataType.Level1, self.Security)
asks.MarketData.BuildField = Level1Fields.BestAskPrice
self.SubscribeLevel1(asks).Bind(self._process_ask).Start()
candles = self.SubscribeCandles(self.candle_type)
candles.Bind(self._process_candle).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, candles)
self.DrawOwnTrades(area)
def _is_pending(self, order):
return order is not None and order.State not in (OrderStates.Done, OrderStates.Failed)
def _process_ask(self, message):
if message.Changes.ContainsKey(Level1Fields.BestAskPrice):
ask = message.Changes[Level1Fields.BestAskPrice]
if ask is not None and ask > Decimal(0):
self._check_stop(ask)
def _check_stop(self, executable_ask):
if self.Position < 0 and self._pattern_stop is not None and executable_ask >= self._pattern_stop and not self._is_pending(self._exit_order):
self._exit_order = self.BuyMarket(Math.Abs(self.Position))
# Never reuse an exited pattern for another entry.
self._two_back = None
self._previous = None
self._last_pivot = None
self._candidate_high = None
def _process_candle(self, candle):
if candle.State != CandleStates.Finished:
return
self._bar += 1
older = self._two_back
middle = self._previous
self._two_back = middle
self._previous = candle
if self.Position < 0:
# A bar-low fallback handles absent/stale quotes or gaps; fills can slip.
self._check_stop(candle.HighPrice)
return
if self._is_pending(self._entry_order) or self._is_pending(self._exit_order):
return
if older is not None and middle is not None and middle.HighPrice > older.HighPrice and middle.HighPrice >= candle.HighPrice:
pivot = (self._bar - 1, middle.HighPrice)
first = self._last_pivot
if first is not None and pivot[0] - first[0] >= self._distance.Value and Math.Abs(pivot[1] - first[1]) * Decimal(100) <= first[1] * Decimal(self._similarity_percent.Value):
self._candidate_high = max(first[1], pivot[1])
self._candidate_expires = self._bar + self._distance.Value
self._last_pivot = pivot
high = self._candidate_high
if high is None:
return
if self._bar > self._candidate_expires or candle.HighPrice > high:
self._candidate_high = None
return
if candle.ClosePrice >= candle.OpenPrice or not self.IsFormedAndOnlineAndAllowTrading():
return
self._pattern_stop = high * (Decimal(1) + Decimal(self._stop_loss_percent.Value) / Decimal(100))
self._entry_order = self.SellMarket(self.Volume)
self._candidate_high = None
self._last_pivot = None
def CreateClone(self):
return double_top_strategy()