Estrategia Delta MFI
Estrategia basada en la comparación de los valores rápido y lento del Índice de Flujo de Dinero (MFI). Va largo cuando el MFI rápido sube por encima del MFI lento mientras el MFI lento está por encima del nivel de señal. Va corto cuando el MFI rápido cae por debajo del MFI lento mientras el MFI lento está por debajo de 100 menos el nivel de señal.
Detalles
- Criterios de entrada:
- Comprar cuando
slow MFI > Levelyfast MFI > slow MFI - Vender cuando
slow MFI < 100 - Levelyfast MFI < slow MFI
- Comprar cuando
- Largo/Corto: Ambos
- Criterios de salida: Señal opuesta
- Stops: No
- Valores predeterminados:
FastPeriod= 14SlowPeriod= 50Level= 50CandleType= velas de 4 horas
- Filtros:
- Categoría: Indicador
- Dirección: Ambos
- Indicadores: Money Flow Index
- Stops: No
- Complejidad: Básico
- Marco temporal: H4
- 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>
/// Strategy based on the difference between fast and slow Money Flow Index (MFI).
/// Buys when the fast MFI is above the slow MFI and the slow MFI is above the signal level.
/// Sells when the fast MFI is below the slow MFI and the slow MFI is below 100 minus the signal level.
/// </summary>
public class DeltaMfiStrategy : Strategy
{
private readonly StrategyParam<int> _fastPeriod;
private readonly StrategyParam<int> _slowPeriod;
private readonly StrategyParam<int> _level;
private readonly StrategyParam<DataType> _candleType;
/// <summary>
/// Fast MFI period length.
/// </summary>
public int FastPeriod
{
get => _fastPeriod.Value;
set => _fastPeriod.Value = value;
}
/// <summary>
/// Slow MFI period length.
/// </summary>
public int SlowPeriod
{
get => _slowPeriod.Value;
set => _slowPeriod.Value = value;
}
/// <summary>
/// MFI level used to confirm signals.
/// </summary>
public int Level
{
get => _level.Value;
set => _level.Value = value;
}
/// <summary>
/// The type of candles used for calculations.
/// </summary>
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
/// <summary>
/// Constructor.
/// </summary>
public DeltaMfiStrategy()
{
_fastPeriod = Param(nameof(FastPeriod), 14)
.SetGreaterThanZero()
.SetDisplay("Fast MFI Period", "Period for fast Money Flow Index", "Parameters")
.SetOptimize(5, 30, 5);
_slowPeriod = Param(nameof(SlowPeriod), 50)
.SetGreaterThanZero()
.SetDisplay("Slow MFI Period", "Period for slow Money Flow Index", "Parameters")
.SetOptimize(20, 100, 10);
_level = Param(nameof(Level), 50)
.SetGreaterThanZero()
.SetDisplay("Signal Level", "MFI level to confirm signals", "Parameters")
.SetOptimize(30, 70, 5);
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
.SetDisplay("Candle Type", "Type of candles used for analysis", "General");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
// Enable position protection once
StartProtection(null, null);
var fastMfi = new MoneyFlowIndex { Length = FastPeriod };
var slowMfi = new MoneyFlowIndex { Length = SlowPeriod };
var subscription = SubscribeCandles(CandleType);
subscription.Bind(fastMfi, slowMfi, ProcessCandle).Start();
// Draw indicators if a chart is available
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, fastMfi);
DrawIndicator(area, slowMfi);
}
}
private void ProcessCandle(ICandleMessage candle, decimal fastValue, decimal slowValue)
{
if (candle.State != CandleStates.Finished)
return;
// Check strategy readiness and connection state
if (!IsFormedAndOnlineAndAllowTrading())
return;
// Long signal: slow MFI above level and fast MFI above slow MFI
if (slowValue > Level && fastValue > slowValue && Position <= 0)
{
BuyMarket();
return;
}
// Short signal: slow MFI below (100 - level) and fast MFI below slow MFI
if (slowValue < (100 - Level) && fastValue < slowValue && 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 MoneyFlowIndex
from StockSharp.Algo.Strategies import Strategy
class delta_mfi_strategy(Strategy):
def __init__(self):
super(delta_mfi_strategy, self).__init__()
self._fast_period = self.Param("FastPeriod", 14) \
.SetDisplay("Fast MFI Period", "Period for fast Money Flow Index", "Parameters")
self._slow_period = self.Param("SlowPeriod", 50) \
.SetDisplay("Slow MFI Period", "Period for slow Money Flow Index", "Parameters")
self._level = self.Param("Level", 50) \
.SetDisplay("Signal Level", "MFI level to confirm signals", "Parameters")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4))) \
.SetDisplay("Candle Type", "Type of candles used for analysis", "General")
@property
def fast_period(self):
return self._fast_period.Value
@property
def slow_period(self):
return self._slow_period.Value
@property
def level(self):
return self._level.Value
@property
def candle_type(self):
return self._candle_type.Value
def OnStarted2(self, time):
super(delta_mfi_strategy, self).OnStarted2(time)
fast_mfi = MoneyFlowIndex()
fast_mfi.Length = int(self.fast_period)
slow_mfi = MoneyFlowIndex()
slow_mfi.Length = int(self.slow_period)
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(fast_mfi, slow_mfi, self.process_candle).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawIndicator(area, fast_mfi)
self.DrawIndicator(area, slow_mfi)
def process_candle(self, candle, fast_value, slow_value):
if candle.State != CandleStates.Finished:
return
fast_value = float(fast_value)
slow_value = float(slow_value)
lvl = float(self.level)
if slow_value > lvl and fast_value > slow_value and self.Position <= 0:
self.BuyMarket()
elif slow_value < (100 - lvl) and fast_value < slow_value and self.Position >= 0:
self.SellMarket()
def CreateClone(self):
return delta_mfi_strategy()