本页源码来自 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/
// © ben_haeckler
//@version=6
indicator("GOLD MASTER V1 - Unified Market Intelligence", shorttitle="GOLD MASTER V1", overlay=false, max_boxes_count=50, max_labels_count=200)
//====================================================
// 1. INPUTS
//====================================================
string GR_TREND = "01 - Market Regime"
string GR_STRUCT = "02 - Market Structure"
string GR_ZONE = "03 - Supply & Demand"
string GR_SCORE = "04 - Signal Quality"
string GR_VIS = "05 - Display"
tf1 = input.timeframe("15", "Trend TF 1", group=GR_TREND)
tf2 = input.timeframe("60", "Trend TF 2", group=GR_TREND)
tf3 = input.timeframe("240", "Trend TF 3", group=GR_TREND)
emaLen = input.int(50, "HTF EMA Length", minval=2, group=GR_TREND)
atrFastLen = input.int(12, "Fast ATR", minval=2, group=GR_TREND)
atrSlowLen = input.int(50, "Slow ATR", minval=2, group=GR_TREND)
pivotLeft = input.int(10, "Pivot Left", minval=1, group=GR_STRUCT)
pivotRight = input.int(10, "Pivot Right", minval=1, group=GR_STRUCT)
maxBreakATR = input.float(5.0, "Maximum Break Distance (ATR)", minval=0.5, step=0.5, group=GR_STRUCT)
baseLookback = input.int(12, "Base Search Lookback", minval=2, maxval=40, group=GR_ZONE)
impulseAtrMin = input.float(1.0, "Minimum Impulse (ATR)", minval=0.1, step=0.1, group=GR_ZONE)
zoneExtend = input.int(100, "Zone Display Extension", minval=10, maxval=500, group=GR_ZONE)
maxTouches = input.int(2, "Maximum Zone Touches", minval=0, maxval=10, group=GR_ZONE)
signalThreshold = input.int(75, "Minimum Signal Score", minval=1, maxval=100, group=GR_SCORE)
showStructure = input.bool(true, "Show Structure Levels", group=GR_VIS)
showZones = input.bool(true, "Show Supply & Demand", group=GR_VIS)
showSignals = input.bool(true, "Show BUY / SELL Signals", group=GR_VIS)
showDashboard = input.bool(true, "Show Score Dashboard", group=GR_VIS)
showBOSLabels = input.bool(true, "Afficher les étiquettes BOS")
//====================================================
// 2. MARKET REGIME ENGINE
//====================================================
// Previous completed HTF candle is used to avoid
// intrabar HTF repainting.
int trend1 = request.security(
syminfo.tickerid, tf1,
close[1] > ta.ema(close, emaLen)[1] ? 1 : -1,
lookahead=barmerge.lookahead_on)
int trend2 = request.security(
syminfo.tickerid, tf2,
close[1] > ta.ema(close, emaLen)[1] ? 1 : -1,
lookahead=barmerge.lookahead_on)
int trend3 = request.security(
syminfo.tickerid, tf3,
close[1] > ta.ema(close, emaLen)[1] ? 1 : -1,
lookahead=barmerge.lookahead_on)
int bullTrendCount =
(trend1 == 1 ? 1 : 0) +
(trend2 == 1 ? 1 : 0) +
(trend3 == 1 ? 1 : 0)
int bearTrendCount =
(trend1 == -1 ? 1 : 0) +
(trend2 == -1 ? 1 : 0) +
(trend3 == -1 ? 1 : 0)
float atrFast = ta.atr(atrFastLen)
float atrSlow = ta.atr(atrSlowLen)
float volatilityRatio = atrSlow > 0 ? atrFast / atrSlow : 1.0
//====================================================
// 3. STRUCTURAL ENGINE
//====================================================
float pivotHigh = ta.pivothigh(high, pivotLeft, pivotRight)
float pivotLow = ta.pivotlow(low, pivotLeft, pivotRight)
var float lastSwingHigh = na
var float lastSwingLow = na
var int marketBias = 0
if not na(pivotHigh)
lastSwingHigh := pivotHigh
if not na(pivotLow)
lastSwingLow := pivotLow
bool bullBreak =
not na(lastSwingHigh) and
ta.crossover(close, lastSwingHigh)
bool bearBreak =
not na(lastSwingLow) and
ta.crossunder(close, lastSwingLow)
// Breakout extension filter
float bullBreakDistance = not na(lastSwingHigh) ? math.max(close - lastSwingHigh, 0) : 0
float bearBreakDistance = not na(lastSwingLow) ? math.max(lastSwingLow - close, 0) : 0
bool validBullBreak =
bullBreak and
atrFast > 0 and
bullBreakDistance <= atrFast * maxBreakATR
bool validBearBreak =
bearBreak and
atrFast > 0 and
bearBreakDistance <= atrFast * maxBreakATR
if barstate.isconfirmed
if validBullBreak
marketBias := 1
if showBOSLabels
label.new(bar_index, high, "BOS ↑",
style=label.style_label_down,
color=color.new(color.green, 10),
textcolor=color.white,
size=size.tiny,
force_overlay=true)
if validBearBreak
marketBias := -1
if showBOSLabels
label.new(bar_index, low, "BOS ↓",
style=label.style_label_up,
color=color.new(color.red, 10),
textcolor=color.white,
size=size.tiny,
force_overlay=true)
//====================================================
// 4. SUPPLY & DEMAND ENGINE
//====================================================
var float demandTop = na
var float demandBottom = na
var float supplyTop = na
var float supplyBottom = na
var float demandImpulse = 0.0
var float supplyImpulse = 0.0
var int demandTouches = 0
var int supplyTouches = 0
var int demandCreatedBar = na
var int supplyCreatedBar = na
var box demandBox = na
var box supplyBox = na
float candleBody = math.abs(close - open)
// Find the last opposing candle before a bullish BOS.
if barstate.isconfirmed and validBullBreak and candleBody >= atrFast * impulseAtrMin
int foundOffset = na
for i = 1 to baseLookback
if na(foundOffset) and close[i] < open[i]
foundOffset := i
if not na(foundOffset)
if not na(demandBox)
box.delete(demandBox)
demandTop := high[foundOffset]
demandBottom := low[foundOffset]
demandImpulse := candleBody / atrFast
demandTouches := 0
demandCreatedBar := bar_index
if showZones
demandBox := box.new(
left=bar_index-foundOffset,
top=demandTop,
right=bar_index+zoneExtend,
bottom=demandBottom,
xloc=xloc.bar_index,
extend=extend.right,
bgcolor=color.new(color.green, 85),
border_color=color.new(color.green, 25),
force_overlay=true)
// Find the last opposing candle before a bearish BOS.
if barstate.isconfirmed and validBearBreak and candleBody >= atrFast * impulseAtrMin
int foundOffset = na
for i = 1 to baseLookback
if na(foundOffset) and close[i] > open[i]
foundOffset := i
if not na(foundOffset)
if not na(supplyBox)
box.delete(supplyBox)
supplyTop := high[foundOffset]
supplyBottom := low[foundOffset]
supplyImpulse := candleBody / atrFast
supplyTouches := 0
supplyCreatedBar := bar_index
if showZones
supplyBox := box.new(
left=bar_index-foundOffset,
top=supplyTop,
right=bar_index+zoneExtend,
bottom=supplyBottom,
xloc=xloc.bar_index,
extend=extend.right,
bgcolor=color.new(color.red, 85),
border_color=color.new(color.red, 25),
force_overlay=true)
// Zone overlap
bool inDemand =
not na(demandTop) and
low <= demandTop and
high >= demandBottom
bool inSupply =
not na(supplyTop) and
high >= supplyBottom and
low <= supplyTop
// Count distinct zone entries.
if barstate.isconfirmed
if inDemand and not inDemand[1] and bar_index > demandCreatedBar
demandTouches += 1
if inSupply and not inSupply[1] and bar_index > supplyCreatedBar
supplyTouches += 1
// Invalidate broken zones.
if barstate.isconfirmed
if not na(demandBottom) and close < demandBottom
if not na(demandBox)
box.delete(demandBox)
demandBox := na
demandTop := na
demandBottom := na
demandImpulse := 0
demandTouches := 0
if not na(supplyTop) and close > supplyTop
if not na(supplyBox)
box.delete(supplyBox)
supplyBox := na
supplyTop := na
supplyBottom := na
supplyImpulse := 0
supplyTouches := 0
//====================================================
// 5. SIGNAL QUALITY ENGINE
//====================================================
// Trend score: maximum 20 points.
float bullTrendScore = 20.0 * bullTrendCount / 3.0
float bearTrendScore = 20.0 * bearTrendCount / 3.0
// Structure score: maximum 30 points.
float bullStructureScore = marketBias == 1 ? 30.0 : 0.0
float bearStructureScore = marketBias == -1 ? 30.0 : 0.0
// Supply & Demand score: maximum 30 points.
float bullZoneScore = not na(demandTop) ? (inDemand ? 30.0 : 12.0) : 0.0
float bearZoneScore = not na(supplyTop) ? (inSupply ? 30.0 : 12.0) : 0.0
// Quality score: maximum 20 points.
// Impulse strength: 12 points.
// Zone freshness: 8 points.
float bullFreshness =
demandTouches <= maxTouches ?
8.0 * (1.0 - demandTouches / (maxTouches + 1.0)) : 0.0
float bearFreshness =
supplyTouches <= maxTouches ?
8.0 * (1.0 - supplyTouches / (maxTouches + 1.0)) : 0.0
float bullImpulseScore =
not na(demandTop) ?
12.0 * math.min(demandImpulse / impulseAtrMin, 1.0) : 0.0
float bearImpulseScore =
not na(supplyTop) ?
12.0 * math.min(supplyImpulse / impulseAtrMin, 1.0) : 0.0
float bullQualityScore =
not na(demandTop) ? bullFreshness + bullImpulseScore : 0.0
float bearQualityScore =
not na(supplyTop) ? bearFreshness + bearImpulseScore : 0.0
//====================================================
// 6. FINAL SCORES
//====================================================
float buyScore = math.min(
bullTrendScore +
bullStructureScore +
bullZoneScore +
bullQualityScore, 100.0)
float sellScore = math.min(
bearTrendScore +
bearStructureScore +
bearZoneScore +
bearQualityScore, 100.0)
bool buySignal =
barstate.isconfirmed and
marketBias == 1 and
inDemand and
demandTouches <= maxTouches and
buyScore >= signalThreshold and
not inDemand[1]
bool sellSignal =
barstate.isconfirmed and
marketBias == -1 and
inSupply and
supplyTouches <= maxTouches and
sellScore >= signalThreshold and
not inSupply[1]
//====================================================
// 7. MAIN CHART VISUALS
//====================================================
plot(
showStructure ? lastSwingHigh : na,
"Last Swing High",
color=color.new(color.red, 35),
linewidth=1,
style=plot.style_linebr,
force_overlay=true)
plot(
showStructure ? lastSwingLow : na,
"Last Swing Low",
color=color.new(color.green, 35),
linewidth=1,
style=plot.style_linebr,
force_overlay=true)
if showSignals and buySignal
label.new(
bar_index, low,
"GOLD MASTER\nBUY\n" + str.tostring(buyScore, "#") + "/100",
style=label.style_label_up,
color=color.green,
textcolor=color.white,
size=size.small,
force_overlay=true)
if showSignals and sellSignal
label.new(
bar_index, high,
"GOLD MASTER\nSELL\n" + str.tostring(sellScore, "#") + "/100",
style=label.style_label_down,
color=color.red,
textcolor=color.white,
size=size.small,
force_overlay=true)
//====================================================
// 8. SCORING PANE
//====================================================
plot(buyScore, "BUY Score", color=color.lime, linewidth=2)
plot(sellScore, "SELL Score", color=color.red, linewidth=2)
hline(75, "High Quality", color=color.new(color.green, 20), linestyle=hline.style_solid)
hline(60, "Setup Threshold", color=color.new(color.orange, 20), linestyle=hline.style_dashed)
hline(40, "Weak Setup", color=color.new(color.gray, 50), linestyle=hline.style_dotted)
plot(volatilityRatio, "ATR Fast / Slow", color=color.blue, display=display.data_window)
//====================================================
// 9. DASHBOARD
//====================================================
var table dashboard = table.new(position.top_right, 2, 6, border_width=1)
if barstate.islast and showDashboard
table.cell(dashboard, 0, 0, "GOLD MASTER", bgcolor=color.rgb(30, 30, 30), text_color=color.white)
table.cell(dashboard, 1, 0, "V1.0", bgcolor=color.rgb(30, 30, 30), text_color=color.white)
table.cell(dashboard, 0, 1, "HTF TREND", text_color=color.white)
table.cell(dashboard, 1, 1,
str.tostring(bullTrendCount) + "/3 BULL",
bgcolor=bullTrendCount >= 2 ? color.green : color.gray,
text_color=color.white)
table.cell(dashboard, 0, 2, "STRUCTURE", text_color=color.white)
table.cell(dashboard, 1, 2,
marketBias == 1 ? "BULLISH" : marketBias == -1 ? "BEARISH" : "NEUTRAL",
text_color=color.white)
table.cell(dashboard, 0, 3, "ATR RATIO", text_color=color.white)
table.cell(dashboard, 1, 3, str.tostring(volatilityRatio, "#.##"), text_color=color.white)
table.cell(dashboard, 0, 4, "BUY / SELL", text_color=color.white)
table.cell(dashboard, 1, 4,
str.tostring(buyScore, "#") + " / " + str.tostring(sellScore, "#"),
text_color=color.white)
table.cell(dashboard, 0, 5, "DECISION", text_color=color.white)
table.cell(dashboard, 1, 5,
buySignal ? "BUY" : sellSignal ? "SELL" : "WAIT",
bgcolor=buySignal ? color.green : sellSignal ? color.red : color.gray,
text_color=color.white)
//--------------------------------------------------
// FOND DYNAMIQUE SELON LE SCORE DOMINANT
//--------------------------------------------------
showScoreBackground = input.bool(true, "Fond selon score dominant")
bgTransparency = input.int(88, "Transparence du fond", minval=0, maxval=100)
dominantColor = buyScore > sellScore ? color.new(color.green, bgTransparency) :
sellScore > buyScore ? color.new(color.red, bgTransparency) : na
bgcolor(showScoreBackground ? dominantColor : na, title="Score dominant", force_overlay=true)
//====================================================
// 10. ALERTS
//====================================================
alertcondition(buySignal, "GOLD MASTER BUY", "GOLD MASTER: BUY signal confirmed.")
alertcondition(sellSignal, "GOLD MASTER SELL", "GOLD MASTER: SELL signal confirmed.")