本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("DHARA Precision Pro", shorttitle="DHARA PRO", overlay=true, max_labels_count=500, max_lines_count=200)
// RESEARCH-ONLY, MANUAL DECISION SUPPORT. BUY = bullish CE bias; SELL = bearish PE bias.
// A repeated state is not an instruction to add another position.
gStructure = "01 • Confirmed market structure"
leftBars = input.int(4, "Pivot left bars", minval=1, maxval=20, group=gStructure)
rightBars = input.int(4, "Pivot confirmation bars", minval=1, maxval=20, group=gStructure)
minSwingATR = input.float(0.8, "Minimum alternating swing size (ATR)", minval=0, step=0.1, group=gStructure)
showSwings = input.bool(false, "HH / HL / LH / LL", group=gStructure)
showBreaks = input.bool(false, "BOS / CHoCH", group=gStructure)
showZigzag = input.bool(false, "Confirmed swing zigzag", group=gStructure)
showSweeps = input.bool(false, "Show failed level breaks (sweeps)", group=gStructure)
gTrend = "02 • Trend, momentum and volume"
fastLen = input.int(9, "Fast EMA", minval=1, group=gTrend)
slowLen = input.int(21, "Slow EMA", minval=2, group=gTrend)
trendLen = input.int(50, "Context EMA", minval=3, group=gTrend)
rsiLen = input.int(7, "RSI length", minval=2, group=gTrend)
adxLen = input.int(14, "DMI length", minval=2, group=gTrend)
adxSmooth = input.int(14, "ADX smoothing", minval=2, group=gTrend)
adxMin = input.float(20.0, "Minimum ADX", minval=1, step=0.5, group=gTrend)
volMultiplier = input.float(1.0, "Minimum relative volume", minval=0, step=0.05, group=gTrend)
requireVolume = input.bool(true, "Require relative volume", group=gTrend)
requireHTF = input.bool(true, "Require completed higher-timeframe trend", group=gTrend)
htf = input.timeframe("15", "Higher timeframe (use > chart timeframe)", group=gTrend)
requireStructure = input.bool(true, "Require confirmed structure direction", group=gTrend)
minScore = input.int(6, "Required directional rules (of 7)", minval=4, maxval=7, group=gTrend)
gSession = "03 • Trading session / breakout"
tradeSession = input.session("0925-1450", "Signal window (exchange timezone)", group=gSession)
restrictSession = input.bool(true, "WAIT outside signal window", group=gSession)
useOpeningRange = input.bool(false, "Require opening-range direction", group=gSession)
orbSession = input.session("0915-0930", "Opening range", group=gSession)
showOpeningRange = input.bool(false, "Plot opening range", group=gSession)
showPreviousDay = input.bool(false, "Plot previous day's high/low", group=gSession)
gRisk = "04 • Risk and chart"
atrLen = input.int(14, "ATR length", minval=2, group=gRisk)
atrMult = input.float(1.3, "Spot stop distance in ATR", minval=0.2, step=0.1, group=gRisk)
maxExtendedATR = input.float(2.0, "Max distance from EMA21 in ATR", minval=0.1, step=0.1, group=gRisk)
showEMAs = input.bool(true, "Display EMA lines", group=gRisk)
showVWAP = input.bool(true, "Display VWAP", group=gRisk)
showContextEMA = input.bool(false, "Display EMA50", group=gRisk)
showVWMA = input.bool(false, "Display VWMA20 (magenta)", group=gRisk)
showSupertrend = input.bool(true, "Display Supertrend trailing context", group=gRisk)
vwmaLen = input.int(20, "VWMA length", minval=2, group=gRisk)
superATR = input.int(10, "Supertrend ATR period", minval=2, group=gRisk)
superFactor = input.float(3.0, "Supertrend factor", minval=0.5, step=0.25, group=gRisk)
useSTFilter = input.bool(true, "Require Supertrend direction", group=gRisk)
useVWMAFilter = input.bool(false, "Require VWMA direction", group=gRisk)
stopMethod = input.string("ATR + Structure", "Spot stop model", options=["ATR", "ATR + Structure"], group=gRisk)
maxStopATR = input.float(3.0, "Maximum stop distance in ATR", minval=0.5, group=gRisk)
targetR1 = input.float(1.5, "Target 1 reward:risk", minval=0.5, step=0.25, group=gRisk)
targetR2 = input.float(2.0, "Target 2 reward:risk", minval=0.5, step=0.25, group=gRisk)
levelWidth = input.int(15, "Latest levels span (bars)", minval=3, maxval=100, group=gRisk)
showRiskLines = input.bool(true, "Show latest setup entry/SL/targets", group=gRisk)
labelEveryCandle = input.bool(true, "Small state marker on every closed candle", group=gRisk)
requireMomentum = input.bool(true, "Require RSI and DMI direction", group=gTrend)
useContextFilter = input.bool(true, "Require EMA50 context", group=gTrend)
showDashboard = input.bool(true, "Compact dashboard", group=gRisk)
emaFast = ta.ema(close, fastLen)
emaSlow = ta.ema(close, slowLen)
emaContext = ta.ema(close, trendLen)
vwmaValue = ta.vwma(close, vwmaLen)
[superValue, superDir] = ta.supertrend(superFactor, superATR)
stBull = superDir < 0
stBear = superDir > 0
vwapVal = ta.vwap(hlc3)
rsi = ta.rsi(close, rsiLen)
[diP, diM, adx] = ta.dmi(adxLen, adxSmooth)
atr = ta.atr(atrLen)
volAvg = ta.sma(volume, 20)
relVol = not na(volume) and volAvg > 0 ? volume / volAvg : na
volumeOK = not requireVolume or (not na(relVol) and relVol >= volMultiplier)
// Last fully CLOSED HTF candle; no future-looking HTF values.
htfClose = request.security(syminfo.tickerid, htf, close[1], lookahead=barmerge.lookahead_on)
htfEma = request.security(syminfo.tickerid, htf, ta.ema(close, slowLen)[1], lookahead=barmerge.lookahead_on)
htfBull = htfClose > htfEma
htfBear = htfClose < htfEma
validHTF = timeframe.in_seconds(htf) > timeframe.in_seconds(timeframe.period)
// Alternating, confirmed price pivots. Same-side pivots only replace the extremum.
ph = ta.pivothigh(high, leftBars, rightBars)
pl = ta.pivotlow(low, leftBars, rightBars)
var int lastType = 0 // 1 high, -1 low
var float lastPivot = na
var int lastPivotBar = na
var float prevConfirmedHigh = na
var float prevConfirmedLow = na
var float currentHigh = na
var float currentLow = na
var int structure = 0 // 1 bullish, -1 bearish, 0 transition/unset
var int lastBrokenHighBar = na
var int lastBrokenLowBar = na
var int highPivotBar = na
var int lowPivotBar = na
bool newHigh = false
bool newLow = false
float newPoint = na
int newPointBar = na
float pivotAtr = atr[rightBars]
// If both a high and a low confirm simultaneously, skip ambiguous outside-bar pivots.
if barstate.isconfirmed and not (not na(ph) and not na(pl))
if not na(ph)
int b = bar_index - rightBars
if lastType == 1
if ph > lastPivot
lastPivot := ph
lastPivotBar := b
currentHigh := ph
highPivotBar := b
// Do not relabel or retrospectively change old historical labels.
else if na(lastPivot) or ph - lastPivot >= nz(pivotAtr, atr) * minSwingATR
newHigh := true
newPoint := ph
newPointBar := b
lastType := 1
lastPivot := ph
lastPivotBar := b
currentHigh := ph
highPivotBar := b
if not na(pl)
int b = bar_index - rightBars
if lastType == -1
if pl < lastPivot
lastPivot := pl
lastPivotBar := b
currentLow := pl
lowPivotBar := b
else if na(lastPivot) or lastPivot - pl >= nz(pivotAtr, atr) * minSwingATR
newLow := true
newPoint := pl
newPointBar := b
lastType := -1
lastPivot := pl
lastPivotBar := b
currentLow := pl
lowPivotBar := b
// Price-based HH/LH and HL/LL, independent of RSI readings.
if newHigh
string highTag = na(prevConfirmedHigh) ? "H" : newPoint > prevConfirmedHigh ? "HH" : newPoint < prevConfirmedHigh ? "LH" : "EH"
if showSwings
label.new(newPointBar, newPoint, highTag, style=label.style_label_down, color=color.new(color.red, 25), textcolor=color.white, size=size.tiny)
prevConfirmedHigh := newPoint
if newLow
string lowTag = na(prevConfirmedLow) ? "L" : newPoint > prevConfirmedLow ? "HL" : newPoint < prevConfirmedLow ? "LL" : "EL"
if showSwings
label.new(newPointBar, newPoint, lowTag, style=label.style_label_up, color=color.new(color.green, 25), textcolor=color.white, size=size.tiny)
prevConfirmedLow := newPoint
// Zigzag joins accepted confirmed alternating pivots only.
var float lastDrawPrice = na
var int lastDrawBar = na
if (newHigh or newLow)
if showZigzag and not na(lastDrawPrice) and not na(lastDrawBar)
line.new(lastDrawBar, lastDrawPrice, newPointBar, newPoint, color=color.new(color.aqua, 25), width=2)
lastDrawPrice := newPoint
lastDrawBar := newPointBar
// Structural break can occur only on a completed candle after a pivot is confirmed.
// Distinguish first reversal (CHoCH) from continuation (BOS).
upBreak = barstate.isconfirmed and not na(currentHigh) and close > currentHigh and close[1] <= currentHigh and bar_index > highPivotBar and (na(lastBrokenHighBar) or lastBrokenHighBar != highPivotBar)
downBreak = barstate.isconfirmed and not na(currentLow) and close < currentLow and close[1] >= currentLow and bar_index > lowPivotBar and (na(lastBrokenLowBar) or lastBrokenLowBar != lowPivotBar)
if upBreak
bool reversal = structure == -1
if showBreaks
label.new(bar_index, low, reversal ? "CHoCH ↑" : "BOS ↑", style=label.style_label_up, color=reversal ? color.orange : color.green, textcolor=color.white, size=size.tiny)
structure := 1
lastBrokenHighBar := highPivotBar
if downBreak
bool reversal = structure == 1
if showBreaks
label.new(bar_index, high, reversal ? "CHoCH ↓" : "BOS ↓", style=label.style_label_down, color=reversal ? color.orange : color.red, textcolor=color.white, size=size.tiny)
structure := -1
lastBrokenLowBar := lowPivotBar
sweepHigh = barstate.isconfirmed and not na(currentHigh) and high > currentHigh and close < currentHigh
sweepLow = barstate.isconfirmed and not na(currentLow) and low < currentLow and close > currentLow
plotshape(showSweeps and sweepHigh, "Rejected swing high", shape.xcross, location.abovebar, color=color.orange, size=size.tiny)
plotshape(showSweeps and sweepLow, "Rejected swing low", shape.xcross, location.belowbar, color=color.aqua, size=size.tiny)
inTradingSession = not na(time(timeframe.period, tradeSession))
inORB = not na(time(timeframe.period, orbSession))
newDay = timeframe.change("D")
var float orHigh = na
var float orLow = na
var bool orbSeen = false
if newDay
orHigh := na
orLow := na
orbSeen := false
if inORB
orHigh := na(orHigh) ? high : math.max(orHigh, high)
orLow := na(orLow) ? low : math.min(orLow, low)
orbSeen := true
orbReady = orbSeen and not inORB
orbBull = orbReady and close > orHigh
orbBear = orbReady and close < orLow
prevHigh = request.security(syminfo.tickerid, "D", high[1], lookahead=barmerge.lookahead_on)
prevLow = request.security(syminfo.tickerid, "D", low[1], lookahead=barmerge.lookahead_on)
// Exactly seven directional rules. Thresholds are user-configurable.
bullScore = (emaFast > emaSlow ? 1 : 0) + (close > vwapVal ? 1 : 0) + (rsi > 53 and diP > diM ? 1 : 0) + (adx >= adxMin ? 1 : 0) + (volumeOK ? 1 : 0) + (htfBull ? 1 : 0) + (structure == 1 ? 1 : 0)
bearScore = (emaFast < emaSlow ? 1 : 0) + (close < vwapVal ? 1 : 0) + (rsi < 47 and diM > diP ? 1 : 0) + (adx >= adxMin ? 1 : 0) + (volumeOK ? 1 : 0) + (htfBear ? 1 : 0) + (structure == -1 ? 1 : 0)
sessionOK = not restrictSession or inTradingSession
htfOK = not requireHTF or validHTF
notExtended = not na(atr) and math.abs(close - emaSlow) <= atr * maxExtendedATR
bullMust = close > vwapVal and emaFast > emaSlow and adx >= adxMin and volumeOK and (not requireHTF or htfBull) and (not requireStructure or structure == 1) and (not useOpeningRange or orbBull) and (not useSTFilter or stBull) and (not useVWMAFilter or close > vwmaValue) and (not requireMomentum or (rsi > 53 and diP > diM)) and (not useContextFilter or close > emaContext)
bearMust = close < vwapVal and emaFast < emaSlow and adx >= adxMin and volumeOK and (not requireHTF or htfBear) and (not requireStructure or structure == -1) and (not useOpeningRange or orbBear) and (not useSTFilter or stBear) and (not useVWMAFilter or close < vwmaValue) and (not requireMomentum or (rsi < 47 and diM > diP)) and (not useContextFilter or close < emaContext)
bullReady = sessionOK and htfOK and notExtended and bullMust and bullScore >= minScore
bearReady = sessionOK and htfOK and notExtended and bearMust and bearScore >= minScore
int state = bullReady and not bearReady ? 1 : bearReady and not bullReady ? -1 : 0
// Display BUY or SELL only after a completed candle. Neutral bars receive no marker.
plotshape(labelEveryCandle and barstate.isconfirmed and state == 1, title="BUY each close", text="BUY", style=shape.labelup, location=location.belowbar, color=color.new(color.green, 28), textcolor=color.white, size=size.tiny)
plotshape(labelEveryCandle and barstate.isconfirmed and state == -1, title="SELL each close", text="SELL", style=shape.labeldown, location=location.abovebar, color=color.new(color.red, 25), textcolor=color.white, size=size.tiny)
plot(showEMAs ? emaFast : na, "EMA 9 / Fast", color=color.aqua, linewidth=2)
plot(showEMAs ? emaSlow : na, "EMA 21 / Slow", color=color.orange, linewidth=2)
plot(showContextEMA ? emaContext : na, "EMA 50 / Context", color=color.purple)
plot(showVWAP ? vwapVal : na, "Session VWAP", color=color.yellow, linewidth=2)
plot(showVWMA ? vwmaValue : na, "Volume weighted moving average", color=color.fuchsia, linewidth=2)
plot(showSupertrend and stBull ? superValue : na, "Supertrend bullish", color=color.new(color.green, 12), linewidth=2, style=plot.style_linebr)
plot(showSupertrend and stBear ? superValue : na, "Supertrend bearish", color=color.new(color.red, 12), linewidth=2, style=plot.style_linebr)
plot(showOpeningRange ? orHigh : na, "Opening range high", color=color.new(color.green, 55), style=plot.style_linebr)
plot(showOpeningRange ? orLow : na, "Opening range low", color=color.new(color.red, 55), style=plot.style_linebr)
plot(showPreviousDay ? prevHigh : na, "Previous day high", color=color.new(color.fuchsia, 50), style=plot.style_stepline)
plot(showPreviousDay ? prevLow : na, "Previous day low", color=color.new(color.fuchsia, 50), style=plot.style_stepline)
// SPOT-only setup lifecycle. One qualified setup at a time, with stale levels cleared.
var float entryLevel = na
var float stopLevel = na
var float target1Level = na
var float target2Level = na
var int activeDir = 0
var int entryBar = na
var line entryDraw = na
var line stopDraw = na
var line target1Draw = na
var line target2Draw = na
var label entryTag = na
var label stopTag = na
var label target1Tag = na
var label target2Tag = na
// A setup is valid only after confirmed close; no more than one active setup.
freshBuy = barstate.isconfirmed and state == 1 and nz(state[1]) != 1
freshSell = barstate.isconfirmed and state == -1 and nz(state[1]) != -1
if barstate.isconfirmed
// Stop-first convention if both stop and target are hit within one OHLC bar.
bool stopped = activeDir == 1 ? low <= stopLevel : activeDir == -1 ? high >= stopLevel : false
bool reached = activeDir == 1 ? high >= target2Level : activeDir == -1 ? low <= target2Level : false
bool invalid = activeDir != 0 and (state == -activeDir or not sessionOK or newDay)
if activeDir != 0 and (stopped or reached or invalid)
activeDir := 0
entryLevel := na
stopLevel := na
target1Level := na
target2Level := na
entryBar := na
if not na(entryDraw)
line.delete(entryDraw)
line.delete(stopDraw)
line.delete(target1Draw)
line.delete(target2Draw)
label.delete(entryTag)
label.delete(stopTag)
label.delete(target1Tag)
label.delete(target2Tag)
entryDraw := na
stopDraw := na
target1Draw := na
target2Draw := na
entryTag := na
stopTag := na
target1Tag := na
target2Tag := na
// New setup starts only on a genuine state transition, with valid underlying risk.
if activeDir == 0 and (freshBuy or freshSell) and not newDay
float candidateEntry = close
float atrStop = freshBuy ? close - atr * atrMult : close + atr * atrMult
float swingStop = freshBuy ? currentLow : currentHigh
float candidateStop = atrStop
if stopMethod == "ATR + Structure" and not na(swingStop)
candidateStop := freshBuy ? math.min(atrStop, swingStop) : math.max(atrStop, swingStop)
float dist = math.abs(candidateEntry - candidateStop)
bool validRisk = not na(dist) and not na(atr) and dist > syminfo.mintick and dist <= maxStopATR * atr
if validRisk
entryLevel := candidateEntry
stopLevel := candidateStop
target1Level := freshBuy ? candidateEntry + targetR1 * dist : candidateEntry - targetR1 * dist
target2Level := freshBuy ? candidateEntry + targetR2 * dist : candidateEntry - targetR2 * dist
activeDir := freshBuy ? 1 : -1
entryBar := bar_index
if showRiskLines
entryDraw := line.new(bar_index, entryLevel, bar_index + levelWidth, entryLevel, color=color.white, width=2)
stopDraw := line.new(bar_index, stopLevel, bar_index + levelWidth, stopLevel, color=color.red, width=2)
target1Draw := line.new(bar_index, target1Level, bar_index + levelWidth, target1Level, color=color.lime, width=2)
target2Draw := line.new(bar_index, target2Level, bar_index + levelWidth, target2Level, color=color.green, width=2, style=line.style_dashed)
entryTag := label.new(bar_index + levelWidth, entryLevel, "ENTRY " + str.tostring(entryLevel, format.mintick), style=label.style_label_left, color=color.gray, textcolor=color.white, size=size.tiny)
stopTag := label.new(bar_index + levelWidth, stopLevel, "SL " + str.tostring(stopLevel, format.mintick), style=label.style_label_left, color=color.red, textcolor=color.white, size=size.tiny)
target1Tag := label.new(bar_index + levelWidth, target1Level, "T1 " + str.tostring(target1Level, format.mintick), style=label.style_label_left, color=color.green, textcolor=color.white, size=size.tiny)
target2Tag := label.new(bar_index + levelWidth, target2Level, "T2 " + str.tostring(target2Level, format.mintick), style=label.style_label_left, color=color.new(color.green, 20), textcolor=color.white, size=size.tiny)
if barstate.islast and showRiskLines and activeDir != 0 and not na(entryDraw)
int xRight = bar_index + 3
line.set_x2(entryDraw, xRight)
line.set_x2(stopDraw, xRight)
line.set_x2(target1Draw, xRight)
line.set_x2(target2Draw, xRight)
label.set_x(entryTag, xRight)
label.set_x(stopTag, xRight)
label.set_x(target1Tag, xRight)
label.set_x(target2Tag, xRight)
string reason = not sessionOK ? "Outside signal window" : not htfOK ? "HTF must exceed chart TF" : not notExtended ? "Price overextended" : not volumeOK ? "Weak relative volume" : adx < adxMin ? "Weak ADX" : structure == 0 and requireStructure ? "Structure not established" : not bullReady and not bearReady ? "Conditions disagree" : "Qualified technical bias"
var table board = table.new(position.top_right, 2, 8, border_width=1, bgcolor=color.new(color.black, 20))
if barstate.islast and showDashboard
table.cell(board, 0, 0, "DHARA PRECISION PRO", text_color=color.white, bgcolor=color.rgb(24,55,92), text_size=size.small)
table.cell(board, 1, 0, state == 1 ? "BUY" : state == -1 ? "SELL" : "NO SIGNAL", text_color=color.white, bgcolor=state == 1 ? color.green : state == -1 ? color.red : color.gray, text_size=size.small)
table.cell(board, 0, 1, "Price structure", text_color=color.white, text_size=size.tiny)
table.cell(board, 1, 1, structure == 1 ? "BULL" : structure == -1 ? "BEAR" : "UNSET", text_color=color.white, text_size=size.tiny)
table.cell(board, 0, 2, "RSI / ADX", text_color=color.white, text_size=size.tiny)
table.cell(board, 1, 2, str.tostring(rsi, "#.0") + " / " + str.tostring(adx, "#.0"), text_color=color.white, text_size=size.tiny)
table.cell(board, 0, 3, "Relative volume", text_color=color.white, text_size=size.tiny)
table.cell(board, 1, 3, str.tostring(relVol, "#.00") + "x", text_color=color.white, text_size=size.tiny)
table.cell(board, 0, 4, "Bull / Bear rules", text_color=color.white, text_size=size.tiny)
table.cell(board, 1, 4, str.tostring(bullScore) + "/7 | " + str.tostring(bearScore) + "/7", text_color=color.white, text_size=size.tiny)
table.cell(board, 0, 5, "Why?", text_color=color.white, text_size=size.tiny)
table.cell(board, 1, 5, reason, text_color=color.yellow, text_size=size.tiny)
table.cell(board, 0, 6, "Latest entry / SL", text_color=color.white, text_size=size.tiny)
table.cell(board, 1, 6, activeDir == 0 ? "—" : str.tostring(entryLevel, format.mintick) + " / " + str.tostring(stopLevel, format.mintick), text_color=color.white, text_size=size.tiny)
table.cell(board, 0, 7, "Targets T1 / T2", text_color=color.white, text_size=size.tiny)
table.cell(board, 1, 7, activeDir == 0 ? "—" : str.tostring(target1Level, format.mintick) + " / " + str.tostring(target2Level, format.mintick), text_color=color.lime, text_size=size.tiny)
alertcondition(freshBuy and activeDir == 1, "DHARA BUY state started", "DHARA Precision Pro BUY (bullish spot bias) on {{ticker}} at {{close}}")
alertcondition(freshSell and activeDir == -1, "DHARA SELL state started", "DHARA Precision Pro SELL (bearish spot bias) on {{ticker}} at {{close}}")
alertcondition(barstate.isconfirmed and state == 0 and nz(state[1]) != 0, "DHARA WAIT state started", "DHARA Precision Pro WAIT on {{ticker}} at {{close}}")