ICT NY Kill Zone Auto Trading
该策略在纽约杀戮时段内利用公平价值缺口和订单块进行交易。
详情
- 入场条件: Kill zone 内的公平价值缺口和订单块。
- 多空方向: 双向。
- 退出条件: 位置保护。
- 止损: 是。
- 默认值:
StopLoss= 30TakeProfit= 60CandleType= TimeSpan.FromMinutes(5)
- 过滤器:
- 类型: Breakout
- 方向: 双向
- 指标: Price Action
- 止损: 是
- 复杂度: 基础
- 时间框架: 日内 (5m)
- 季节性: 否
- 神经网络: 否
- 背离: 否
- 风险等级: 中等
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 NY Kill Zone Auto Trading strategy.
/// Trades during the New York kill zone when a fair value gap and order block appear.
/// Uses fixed take profit and stop loss.
/// </summary>
public class IctNyKillZoneAutoTradingStrategy : Strategy
{
private readonly StrategyParam<decimal> _stopLoss;
private readonly StrategyParam<decimal> _takeProfit;
private readonly StrategyParam<DataType> _candleType;
private ICandleMessage _prev1;
private ICandleMessage _prev2;
/// <summary>
/// Stop loss in price units.
/// </summary>
public decimal StopLoss
{
get => _stopLoss.Value;
set => _stopLoss.Value = value;
}
/// <summary>
/// Take profit in price units.
/// </summary>
public decimal TakeProfit
{
get => _takeProfit.Value;
set => _takeProfit.Value = value;
}
/// <summary>
/// Candle type used for calculations.
/// </summary>
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
/// <summary>
/// Initialize strategy parameters.
/// </summary>
public IctNyKillZoneAutoTradingStrategy()
{
_stopLoss = Param(nameof(StopLoss), 30m)
.SetDisplay("Stop Loss", "Stop loss in ticks", "Risk Management")
.SetOptimize(10m, 100m, 10m);
_takeProfit = Param(nameof(TakeProfit), 60m)
.SetDisplay("Take Profit", "Take profit in ticks", "Risk Management")
.SetOptimize(20m, 200m, 10m);
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
.SetDisplay("Candle Type", "Type of candles", "General");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
=> [(Security, CandleType)];
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_prev1 = null;
_prev2 = null;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(ProcessCandle)
.Start();
StartProtection(
takeProfit: new Unit(TakeProfit, UnitTypes.Absolute),
stopLoss: new Unit(StopLoss, UnitTypes.Absolute));
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle)
{
if (candle.State != CandleStates.Finished)
return;
if (_prev1 != null && _prev2 != null)
{
var isKillZone = IsKillZone(candle.OpenTime);
var isFvg = _prev2.HighPrice < candle.LowPrice && _prev1.HighPrice < candle.LowPrice;
var bullishOb = _prev2.ClosePrice < _prev2.OpenPrice && _prev1.ClosePrice > _prev1.OpenPrice && candle.ClosePrice > candle.OpenPrice;
var bearishOb = _prev2.ClosePrice > _prev2.OpenPrice && _prev1.ClosePrice < _prev1.OpenPrice && candle.ClosePrice < candle.OpenPrice;
if (isKillZone && isFvg && bullishOb && Position <= 0)
BuyMarket();
else if (isKillZone && isFvg && bearishOb && Position >= 0)
SellMarket();
}
_prev2 = _prev1;
_prev1 = candle;
}
private static bool IsKillZone(DateTimeOffset time)
{
var start = new DateTimeOffset(time.Year, time.Month, time.Day, 9, 30, 0, time.Offset);
var end = new DateTimeOffset(time.Year, time.Month, time.Day, 16, 0, 0, time.Offset);
return time >= start && time <= end;
}
}
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.Strategies import Strategy
class ict_ny_kill_zone_auto_trading_strategy(Strategy):
def __init__(self):
super(ict_ny_kill_zone_auto_trading_strategy, self).__init__()
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(5))) \
.SetDisplay("Candle Type", "Type of candles", "General")
self._prev1 = None
self._prev2 = None
self._entry_price = 0.0
self._stop_loss = 0.0
self._take_profit = 0.0
@property
def candle_type(self):
return self._candle_type.Value
@candle_type.setter
def candle_type(self, value):
self._candle_type.Value = value
def OnReseted(self):
super(ict_ny_kill_zone_auto_trading_strategy, self).OnReseted()
self._prev1 = None
self._prev2 = None
self._entry_price = 0.0
self._stop_loss = 0.0
self._take_profit = 0.0
def OnStarted2(self, time):
super(ict_ny_kill_zone_auto_trading_strategy, self).OnStarted2(time)
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(self.OnProcess).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawOwnTrades(area)
def _is_kill_zone(self, t):
hour = t.Hour
return 9 <= hour < 16 or (hour == 16 and t.Minute == 0)
def OnProcess(self, candle):
if candle.State != CandleStates.Finished:
return
close = float(candle.ClosePrice)
# Check stop/tp for existing position
if self.Position > 0 and self._entry_price > 0:
if close <= self._stop_loss or close >= self._take_profit:
self.SellMarket()
self._entry_price = 0.0
elif self.Position < 0 and self._entry_price > 0:
if close >= self._stop_loss or close <= self._take_profit:
self.BuyMarket()
self._entry_price = 0.0
if self._prev1 is not None and self._prev2 is not None:
is_kz = self._is_kill_zone(candle.OpenTime)
is_fvg = float(self._prev2.HighPrice) < float(candle.LowPrice) and float(self._prev1.HighPrice) < float(candle.LowPrice)
bullish_ob = float(self._prev2.ClosePrice) < float(self._prev2.OpenPrice) and float(self._prev1.ClosePrice) > float(self._prev1.OpenPrice) and close > float(candle.OpenPrice)
bearish_ob = float(self._prev2.ClosePrice) > float(self._prev2.OpenPrice) and float(self._prev1.ClosePrice) < float(self._prev1.OpenPrice) and close < float(candle.OpenPrice)
if is_kz and is_fvg and bullish_ob and self.Position <= 0:
self.BuyMarket()
self._entry_price = close
self._stop_loss = close - 30.0
self._take_profit = close + 60.0
elif is_kz and is_fvg and bearish_ob and self.Position >= 0:
self.SellMarket()
self._entry_price = close
self._stop_loss = close + 30.0
self._take_profit = close - 60.0
self._prev2 = self._prev1
self._prev1 = candle
def CreateClone(self):
return ict_ny_kill_zone_auto_trading_strategy()