OPEN-SOURCE SCRIPT
Telah dikemas kini Ali 3-Bar MC v5 (Structure Exit)

Ali 3 bar MC implemented by Joo
//version=5
strategy("Ali 3-Bar MC v5 (Structure Exit)", overlay=true, default_qty_type=strategy.fixed, default_qty_value=1)
// === INPUTS ===
showLabels = input.bool(true, title="Show Entry Labels")
rewardMultiple = input.float(1.0, title="Reward : Risk")
minStrongCloseRatio = input.float(0.75, title="Strong Close Threshold")
atrLength = input.int(4, title="ATR Length")
atrMult = 2.0
// === ATR ===
atr = ta.sma(ta.tr(true), atrLength)
tick = syminfo.mintick
// === Ali BULL MC ===
bullBar1 = close[3] > open[3]
bullBar2 = close[2] > open[2]
bullBar3 = close[1] > open[1]
bullStrong1 = (close[3] - low[3]) / (high[3] - low[3] + 0.01) > minStrongCloseRatio
bullStrong2 = (close[2] - low[2]) / (high[2] - low[2] + 0.01) > minStrongCloseRatio
bullStrong3 = (close[1] - low[1]) / (high[1] - low[1] + 0.01) > minStrongCloseRatio
bullHasStrong = bullStrong1 or bullStrong2 or bullStrong3
bullMicroGap = low[1] > high[3]
bullTrendLow = low[2] > low[3] and low[1] > low[2] and low > low[1]
isAliBull = bullBar1 and bullBar2 and bullBar3 and bullHasStrong and bullMicroGap and bullTrendLow
// === Ali BEAR MC ===
bearBar1 = close[3] < open[3]
bearBar2 = close[2] < open[2]
bearBar3 = close[1] < open[1]
bearStrong1 = (close[3] - low[3]) / (high[3] - low[3] + 0.01) < 1 - minStrongCloseRatio
bearStrong2 = (close[2] - low[2]) / (high[2] - low[2] + 0.01) < 1 - minStrongCloseRatio
bearStrong3 = (close[1] - low[1]) / (high[1] - low[1] + 0.01) < 1 - minStrongCloseRatio
bearHasStrong = bearStrong1 or bearStrong2 or bearStrong3
bearMicroGap = high[1] < low[3]
bearTrendHigh = high[2] < high[3] and high[1] < high[2] and high < high[1]
isAliBear = bearBar1 and bearBar2 and bearBar3 and bearHasStrong and bearMicroGap and bearTrendHigh
// === ENTRY/RISK/TARGET ===
bullEntry = high[1] + tick
bullRisk = atr * atrMult
bullStop = bullEntry - bullRisk
bullTarget = bullEntry + bullRisk * rewardMultiple
bearEntry = low[1] - tick
bearRisk = atr * atrMult
bearStop = bearEntry + bearRisk
bearTarget = bearEntry - bearRisk * rewardMultiple
// === STATE ===
var float bullGapCloseLine = na
var float bearGapCloseLine = na
var bool inLong = false
var bool inShort = false
var bool bullStructureExitArmed = false
var bool bearStructureExitArmed = false
var float lastBullOpen = na
var float lastBearOpen = na
// === BULL ENTRY ===
endOfDayEntryCutoff = time >= timestamp("America/New_York", year, month, dayofmonth, 15, 55)
if isAliBull and not endOfDayEntryCutoff and strategy.position_size == 0
strategy.entry("Ali Long", strategy.long, stop=bullEntry)
strategy.exit("Long SL", from_entry="Ali Long", stop=bullStop)
bullGapCloseLine := low[1]
lastBullOpen := open[0]
inLong := true
bullStructureExitArmed := false
// === BEAR ENTRY ===
if isAliBear and not endOfDayEntryCutoff and strategy.position_size == 0
strategy.entry("Ali Short", strategy.short, stop=bearEntry)
strategy.exit("Short SL", from_entry="Ali Short", stop=bearStop)
bearGapCloseLine := high[1]
lastBearOpen := open[0]
inShort := true
bearStructureExitArmed := false
// === GAP CLOSE ===
// === Exit label handled per-exit; no shared label variable
if inLong and low <= bullGapCloseLine
strategy.close("Ali Long", comment="Gap Closed")
label.new(bar_index, low, text="Exit: Gap Closed", style=label.style_label_down, color=color.red, textcolor=color.white)
inLong := false
if inShort and high >= bearGapCloseLine
strategy.close("Ali Short", comment="Gap Closed")
label.new(bar_index, high, text="Exit: Gap Closed", style=label.style_label_up, color=color.orange, textcolor=color.white)
inShort := false
// === STRUCTURE-BASED TRAILING ===
isBearBar = close < open
engulfBull = isBearBar and close < lastBullOpen
isBullBar = close > open
engulfBear = isBullBar and close > lastBearOpen
if inLong
if not bullStructureExitArmed and high >= bullTarget
strategy.exit("Lock Long", from_entry="Ali Long", stop=bullTarget)
bullStructureExitArmed := true
if bullStructureExitArmed and engulfBull
strategy.close("Ali Long", comment="Bear bar engulf exit")
label.new(bar_index, close, text="Exit: Engulf Bar", style=label.style_label_down, color=color.green, textcolor=color.white)
inLong := false
bullStructureExitArmed := false
if inShort
if not bearStructureExitArmed and low <= bearTarget
strategy.exit("Lock Short", from_entry="Ali Short", stop=bearTarget)
bearStructureExitArmed := true
if bearStructureExitArmed and close > open and close > lastBearOpen
strategy.close("Ali Short", comment="Bull bar engulf exit")
label.new(bar_index, close, text="Exit: Engulf Bar", style=label.style_label_up, color=color.lime, textcolor=color.white)
inShort := false
bearStructureExitArmed := false
// === END OF DAY EXIT ===
endOfDay = time >= timestamp("America/New_York", year, month, dayofmonth, 15, 30) // 可视为收盘前5分钟(适用于美股时间)
if inLong and endOfDay
strategy.close("Ali Long", comment="EOD Exit")
label.new(bar_index, close, text="Exit: EOD", style=label.style_label_down, color=color.gray, textcolor=color.white, size=size.small)
inLong := false
if inShort and endOfDay
strategy.close("Ali Short", comment="EOD Exit")
label.new(bar_index, close, text="Exit: EOD", style=label.style_label_up, color=color.gray, textcolor=color.white, size=size.small)
inShort := false
// === RESET ===
if strategy.position_size == 0
inLong := false
inShort := false
bullStructureExitArmed := false
bearStructureExitArmed := false
// === PLOTS ===
plotshape(isAliBull and showLabels, location=location.belowbar, style=shape.labelup, color=color.green, text="Bull 3MC")
plotshape(isAliBear and showLabels, location=location.abovebar, style=shape.labeldown, color=color.red, text="Bear 3MC")
//version=5
strategy("Ali 3-Bar MC v5 (Structure Exit)", overlay=true, default_qty_type=strategy.fixed, default_qty_value=1)
// === INPUTS ===
showLabels = input.bool(true, title="Show Entry Labels")
rewardMultiple = input.float(1.0, title="Reward : Risk")
minStrongCloseRatio = input.float(0.75, title="Strong Close Threshold")
atrLength = input.int(4, title="ATR Length")
atrMult = 2.0
// === ATR ===
atr = ta.sma(ta.tr(true), atrLength)
tick = syminfo.mintick
// === Ali BULL MC ===
bullBar1 = close[3] > open[3]
bullBar2 = close[2] > open[2]
bullBar3 = close[1] > open[1]
bullStrong1 = (close[3] - low[3]) / (high[3] - low[3] + 0.01) > minStrongCloseRatio
bullStrong2 = (close[2] - low[2]) / (high[2] - low[2] + 0.01) > minStrongCloseRatio
bullStrong3 = (close[1] - low[1]) / (high[1] - low[1] + 0.01) > minStrongCloseRatio
bullHasStrong = bullStrong1 or bullStrong2 or bullStrong3
bullMicroGap = low[1] > high[3]
bullTrendLow = low[2] > low[3] and low[1] > low[2] and low > low[1]
isAliBull = bullBar1 and bullBar2 and bullBar3 and bullHasStrong and bullMicroGap and bullTrendLow
// === Ali BEAR MC ===
bearBar1 = close[3] < open[3]
bearBar2 = close[2] < open[2]
bearBar3 = close[1] < open[1]
bearStrong1 = (close[3] - low[3]) / (high[3] - low[3] + 0.01) < 1 - minStrongCloseRatio
bearStrong2 = (close[2] - low[2]) / (high[2] - low[2] + 0.01) < 1 - minStrongCloseRatio
bearStrong3 = (close[1] - low[1]) / (high[1] - low[1] + 0.01) < 1 - minStrongCloseRatio
bearHasStrong = bearStrong1 or bearStrong2 or bearStrong3
bearMicroGap = high[1] < low[3]
bearTrendHigh = high[2] < high[3] and high[1] < high[2] and high < high[1]
isAliBear = bearBar1 and bearBar2 and bearBar3 and bearHasStrong and bearMicroGap and bearTrendHigh
// === ENTRY/RISK/TARGET ===
bullEntry = high[1] + tick
bullRisk = atr * atrMult
bullStop = bullEntry - bullRisk
bullTarget = bullEntry + bullRisk * rewardMultiple
bearEntry = low[1] - tick
bearRisk = atr * atrMult
bearStop = bearEntry + bearRisk
bearTarget = bearEntry - bearRisk * rewardMultiple
// === STATE ===
var float bullGapCloseLine = na
var float bearGapCloseLine = na
var bool inLong = false
var bool inShort = false
var bool bullStructureExitArmed = false
var bool bearStructureExitArmed = false
var float lastBullOpen = na
var float lastBearOpen = na
// === BULL ENTRY ===
endOfDayEntryCutoff = time >= timestamp("America/New_York", year, month, dayofmonth, 15, 55)
if isAliBull and not endOfDayEntryCutoff and strategy.position_size == 0
strategy.entry("Ali Long", strategy.long, stop=bullEntry)
strategy.exit("Long SL", from_entry="Ali Long", stop=bullStop)
bullGapCloseLine := low[1]
lastBullOpen := open[0]
inLong := true
bullStructureExitArmed := false
// === BEAR ENTRY ===
if isAliBear and not endOfDayEntryCutoff and strategy.position_size == 0
strategy.entry("Ali Short", strategy.short, stop=bearEntry)
strategy.exit("Short SL", from_entry="Ali Short", stop=bearStop)
bearGapCloseLine := high[1]
lastBearOpen := open[0]
inShort := true
bearStructureExitArmed := false
// === GAP CLOSE ===
// === Exit label handled per-exit; no shared label variable
if inLong and low <= bullGapCloseLine
strategy.close("Ali Long", comment="Gap Closed")
label.new(bar_index, low, text="Exit: Gap Closed", style=label.style_label_down, color=color.red, textcolor=color.white)
inLong := false
if inShort and high >= bearGapCloseLine
strategy.close("Ali Short", comment="Gap Closed")
label.new(bar_index, high, text="Exit: Gap Closed", style=label.style_label_up, color=color.orange, textcolor=color.white)
inShort := false
// === STRUCTURE-BASED TRAILING ===
isBearBar = close < open
engulfBull = isBearBar and close < lastBullOpen
isBullBar = close > open
engulfBear = isBullBar and close > lastBearOpen
if inLong
if not bullStructureExitArmed and high >= bullTarget
strategy.exit("Lock Long", from_entry="Ali Long", stop=bullTarget)
bullStructureExitArmed := true
if bullStructureExitArmed and engulfBull
strategy.close("Ali Long", comment="Bear bar engulf exit")
label.new(bar_index, close, text="Exit: Engulf Bar", style=label.style_label_down, color=color.green, textcolor=color.white)
inLong := false
bullStructureExitArmed := false
if inShort
if not bearStructureExitArmed and low <= bearTarget
strategy.exit("Lock Short", from_entry="Ali Short", stop=bearTarget)
bearStructureExitArmed := true
if bearStructureExitArmed and close > open and close > lastBearOpen
strategy.close("Ali Short", comment="Bull bar engulf exit")
label.new(bar_index, close, text="Exit: Engulf Bar", style=label.style_label_up, color=color.lime, textcolor=color.white)
inShort := false
bearStructureExitArmed := false
// === END OF DAY EXIT ===
endOfDay = time >= timestamp("America/New_York", year, month, dayofmonth, 15, 30) // 可视为收盘前5分钟(适用于美股时间)
if inLong and endOfDay
strategy.close("Ali Long", comment="EOD Exit")
label.new(bar_index, close, text="Exit: EOD", style=label.style_label_down, color=color.gray, textcolor=color.white, size=size.small)
inLong := false
if inShort and endOfDay
strategy.close("Ali Short", comment="EOD Exit")
label.new(bar_index, close, text="Exit: EOD", style=label.style_label_up, color=color.gray, textcolor=color.white, size=size.small)
inShort := false
// === RESET ===
if strategy.position_size == 0
inLong := false
inShort := false
bullStructureExitArmed := false
bearStructureExitArmed := false
// === PLOTS ===
plotshape(isAliBull and showLabels, location=location.belowbar, style=shape.labelup, color=color.green, text="Bull 3MC")
plotshape(isAliBear and showLabels, location=location.abovebar, style=shape.labeldown, color=color.red, text="Bear 3MC")
Nota Keluaran
Ali 3bar micro channel startegyNota Keluaran
update simple exitNota Keluaran
implement Ali 10 factorNota Keluaran
10 factor with trailing stopNota Keluaran
Ali 3-Bar MC simple 2RSkrip sumber terbuka
Dalam semangat sebenar TradingView, pencipta skrip ini telah menjadikannya sumber terbuka supaya pedagang dapat menilai dan mengesahkan kefungsiannya. Terima kasih kepada penulis! Walaupun anda boleh menggunakannya secara percuma, ingat bahawa menerbitkan semula kod ini adalah tertakluk kepada Peraturan Dalaman kami.
Penafian
Maklumat dan penerbitan adalah tidak dimaksudkan untuk menjadi, dan tidak membentuk, nasihat untuk kewangan, pelaburan, perdagangan dan jenis-jenis lain atau cadangan yang dibekalkan atau disahkan oleh TradingView. Baca dengan lebih lanjut di Terma Penggunaan.
Skrip sumber terbuka
Dalam semangat sebenar TradingView, pencipta skrip ini telah menjadikannya sumber terbuka supaya pedagang dapat menilai dan mengesahkan kefungsiannya. Terima kasih kepada penulis! Walaupun anda boleh menggunakannya secara percuma, ingat bahawa menerbitkan semula kod ini adalah tertakluk kepada Peraturan Dalaman kami.
Penafian
Maklumat dan penerbitan adalah tidak dimaksudkan untuk menjadi, dan tidak membentuk, nasihat untuk kewangan, pelaburan, perdagangan dan jenis-jenis lain atau cadangan yang dibekalkan atau disahkan oleh TradingView. Baca dengan lebih lanjut di Terma Penggunaan.