流动性内部结构转向策略
该策略结合最近高低点的流动性线与内部结构转向。当价格触及流动性线后出现相反方向的结构突破时开仓。可选择仅做多、仅做空或双向交易。
细节
- 入场条件:
- 多头:价格收于先前空头结构之上并触及最近低点的流动性线。
- 空头:价格收于先前多头结构之下并触及最近高点的流动性线。
- 方向:双向或可选仅多 / 仅空。
- 离场条件:
- 入场后出现反向信号。
StopLossPips点的止损。- 可选
TakeProfitPips点的止盈。
- 止损:支持止损并可选止盈。
- 过滤:
- 仅在指定时间范围内交易。
- 信号锁定防止在数根K线内重复开仓。
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>
/// Liquidity Internal Market Shift strategy.
/// Detects internal market structure shifts at liquidity zones.
/// </summary>
public class LiquidityInternalMarketShiftStrategy : Strategy
{
private readonly StrategyParam<int> _upperLB;
private readonly StrategyParam<int> _lowerLB;
private readonly StrategyParam<DataType> _candleType;
public int UpperLiquidityLookback { get => _upperLB.Value; set => _upperLB.Value = value; }
public int LowerLiquidityLookback { get => _lowerLB.Value; set => _lowerLB.Value = value; }
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public LiquidityInternalMarketShiftStrategy()
{
_upperLB = Param(nameof(UpperLiquidityLookback), 14).SetGreaterThanZero().SetDisplay("Upper LB", "Upper", "Signals");
_lowerLB = Param(nameof(LowerLiquidityLookback), 14).SetGreaterThanZero().SetDisplay("Lower LB", "Lower", "Signals");
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame()).SetDisplay("Candle Type", "Candles", "General");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var highest = new Highest { Length = UpperLiquidityLookback };
var lowest = new Lowest { Length = LowerLiquidityLookback };
var sub = SubscribeCandles(CandleType);
sub.Bind(highest, lowest, ProcessCandle).Start();
var area = CreateChartArea();
if (area != null) { DrawCandles(area, sub); DrawOwnTrades(area); }
}
private void ProcessCandle(ICandleMessage candle, decimal highestVal, decimal lowestVal)
{
if (candle.State != CandleStates.Finished)
return;
if (highestVal == 0m || lowestVal == 0m)
return;
var bull = candle.ClosePrice > candle.OpenPrice;
var bear = candle.ClosePrice < candle.OpenPrice;
var body = Math.Abs(candle.ClosePrice - candle.OpenPrice);
var range = candle.HighPrice - candle.LowPrice;
// Market shift detection: strong candle at liquidity zone
var strongCandle = range > 0 && body / range > 0.5m;
// Bullish shift: price sweeps low and closes strong bullish
var bullSignal = bull && strongCandle && candle.LowPrice <= lowestVal && candle.ClosePrice > lowestVal;
// Bearish shift: price sweeps high and closes strong bearish
var bearSignal = bear && strongCandle && candle.HighPrice >= highestVal && candle.ClosePrice < highestVal;
if (bearSignal && Position >= 0)
SellMarket();
else if (bullSignal && Position <= 0)
BuyMarket();
}
}
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.Indicators import Highest, Lowest
from StockSharp.Algo.Strategies import Strategy
class liquidity_internal_market_shift_strategy(Strategy):
def __init__(self):
super(liquidity_internal_market_shift_strategy, self).__init__()
self._upper_lb = self.Param("UpperLiquidityLookback", 14) \
.SetGreaterThanZero() \
.SetDisplay("Upper LB", "Upper", "Signals")
self._lower_lb = self.Param("LowerLiquidityLookback", 14) \
.SetGreaterThanZero() \
.SetDisplay("Lower LB", "Lower", "Signals")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(5))) \
.SetDisplay("Candle Type", "Candles", "General")
@property
def candle_type(self):
return self._candle_type.Value
@candle_type.setter
def candle_type(self, value):
self._candle_type.Value = value
def OnStarted2(self, time):
super(liquidity_internal_market_shift_strategy, self).OnStarted2(time)
highest = Highest()
highest.Length = self._upper_lb.Value
lowest = Lowest()
lowest.Length = self._lower_lb.Value
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(highest, lowest, self.OnProcess).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawOwnTrades(area)
def OnProcess(self, candle, highest_val, lowest_val):
if candle.State != CandleStates.Finished:
return
hv = float(highest_val)
lv = float(lowest_val)
if hv == 0.0 or lv == 0.0:
return
close = float(candle.ClosePrice)
opn = float(candle.OpenPrice)
high = float(candle.HighPrice)
low = float(candle.LowPrice)
bull = close > opn
bear = close < opn
body = abs(close - opn)
rng = high - low
strong_candle = rng > 0 and body / rng > 0.5
bull_signal = bull and strong_candle and low <= lv and close > lv
bear_signal = bear and strong_candle and high >= hv and close < hv
if bear_signal and self.Position >= 0:
self.SellMarket()
elif bull_signal and self.Position <= 0:
self.BuyMarket()
def CreateClone(self):
return liquidity_internal_market_shift_strategy()