本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=5
indicator("LG • HA Entry + Donchian + UT Bot (VISUAL) + PineConnector (NO RangeBox)", overlay=true, max_labels_count=500, max_lines_count=500)
//=============================================================================
// PINECONNECTOR + TP/SL INPUTS
//=============================================================================
pipSize = input.float(0.1, "Pip Size (Gold)", step=0.01, group="PineConnector")
sl_pips = input.int(200, "Stop Loss (pips)", minval=1, group="PineConnector")
tp1_pips = input.int(240, "TP1 (pips)", minval=1, group="PineConnector")
tp2_pips = input.int(340, "TP2 (pips)", minval=1, group="PineConnector")
tp3_pips = input.int(500, "TP3 (pips)", minval=1, group="PineConnector")
closePct = input.int(50, "Partial close % (TP1/TP2)", minval=1, maxval=100, group="PineConnector")
tpCloseMode = input.string("TP1 50% then TP2 50% then TP3 CLOSE REST", "TP Close Mode",
["TP1 50% then TP2 CLOSE REST", "TP1 50% then TP2 50% then TP3 CLOSE REST"], group="PineConnector")
oneSignalPerBarClose = input.bool(false, "Signals on bar close only", group="PineConnector")
pc_license = input.string("8762990428373", "License ID", group="PineConnector")
pc_symbol = input.string("XAUUSD+", "Symbol", group="PineConnector")
pc_lots = input.float(0.1, "Volume Lots", step=0.01, group="PineConnector")
blockSameDirReEntryWhenFlat = input.bool(true, "Anti-spam: flat után csak ellentétes irányba léphet", group="Signals")
f_num(x) => str.tostring(x)
// PineConnector messages (SINGLE-LINE)
msg_buy() => str.format("{0},buy,{1},vol_lots={2},sl_pips={3},tp_pips={4}", pc_license, pc_symbol, f_num(pc_lots), f_num(sl_pips), f_num(tp3_pips))
msg_sell() => str.format("{0},sell,{1},vol_lots={2},sl_pips={3},tp_pips={4}", pc_license, pc_symbol, f_num(pc_lots), f_num(sl_pips), f_num(tp3_pips))
msg_close_long_all() => str.format("{0},closelong,{1}", pc_license, pc_symbol)
msg_close_short_all()=> str.format("{0},closeshort,{1}", pc_license, pc_symbol)
msg_close_long_pct() => str.format("{0},closelongpct,{1},pct={2}", pc_license, pc_symbol, f_num(closePct))
msg_close_short_pct()=> str.format("{0},closeshortpct,{1},pct={2}", pc_license, pc_symbol, f_num(closePct))
f_alert(_msg) =>
if oneSignalPerBarClose
alert(_msg, alert.freq_once_per_bar_close)
else
alert(_msg, alert.freq_once_per_bar)
canFire = oneSignalPerBarClose ? barstate.isconfirmed : barstate.isnew
//=============================================================================
// 1) HA ENTRY (ALAPKÓD)
//=============================================================================
nSame = input.int(2, "N db azonos színű lezárt HA (entry előtt)", minval=1, maxval=50)
minCandleMode = input.string("Body", "Min méret mód (HA)", options=["Range","Body"])
minCandleAtrMult = input.float(0.0, "Min méret (ATR szorzó, 0=ki)", step=0.1)
minCandleAbs = input.float(0.0, "Min méret (abs ár, 0=ki)", step=0.1)
minAtrLen = input.int(13, "ATR period", minval=1)
useWick = input.bool(true, "Wick feltétel (entry előtti HA gyertya)")
wickTolTicks = input.int(0, "Wick tolerancia (ticks)", minval=0, maxval=200)
// EZ A RÉGI RESET (CSAK EZ)
oppResetCount = input.int(3, "Reset: ennyi db ellentétes HA szín egymás után (lezárt)", minval=1, maxval=20)
showEntryLabels = input.bool(true, "Show Entry Labels")
showCloseLabels = input.bool(false, "Show Close Labels")
entryLabelSize = input.string("tiny", "Label Size", options=["tiny","small","normal"])
//=============================================================================
// HEIKIN ASHI OHLC (same TF)
//=============================================================================
haTicker = ticker.heikinashi(syminfo.tickerid)
haOpen = request.security(haTicker, timeframe.period, open, barmerge.gaps_off, barmerge.lookahead_off)
haHigh = request.security(haTicker, timeframe.period, high, barmerge.gaps_off, barmerge.lookahead_off)
haLow = request.security(haTicker, timeframe.period, low, barmerge.gaps_off, barmerge.lookahead_off)
haClose = request.security(haTicker, timeframe.period, close, barmerge.gaps_off, barmerge.lookahead_off)
// HA "szín" stabilizálás
var int haDir = na
haDir := haClose > haOpen ? 1 : haClose < haOpen ? -1 : nz(haDir[1], 1)
haBull = haDir == 1
haBear = haDir == -1
// consecutive same color
var int segCount = 0
segCount := (haDir == haDir[1]) ? (segCount + 1) : 1
prevSegCount = segCount[1]
flipHistoryOK = bar_index > prevSegCount + 1
prevHasN = prevSegCount >= nSame
// SIZE FILTER (entry előtti HA gyertyán -> [1])
haRange = haHigh - haLow
haBody = math.abs(haClose - haOpen)
haSize = (minCandleMode == "Body") ? haBody : haRange
haAtr = ta.atr(minAtrLen)
minSizeAtr = minCandleAtrMult > 0 ? haAtr * minCandleAtrMult : 0.0
minSizeAbs = minCandleAbs > 0 ? minCandleAbs : 0.0
minSize = math.max(minSizeAtr, minSizeAbs)
prevSizeOK = (minSize == 0.0) ? true : (haSize[1] >= minSize)
// WICK FILTER (entry előtti HA gyertyán -> [1])
tol = wickTolTicks * syminfo.mintick
prevUpperWick = haHigh[1] - math.max(haOpen[1], haClose[1])
prevLowerWick = math.min(haOpen[1], haClose[1]) - haLow[1]
prevNoUpperWick = prevUpperWick <= tol
prevNoLowerWick = prevLowerWick <= tol
wickOK_Sell = useWick ? prevNoUpperWick : true
wickOK_Buy = useWick ? prevNoLowerWick : true
//=============================================================================
// 3) UT BOT (V5) – trend + vizuál
//=============================================================================
utKey = input.float(1.0, "UT Key Value (Sensitivity)", step=0.1, group="UT Bot")
utAtrPeriod = input.int(10, "UT ATR Period", minval=1, group="UT Bot")
utFromHA = input.bool(false, "UT: Signals from Heikin Ashi Candles", group="UT Bot")
utSrc = utFromHA ? haClose : close
utATR = ta.atr(utAtrPeriod)
utNLoss = utKey * utATR
var float utStop = na
prevStop = nz(utStop[1], utSrc)
// (egy sorban, hogy ne legyen sortöréses ternary gond)
utStop := utSrc > prevStop and utSrc[1] > prevStop ? math.max(prevStop, utSrc - utNLoss) : utSrc < prevStop and utSrc[1] < prevStop ? math.min(prevStop, utSrc + utNLoss) : utSrc > prevStop ? (utSrc - utNLoss) : (utSrc + utNLoss)
utEma = ta.ema(utSrc, 1)
utAbove = ta.crossover(utEma, utStop)
utBelow = ta.crossover(utStop, utEma)
utBuy = (utSrc > utStop) and utAbove
utSell = (utSrc < utStop) and utBelow
utTrendUp = utSrc > utStop
utTrendDown = utSrc < utStop
//=============================================================================
// HTF VÉTÓ (kapcsolható) — UT trend alapján, az UTOLSÓ LEZÁRT HTF gyertyán
//=============================================================================
useHTFVeto = input.bool(true, "Use HTF veto (only blocks opposite)", group="HTF Veto")
htfTf = input.timeframe("5", "HTF timeframe (veto)", group="HTF Veto")
// UT stop kalkuláció független függvényben (egyértelmű return)
f_utStop_tf(_src) =>
float _atr = ta.atr(utAtrPeriod)
float _nloss = utKey * _atr
var float _stop = na
float _prev = nz(_stop[1], _src)
_stop := _src > _prev and _src[1] > _prev ? math.max(_prev, _src - _nloss) : _src < _prev and _src[1] < _prev ? math.min(_prev, _src + _nloss) : _src > _prev ? (_src - _nloss) : (_src + _nloss)
_stop
// HTF utolsó LEZÁRT bar trendje: close[1] vs stop[1]
htfTrendUp_1 = request.security(syminfo.tickerid, htfTf, close[1] > f_utStop_tf(close)[1], barmerge.gaps_off, barmerge.lookahead_off)
htfTrendDown_1 = request.security(syminfo.tickerid, htfTf, close[1] < f_utStop_tf(close)[1], barmerge.gaps_off, barmerge.lookahead_off)
// Vétó: csak akkor tilt, ha HTF tényleg ellentétes.
vetoBuy = (not useHTFVeto) or (not htfTrendDown_1)
vetoSell = (not useHTFVeto) or (not htfTrendUp_1)
//=============================================================================
// BASE JEL (entry a MOSTANI OPEN-re) + UT trend filter az előző gyertyán + HTF VÉTÓ
//=============================================================================
baseSell = flipHistoryOK and prevHasN and haBear[1] and prevSizeOK and wickOK_Sell and utTrendDown[1] and vetoSell
baseBuy = flipHistoryOK and prevHasN and haBull[1] and prevSizeOK and wickOK_Buy and utTrendUp[1] and vetoBuy
//=============================================================================
// NETTING ÁLLAPOT + CLOSE LABEL PIP SZÁM (Reset CSAK: oppResetCount)
//=============================================================================
var int posDir = 0 // 0 none, 1 buy, -1 sell
var int lastDir = 0 // anti-spamhez
var int oppStreak = 0
var float entryPrice = na
var bool tp1Done = false
var bool tp2Done = false
var bool tp3Done = false
f_lbl_size(_s) => _s == "tiny" ? size.tiny : _s == "small" ? size.small : size.normal
f_closeText(_posDir, _entry, _refPrice) =>
float pm = 0.0
if not na(_entry)
if _posDir == 1
pm := (_refPrice - _entry) / pipSize
else if _posDir == -1
pm := (_entry - _refPrice) / pipSize
int pmR = math.round(pm)
(pmR >= 0 ? "+" : "") + str.tostring(pmR)
// oppStreak frissítés (lezárt HA alapján)
if posDir == 0
oppStreak := 0
else if posDir == 1
oppStreak := (haDir[1] == -1) ? (oppStreak + 1) : 0
else if posDir == -1
oppStreak := (haDir[1] == 1) ? (oppStreak + 1) : 0
// Flag: ha ezen a baron reseteltünk, ne menjen TP logika is
var bool didResetThisBar = false
didResetThisBar := false
doResetByOppColors = (posDir != 0) and (oppStreak >= oppResetCount)
if doResetByOppColors
didResetThisBar := true
if showCloseLabels
label.new(bar_index, open, f_closeText(posDir, entryPrice, open), style=label.style_label_left, color=color.new(color.gray, 0), textcolor=color.white, size=f_lbl_size(entryLabelSize), force_overlay=true)
if canFire
if posDir == 1
f_alert(msg_close_long_all())
else if posDir == -1
f_alert(msg_close_short_all())
lastDir := posDir
posDir := 0
oppStreak := 0
entryPrice := na
tp1Done := false
tp2Done := false
tp3Done := false
//=============================================================================
// ENTRY/REVERSE
//=============================================================================
wantBuy = baseBuy and not baseSell
wantSell = baseSell and not baseBuy
allowBuyWhenFlat = (not blockSameDirReEntryWhenFlat) or (lastDir != 1)
allowSellWhenFlat = (not blockSameDirReEntryWhenFlat) or (lastDir != -1)
doBuy = canFire and wantBuy and posDir != 1 and (posDir != 0 or allowBuyWhenFlat)
doSell = canFire and wantSell and posDir != -1 and (posDir != 0 or allowSellWhenFlat)
if doBuy
int prevPosDir = posDir
float prevEntry = entryPrice
if prevPosDir == 1
f_alert(msg_close_long_all())
else if prevPosDir == -1
f_alert(msg_close_short_all())
f_alert(msg_buy())
if showCloseLabels and prevPosDir != 0
label.new(bar_index, open, f_closeText(prevPosDir, prevEntry, open), style=label.style_label_down, color=color.new(color.gray, 0), textcolor=color.white, size=f_lbl_size(entryLabelSize), force_overlay=true)
posDir := 1
lastDir := 1
oppStreak := 0
entryPrice := open
tp1Done := false
tp2Done := false
tp3Done := false
if showEntryLabels
label.new(bar_index, open, "BUY", style=label.style_label_up, color=color.new(color.lime, 0), textcolor=color.white, size=f_lbl_size(entryLabelSize), force_overlay=true)
if doSell
int prevPosDir = posDir
float prevEntry = entryPrice
if prevPosDir == 1
f_alert(msg_close_long_all())
else if prevPosDir == -1
f_alert(msg_close_short_all())
f_alert(msg_sell())
if showCloseLabels and prevPosDir != 0
label.new(bar_index, open, f_closeText(prevPosDir, prevEntry, open), style=label.style_label_up, color=color.new(color.gray, 0), textcolor=color.white, size=f_lbl_size(entryLabelSize), force_overlay=true)
posDir := -1
lastDir := -1
oppStreak := 0
entryPrice := open
tp1Done := false
tp2Done := false
tp3Done := false
if showEntryLabels
label.new(bar_index, open, "SELL", style=label.style_label_down, color=color.new(color.red, 0), textcolor=color.white, size=f_lbl_size(entryLabelSize), force_overlay=true)
//=============================================================================
// TP LOGIC (stabilizálva)
// - TP3 prioritás
// - TP3 csak egyszer (tp3Done)
// - Reset baron nem fut
//=============================================================================
tp1Hit = posDir != 0 and not na(entryPrice) and (posDir == 1 ? high >= entryPrice + tp1_pips * pipSize : low <= entryPrice - tp1_pips * pipSize)
tp2Hit = posDir != 0 and not na(entryPrice) and (posDir == 1 ? high >= entryPrice + tp2_pips * pipSize : low <= entryPrice - tp2_pips * pipSize)
tp3Hit = posDir != 0 and not na(entryPrice) and (posDir == 1 ? high >= entryPrice + tp3_pips * pipSize : low <= entryPrice - tp3_pips * pipSize)
if canFire and (not didResetThisBar) and tp3Hit and (not tp3Done)
tp3Done := true
f_alert(posDir == 1 ? msg_close_long_all() : msg_close_short_all())
lastDir := posDir
posDir := 0
oppStreak := 0
entryPrice := na
tp1Done := false
tp2Done := false
tp3Done := false
if canFire and (not didResetThisBar) and posDir != 0 and tp1Hit and (not tp1Done)
tp1Done := true
f_alert(posDir == 1 ? msg_close_long_pct() : msg_close_short_pct())
if canFire and (not didResetThisBar) and posDir != 0 and tp2Hit and (not tp2Done)
tp2Done := true
if tpCloseMode == "TP1 50% then TP2 CLOSE REST"
f_alert(posDir == 1 ? msg_close_long_all() : msg_close_short_all())
lastDir := posDir
posDir := 0
oppStreak := 0
entryPrice := na
tp1Done := false
tp2Done := false
tp3Done := false
else
f_alert(posDir == 1 ? msg_close_long_pct() : msg_close_short_pct())
//=============================================================================
// DONCHIAN CHANNELS (V5) – vizuál
//=============================================================================
dcLen = input.int(20, "Donchian Length", minval=1, group="Donchian")
dcOff = input.int(0, "Donchian Offset", group="Donchian")
dcUseHA = input.bool(false, "Donchian számítás Heikin Ashi-ból", group="Donchian")
dcLower = dcUseHA ? ta.lowest(haLow, dcLen) : ta.lowest(low, dcLen)
dcUpper = dcUseHA ? ta.highest(haHigh, dcLen): ta.highest(high, dcLen)
dcBasis = math.avg(dcUpper, dcLower)
plot(dcBasis, "DC Basis", color=#FF6D00, offset=dcOff)
dcU = plot(dcUpper, "DC Upper", color=#2962FF, offset=dcOff)
dcL = plot(dcLower, "DC Lower", color=#2962FF, offset=dcOff)
fill(dcU, dcL, color=color.rgb(33, 150, 243, 95), title="DC Background")
//=============================================================================
// UT VISUALS
//=============================================================================
showUTShapes = input.bool(true, "Show UT Buy/Sell Labels", group="UT Bot")
showUTBarColor= input.bool(false, "UT Barcolor", group="UT Bot")
plotUTStop = input.bool(true, "Plot UT Stop Line", group="UT Bot")
plot(plotUTStop ? utStop : na, title="UT ATR Trailing Stop", color=color.new(color.yellow, 0), linewidth=2)
plotshape(showUTShapes and utBuy, title="UT Buy", text="Buy", style=shape.labelup, location=location.belowbar, color=color.new(color.green, 0), textcolor=color.white, size=size.tiny)
plotshape(showUTShapes and utSell, title="UT Sell", text="Sell", style=shape.labeldown, location=location.abovebar, color=color.new(color.red, 0), textcolor=color.white, size=size.tiny)
barcolor(showUTBarColor and utTrendUp ? color.new(#0400ff, 0) : na)
barcolor(showUTBarColor and utTrendDown ? color.new(#ddd019, 0) : na)
alertcondition(utBuy, "UT Long", "UT Long")
alertcondition(utSell, "UT Short", "UT Short")