本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © TradingFinder
//@version=6
indicator("Structural Liquidity Sweep & Order Block Zones", "SLS OB Zones", overlay = true, max_bars_back = 5000, max_lines_count = 500, max_labels_count = 500)
import TFlab/OrderBlockRefiner_TradingFinder/2 as Refiner
// ============================================================================
// 1. การตั้งค่าพารามิเตอร์ (Inputs)
// ============================================================================
// Group: Detection
PP = input.int(3, "One & Two Setup Pivot Period", minval = 2, maxval = 100, group = "Detection",
tooltip = "Number of left and right bars required to confirm swing pivots for One and Two Liquidity Setups (Setup A & B). Lower values detect smaller, more frequent swings; higher values focus on broader market structures.")
cPP = input.int(9, "Midway Liquidity Setup Pivot Period", minval = 2, maxval = 100, group = "Detection",
tooltip = "Number of left and right bars required to confirm swing pivots and BOS levels for the Midway Liquidity Setup (Setup C). Higher values filter out minor noise and capture more significant structural breaks.")
enableB = input.bool(true, "One Liquidity Setup", group = "Detection",
tooltip = "Enable or disable Setup B (One Liquidity Setup), which plots Order Blocks confirmed by a single untouched internal liquidity level sitting ahead of the zone.")
enableA = input.bool(true, "Two Liquidity Setup", group = "Detection",
tooltip = "Enable or disable Setup A (Two Liquidity Setup), which plots high-probability Order Blocks backed by two separate untouched liquidity levels resting ahead of the zone.")
enableC = input.bool(true, "Midway Liquidity Setup", group = "Detection",
tooltip = "Enable or disable Setup C (Midway Liquidity Setup), which identifies Order Blocks formed after a valid Break of Structure (BOS) followed by a fresh inducement swing.")
cQualityMode = input.string("Defensive", "Midway Liquidity Mode", options = ["Aggressive", "Moderate", "Defensive"], group = "Detection",
tooltip = "Selects the filtering strictness for Setup C. Aggressive allows more signals with lighter BOS and swing requirements. Moderate offers balanced filtering. Defensive requires stronger BOS momentum, a shorter formation window, and all four quality criteria.")
// Group: Order Blocks
OBVaP = input.int(1000, "OB Validity (Bars From Origin)", minval = 10, maxval = 4998, group = "Order Blocks",
tooltip = "Maximum lifespan of an Order Block measured in bars from its origin candle. Once this bar limit is exceeded without mitigation, the Order Block expires and stops extending.")
MLOB = input.string("Proximal", "OB Mitigation Level", options = ["Proximal", "50 % OB", "Distal"], group = "Order Blocks",
tooltip = "Defines how deeply price must penetrate an active Order Block to consider it mitigated: Proximal (nearest edge), 50% OB (midpoint of the zone), or Distal (farthest edge).")
Refine = input.bool(true, "Refine OB", inline = "ref", group = "Order Blocks",
tooltip = "Toggle automatic Order Block zone refinement to narrow down wide candles into tighter, more precise entry zones.")
RefineMe = input.string("Defensive", "", options = ["Defensive", "Aggressive"], inline = "ref", group = "Order Blocks",
tooltip = "Selects the Order Block refinement style when Refine OB is enabled: Defensive keeps a slightly wider, safer zone, while Aggressive tightens the zone for sharper risk-to-reward entries.")
// Group: Display Settings
showOB = input.bool(true, "Show Order Blocks", inline = "obcolor", group = "Display Settings",
tooltip = "Toggle the visibility of Demand and Supply Order Block zones and their labels on the chart.")
DemandColor = input.color(#00B4EB, "Demand", inline = "obcolor", group = "Display Settings",
tooltip = "Sets the border and fill color for bullish Demand Order Block zones.")
SupplyColor = input.color(#E7EB00, "Supply", inline = "obcolor", group = "Display Settings",
tooltip = "Sets the border and fill color for bearish Supply Order Block zones.")
showLiquidity = input.bool(true, "Show Liquidity", inline = "liqcolor", group = "Display Settings",
tooltip = "Toggle the visibility of horizontal liquidity lines associated with active Order Block setups.")
bullLiquidityColor = input.color(#5BE660, "Bullish", inline = "liqcolor", group = "Display Settings",
tooltip = "Sets the line color for liquidity levels associated with bullish Demand setups.")
bearLiquidityColor = input.color(#E65B5B, "Bearish", inline = "liqcolor", group = "Display Settings",
tooltip = "Sets the line color for liquidity levels associated with bearish Supply setups.")
showLiquidityText = input.bool(true, "Show Liquidity Text", group = "Display Settings",
tooltip = "Show or hide the 'Liquidity' text labels centered on active liquidity lines.")
showSignals = input.bool(true, "Show OB Entry Signals", group = "Display Settings",
tooltip = "Show or hide Long and Short entry signal triangles and labels when price taps an active Order Block for the first time.")
// ============================================================================
// 2. โครงสร้างข้อมูล (User-Defined Types - UDTs)
// ============================================================================
type LiquidityLevel
int originBar
float price
bool extending
line ln
label tag
type MidwayCandidate
int direction
int originBar
int breakBar
float top
float bottom
bool strongBos
bool goodOB
bool cleanApproach
int oppositeStreak
type OrderBlock
int direction
int originBar
int createdBar
float top
float bottom
bool active
line topLine
line bottomLine
linefill zoneFill
label obTag
bool entryMarked
array<LiquidityLevel> levels
int setupVersion
int breakBar
// ============================================================================
// 3. ตรรกะการตรวจจับโครงสร้างและ Liquidity Setups
// ============================================================================
atr = ta.atr(50)
float cBosBreakATR = cQualityMode == "Aggressive" ? 0.10 : cQualityMode == "Moderate" ? 0.15 : 0.25
float cBosBodyATR = cQualityMode == "Aggressive" ? 0.30 : cQualityMode == "Moderate" ? 0.45 : 0.60
float cSwingReactionATR = cQualityMode == "Aggressive" ? 0.25 : cQualityMode == "Moderate" ? 0.35 : 0.50
int cFormationBars = cQualityMode == "Aggressive" ? 75 : cQualityMode == "Moderate" ? 50 : 30
int cMinScore = cQualityMode == "Aggressive" ? 2 : cQualityMode == "Moderate" ? 3 : 4
var ArrayType = array.new_string()
var ArrayValue = array.new_float()
var ArrayIndex = array.new_int()
HighPivot = ta.pivothigh(PP, PP)
LowPivot = ta.pivotlow(PP, PP)
HighValue = ta.valuewhen(not na(HighPivot), high[PP], 0)
LowValue = ta.valuewhen(not na(LowPivot), low[PP], 0)
HighIndex = ta.valuewhen(not na(HighPivot), bar_index[PP], 0)
LowIndex = ta.valuewhen(not na(LowPivot), bar_index[PP], 0)
Correct_HighPivot = 0.0
Correct_LowPivot = 0.0
PASS = 0
if not na(HighPivot) and not na(LowPivot)
if ArrayType.size() == 0
PASS := 1
else if ArrayType.size() >= 1
if ((ArrayType.get(ArrayType.size() - 1)) == "L" or (ArrayType.get(ArrayType.size() - 1)) == "LL")
if LowPivot < ArrayValue.get(ArrayType.size() - 1)
array.remove(ArrayType, ArrayType.size() - 1)
array.remove(ArrayValue, ArrayValue.size() - 1)
array.remove(ArrayIndex, ArrayIndex.size() - 1)
array.push(ArrayType, ArrayType.size() > 2 ? ArrayValue.get(ArrayType.size() - 2) < LowValue ? "HL" : "LL" : "L")
array.push(ArrayValue, LowValue)
array.push(ArrayIndex, LowIndex)
Correct_LowPivot := LowValue
else
array.push(ArrayType, ArrayType.size() > 2 ? ArrayValue.get(ArrayType.size() - 2) < HighValue ? "HH" : "LH" : "H")
array.push(ArrayValue, HighValue)
array.push(ArrayIndex, HighIndex)
Correct_HighPivot := HighValue
else if (ArrayType.get(ArrayType.size() - 1)) == "H" or (ArrayType.get(ArrayType.size() - 1)) == "HH"
if HighPivot > ArrayValue.get(ArrayType.size() - 1)
array.remove(ArrayType, ArrayType.size() - 1)
array.remove(ArrayValue, ArrayValue.size() - 1)
array.remove(ArrayIndex, ArrayIndex.size() - 1)
array.push(ArrayType, ArrayType.size() > 2 ? ArrayValue.get(ArrayType.size() - 2) < HighValue ? "HH" : "LH" : "H")
array.push(ArrayValue, HighValue)
array.push(ArrayIndex, HighIndex)
Correct_HighPivot := HighValue
else
array.push(ArrayType, ArrayType.size() > 2 ? ArrayValue.get(ArrayType.size() - 2) < LowValue ? "HL" : "LL" : "L")
array.push(ArrayValue, LowValue)
array.push(ArrayIndex, LowIndex)
Correct_LowPivot := LowValue
else if (ArrayType.get(ArrayType.size() - 1)) == "LH"
if HighPivot < ArrayValue.get(ArrayType.size() - 1)
array.push(ArrayType, ArrayType.size() > 2 ? ArrayValue.get(ArrayType.size() - 2) < LowValue ? "HL" : "LL" : "L")
array.push(ArrayValue, LowValue)
array.push(ArrayIndex, LowIndex)
Correct_LowPivot := LowValue
else if HighPivot > ArrayValue.get(ArrayType.size() - 1)
if close < ArrayValue.get(ArrayType.size() - 1)
array.remove(ArrayType, ArrayType.size() - 1)
array.remove(ArrayValue, ArrayValue.size() - 1)
array.remove(ArrayIndex, ArrayIndex.size() - 1)
array.push(ArrayType, ArrayType.size() > 2 ? ArrayValue.get(ArrayType.size() - 2) < HighValue ? "HH" : "LH" : "H")
array.push(ArrayValue, HighValue)
array.push(ArrayIndex, HighIndex)
Correct_HighPivot := HighValue
else if close > ArrayValue.get(ArrayType.size() - 1)
array.push(ArrayType, ArrayType.size() > 2 ? ArrayValue.get(ArrayType.size() - 2) < LowValue ? "HL" : "LL" : "L")
array.push(ArrayValue, LowValue)
array.push(ArrayIndex, LowIndex)
Correct_LowPivot := LowValue
else if (ArrayType.get(ArrayType.size() - 1)) == "HL"
if LowPivot > ArrayValue.get(ArrayType.size() - 1)
array.push(ArrayType, ArrayType.size() > 2 ? ArrayValue.get(ArrayType.size() - 2) < HighValue ? "HH" : "LH" : "H")
array.push(ArrayValue, HighValue)
array.push(ArrayIndex, HighIndex)
Correct_HighPivot := HighValue
else if LowPivot < ArrayValue.get(ArrayType.size() - 1)
if close > ArrayValue.get(ArrayType.size() - 1)
array.remove(ArrayType, ArrayType.size() - 1)
array.remove(ArrayValue, ArrayValue.size() - 1)
array.remove(ArrayIndex, ArrayIndex.size() - 1)
array.push(ArrayType, ArrayType.size() > 2 ? ArrayValue.get(ArrayType.size() - 2) < LowValue ? "HL" : "LL" : "L")
array.push(ArrayValue, LowValue)
array.push(ArrayIndex, LowIndex)
Correct_LowPivot := LowValue
else if close < ArrayValue.get(ArrayType.size() - 1)
array.push(ArrayType, ArrayType.size() > 2 ? ArrayValue.get(ArrayType.size() - 2) < HighValue ? "HH" : "LH" : "H")
array.push(ArrayValue, HighValue)
array.push(ArrayIndex, HighIndex)
Correct_HighPivot := HighValue
else if not na(HighPivot)
if ArrayType.size() == 0
array.insert(ArrayType, 0, "H")
array.insert(ArrayValue, 0, HighValue)
array.insert(ArrayIndex, 0, HighIndex)
Correct_HighPivot := HighValue
else if ArrayType.size() >= 1
if ((ArrayType.get(ArrayType.size() - 1)) == "L" or (ArrayType.get(ArrayType.size() - 1)) == "HL" or (ArrayType.get(ArrayType.size() - 1)) == "LL")
if HighPivot > ArrayValue.get(ArrayType.size() - 1)
array.push(ArrayType, ArrayType.size() > 2 ? ArrayValue.get(ArrayType.size() - 2) < HighValue ? "HH" : "LH" : "H")
array.push(ArrayValue, HighValue)
array.push(ArrayIndex, HighIndex)
Correct_HighPivot := HighValue
else if HighPivot < ArrayValue.get(ArrayType.size() - 1)
array.remove(ArrayType, ArrayType.size() - 1)
array.remove(ArrayValue, ArrayValue.size() - 1)
array.remove(ArrayIndex, ArrayIndex.size() - 1)
array.push(ArrayType, ArrayType.size() > 2 ? ArrayValue.get(ArrayType.size() - 2) < LowValue ? "HL" : "LL" : "L")
array.push(ArrayValue, LowValue)
array.push(ArrayIndex, LowIndex)
Correct_LowPivot := LowValue
else if (ArrayType.get(ArrayType.size() - 1)) == "H" or (ArrayType.get(ArrayType.size() - 1)) == "HH" or (ArrayType.get(ArrayType.size() - 1)) == "LH"
if (ArrayValue.get(ArrayValue.size() - 1)) < HighValue
array.remove(ArrayType, ArrayType.size() - 1)
array.remove(ArrayValue, ArrayValue.size() - 1)
array.remove(ArrayIndex, ArrayIndex.size() - 1)
array.push(ArrayType, ArrayType.size() > 2 ? ArrayValue.get(ArrayType.size() - 2) < HighValue ? "HH" : "LH" : "H")
array.push(ArrayValue, HighValue)
array.push(ArrayIndex, HighIndex)
Correct_HighPivot := HighValue
else if not na(LowPivot)
if ArrayType.size() == 0
array.insert(ArrayType, 0, "L")
array.insert(ArrayValue, 0, LowValue)
array.insert(ArrayIndex, 0, LowIndex)
Correct_LowPivot := LowValue
else if ArrayType.size() >= 1
if (ArrayType.get(ArrayType.size() - 1)) == "H" or (ArrayType.get(ArrayType.size() - 1)) == "HH" or (ArrayType.get(ArrayType.size() - 1)) == "LH"
if LowPivot < ArrayValue.get(ArrayType.size() - 1)
array.push(ArrayType, ArrayType.size() > 2 ? ArrayValue.get(ArrayType.size() - 2) < LowValue ? "HL" : "LL" : "L")
array.push(ArrayValue, LowValue)
array.push(ArrayIndex, LowIndex)
Correct_LowPivot := LowValue
else if LowPivot > ArrayValue.get(ArrayType.size() - 1)
array.remove(ArrayType, ArrayType.size() - 1)
array.remove(ArrayValue, ArrayValue.size() - 1)
array.remove(ArrayIndex, ArrayIndex.size() - 1)
array.push(ArrayType, ArrayType.size() > 2 ? ArrayValue.get(ArrayType.size() - 2) < HighValue ? "HH" : "LH" : "H")
array.push(ArrayValue, HighValue)
array.push(ArrayIndex, HighIndex)
Correct_HighPivot := HighValue
else if (ArrayType.get(ArrayType.size() - 1)) == "L" or (ArrayType.get(ArrayType.size() - 1)) == "HL" or (ArrayType.get(ArrayType.size() - 1)) == "LL"
if (ArrayValue.get(ArrayValue.size() - 1)) > LowValue
array.remove(ArrayType, ArrayType.size() - 1)
array.remove(ArrayValue, ArrayValue.size() - 1)
array.remove(ArrayIndex, ArrayIndex.size() - 1)
array.push(ArrayType, ArrayType.size() > 2 ? ArrayValue.get(ArrayType.size() - 2) < LowValue ? "HL" : "LL" : "L")
array.push(ArrayValue, LowValue)
array.push(ArrayIndex, LowIndex)
Correct_LowPivot := LowValue
// ตรวจจับรูปแบบ 5 สวิง และ 7 สวิง (Setup A & B)
int count = array.size(ArrayType)
bool enough5 = count >= 5
bool enoughOriginal = count > 8
string t0 = enough5 ? array.get(ArrayType, count - 1) : ""
string t1 = enough5 ? array.get(ArrayType, count - 2) : ""
string t2 = enough5 ? array.get(ArrayType, count - 3) : ""
string t3 = enough5 ? array.get(ArrayType, count - 4) : ""
string t4 = enough5 ? array.get(ArrayType, count - 5) : ""
string t5 = enoughOriginal ? array.get(ArrayType, count - 6) : ""
string t6 = enoughOriginal ? array.get(ArrayType, count - 7) : ""
float p0 = enough5 ? array.get(ArrayValue, count - 1) : na
float p1 = enough5 ? array.get(ArrayValue, count - 2) : na
float p3 = enough5 ? array.get(ArrayValue, count - 4) : na
float p4 = enough5 ? array.get(ArrayValue, count - 5) : na
int x0 = enough5 ? array.get(ArrayIndex, count - 1) : na
int x1 = enough5 ? array.get(ArrayIndex, count - 2) : na
int x3 = enough5 ? array.get(ArrayIndex, count - 4) : na
int x5 = enoughOriginal ? array.get(ArrayIndex, count - 6) : na
bool confirmedPivot = barstate.isconfirmed and enough5 and x0 == bar_index - PP
bool bullOriginal = confirmedPivot and enoughOriginal and t0 == "HH" and t1 == "HL" and t2 == "LH" and t3 == "HL" and t4 == "HH" and t5 == "HL" and t6 == "HH" and p0 < p4
bool bearOriginal = confirmedPivot and enoughOriginal and t0 == "LL" and t1 == "LH" and t2 == "HL" and t3 == "LH" and t4 == "LL" and t5 == "LH" and t6 == "LL" and p0 > p4
bool bullShort = confirmedPivot and t0 == "HH" and t1 == "HL" and t2 == "LH" and t3 == "HL" and t4 == "HH" and p0 < p4
bool bearShort = confirmedPivot and t0 == "LL" and t1 == "LH" and t2 == "HL" and t3 == "LH" and t4 == "LL" and p0 > p4
bool bullOriginalEligible = bullOriginal and not na(x5) and bar_index - x5 <= OBVaP
bool bearOriginalEligible = bearOriginal and not na(x5) and bar_index - x5 <= OBVaP
bool bullShortEligible = bullShort and not na(x3) and bar_index - x3 <= OBVaP
bool bearShortEligible = bearShort and not na(x3) and bar_index - x3 <= OBVaP
bool bullRaw = bullOriginalEligible or bullShortEligible
bool bearRaw = bearOriginalEligible or bearShortEligible
var int buOBI = 0
var int beOBI = 0
if bullRaw
buOBI := bullOriginalEligible ? x5 : x3
if bearRaw
beOBI := bearOriginalEligible ? x5 : x3
// ฟังก์ชันตรวจสอบว่าระดับราคายังไม่ถูกชน
levelUntouched(int dir, float price, int pivotBar) =>
bool valid = not na(price) and not na(pivotBar) and pivotBar < bar_index and bar_index - pivotBar <= 4998
if valid
int elapsed = bar_index - pivotBar
for offset = 0 to elapsed - 1
if dir == 1 ? low[offset] <= price : high[offset] >= price
valid := false
break
valid
// Setup C (Midway Liquidity Setup)
cHighPivot = ta.pivothigh(cPP, cPP)
cLowPivot = ta.pivotlow(cPP, cPP)
float cLowReaction = ta.highest(high, cPP) - low[cPP]
float cHighReaction = high[cPP] - ta.lowest(low, cPP)
var float cLastHigh = na
var float cLastLow = na
var int cLastHighBar = na
var int cLastLowBar = na
bool cBullBreak = false
bool cBearBreak = false
bool cBullBosStrong = false
bool cBearBosStrong = false
int cBullAnchor = na
int cBearAnchor = na
float cBuffer = math.max(syminfo.mintick, nz(atr) * cBosBreakATR)
if enableC and barstate.isconfirmed and not na(atr)
if not na(cLastHigh) and not na(cLastLow) and cLastLowBar > cLastHighBar and bar_index > cLastLowBar
cBullBreak := close > open and close - open >= atr * cBosBodyATR and close > cLastHigh + cBuffer and close[1] <= cLastHigh + cBuffer
if cBullBreak
cBullAnchor := cLastLowBar
cBullBosStrong := close - cLastHigh >= atr * math.max(0.30, cBosBreakATR * 1.5) and close - open >= atr * math.max(0.65, cBosBodyATR * 1.3)
if not na(cLastHigh) and not na(cLastLow) and cLastHighBar > cLastLowBar and bar_index > cLastHighBar
cBearBreak := close < open and open - close >= atr * cBosBodyATR and close < cLastLow - cBuffer and close[1] >= cLastLow - cBuffer
if cBearBreak
cBearAnchor := cLastHighBar
cBearBosStrong := cLastLow - close >= atr * math.max(0.30, cBosBreakATR * 1.5) and open - close >= atr * math.max(0.65, cBosBodyATR * 1.3)
if barstate.isconfirmed
if not na(cHighPivot)
cLastHigh := high[cPP]
cLastHighBar := bar_index - cPP
if not na(cLowPivot)
cLastLow := low[cPP]
cLastLowBar := bar_index - cPP
// ปรับแต่งกรอบ Order Block ด้วย Library Refiner
[Bu_Xd1, Bu_Xd2, Bu_Yd12, Bu_Xp1, Bu_Xp2, Bu_Yp12] = Refiner.OBRefiner("Demand", Refine ? "On" : "Off", RefineMe, bullRaw, buOBI)
[Be_Xd1, Be_Xd2, Be_Yd12, Be_Xp1, Be_Xp2, Be_Yp12] = Refiner.OBRefiner("Supply", Refine ? "On" : "Off", RefineMe, bearRaw, beOBI)
[CBu_Xd1, CBu_Xd2, CBu_Yd12, CBu_Xp1, CBu_Xp2, CBu_Yp12] = Refiner.OBRefiner("Demand", Refine ? "On" : "Off", RefineMe, cBullBreak, nz(cBullAnchor, 0))
[CBe_Xd1, CBe_Xd2, CBe_Yd12, CBe_Xp1, CBe_Xp2, CBe_Yp12] = Refiner.OBRefiner("Supply", Refine ? "On" : "Off", RefineMe, cBearBreak, nz(cBearAnchor, 0))
// ============================================================================
// 4. การวาดโซน OB, เส้น Liquidity และจัดการ Lifecycle
// ============================================================================
var array<OrderBlock> blocks = array.new<OrderBlock>()
var array<MidwayCandidate> midwayCandidates = array.new<MidwayCandidate>()
lineStyle = line.style_dashed
levelTagColor(int dir) => dir == 1 ? #16853D : #B83143
newLiquidity(int dir, int originBar, float price) =>
line ln = na
label tag = na
if showLiquidity
color c = dir == 1 ? bullLiquidityColor : bearLiquidityColor
ln := line.new(originBar, price, bar_index, price, xloc = xloc.bar_index, color = c, style = lineStyle, width = 1)
if showLiquidityText
int mid = int(math.round((originBar + bar_index) / 2.0))
tag := label.new(mid, price, "Liquidity", xloc = xloc.bar_index, color = levelTagColor(dir), textcolor = color.white, style = label.style_label_center, size = size.small)
LiquidityLevel.new(originBar, price, true, ln, tag)
newBlock(int dir, int originBar, float top, float bottom, int levelsCount, int firstX, float firstP, int secondX, float secondP) =>
line topLine = na
line bottomLine = na
linefill zoneFill = na
label obTag = na
color c = dir == 1 ? DemandColor : SupplyColor
if showOB
topLine := line.new(originBar, top, bar_index, top, xloc = xloc.bar_index, style = line.style_dashed, color = c, width = 1)
bottomLine := line.new(originBar, bottom, bar_index, bottom, xloc = xloc.bar_index, style = line.style_dashed, color = c, width = 1)
zoneFill := linefill.new(topLine, bottomLine, color.new(c, 85))
int mid = int(math.round((originBar + bar_index) / 2.0))
obTag := label.new(mid, math.avg(top, bottom), dir == 1 ? "Demand OB" : "Supply OB", xloc = xloc.bar_index, color = color.new(levelTagColor(dir), 100), textcolor = color.white, style = label.style_label_center, size = size.small)
array<LiquidityLevel> levels = array.new<LiquidityLevel>()
if levelsCount >= 1
array.push(levels, newLiquidity(dir, firstX, firstP))
if levelsCount == 2
array.push(levels, newLiquidity(dir, secondX, secondP))
OrderBlock.new(dir, originBar, bar_index, top, bottom, true, topLine, bottomLine, zoneFill, obTag, false, levels, 0, na)
setBlockRight(OrderBlock ob, int rightBar) =>
if not na(ob.topLine)
line.set_x2(ob.topLine, rightBar)
if not na(ob.bottomLine)
line.set_x2(ob.bottomLine, rightBar)
if not na(ob.obTag)
label.set_x(ob.obTag, int(math.round((ob.originBar + rightBar) / 2.0)))
setLiquidityRight(LiquidityLevel lvl, int rightBar) =>
if not na(lvl.ln)
line.set_x2(lvl.ln, rightBar)
if not na(lvl.tag)
label.set_x(lvl.tag, int(math.round((lvl.originBar + rightBar) / 2.0)))
finishBlock(OrderBlock ob) =>
ob.active := false
setBlockRight(ob, bar_index)
for lvl in ob.levels
if lvl.extending
setLiquidityRight(lvl, bar_index)
lvl.extending := false
deleteBlock(OrderBlock ob) =>
if not na(ob.zoneFill)
linefill.delete(ob.zoneFill)
if not na(ob.topLine)
line.delete(ob.topLine)
if not na(ob.bottomLine)
line.delete(ob.bottomLine)
if not na(ob.obTag)
label.delete(ob.obTag)
for lvl in ob.levels
if not na(lvl.ln)
line.delete(lvl.ln)
if not na(lvl.tag)
label.delete(lvl.tag)
trimBlocks() =>
while array.size(blocks) > 100
OrderBlock old = array.shift(blocks)
deleteBlock(old)
// สร้าง Setup A และ Setup B
float minGap = math.max(2.0 * syminfo.mintick, nz(atr) * 0.2)
bool bullTwo = false
bool bearTwo = false
bool bullOne = false
bool bearOne = false
if bullRaw and not na(Bu_Xd1) and not na(Bu_Yd12) and not na(Bu_Yp12)
float top = math.max(Bu_Yd12, Bu_Yp12)
float bottom = math.min(Bu_Yd12, Bu_Yp12)
int origin = Bu_Xd1
if top > bottom and origin <= bar_index and bar_index - origin <= OBVaP
bool a = bullOriginalEligible and levelUntouched(1, p3, x3) and p3 > top
bool b = levelUntouched(1, p1, x1) and p1 > top
bool two = a and b and math.abs(p3 - p1) >= minGap
int n = two ? 2 : (a or b ? 1 : 0)
bool show = (two and enableA) or (n == 1 and enableB)
if show
int start1 = two ? x3 : b ? x1 : x3
float price1 = two ? p3 : b ? p1 : p3
int start2 = two ? x1 : na
float price2 = two ? p1 : na
array.push(blocks, newBlock(1, origin, top, bottom, n, start1, price1, start2, price2))
trimBlocks()
bullTwo := two
bullOne := not two
if bearRaw and not na(Be_Xd1) and not na(Be_Yd12) and not na(Be_Yp12)
float top = math.max(Be_Yd12, Be_Yp12)
float bottom = math.min(Be_Yd12, Be_Yp12)
int origin = Be_Xd1
if top > bottom and origin <= bar_index and bar_index - origin <= OBVaP
bool a = bearOriginalEligible and levelUntouched(-1, p3, x3) and p3 < bottom
bool b = levelUntouched(-1, p1, x1) and p1 < bottom
bool two = a and b and math.abs(p3 - p1) >= minGap
int n = two ? 2 : (a or b ? 1 : 0)
bool show = (two and enableA) or (n == 1 and enableB)
if show
int start1 = two ? x3 : b ? x1 : x3
float price1 = two ? p3 : b ? p1 : p3
int start2 = two ? x1 : na
float price2 = two ? p1 : na
array.push(blocks, newBlock(-1, origin, top, bottom, n, start1, price1, start2, price2))
trimBlocks()
bearTwo := two
bearOne := not two
// สร้างและยืนยัน Setup C (Midway Liquidity Setup)
bool bullC = false
bool bearC = false
if enableC and barstate.isconfirmed
if cBullBreak and not na(CBu_Xd1) and not na(CBu_Yd12) and not na(CBu_Yp12)
float hi = math.max(CBu_Yd12, CBu_Yp12)
float lo = math.min(CBu_Yd12, CBu_Yp12)
if hi > lo and CBu_Xd1 <= bar_index and bar_index - CBu_Xd1 < OBVaP
bool duplicate = false
for c in midwayCandidates
if c.direction == 1 and c.originBar == CBu_Xd1
duplicate := true
for ob in blocks
if ob.setupVersion == 3 and ob.direction == 1 and ob.originBar == CBu_Xd1
duplicate := true
if not duplicate
bool goodOB = (hi - lo <= atr * 1.5) and close - hi >= atr * 0.9
array.push(midwayCandidates, MidwayCandidate.new(1, CBu_Xd1, bar_index, hi, lo, cBullBosStrong, goodOB, true, 0))
if cBearBreak and not na(CBe_Xd1) and not na(CBe_Yd12) and not na(CBe_Yp12)
float hi = math.max(CBe_Yd12, CBe_Yp12)
float lo = math.min(CBe_Yd12, CBe_Yp12)
if hi > lo and CBe_Xd1 <= bar_index and bar_index - CBe_Xd1 < OBVaP
bool duplicate = false
for c in midwayCandidates
if c.direction == -1 and c.originBar == CBe_Xd1
duplicate := true
for ob in blocks
if ob.setupVersion == 3 and ob.direction == -1 and ob.originBar == CBe_Xd1
duplicate := true
if not duplicate
bool goodOB = (hi - lo <= atr * 1.5) and lo - close >= atr * 0.9
array.push(midwayCandidates, MidwayCandidate.new(-1, CBe_Xd1, bar_index, hi, lo, cBearBosStrong, goodOB, true, 0))
if enableC and array.size(midwayCandidates) > 0
for i = array.size(midwayCandidates) - 1 to 0
MidwayCandidate c = array.get(midwayCandidates, i)
bool expired = bar_index - c.originBar >= OBVaP or bar_index - c.breakBar > cFormationBars
bool tested = bar_index > c.breakBar and (c.direction == 1 ? low <= c.top : high >= c.bottom)
if expired or tested
array.remove(midwayCandidates, i)
else if barstate.isconfirmed
bool oppositeImpulse = c.direction == 1 ? close < open and open - close >= atr * 0.8 : close > open and close - open >= atr * 0.8
c.oppositeStreak := oppositeImpulse ? c.oppositeStreak + 1 : 0
if c.oppositeStreak >= 2
c.cleanApproach := false
bool hasPivot = c.direction == 1 ? not na(cLowPivot) : not na(cHighPivot)
int px = bar_index - cPP
float py = c.direction == 1 ? low[cPP] : high[cPP]
float reaction = c.direction == 1 ? cLowReaction : cHighReaction
bool separated = c.direction == 1 ? py > c.top : py < c.bottom
bool fresh = hasPivot and px > c.breakBar and separated and levelUntouched(c.direction, py, px)
if fresh and not na(reaction) and reaction >= atr * cSwingReactionATR
int score = (c.strongBos ? 1 : 0) + (reaction >= atr * math.max(0.55, cSwingReactionATR * 1.5) ? 1 : 0) + (c.goodOB ? 1 : 0) + (c.cleanApproach ? 1 : 0)
if score >= cMinScore
bool alreadyDisplayed = false
for ob in blocks
if ob.direction == c.direction and ob.originBar == c.originBar and ob.active
alreadyDisplayed := true
if not alreadyDisplayed
OrderBlock newOB = newBlock(c.direction, c.originBar, c.top, c.bottom, 1, px, py, na, na)
newOB.setupVersion := 3
newOB.breakBar := c.breakBar
array.push(blocks, newOB)
trimBlocks()
if c.direction == 1
bullC := true
else
bearC := true
array.remove(midwayCandidates, i)
// เพิ่มระดับ Liquidity เพิ่มเติม (สูงสุด 3 ระดับ) สำหรับ Setup C ที่ยัง Active
if enableC and barstate.isconfirmed and array.size(blocks) > 0
for i = 0 to array.size(blocks) - 1
OrderBlock ob = array.get(blocks, i)
if ob.setupVersion == 3 and ob.active and not ob.entryMarked and array.size(ob.levels) < 3
bool hasPivot = ob.direction == 1 ? not na(cLowPivot) : not na(cHighPivot)
float py = ob.direction == 1 ? low[cPP] : high[cPP]
float reaction = ob.direction == 1 ? cLowReaction : cHighReaction
int px = bar_index - cPP
bool separated = ob.direction == 1 ? py > ob.top : py < ob.bottom
if hasPivot and px > ob.breakBar and separated and not na(reaction) and reaction >= atr * cSwingReactionATR and levelUntouched(ob.direction, py, px)
bool uniqueLevel = true
for lvl in ob.levels
if math.abs(lvl.price - py) < minGap
uniqueLevel := false
if uniqueLevel
array.push(ob.levels, newLiquidity(ob.direction, px, py))
while array.size(midwayCandidates) > 24
array.shift(midwayCandidates)
// ============================================================================
// 5. การตรวจสอบสัญญาณเข้าเทรด (First Entry) และ Mitigation
// ============================================================================
bool bullishOBEntry = false
bool bearishOBEntry = false
if array.size(blocks) > 0
for i = 0 to array.size(blocks) - 1
OrderBlock ob = array.get(blocks, i)
if ob.active
bool expired = bar_index - ob.originBar >= OBVaP
bool firstEntry = not expired and not ob.entryMarked and bar_index > ob.createdBar and low <= ob.top and high >= ob.bottom
if firstEntry
ob.entryMarked := true
if ob.direction == 1
bullishOBEntry := true
else
bearishOBEntry := true
bool invalid = ob.direction == 1 ? close < ob.bottom : close > ob.top
float mitigation = MLOB == "50 % OB" ? math.avg(ob.top, ob.bottom) : MLOB == "Proximal" ? (ob.direction == 1 ? ob.top : ob.bottom) : (ob.direction == 1 ? ob.bottom : ob.top)
bool obMitigated = bar_index > ob.createdBar and (ob.direction == 1 ? low <= mitigation : high >= mitigation)
if expired or invalid or obMitigated
finishBlock(ob)
else
setBlockRight(ob, bar_index)
for lvl in ob.levels
if lvl.extending
setLiquidityRight(lvl, bar_index)
bool consumed = ob.direction == 1 ? low <= lvl.price : high >= lvl.price
if consumed
lvl.extending := false
// ============================================================================
// 6. การแสดงผลสัญญาณ Buy / Sell และการตั้งค่า Alerts
// ============================================================================
float signalSpace = math.max(nz(atr, math.max(high - low, syminfo.mintick)) * 0.45, syminfo.mintick * 12.0)
float bullTriangleY = low - signalSpace * 2.4
float bullLabelY = low - signalSpace * 3.9
float bearTriangleY = high + signalSpace * 0.9
float bearLabelY = high + signalSpace * 2.4
plotshape(showSignals and bullishOBEntry ? bullTriangleY : na, title = "Demand OB First Entry", style = shape.triangleup, location = location.absolute, color = #16853D, size = size.normal)
plotshape(showSignals and bearishOBEntry ? bearTriangleY : na, title = "Supply OB First Entry", style = shape.triangledown, location = location.absolute, color = #B83143, size = size.normal)
if showSignals and bullishOBEntry
label.new(bar_index, bullLabelY, "Long Signal", xloc = xloc.bar_index, style = label.style_label_center, color = #46515D, textcolor = #DDE2E8, size = size.small)
if showSignals and bearishOBEntry
label.new(bar_index, bearLabelY, "Short Signal", xloc = xloc.bar_index, style = label.style_label_center, color = #46515D, textcolor = #DDE2E8, size = size.small)
alertcondition(bullTwo, "Bullish Two-Level Setup", "Bullish two-level liquidity setup confirmed")
alertcondition(bearTwo, "Bearish Two-Level Setup", "Bearish two-level liquidity setup confirmed")
alertcondition(bullOne, "Bullish One-Level Setup", "Bullish one-level liquidity setup confirmed")
alertcondition(bearOne, "Bearish One-Level Setup", "Bearish one-level liquidity setup confirmed")
alertcondition(bullC, "Bullish Midway Setup C", "Bullish midway liquidity setup C confirmed")
alertcondition(bearC, "Bearish Midway Setup C", "Bearish midway liquidity setup C confirmed")