在 GitHub 上查看

Color JLaguerre

基于彩色 Laguerre 振荡器的策略。

该指标使用 Jurik 滤波器平滑价格,并根据相对水平着色。当颜色改变时,可能意味着趋势转向。

当振荡器向上穿越中线时策略做多,向下穿越时做空。达到极端水平或出现反向信号时平仓。

细节

  • 入场条件:Laguerre 振荡器在中线附近的颜色变化。
  • 多头/空头:双向。
  • 出场条件:反向信号或触及极端水平。
  • 止损:是。
  • 默认值
    • RsiLength = 14
    • HighLevel = 85
    • MiddleLevel = 50
    • LowLevel = 15
    • StopLossPercent = 2m
    • CandleType = TimeSpan.FromHours(1)
  • 过滤器
    • 类别:振荡器
    • 方向:双向
    • 指标:RSI
    • 止损:是
    • 复杂度:基础
    • 时间框架:1小时
    • 季节性:否
    • 神经网络:否
    • 背离:否
    • 风险级别:中
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>
/// Strategy based on color-coded Laguerre oscillator approximated by RSI.
/// </summary>
public class ColorJLaguerreStrategy : Strategy
{
	private readonly StrategyParam<int> _rsiLength;
	private readonly StrategyParam<decimal> _highLevel;
	private readonly StrategyParam<decimal> _middleLevel;
	private readonly StrategyParam<decimal> _lowLevel;
	private readonly StrategyParam<decimal> _stopLossPercent;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prevRsi;
	private bool _hasPrev;

	public int RsiLength { get => _rsiLength.Value; set => _rsiLength.Value = value; }
	public decimal HighLevel { get => _highLevel.Value; set => _highLevel.Value = value; }
	public decimal MiddleLevel { get => _middleLevel.Value; set => _middleLevel.Value = value; }
	public decimal LowLevel { get => _lowLevel.Value; set => _lowLevel.Value = value; }
	public decimal StopLossPercent { get => _stopLossPercent.Value; set => _stopLossPercent.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public ColorJLaguerreStrategy()
	{
		_rsiLength = Param(nameof(RsiLength), 14)
			.SetDisplay("RSI Length", "Period for RSI", "Indicators");

		_highLevel = Param(nameof(HighLevel), 85m)
			.SetDisplay("High Level", "Upper threshold", "Levels");

		_middleLevel = Param(nameof(MiddleLevel), 50m)
			.SetDisplay("Middle Level", "Central threshold", "Levels");

		_lowLevel = Param(nameof(LowLevel), 15m)
			.SetDisplay("Low Level", "Lower threshold", "Levels");

		_stopLossPercent = Param(nameof(StopLossPercent), 2m)
			.SetDisplay("Stop Loss %", "Stop loss percentage", "Risk Management");

		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(1).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles to use", "General");
	}

	/// <inheritdoc />
	public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
		=> [(Security, CandleType)];

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_prevRsi = 0;
		_hasPrev = false;
	}

	/// <inheritdoc />
	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);

		var rsi = new RelativeStrengthIndex { Length = RsiLength };

		SubscribeCandles(CandleType)
			.Bind(rsi, ProcessCandle)
			.Start();

		StartProtection(
			takeProfit: new Unit(4, UnitTypes.Percent),
			stopLoss: new Unit(StopLossPercent, UnitTypes.Percent)
		);
	}

	private void ProcessCandle(ICandleMessage candle, decimal rsiValue)
	{
		if (candle.State != CandleStates.Finished)
			return;

		if (!_hasPrev)
		{
			_prevRsi = rsiValue;
			_hasPrev = true;
			return;
		}

		// Open long when RSI crosses above middle level
		if (_prevRsi <= MiddleLevel && rsiValue > MiddleLevel && Position <= 0)
		{
			if (Position < 0) BuyMarket();
			BuyMarket();
		}
		// Open short when RSI crosses below middle level
		else if (_prevRsi >= MiddleLevel && rsiValue < MiddleLevel && Position >= 0)
		{
			if (Position > 0) SellMarket();
			SellMarket();
		}

		// Exit long at high level
		if (Position > 0 && rsiValue >= HighLevel)
		{
			SellMarket();
		}
		// Exit short at low level
		else if (Position < 0 && rsiValue <= LowLevel)
		{
			BuyMarket();
		}

		_prevRsi = rsiValue;
	}
}