Momentum Quadrático de Elliott
A estratégia Elliott's Quadratic Momentum combina múltiplos indicadores SuperTrend para capturar o momentum inspirado nas ondas de Elliott.
A estratégia entra comprado quando todas as quatro linhas SuperTrend sinalizam tendência de alta e entra vendido quando todas sinalizam tendência de baixa. As posições são fechadas quando qualquer SuperTrend reverte sua direção.
Detalhes
- Critérios de entrada: Todos os indicadores SuperTrend alinhados na mesma direção.
- Comprado/Vendido: Ambas as direções.
- Critérios de saída: Qualquer SuperTrend vira contra a posição.
- Stops: Sem stops explícitos.
- Valores padrão:
AtrLength1 = 7Multiplier1 = 4.0mAtrLength2 = 14Multiplier2 = 3.618mAtrLength3 = 21Multiplier3 = 3.5mAtrLength4 = 28Multiplier4 = 3.382mCandleType = TimeSpan.FromMinutes(5).TimeFrame()
- Filtros:
- Categoria: Seguidor de tendência
- Direção: Ambos
- Indicadores: SuperTrend
- Stops: Não
- Complexidade: Intermediário
- Período: Intradiário (5m)
- Sazonalidade: Não
- Redes neurais: Não
- Divergência: Não
- Nível de risco: Médio
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>
/// Elliott's Quadratic Momentum strategy.
/// Four SuperTrend indicators with different ATR lengths and multipliers vote on the trend. When all four are up the
/// strategy goes long and when all four are down it goes short. A position closes as soon as any SuperTrend turns
/// against it.
/// </summary>
public class ElliottsQuadraticMomentumStrategy : Strategy
{
private readonly StrategyParam<int> _atrLength1;
private readonly StrategyParam<decimal> _multiplier1;
private readonly StrategyParam<int> _atrLength2;
private readonly StrategyParam<decimal> _multiplier2;
private readonly StrategyParam<int> _atrLength3;
private readonly StrategyParam<decimal> _multiplier3;
private readonly StrategyParam<int> _atrLength4;
private readonly StrategyParam<decimal> _multiplier4;
private readonly StrategyParam<DataType> _candleType;
/// <summary>
/// ATR length of the first SuperTrend.
/// </summary>
public int AtrLength1
{
get => _atrLength1.Value;
set => _atrLength1.Value = value;
}
/// <summary>
/// Multiplier of the first SuperTrend.
/// </summary>
public decimal Multiplier1
{
get => _multiplier1.Value;
set => _multiplier1.Value = value;
}
/// <summary>
/// ATR length of the second SuperTrend.
/// </summary>
public int AtrLength2
{
get => _atrLength2.Value;
set => _atrLength2.Value = value;
}
/// <summary>
/// Multiplier of the second SuperTrend.
/// </summary>
public decimal Multiplier2
{
get => _multiplier2.Value;
set => _multiplier2.Value = value;
}
/// <summary>
/// ATR length of the third SuperTrend.
/// </summary>
public int AtrLength3
{
get => _atrLength3.Value;
set => _atrLength3.Value = value;
}
/// <summary>
/// Multiplier of the third SuperTrend.
/// </summary>
public decimal Multiplier3
{
get => _multiplier3.Value;
set => _multiplier3.Value = value;
}
/// <summary>
/// ATR length of the fourth SuperTrend.
/// </summary>
public int AtrLength4
{
get => _atrLength4.Value;
set => _atrLength4.Value = value;
}
/// <summary>
/// Multiplier of the fourth SuperTrend.
/// </summary>
public decimal Multiplier4
{
get => _multiplier4.Value;
set => _multiplier4.Value = value;
}
/// <summary>
/// Candle type.
/// </summary>
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
/// <summary>
/// Constructor.
/// </summary>
public ElliottsQuadraticMomentumStrategy()
{
_atrLength1 = Param(nameof(AtrLength1), 7)
.SetGreaterThanZero()
.SetDisplay("ATR Length 1", "ATR length of the first SuperTrend", "SuperTrend 1");
_multiplier1 = Param(nameof(Multiplier1), 4.0m)
.SetGreaterThanZero()
.SetDisplay("Multiplier 1", "Multiplier of the first SuperTrend", "SuperTrend 1");
_atrLength2 = Param(nameof(AtrLength2), 14)
.SetGreaterThanZero()
.SetDisplay("ATR Length 2", "ATR length of the second SuperTrend", "SuperTrend 2");
_multiplier2 = Param(nameof(Multiplier2), 3.618m)
.SetGreaterThanZero()
.SetDisplay("Multiplier 2", "Multiplier of the second SuperTrend", "SuperTrend 2");
_atrLength3 = Param(nameof(AtrLength3), 21)
.SetGreaterThanZero()
.SetDisplay("ATR Length 3", "ATR length of the third SuperTrend", "SuperTrend 3");
_multiplier3 = Param(nameof(Multiplier3), 3.5m)
.SetGreaterThanZero()
.SetDisplay("Multiplier 3", "Multiplier of the third SuperTrend", "SuperTrend 3");
_atrLength4 = Param(nameof(AtrLength4), 28)
.SetGreaterThanZero()
.SetDisplay("ATR Length 4", "ATR length of the fourth SuperTrend", "SuperTrend 4");
_multiplier4 = Param(nameof(Multiplier4), 3.382m)
.SetGreaterThanZero()
.SetDisplay("Multiplier 4", "Multiplier of the fourth SuperTrend", "SuperTrend 4");
_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()
{
return [(Security, CandleType)];
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var st1 = new SuperTrend { Length = AtrLength1, Multiplier = Multiplier1 };
var st2 = new SuperTrend { Length = AtrLength2, Multiplier = Multiplier2 };
var st3 = new SuperTrend { Length = AtrLength3, Multiplier = Multiplier3 };
var st4 = new SuperTrend { Length = AtrLength4, Multiplier = Multiplier4 };
var subscription = SubscribeCandles(CandleType);
subscription
.BindEx(st1, st2, st3, st4, ProcessCandle)
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, st1);
DrawIndicator(area, st2);
DrawIndicator(area, st3);
DrawIndicator(area, st4);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle, IIndicatorValue v1, IIndicatorValue v2, IIndicatorValue v3, IIndicatorValue v4)
{
if (candle.State != CandleStates.Finished)
return;
if (!v1.IsFormed || !v2.IsFormed || !v3.IsFormed || !v4.IsFormed)
return;
if (!IsFormedAndOnlineAndAllowTrading())
return;
var up1 = ((SuperTrendIndicatorValue)v1).IsUpTrend;
var up2 = ((SuperTrendIndicatorValue)v2).IsUpTrend;
var up3 = ((SuperTrendIndicatorValue)v3).IsUpTrend;
var up4 = ((SuperTrendIndicatorValue)v4).IsUpTrend;
var allUp = up1 && up2 && up3 && up4;
var allDown = !up1 && !up2 && !up3 && !up4;
if (allUp && Position <= 0)
BuyMarket(Volume + Math.Abs(Position));
else if (allDown && Position >= 0)
SellMarket(Volume + Math.Abs(Position));
else if (Position > 0 && !allUp)
SellMarket(Position);
else if (Position < 0 && !allDown)
BuyMarket(-Position);
}
}
import clr
clr.AddReference("StockSharp.Messages")
clr.AddReference("StockSharp.Algo")
clr.AddReference("StockSharp.Algo.Indicators")
clr.AddReference("StockSharp.Algo.Strategies")
from System import TimeSpan, Decimal
from StockSharp.Messages import DataType, CandleStates
from StockSharp.Algo.Indicators import SuperTrend
from StockSharp.Algo.Strategies import Strategy
class elliotts_quadratic_momentum_strategy(Strategy):
"""
Elliott's Quadratic Momentum strategy.
Four SuperTrend indicators with different ATR lengths and multipliers vote on the trend. When all four are up the
strategy goes long and when all four are down it goes short. A position closes as soon as any SuperTrend turns
against it.
"""
def __init__(self):
super(elliotts_quadratic_momentum_strategy, self).__init__()
self._atr_length1 = self.Param("AtrLength1", 7).SetGreaterThanZero().SetDisplay("ATR Length 1", "ATR length of the first SuperTrend", "SuperTrend 1")
self._multiplier1 = self.Param("Multiplier1", 4.0).SetGreaterThanZero().SetDisplay("Multiplier 1", "Multiplier of the first SuperTrend", "SuperTrend 1")
self._atr_length2 = self.Param("AtrLength2", 14).SetGreaterThanZero().SetDisplay("ATR Length 2", "ATR length of the second SuperTrend", "SuperTrend 2")
self._multiplier2 = self.Param("Multiplier2", 3.618).SetGreaterThanZero().SetDisplay("Multiplier 2", "Multiplier of the second SuperTrend", "SuperTrend 2")
self._atr_length3 = self.Param("AtrLength3", 21).SetGreaterThanZero().SetDisplay("ATR Length 3", "ATR length of the third SuperTrend", "SuperTrend 3")
self._multiplier3 = self.Param("Multiplier3", 3.5).SetGreaterThanZero().SetDisplay("Multiplier 3", "Multiplier of the third SuperTrend", "SuperTrend 3")
self._atr_length4 = self.Param("AtrLength4", 28).SetGreaterThanZero().SetDisplay("ATR Length 4", "ATR length of the fourth SuperTrend", "SuperTrend 4")
self._multiplier4 = self.Param("Multiplier4", 3.382).SetGreaterThanZero().SetDisplay("Multiplier 4", "Multiplier of the fourth SuperTrend", "SuperTrend 4")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(5))).SetDisplay("Candle Type", "Type of candles to use", "General")
@property
def candle_type(self):
return self._candle_type.Value
def _create_supertrend(self, length, multiplier):
st = SuperTrend()
st.Length = length
st.Multiplier = Decimal(multiplier)
return st
def OnStarted2(self, time):
super(elliotts_quadratic_momentum_strategy, self).OnStarted2(time)
st1 = self._create_supertrend(self._atr_length1.Value, self._multiplier1.Value)
st2 = self._create_supertrend(self._atr_length2.Value, self._multiplier2.Value)
st3 = self._create_supertrend(self._atr_length3.Value, self._multiplier3.Value)
st4 = self._create_supertrend(self._atr_length4.Value, self._multiplier4.Value)
subscription = self.SubscribeCandles(self.candle_type)
subscription.BindEx(st1, st2, st3, st4, self._process_candle).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawIndicator(area, st1)
self.DrawIndicator(area, st2)
self.DrawIndicator(area, st3)
self.DrawIndicator(area, st4)
self.DrawOwnTrades(area)
def _process_candle(self, candle, v1, v2, v3, v4):
if candle.State != CandleStates.Finished:
return
if not v1.IsFormed or not v2.IsFormed or not v3.IsFormed or not v4.IsFormed:
return
if not self.IsFormedAndOnlineAndAllowTrading():
return
ups = [bool(v.IsUpTrend) for v in (v1, v2, v3, v4)]
all_up = all(ups)
all_down = not any(ups)
if all_up and self.Position <= 0:
self.BuyMarket(self.Volume + abs(self.Position))
elif all_down and self.Position >= 0:
self.SellMarket(self.Volume + abs(self.Position))
elif self.Position > 0 and not all_up:
self.SellMarket(self.Position)
elif self.Position < 0 and not all_down:
self.BuyMarket(-self.Position)
def CreateClone(self):
return elliotts_quadratic_momentum_strategy()