Color Laguerre
Трендовая стратегия на основе осциллятора Color Laguerre.
Color Laguerre сглаживает ценовой ряд фильтром Лагерра и меняет цвет при смене направления тренда. Стратегия покупает, когда осциллятор переходит в бычью зону, и продаёт при переходе в медвежью. Достижение крайних уровней может принудительно закрывать позиции при ослаблении импульса.
Детали
- Условия входа: пересечение средней линии осциллятора.
- Длинные/короткие: оба направления.
- Условия выхода: противоположный сигнал или стоп.
- Стопы: да.
- Значения по умолчанию:
Gamma= 0.7mHighLevel= 85MiddleLevel= 50LowLevel= 15StopLossPercent= 2mCandleType= TimeSpan.FromHours(1)
- Фильтры:
- Категория: Тренд
- Направление: Оба
- Индикаторы: Осциллятор
- Стопы: Да
- Сложность: Базовая
- Таймфрейм: Внутридневной (1ч)
- Сезонность: Нет
- Нейросети: Нет
- Дивергенция: Нет
- Уровень риска: Средний
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>
/// Trend-following strategy using Color Laguerre oscillator.
/// </summary>
public class ColorLaguerreStrategy : Strategy
{
private readonly StrategyParam<decimal> _gamma;
private readonly StrategyParam<int> _highLevel;
private readonly StrategyParam<int> _middleLevel;
private readonly StrategyParam<int> _lowLevel;
private readonly StrategyParam<bool> _buyOpen;
private readonly StrategyParam<bool> _sellOpen;
private readonly StrategyParam<bool> _buyClose;
private readonly StrategyParam<bool> _sellClose;
private readonly StrategyParam<decimal> _stopLossPercent;
private readonly StrategyParam<DataType> _candleType;
private int? _prevSignal;
/// <summary>
/// Gamma parameter for the Laguerre filter.
/// </summary>
public decimal Gamma
{
get => _gamma.Value;
set => _gamma.Value = value;
}
/// <summary>
/// Upper threshold where overbought conditions are assumed.
/// </summary>
public int HighLevel
{
get => _highLevel.Value;
set => _highLevel.Value = value;
}
/// <summary>
/// Middle level separating bullish and bearish zones.
/// </summary>
public int MiddleLevel
{
get => _middleLevel.Value;
set => _middleLevel.Value = value;
}
/// <summary>
/// Lower threshold where oversold conditions are assumed.
/// </summary>
public int LowLevel
{
get => _lowLevel.Value;
set => _lowLevel.Value = value;
}
/// <summary>
/// Allow opening long positions.
/// </summary>
public bool BuyOpen
{
get => _buyOpen.Value;
set => _buyOpen.Value = value;
}
/// <summary>
/// Allow opening short positions.
/// </summary>
public bool SellOpen
{
get => _sellOpen.Value;
set => _sellOpen.Value = value;
}
/// <summary>
/// Allow closing long positions on opposite signals.
/// </summary>
public bool BuyClose
{
get => _buyClose.Value;
set => _buyClose.Value = value;
}
/// <summary>
/// Allow closing short positions on opposite signals.
/// </summary>
public bool SellClose
{
get => _sellClose.Value;
set => _sellClose.Value = value;
}
/// <summary>
/// Stop loss as percent of entry price.
/// </summary>
public decimal StopLossPercent
{
get => _stopLossPercent.Value;
set => _stopLossPercent.Value = value;
}
/// <summary>
/// Candle type used for analysis.
/// </summary>
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
/// <summary>
/// Initializes a new instance of <see cref="ColorLaguerreStrategy"/>.
/// </summary>
public ColorLaguerreStrategy()
{
_gamma = Param(nameof(Gamma), 0.7m)
.SetRange(0.1m, 0.9m)
.SetDisplay("Gamma", "Laguerre filter gamma", "Indicators")
;
_highLevel = Param(nameof(HighLevel), 85)
.SetRange(50, 95)
.SetDisplay("High Level", "Upper oscillator level", "Indicators");
_middleLevel = Param(nameof(MiddleLevel), 50)
.SetRange(10, 90)
.SetDisplay("Middle Level", "Middle oscillator level", "Indicators");
_lowLevel = Param(nameof(LowLevel), 15)
.SetRange(5, 40)
.SetDisplay("Low Level", "Lower oscillator level", "Indicators");
_buyOpen = Param(nameof(BuyOpen), true)
.SetDisplay("Buy Open", "Allow opening long positions", "Trading");
_sellOpen = Param(nameof(SellOpen), true)
.SetDisplay("Sell Open", "Allow opening short positions", "Trading");
_buyClose = Param(nameof(BuyClose), true)
.SetDisplay("Buy Close", "Allow closing longs", "Trading");
_sellClose = Param(nameof(SellClose), true)
.SetDisplay("Sell Close", "Allow closing shorts", "Trading");
_stopLossPercent = Param(nameof(StopLossPercent), 2m)
.SetRange(0.5m, 5m)
.SetDisplay("Stop Loss %", "Stop loss percentage", "Risk")
;
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
.SetDisplay("Candle Type", "Type of candles to use", "General");
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_prevSignal = null;
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
// Use RSI as a proxy for the Laguerre oscillator
var rsi = new RelativeStrengthIndex { Length = 14 };
var subscription = SubscribeCandles(CandleType);
subscription
.BindEx(rsi, ProcessCandle)
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, rsi);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle, IIndicatorValue rsiValue)
{
if (candle.State != CandleStates.Finished)
return;
if (!rsiValue.IsFormed)
return;
var value = rsiValue.GetValue<decimal>();
int signal = value >= MiddleLevel ? 2 : 1;
if (!IsFormedAndOnlineAndAllowTrading())
{
_prevSignal = signal;
return;
}
if (_prevSignal is not int prev)
{
_prevSignal = signal;
return;
}
if (prev == 1 && signal == 2 && Position <= 0)
{
BuyMarket();
}
else if (prev == 2 && signal == 1 && Position >= 0)
{
SellMarket();
}
_prevSignal = signal;
if (Position > 0 && value <= LowLevel && SellClose)
SellMarket();
else if (Position < 0 && value >= HighLevel && BuyClose)
BuyMarket();
}
}
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 RelativeStrengthIndex
from StockSharp.Algo.Strategies import Strategy
class color_laguerre_strategy(Strategy):
def __init__(self):
super(color_laguerre_strategy, self).__init__()
self._gamma = self.Param("Gamma", 0.7) \
.SetDisplay("Gamma", "Laguerre filter gamma", "Indicators")
self._high_level = self.Param("HighLevel", 85) \
.SetDisplay("High Level", "Upper oscillator level", "Indicators")
self._middle_level = self.Param("MiddleLevel", 50) \
.SetDisplay("Middle Level", "Middle oscillator level", "Indicators")
self._low_level = self.Param("LowLevel", 15) \
.SetDisplay("Low Level", "Lower oscillator level", "Indicators")
self._buy_open = self.Param("BuyOpen", True) \
.SetDisplay("Buy Open", "Allow opening long positions", "Trading")
self._sell_open = self.Param("SellOpen", True) \
.SetDisplay("Sell Open", "Allow opening short positions", "Trading")
self._buy_close = self.Param("BuyClose", True) \
.SetDisplay("Buy Close", "Allow closing longs", "Trading")
self._sell_close = self.Param("SellClose", True) \
.SetDisplay("Sell Close", "Allow closing shorts", "Trading")
self._stop_loss_percent = self.Param("StopLossPercent", 2.0) \
.SetDisplay("Stop Loss %", "Stop loss percentage", "Risk")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4))) \
.SetDisplay("Candle Type", "Type of candles to use", "General")
self._prev_signal = None
@property
def gamma(self):
return self._gamma.Value
@property
def high_level(self):
return self._high_level.Value
@property
def middle_level(self):
return self._middle_level.Value
@property
def low_level(self):
return self._low_level.Value
@property
def buy_open(self):
return self._buy_open.Value
@property
def sell_open(self):
return self._sell_open.Value
@property
def buy_close(self):
return self._buy_close.Value
@property
def sell_close(self):
return self._sell_close.Value
@property
def stop_loss_percent(self):
return self._stop_loss_percent.Value
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(color_laguerre_strategy, self).OnReseted()
self._prev_signal = None
def OnStarted2(self, time):
super(color_laguerre_strategy, self).OnStarted2(time)
rsi = RelativeStrengthIndex()
rsi.Length = 14
subscription = self.SubscribeCandles(self.candle_type)
subscription.BindEx(rsi, self.process_candle).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawIndicator(area, rsi)
self.DrawOwnTrades(area)
def process_candle(self, candle, rsi_value):
if candle.State != CandleStates.Finished:
return
if not rsi_value.IsFormed:
return
value = float(rsi_value)
mid = float(self.middle_level)
signal = 2 if value >= mid else 1
if self._prev_signal is None:
self._prev_signal = signal
return
prev = self._prev_signal
if prev == 1 and signal == 2 and self.Position <= 0:
self.BuyMarket()
elif prev == 2 and signal == 1 and self.Position >= 0:
self.SellMarket()
self._prev_signal = signal
if self.Position > 0 and value <= float(self.low_level) and self.sell_close:
self.SellMarket()
elif self.Position < 0 and value >= float(self.high_level) and self.buy_close:
self.BuyMarket()
def CreateClone(self):
return color_laguerre_strategy()