Стратегия Macd Cci Lotfy
Стратегия сочетает индикаторы MACD и CCI с коэффициентом масштабирования. Позиция открывается, когда оба индикатора пересекают крайние уровни в одном направлении.
Значение MACD умножается на коэффициент, чтобы привести шкалу к CCI и сравнивать с одинаковым порогом. Подход нацелен на отлов разворотов из зон перекупленности и перепроданности.
Детали
- Условия входа:
- Long:
CCI < -ThresholdиMACD * MacdCoefficient < -Threshold - Short:
CCI > ThresholdиMACD * MacdCoefficient > Threshold
- Long:
- Типы сделок: Long и Short
- Условия выхода: Обратный сигнал разворачивает позицию
- Стопы: Нет
- Значения по умолчанию:
CciPeriod= 8FastPeriod= 13SlowPeriod= 33MacdCoefficient= 86000Threshold= 85CandleType= TimeSpan.FromMinutes(1).TimeFrame()
- Фильтры:
- Категория: Mean reversion
- Направление: Оба
- Индикаторы: MACD, CCI
- Стопы: Нет
- Сложность: Базовая
- Таймфрейм: Краткосрочный
- Сезонность: Нет
- Нейросети: Нет
- Дивергенция: Нет
- Уровень риска: Средний
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 combining MACD and RSI indicators.
/// Opens long when both indicators reach oversold conditions.
/// Opens short when both indicators reach overbought conditions.
/// </summary>
public class MacdCciLotfyStrategy : Strategy
{
private readonly StrategyParam<int> _rsiPeriod;
private readonly StrategyParam<int> _fastPeriod;
private readonly StrategyParam<int> _slowPeriod;
private readonly StrategyParam<decimal> _macdCoefficient;
private readonly StrategyParam<decimal> _threshold;
private readonly StrategyParam<DataType> _candleType;
public int RsiPeriod { get => _rsiPeriod.Value; set => _rsiPeriod.Value = value; }
public int FastPeriod { get => _fastPeriod.Value; set => _fastPeriod.Value = value; }
public int SlowPeriod { get => _slowPeriod.Value; set => _slowPeriod.Value = value; }
public decimal MacdCoefficient { get => _macdCoefficient.Value; set => _macdCoefficient.Value = value; }
public decimal Threshold { get => _threshold.Value; set => _threshold.Value = value; }
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public MacdCciLotfyStrategy()
{
_rsiPeriod = Param(nameof(RsiPeriod), 8)
.SetGreaterThanZero()
.SetDisplay("RSI Period", "Period of RSI indicator", "General");
_fastPeriod = Param(nameof(FastPeriod), 13)
.SetGreaterThanZero()
.SetDisplay("MACD Fast EMA", "Fast EMA length for MACD", "MACD");
_slowPeriod = Param(nameof(SlowPeriod), 33)
.SetGreaterThanZero()
.SetDisplay("MACD Slow EMA", "Slow EMA length for MACD", "MACD");
_macdCoefficient = Param(nameof(MacdCoefficient), 86000m)
.SetGreaterThanZero()
.SetDisplay("MACD Coefficient", "Multiplier for MACD value", "MACD");
_threshold = Param(nameof(Threshold), 25m)
.SetGreaterThanZero()
.SetDisplay("Threshold", "Absolute level for signals", "General");
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(30).TimeFrame())
.SetDisplay("Candle Type", "Type of candles", "General");
}
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var rsi = new RelativeStrengthIndex { Length = RsiPeriod };
var macd = new MovingAverageConvergenceDivergence
{
ShortMa = { Length = FastPeriod },
LongMa = { Length = SlowPeriod },
};
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(macd, rsi, ProcessCandle)
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, macd);
DrawIndicator(area, rsi);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle, decimal macdValue, decimal rsiValue)
{
if (candle.State != CandleStates.Finished)
return;
if (!IsFormedAndOnlineAndAllowTrading())
return;
var scaledMacd = macdValue * MacdCoefficient;
if (rsiValue < 50m - Threshold && scaledMacd < -Threshold)
{
if (Position <= 0)
BuyMarket(Volume + Math.Abs(Position));
}
else if (rsiValue > 50m + Threshold && scaledMacd > Threshold)
{
if (Position >= 0)
SellMarket(Volume + Math.Abs(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, Math
from StockSharp.Messages import DataType, CandleStates
from StockSharp.Algo.Indicators import MovingAverageConvergenceDivergence, RelativeStrengthIndex
from StockSharp.Algo.Strategies import Strategy
class macd_cci_lotfy_strategy(Strategy):
def __init__(self):
super(macd_cci_lotfy_strategy, self).__init__()
self._rsi_period = self.Param("RsiPeriod", 8)
self._fast_period = self.Param("FastPeriod", 13)
self._slow_period = self.Param("SlowPeriod", 33)
self._macd_coefficient = self.Param("MacdCoefficient", 86000.0)
self._threshold = self.Param("Threshold", 25.0)
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(30)))
@property
def RsiPeriod(self):
return self._rsi_period.Value
@RsiPeriod.setter
def RsiPeriod(self, value):
self._rsi_period.Value = value
@property
def FastPeriod(self):
return self._fast_period.Value
@FastPeriod.setter
def FastPeriod(self, value):
self._fast_period.Value = value
@property
def SlowPeriod(self):
return self._slow_period.Value
@SlowPeriod.setter
def SlowPeriod(self, value):
self._slow_period.Value = value
@property
def MacdCoefficient(self):
return self._macd_coefficient.Value
@MacdCoefficient.setter
def MacdCoefficient(self, value):
self._macd_coefficient.Value = value
@property
def Threshold(self):
return self._threshold.Value
@Threshold.setter
def Threshold(self, value):
self._threshold.Value = value
@property
def CandleType(self):
return self._candle_type.Value
@CandleType.setter
def CandleType(self, value):
self._candle_type.Value = value
def OnStarted2(self, time):
super(macd_cci_lotfy_strategy, self).OnStarted2(time)
macd = MovingAverageConvergenceDivergence()
macd.ShortMa.Length = self.FastPeriod
macd.LongMa.Length = self.SlowPeriod
rsi = RelativeStrengthIndex()
rsi.Length = self.RsiPeriod
subscription = self.SubscribeCandles(self.CandleType)
subscription.Bind(macd, rsi, self.ProcessCandle).Start()
def ProcessCandle(self, candle, macd_value, rsi_value):
if candle.State != CandleStates.Finished:
return
if not self.IsFormedAndOnlineAndAllowTrading():
return
macd_val = float(macd_value)
rsi_val = float(rsi_value)
coeff = float(self.MacdCoefficient)
thresh = float(self.Threshold)
scaled_macd = macd_val * coeff
pos = float(self.Position)
vol = float(self.Volume)
if rsi_val < 50.0 - thresh and scaled_macd < -thresh:
if pos <= 0:
self.BuyMarket(vol + abs(pos))
elif rsi_val > 50.0 + thresh and scaled_macd > thresh:
if pos >= 0:
self.SellMarket(vol + abs(pos))
def OnReseted(self):
super(macd_cci_lotfy_strategy, self).OnReseted()
def CreateClone(self):
return macd_cci_lotfy_strategy()