Auf GitHub ansehen

Weighted Ichimoku Strategy

Strategy combines Ichimoku signals into a weighted score. It buys when the score exceeds the buy threshold and exits when the score drops below the sell threshold.

Details

  • Entry Criteria: score >= BuyThreshold
  • Long/Short: Long only
  • Exit Criteria: score <= SellThreshold or below zero if threshold disabled
  • Stops: No
  • Default Values:
    • TenkanPeriod = 9
    • KijunPeriod = 26
    • SenkouSpanBPeriod = 52
    • Offset = 26
    • BuyThreshold = 60
    • SellThreshold = -49
  • Filters:
    • Category: Trend
    • Direction: Long
    • Indicators: Ichimoku
    • Stops: No
    • Complexity: Basic
    • Timeframe: Intraday
    • Seasonality: No
    • Neural networks: No
    • Divergence: No
    • Risk level: Medium
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>
/// Weighted Ichimoku strategy using score from Tenkan/Kijun cross and Kumo breakout.
/// Opens long when score exceeds the buy threshold and closes when score drops below the sell threshold.
/// </summary>
public class WeightedIchimokuStrategy : Strategy
{
	private readonly StrategyParam<int> _tenkanPeriod;
	private readonly StrategyParam<int> _kijunPeriod;
	private readonly StrategyParam<int> _senkouSpanBPeriod;
	private readonly StrategyParam<decimal> _buyThreshold;
	private readonly StrategyParam<decimal> _sellThreshold;
	private readonly StrategyParam<DataType> _candleType;

	public int TenkanPeriod { get => _tenkanPeriod.Value; set => _tenkanPeriod.Value = value; }
	public int KijunPeriod { get => _kijunPeriod.Value; set => _kijunPeriod.Value = value; }
	public int SenkouSpanBPeriod { get => _senkouSpanBPeriod.Value; set => _senkouSpanBPeriod.Value = value; }
	public decimal BuyThreshold { get => _buyThreshold.Value; set => _buyThreshold.Value = value; }
	public decimal SellThreshold { get => _sellThreshold.Value; set => _sellThreshold.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public WeightedIchimokuStrategy()
	{
		_tenkanPeriod = Param(nameof(TenkanPeriod), 9)
			.SetGreaterThanZero()
			.SetDisplay("Tenkan Period", "Tenkan length", "Ichimoku");

		_kijunPeriod = Param(nameof(KijunPeriod), 26)
			.SetGreaterThanZero()
			.SetDisplay("Kijun Period", "Kijun length", "Ichimoku");

		_senkouSpanBPeriod = Param(nameof(SenkouSpanBPeriod), 52)
			.SetGreaterThanZero()
			.SetDisplay("Senkou B Period", "Span B length", "Ichimoku");

		_buyThreshold = Param(nameof(BuyThreshold), 70m)
			.SetDisplay("Buy Threshold", "Score to enter long", "General");

		_sellThreshold = Param(nameof(SellThreshold), -70m)
			.SetDisplay("Sell Threshold", "Score to exit/short", "General");

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

	public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
	{
		return [(Security, CandleType)];
	}

	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);

		var ichimoku = new Ichimoku
		{
			Tenkan = { Length = TenkanPeriod },
			Kijun = { Length = KijunPeriod },
			SenkouB = { Length = SenkouSpanBPeriod }
		};

		var subscription = SubscribeCandles(CandleType);
		subscription.BindEx(ichimoku, ProcessCandle).Start();

		var area = CreateChartArea();
		if (area != null)
		{
			DrawCandles(area, subscription);
			DrawIndicator(area, ichimoku);
			DrawOwnTrades(area);
		}
	}

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

		var ichimokuValue = (IchimokuValue)value;

		if (ichimokuValue.Tenkan is not decimal tenkan ||
			ichimokuValue.Kijun is not decimal kijun ||
			ichimokuValue.SenkouA is not decimal senkouA ||
			ichimokuValue.SenkouB is not decimal senkouB)
		{
			return;
		}

		var cloudTop = Math.Max(senkouA, senkouB);
		var cloudBottom = Math.Min(senkouA, senkouB);

		decimal score = 0m;
		// Tenkan/Kijun cross score
		score += tenkan > kijun ? 25m : tenkan < kijun ? -25m : 0m;
		// Cloud position score
		score += candle.ClosePrice > cloudTop ? 30m : candle.ClosePrice < cloudBottom ? -30m : 0m;
		// Price vs Kijun
		score += candle.ClosePrice > kijun ? 15m : candle.ClosePrice < kijun ? -15m : 0m;

		if (score >= BuyThreshold && Position <= 0)
			BuyMarket();
		else if (score <= SellThreshold && Position >= 0)
			SellMarket();
	}
}