ICT Master Suite Trading IQ戦略
ICT Master Suite戦略は、日次セッションの高値と安値のブレイクアウトを取引します。価格がセッション高値を上回って終値を付けた場合にロングポジションを建て、セッション安値を下回って終値を付けた場合にショートポジションを建てます。ポジションはATRベースのトレーリングストップで管理されます。
詳細
- エントリー条件:
- 価格が現在のセッション高値を上回って終値を付けた場合(ロング)。
- 価格が現在のセッション安値を下回って終値を付けた場合(ショート)。
- ロング/ショート: ロングおよびショート。
- エグジット条件:
- ATRベースのトレーリングストップ。
- ストップ: ATRトレーリングストップ。
- デフォルト値:
AtrPeriod= 14AtrMultiplier= 1.5AllowLong= trueAllowShort= true
- フィルター:
- カテゴリ: ブレイクアウト
- 方向: 両方
- インジケーター: ATR
- ストップ: はい
- 複雑さ: 低
- 時間軸: イントラデイ
- 季節性: いいえ
- ニューラルネットワーク: いいえ
- ダイバージェンス: いいえ
- リスクレベル: 中
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>
/// ICT Master Suite session breakout strategy.
/// The session is the UTC day. A close above the high of the session's earlier candles goes long and a close below their low goes short,
/// reversing an opposite position. An open position is protected by a trailing stop AtrMultiplier ATRs away from the close that only moves
/// in the trade's favour; the position closes when a candle touches it.
/// </summary>
public class IctMasterSuiteTradingIqStrategy : Strategy
{
private readonly StrategyParam<int> _atrPeriod;
private readonly StrategyParam<decimal> _atrMultiplier;
private readonly StrategyParam<bool> _allowLong;
private readonly StrategyParam<bool> _allowShort;
private readonly StrategyParam<DataType> _candleType;
private DateTime? _sessionDate;
private decimal _sessionHigh;
private decimal _sessionLow;
private decimal? _stopPrice;
/// <summary>
/// ATR period.
/// </summary>
public int AtrPeriod
{
get => _atrPeriod.Value;
set => _atrPeriod.Value = value;
}
/// <summary>
/// ATR multiplier for the trailing stop distance.
/// </summary>
public decimal AtrMultiplier
{
get => _atrMultiplier.Value;
set => _atrMultiplier.Value = value;
}
/// <summary>
/// Allow long trades.
/// </summary>
public bool AllowLong
{
get => _allowLong.Value;
set => _allowLong.Value = value;
}
/// <summary>
/// Allow short trades.
/// </summary>
public bool AllowShort
{
get => _allowShort.Value;
set => _allowShort.Value = value;
}
/// <summary>
/// Candle type.
/// </summary>
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
/// <summary>
/// Constructor.
/// </summary>
public IctMasterSuiteTradingIqStrategy()
{
_atrPeriod = Param(nameof(AtrPeriod), 14)
.SetGreaterThanZero()
.SetDisplay("ATR Period", "ATR calculation period", "Risk Management")
.SetOptimize(5, 30, 1);
_atrMultiplier = Param(nameof(AtrMultiplier), 1.5m)
.SetGreaterThanZero()
.SetDisplay("ATR Multiplier", "ATR multiplier for the trailing stop", "Risk Management")
.SetOptimize(1m, 3m, 0.5m);
_allowLong = Param(nameof(AllowLong), true)
.SetDisplay("Allow Long", "Enable long trades", "General");
_allowShort = Param(nameof(AllowShort), true)
.SetDisplay("Allow Short", "Enable short trades", "General");
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
.SetDisplay("Candle Type", "Type of candles", "General");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
ResetState();
}
private void ResetState()
{
_sessionDate = null;
_sessionHigh = 0m;
_sessionLow = 0m;
_stopPrice = null;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
ResetState();
var atr = new AverageTrueRange { Length = AtrPeriod };
var subscription = SubscribeCandles(CandleType);
subscription
.BindEx(atr, ProcessCandle)
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, atr);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle, IIndicatorValue atrValue)
{
if (candle.State != CandleStates.Finished)
return;
var date = candle.OpenTime.Date;
// The first candle of a session only opens the range; breakouts need earlier candles of the same session.
if (_sessionDate != date)
{
_sessionDate = date;
_sessionHigh = candle.HighPrice;
_sessionLow = candle.LowPrice;
ManageStop(candle, atrValue);
return;
}
var sessionHigh = _sessionHigh;
var sessionLow = _sessionLow;
_sessionHigh = Math.Max(_sessionHigh, candle.HighPrice);
_sessionLow = Math.Min(_sessionLow, candle.LowPrice);
if (ManageStop(candle, atrValue))
return;
if (!atrValue.IsFormed || !IsFormedAndOnlineAndAllowTrading())
return;
var atr = atrValue.GetValue<decimal>();
var close = candle.ClosePrice;
if (AllowLong && close > sessionHigh && Position <= 0)
{
BuyMarket(Volume + Math.Abs(Position));
_stopPrice = close - atr * AtrMultiplier;
}
else if (AllowShort && close < sessionLow && Position >= 0)
{
SellMarket(Volume + Math.Abs(Position));
_stopPrice = close + atr * AtrMultiplier;
}
}
// Returns true when the position was closed by the trailing stop on this candle.
private bool ManageStop(ICandleMessage candle, IIndicatorValue atrValue)
{
if (Position == 0)
{
_stopPrice = null;
return false;
}
if (_stopPrice is not decimal stop)
return false;
if (Position > 0 && candle.LowPrice <= stop)
{
SellMarket(Position);
_stopPrice = null;
return true;
}
if (Position < 0 && candle.HighPrice >= stop)
{
BuyMarket(-Position);
_stopPrice = null;
return true;
}
if (atrValue.IsFormed)
{
var distance = atrValue.GetValue<decimal>() * AtrMultiplier;
_stopPrice = Position > 0
? Math.Max(stop, candle.ClosePrice - distance)
: Math.Min(stop, candle.ClosePrice + distance);
}
return false;
}
}
import clr
clr.AddReference("StockSharp.Messages")
clr.AddReference("StockSharp.Algo")
clr.AddReference("StockSharp.Algo.Indicators")
clr.AddReference("StockSharp.Algo.Strategies")
from System import TimeSpan, Decimal
from StockSharp.Messages import DataType, CandleStates
from StockSharp.Algo.Indicators import AverageTrueRange
from StockSharp.Algo.Strategies import Strategy
class ict_master_suite_trading_iq_strategy(Strategy):
"""
ICT Master Suite session breakout strategy.
The session is the UTC day. A close above the high of the session's earlier candles goes long and a close below their low goes short,
reversing an opposite position. An open position is protected by a trailing stop AtrMultiplier ATRs away from the close that only moves
in the trade's favour; the position closes when a candle touches it.
"""
def __init__(self):
super(ict_master_suite_trading_iq_strategy, self).__init__()
self._atr_period = self.Param("AtrPeriod", 14).SetGreaterThanZero().SetDisplay("ATR Period", "ATR calculation period", "Risk Management")
self._atr_multiplier = self.Param("AtrMultiplier", 1.5).SetGreaterThanZero().SetDisplay("ATR Multiplier", "ATR multiplier for the trailing stop", "Risk Management")
self._allow_long = self.Param("AllowLong", True).SetDisplay("Allow Long", "Enable long trades", "General")
self._allow_short = self.Param("AllowShort", True).SetDisplay("Allow Short", "Enable short trades", "General")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4))).SetDisplay("Candle Type", "Type of candles", "General")
self._reset_state()
@property
def candle_type(self):
return self._candle_type.Value
def _reset_state(self):
self._session_date = None
self._session_high = None
self._session_low = None
self._stop_price = None
def OnReseted(self):
super(ict_master_suite_trading_iq_strategy, self).OnReseted()
self._reset_state()
def OnStarted2(self, time):
super(ict_master_suite_trading_iq_strategy, self).OnStarted2(time)
self._reset_state()
atr = AverageTrueRange()
atr.Length = self._atr_period.Value
subscription = self.SubscribeCandles(self.candle_type)
subscription.BindEx(atr, self._process_candle).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawIndicator(area, atr)
self.DrawOwnTrades(area)
def _process_candle(self, candle, atr_value):
if candle.State != CandleStates.Finished:
return
date = candle.OpenTime.Date
# The first candle of a session only opens the range; breakouts need earlier candles of the same session.
if self._session_date is None or self._session_date != date:
self._session_date = date
self._session_high = candle.HighPrice
self._session_low = candle.LowPrice
self._manage_stop(candle, atr_value)
return
session_high = self._session_high
session_low = self._session_low
self._session_high = max(self._session_high, candle.HighPrice)
self._session_low = min(self._session_low, candle.LowPrice)
if self._manage_stop(candle, atr_value):
return
if not atr_value.IsFormed or not self.IsFormedAndOnlineAndAllowTrading():
return
atr = atr_value.GetValue[Decimal](None)
multiplier = Decimal(self._atr_multiplier.Value)
close = candle.ClosePrice
if self._allow_long.Value and close > session_high and self.Position <= 0:
self.BuyMarket(self.Volume + abs(self.Position))
self._stop_price = close - atr * multiplier
elif self._allow_short.Value and close < session_low and self.Position >= 0:
self.SellMarket(self.Volume + abs(self.Position))
self._stop_price = close + atr * multiplier
# Returns True when the position was closed by the trailing stop on this candle.
def _manage_stop(self, candle, atr_value):
if self.Position == 0:
self._stop_price = None
return False
stop = self._stop_price
if stop is None:
return False
if self.Position > 0 and candle.LowPrice <= stop:
self.SellMarket(self.Position)
self._stop_price = None
return True
if self.Position < 0 and candle.HighPrice >= stop:
self.BuyMarket(-self.Position)
self._stop_price = None
return True
if atr_value.IsFormed:
distance = atr_value.GetValue[Decimal](None) * Decimal(self._atr_multiplier.Value)
if self.Position > 0:
self._stop_price = max(stop, candle.ClosePrice - distance)
else:
self._stop_price = min(stop, candle.ClosePrice + distance)
return False
def CreateClone(self):
return ict_master_suite_trading_iq_strategy()