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= 9KijunPeriod= 26SenkouSpanBPeriod= 52Offset= 26BuyThreshold= 60SellThreshold= -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();
}
}
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 Ichimoku
from StockSharp.Algo.Strategies import Strategy
class weighted_ichimoku_strategy(Strategy):
def __init__(self):
super(weighted_ichimoku_strategy, self).__init__()
self._tenkan_period = self.Param("TenkanPeriod", 9) \
.SetDisplay("Tenkan Period", "Tenkan length", "Ichimoku")
self._kijun_period = self.Param("KijunPeriod", 26) \
.SetDisplay("Kijun Period", "Kijun length", "Ichimoku")
self._senkou_span_b_period = self.Param("SenkouSpanBPeriod", 52) \
.SetDisplay("Senkou B Period", "Span B length", "Ichimoku")
self._buy_threshold = self.Param("BuyThreshold", 70.0) \
.SetDisplay("Buy Threshold", "Score to enter long", "General")
self._sell_threshold = self.Param("SellThreshold", -70.0) \
.SetDisplay("Sell Threshold", "Score to exit/short", "General")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(1))) \
.SetDisplay("Candle Type", "Type of candles", "General")
@property
def tenkan_period(self):
return self._tenkan_period.Value
@property
def kijun_period(self):
return self._kijun_period.Value
@property
def senkou_span_b_period(self):
return self._senkou_span_b_period.Value
@property
def buy_threshold(self):
return self._buy_threshold.Value
@property
def sell_threshold(self):
return self._sell_threshold.Value
@property
def candle_type(self):
return self._candle_type.Value
def OnStarted2(self, time):
super(weighted_ichimoku_strategy, self).OnStarted2(time)
ichimoku = Ichimoku()
ichimoku.Tenkan.Length = self.tenkan_period
ichimoku.Kijun.Length = self.kijun_period
ichimoku.SenkouB.Length = self.senkou_span_b_period
subscription = self.SubscribeCandles(self.candle_type)
subscription.BindEx(ichimoku, self.on_process).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawIndicator(area, ichimoku)
self.DrawOwnTrades(area)
def on_process(self, candle, value):
if candle.State != CandleStates.Finished:
return
t = value.Tenkan
k = value.Kijun
sa = value.SenkouA
sb = value.SenkouB
if t is None or k is None or sa is None or sb is None:
return
tenkan = float(t)
kijun = float(k)
senkou_a = float(sa)
senkou_b = float(sb)
if tenkan == 0 or kijun == 0 or senkou_a == 0 or senkou_b == 0:
return
cloud_top = max(senkou_a, senkou_b)
cloud_bottom = min(senkou_a, senkou_b)
close = float(candle.ClosePrice)
score = 0.0
if tenkan > kijun:
score += 25.0
elif tenkan < kijun:
score -= 25.0
if close > cloud_top:
score += 30.0
elif close < cloud_bottom:
score -= 30.0
if close > kijun:
score += 15.0
elif close < kijun:
score -= 15.0
if score >= self.buy_threshold and self.Position <= 0:
self.BuyMarket()
elif score <= self.sell_threshold and self.Position >= 0:
self.SellMarket()
def CreateClone(self):
return weighted_ichimoku_strategy()