Stops: Take-profit y stop-loss fijos opcionales en puntos de precio a través de StartProtection.
Descripción de la estrategia
Esta estrategia recrea el flujo de decisión del experto de MetaTrader "Ichimoku" (carpeta MQL/23469). Evalúa la vela cerrada anterior y abre nuevos trades al inicio de la siguiente barra cuando las cuatro confirmaciones están de acuerdo:
Alineación Ichimoku – Tenkan (línea de conversión) debe estar por encima de Kijun (línea base) para trades largos y por debajo para cortos.
Filtro de tendencia LWMA – Una media móvil ponderada lineal rápida debe mantenerse por encima de la LWMA lenta para largos y por debajo para cortos. Ambas medias se calculan en el mismo marco temporal que las velas suscritas.
Fuerza del Momentum – La distancia absoluta del oscilador de momentum desde el nivel neutro 100 debe ser mayor que un umbral configurable en al menos una de las últimas tres velas cerradas.
Confirmación MACD – El histograma MACD debe coincidir con la dirección (línea MACD posicionada más allá de la línea de señal con el mismo signo).
Cuando las cuatro condiciones se alinean alcistamente y la estrategia no está actualmente larga, compra el volumen configurado más las unidades necesarias para aplanar una posición corta existente. Cuando las condiciones cambian a bajistas, refleja el proceso en el lado de la venta. Las señales opuestas siempre cierran posiciones abiertas, proporcionando una salida determinista incluso sin órdenes protectoras.
La gestión de riesgos se maneja a través del StartProtection de StockSharp, permitiendo distancias fijas de take-profit y stop-loss expresadas en puntos del instrumento. Establecer cualquier parámetro en cero deshabilita el tramo de protección correspondiente.
Descripción de parámetros
Parámetro
Descripción
FastMaPeriod
Longitud de la media móvil ponderada lineal rápida usada para el filtro de tendencia.
SlowMaPeriod
Longitud de la media móvil ponderada lineal lenta.
MomentumPeriod
Período de lookback del oscilador de momentum.
MomentumThreshold
Distancia mínima desde 100 que el momentum debe alcanzar en al menos una de las últimas tres velas.
MacdFastPeriod
Longitud EMA rápida del filtro MACD.
MacdSlowPeriod
Longitud EMA lenta del filtro MACD.
MacdSignalPeriod
Longitud EMA de señal del filtro MACD.
TenkanPeriod
Longitud del Ichimoku Tenkan-sen.
KijunPeriod
Longitud del Ichimoku Kijun-sen.
SenkouSpanBPeriod
Longitud del Ichimoku Senkou Span B.
TakeProfitPoints
Distancia de take-profit opcional en puntos de precio (0 deshabilita).
StopLossPoints
Distancia de stop-loss opcional en puntos de precio (0 deshabilita).
CandleType
Marco temporal usado para todos los cálculos de indicadores.
Notas de uso
La estrategia lee solo velas finalizadas y almacena los valores de indicadores de la barra anterior, coincidiendo con la lógica shift=1 del EA de MetaTrader.
Ajustar MomentumThreshold al cambiar a mercados con diferente escala de momentum (p. ej., crypto vs. pares forex).
Las órdenes protectoras se gestionan internamente; no se envían órdenes de corchete a nivel de exchange.
Los gráficos, si están disponibles, mostrarán velas de precio, ambas LWMAs, la nube Ichimoku y los trades ejecutados.
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;
public class IchimokuMomentumMacdStrategy : Strategy
{
private readonly StrategyParam<int> _fastPeriod;
private readonly StrategyParam<int> _slowPeriod;
private readonly StrategyParam<int> _stopLossPoints;
private readonly StrategyParam<int> _takeProfitPoints;
private ExponentialMovingAverage _fast;
private ExponentialMovingAverage _slow;
private decimal _prevFast;
private decimal _prevSlow;
private decimal _entryPrice;
private int _cooldown;
public int FastPeriod { get => _fastPeriod.Value; set => _fastPeriod.Value = value; }
public int SlowPeriod { get => _slowPeriod.Value; set => _slowPeriod.Value = value; }
public int StopLossPoints { get => _stopLossPoints.Value; set => _stopLossPoints.Value = value; }
public int TakeProfitPoints { get => _takeProfitPoints.Value; set => _takeProfitPoints.Value = value; }
public IchimokuMomentumMacdStrategy()
{
_fastPeriod = Param(nameof(FastPeriod), 12).SetGreaterThanZero().SetDisplay("Fast Period", "Fast EMA period", "Indicator");
_slowPeriod = Param(nameof(SlowPeriod), 50).SetGreaterThanZero().SetDisplay("Slow Period", "Slow EMA period", "Indicator");
_stopLossPoints = Param(nameof(StopLossPoints), 200).SetNotNegative().SetDisplay("Stop Loss", "Stop-loss in price steps", "Risk");
_takeProfitPoints = Param(nameof(TakeProfitPoints), 400).SetNotNegative().SetDisplay("Take Profit", "Take-profit in price steps", "Risk");
}
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
yield return (Security, TimeSpan.FromMinutes(5).TimeFrame());
}
protected override void OnReseted()
{
base.OnReseted();
_fast = null; _slow = null;
_prevFast = 0; _prevSlow = 0; _entryPrice = 0; _cooldown = 0;
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_fast = new ExponentialMovingAverage { Length = FastPeriod };
_slow = new ExponentialMovingAverage { Length = SlowPeriod };
var subscription = SubscribeCandles(TimeSpan.FromMinutes(5).TimeFrame());
subscription.Bind(_fast, _slow, ProcessCandle);
subscription.Start();
}
private void ProcessCandle(ICandleMessage candle, decimal fastValue, decimal slowValue)
{
if (candle.State != CandleStates.Finished) return;
if (!_fast.IsFormed || !_slow.IsFormed) { _prevFast = fastValue; _prevSlow = slowValue; return; }
if (_cooldown > 0) { _cooldown--; _prevFast = fastValue; _prevSlow = slowValue; return; }
var close = candle.ClosePrice;
var step = Security?.PriceStep ?? 1m;
if (Position > 0 && _entryPrice > 0)
{
if (StopLossPoints > 0 && close <= _entryPrice - StopLossPoints * step) { SellMarket(); _entryPrice = 0; _cooldown = 100; _prevFast = fastValue; _prevSlow = slowValue; return; }
if (TakeProfitPoints > 0 && close >= _entryPrice + TakeProfitPoints * step) { SellMarket(); _entryPrice = 0; _cooldown = 100; _prevFast = fastValue; _prevSlow = slowValue; return; }
}
else if (Position < 0 && _entryPrice > 0)
{
if (StopLossPoints > 0 && close >= _entryPrice + StopLossPoints * step) { BuyMarket(); _entryPrice = 0; _cooldown = 100; _prevFast = fastValue; _prevSlow = slowValue; return; }
if (TakeProfitPoints > 0 && close <= _entryPrice - TakeProfitPoints * step) { BuyMarket(); _entryPrice = 0; _cooldown = 100; _prevFast = fastValue; _prevSlow = slowValue; return; }
}
if (_prevFast <= _prevSlow && fastValue > slowValue && Position <= 0)
{ if (Position < 0) BuyMarket(); BuyMarket(); _entryPrice = close; _cooldown = 100; }
else if (_prevFast >= _prevSlow && fastValue < slowValue && Position >= 0)
{ if (Position > 0) SellMarket(); SellMarket(); _entryPrice = close; _cooldown = 100; }
_prevFast = fastValue; _prevSlow = slowValue;
}
}
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 ExponentialMovingAverage
from StockSharp.Algo.Strategies import Strategy
class ichimoku_momentum_macd_strategy(Strategy):
"""
EMA crossover with SL/TP in price steps and cooldown.
"""
def __init__(self):
super(ichimoku_momentum_macd_strategy, self).__init__()
self._fast_period = self.Param("FastPeriod", 12) \
.SetDisplay("Fast Period", "Fast EMA period", "Indicator")
self._slow_period = self.Param("SlowPeriod", 50) \
.SetDisplay("Slow Period", "Slow EMA period", "Indicator")
self._stop_loss_points = self.Param("StopLossPoints", 200) \
.SetDisplay("Stop Loss", "Stop-loss in price steps", "Risk")
self._take_profit_points = self.Param("TakeProfitPoints", 400) \
.SetDisplay("Take Profit", "Take-profit in price steps", "Risk")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(5))) \
.SetDisplay("Candle Type", "Type of candles", "General")
self._prev_fast = 0.0
self._prev_slow = 0.0
self._entry_price = 0.0
self._cooldown = 0
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(ichimoku_momentum_macd_strategy, self).OnReseted()
self._prev_fast = 0.0
self._prev_slow = 0.0
self._entry_price = 0.0
self._cooldown = 0
def OnStarted2(self, time):
super(ichimoku_momentum_macd_strategy, self).OnStarted2(time)
fast = ExponentialMovingAverage()
fast.Length = self._fast_period.Value
slow = ExponentialMovingAverage()
slow.Length = self._slow_period.Value
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(fast, slow, self._process_candle).Start()
def _process_candle(self, candle, fast_val, slow_val):
if candle.State != CandleStates.Finished:
return
fast = float(fast_val)
slow = float(slow_val)
if self._cooldown > 0:
self._cooldown -= 1
self._prev_fast = fast
self._prev_slow = slow
return
close = float(candle.ClosePrice)
step = 1.0
if self.Security is not None and self.Security.PriceStep is not None:
step = float(self.Security.PriceStep)
if step <= 0:
step = 1.0
sl = self._stop_loss_points.Value
tp = self._take_profit_points.Value
if self.Position > 0 and self._entry_price > 0:
if sl > 0 and close <= self._entry_price - sl * step:
self.SellMarket()
self._entry_price = 0.0
self._cooldown = 100
self._prev_fast = fast
self._prev_slow = slow
return
if tp > 0 and close >= self._entry_price + tp * step:
self.SellMarket()
self._entry_price = 0.0
self._cooldown = 100
self._prev_fast = fast
self._prev_slow = slow
return
elif self.Position < 0 and self._entry_price > 0:
if sl > 0 and close >= self._entry_price + sl * step:
self.BuyMarket()
self._entry_price = 0.0
self._cooldown = 100
self._prev_fast = fast
self._prev_slow = slow
return
if tp > 0 and close <= self._entry_price - tp * step:
self.BuyMarket()
self._entry_price = 0.0
self._cooldown = 100
self._prev_fast = fast
self._prev_slow = slow
return
if self._prev_fast <= self._prev_slow and fast > slow and self.Position <= 0:
if self.Position < 0:
self.BuyMarket()
self.BuyMarket()
self._entry_price = close
self._cooldown = 100
elif self._prev_fast >= self._prev_slow and fast < slow and self.Position >= 0:
if self.Position > 0:
self.SellMarket()
self.SellMarket()
self._entry_price = close
self._cooldown = 100
self._prev_fast = fast
self._prev_slow = slow
def CreateClone(self):
return ichimoku_momentum_macd_strategy()