本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("BTM - AutoFib and Golden Zone", shorttitle="BTM AutoFib", overlay=true)
int strength = input.int(5, "Swing strength", minval=1, maxval=100, group="Swing")
string higherTf = input.timeframe("240", "Higher timeframe", group="Trend filter")
int emaLength = input.int(50, "Higher-timeframe EMA length", minval=1, group="Trend filter")
bool requireAgreement = input.bool(false, "Require higher-timeframe agreement", group="Trend filter")
string fibView = input.string("All levels", "Fibonacci display", options=["All levels", "Golden Zone only"], group="Display")
string panelView = input.string("Full", "Dashboard", options=["Full", "Compact", "Off"], group="Display")
bool showLabels = input.bool(true, "Show Fibonacci price labels", group="Display")
bool showSwing = input.bool(true, "Show dotted swing line", group="Display")
bool showGuides = input.bool(true, "Show entry, invalidation and target", group="Display")
int transparency = input.int(82, "Golden zone transparency", minval=0, maxval=100, group="Display")
const color GOLD = #F9A825
const color BUY = #29483F
const color SELL = #57363D
const color FLAT = #404A55
const color PANEL = #1F2937
const color DASH_SLATE = #26313A
const color DASH_ACCENT = #627382
priceText(float value) =>
str.tostring(value, format.mintick)
bool validTf = timeframe.in_seconds(higherTf) > timeframe.in_seconds()
[htfClose, htfEma] = request.security(
syminfo.tickerid, validTf ? higherTf : timeframe.period,
[close[1], ta.ema(close, emaLength)[1]], lookahead=barmerge.lookahead_on
)
int htfDirection = validTf and not na(htfEma) ? (htfClose > htfEma ? 1 : htfClose < htfEma ? -1 : 0) : 0
float pivotHigh = ta.pivothigh(high, strength, strength)
float pivotLow = ta.pivotlow(low, strength, strength)
var int pivotSide = 0
var float swingStart = na
var float swingEnd = na
var int startTime = na
var int endTime = na
var float lastClose = na
var bool touched = false
var bool invalidated = false
var bool targetReached = false
var float entry = na
var int entryTime = na
var bool filterBlocked = false
bool firstTouch = false
bool buySignal = false
bool sellSignal = false
bool invalidationSignal = false
if barstate.isconfirmed
lastClose := close
int side = 0
if not na(pivotHigh) and not na(pivotLow)
side := -pivotSide
else if not na(pivotHigh)
side := 1
else if not na(pivotLow)
side := -1
bool swingChanged = false
if side != 0
float pivotPrice = side == 1 ? pivotHigh : pivotLow
if side != pivotSide
swingStart := swingEnd
startTime := endTime
swingEnd := pivotPrice
endTime := time[strength]
pivotSide := side
swingChanged := true
else if (side == 1 and pivotPrice > swingEnd) or (side == -1 and pivotPrice < swingEnd)
swingEnd := pivotPrice
endTime := time[strength]
swingChanged := true
if swingChanged
touched := false
invalidated := false
targetReached := false
entry := na
entryTime := na
filterBlocked := false
if not na(swingStart) and swingStart != swingEnd and not invalidated and not targetReached
int direction = swingEnd > swingStart ? 1 : -1
float boundary618 = swingEnd + (swingStart - swingEnd) * 0.618
float boundary65 = swingEnd + (swingStart - swingEnd) * 0.65
float zoneLow = math.min(boundary618, boundary65)
float zoneHigh = math.max(boundary618, boundary65)
bool breaksOrigin = direction == 1 ? close <= swingStart : close >= swingStart
bool hitsTarget = direction == 1 ? close >= swingEnd : close <= swingEnd
bool overlapsZone = low <= zoneHigh and high >= zoneLow
bool rejectsZone = direction == 1 ? close > zoneHigh : close < zoneLow
if breaksOrigin
invalidated := true
invalidationSignal := true
else if not na(entry)
targetReached := hitsTarget
else
firstTouch := overlapsZone and not touched
touched := touched or overlapsZone
filterBlocked := false
if touched and rejectsZone and not hitsTarget
if not requireAgreement or htfDirection == direction
entry := close
entryTime := time
buySignal := direction == 1
sellSignal := direction == -1
else
filterBlocked := true
else if touched and hitsTarget
targetReached := true
alertcondition(firstTouch, "First golden zone touch", "{{ticker}} {{interval}} touched the golden zone.")
alertcondition(buySignal, "Bullish rejection confirmed", "{{ticker}} {{interval}} bullish golden zone rejection.")
alertcondition(sellSignal, "Bearish rejection confirmed", "{{ticker}} {{interval}} bearish golden zone rejection.")
alertcondition(invalidationSignal, "Swing invalidated", "{{ticker}} {{interval}} closed at or beyond the swing origin.")
var array<float> ratios = array.from(0.0, 0.236, 0.382, 0.5, 0.618, 0.65, 0.786, 1.0)
var array<color> levelColors = array.from(#78909C, #42A5F5, #AB47BC, #26A69A, GOLD, GOLD, #EC407A, #546E7A)
var array<line> fibLines = array.new<line>()
var array<label> fibLabels = array.new<label>()
var array<line> guides = array.new<line>()
var array<label> guideLabels = array.new<label>()
var line swingLine = na
var box zoneBox = na
var table dashboard = table.new(
position.top_right, 2, 9,
frame_color=DASH_ACCENT, frame_width=3,
border_color=DASH_ACCENT, border_width=1
)
drawRow(int row, string heading, string value, color background) =>
table.cell(dashboard, 0, row, heading, bgcolor=DASH_SLATE, text_color=#D8E0E6, text_size=size.small)
table.cell(dashboard, 1, row, value, bgcolor=background, text_color=#EEF2F5, text_size=size.small)
if barstate.islast
// Rebuild the drawings for the current swing.
if not na(zoneBox)
box.delete(zoneBox)
zoneBox := na
for drawing in fibLines
line.delete(drawing)
array.clear(fibLines)
for caption in fibLabels
label.delete(caption)
array.clear(fibLabels)
for drawing in guides
line.delete(drawing)
array.clear(guides)
for caption in guideLabels
label.delete(caption)
array.clear(guideLabels)
line.delete(swingLine)
swingLine := na
bool hasSwing = not na(swingStart) and swingStart != swingEnd
bool bullish = hasSwing and swingEnd > swingStart
string directionText = hasSwing ? (bullish ? "Bullish" : "Bearish") : "Flat"
color directionColor = hasSwing ? (bullish ? BUY : SELL) : FLAT
string htfText = not validTf ? "Choose a higher timeframe" : na(htfEma) ? "Waiting for EMA" : htfDirection == 1 ? "Bullish" : htfDirection == -1 ? "Bearish" : "Flat"
color htfColor = htfDirection == 1 ? BUY : htfDirection == -1 ? SELL : FLAT
string setup = "Waiting for swing"
color setupColor = FLAT
string pullback = "—"
string zonePrices = "—"
string anchors = "—"
string tradePrices = "—"
string rewardRisk = "—"
if hasSwing
float swingRange = swingStart - swingEnd
float boundary618 = swingEnd + swingRange * 0.618
float boundary65 = swingEnd + swingRange * 0.65
float zoneLow = math.min(boundary618, boundary65)
float zoneHigh = math.max(boundary618, boundary65)
float retracement = (lastClose - swingEnd) / swingRange
pullback := str.tostring(retracement * 100, "0.0") + "%"
zonePrices := priceText(zoneLow) + " – " + priceText(zoneHigh)
anchors := priceText(swingStart) + " → " + priceText(swingEnd)
if invalidated
setup := "Invalidated · Swing origin"
else if targetReached
setup := na(entry) ? "Swing endpoint revisited" : "Target reached"
else if not na(entry)
setup := bullish ? "Look to buy · Rejection confirmed" : "Look to sell · Rejection confirmed"
setupColor := directionColor
else if filterBlocked
setup := "Flat · Higher timeframe disagrees"
else if retracement > 0.65
setup := "Flat · Beyond golden zone"
else if touched
setup := lastClose >= zoneLow and lastClose <= zoneHigh ? "In zone · Awaiting rejection" : "Touched · Awaiting rejection"
else
setup := retracement < 0 ? "Waiting for pullback" : "Approaching golden zone"
for [i, ratio] in ratios
bool isBoundary = i == 4 or i == 5
if fibView == "All levels" or isBoundary
float levelPrice = swingEnd + swingRange * ratio
color levelColor = array.get(levelColors, i)
line fib = line.new(
startTime, levelPrice, time, levelPrice,
xloc=xloc.bar_time, color=levelColor, width=isBoundary ? 2 : 1
)
array.push(fibLines, fib)
if showLabels
string caption = str.tostring(ratio * 100, "0.#") + "% " + priceText(levelPrice)
array.push(fibLabels, label.new(
bar_index + 2, levelPrice, caption,
style=label.style_label_left, color=color.new(levelColor, 85),
textcolor=chart.fg_color, size=size.small
))
zoneBox := box.new(
startTime, zoneHigh, time, zoneLow,
xloc=xloc.bar_time, border_color=na,
bgcolor=color.new(GOLD, transparency),
text="Golden Zone", text_color=chart.fg_color,
text_size=size.small, text_halign=text.align_center,
text_valign=text.align_center, text_wrap=text.wrap_auto
)
if showSwing
swingLine := line.new(
startTime, swingStart, endTime, swingEnd,
xloc=xloc.bar_time, style=line.style_dotted, color=chart.fg_color, width=2
)
if not na(entry)
float rr = math.abs(swingEnd - entry) / math.abs(entry - swingStart)
rewardRisk := str.tostring(rr, "0.00") + " : 1"
tradePrices := priceText(entry) + " / " + priceText(swingStart) + " / " + priceText(swingEnd)
if showGuides and not invalidated and not targetReached
for i = 0 to 2
float guidePrice = i == 0 ? entry : i == 1 ? swingStart : swingEnd
color guideColor = i == 0 ? chart.fg_color : i == 1 ? SELL : BUY
string guideText = i == 0 ? "Entry" : i == 1 ? "Invalidation" : "Target"
// The full Fibonacci view already draws the origin and endpoint.
if i == 0 or fibView == "Golden Zone only"
array.push(guides, line.new(
entryTime, guidePrice, time, guidePrice,
xloc=xloc.bar_time, extend=extend.right,
style=line.style_dashed, color=guideColor
))
if showLabels and fibView == "All levels" and i != 0
int labelIndex = i == 1 ? 7 : 0
label.set_text(array.get(fibLabels, labelIndex), guideText + " " + priceText(guidePrice))
else
array.push(guideLabels, label.new(
bar_index + 2, guidePrice, guideText + " " + priceText(guidePrice),
style=label.style_label_left, color=PANEL, textcolor=color.white, size=size.small
))
if panelView != "Off"
drawRow(0, "BTM - AutoFib and Golden Zone", "Confirmed closes", DASH_SLATE)
drawRow(1, "Swing direction", directionText, directionColor)
drawRow(2, "Higher timeframe", htfText, htfColor)
drawRow(3, "Setup", setup, setupColor)
drawRow(4, "Retracement", pullback, DASH_SLATE)
if panelView == "Full"
drawRow(5, "Swing anchors", anchors, DASH_SLATE)
drawRow(6, "Golden zone", zonePrices, DASH_SLATE)
drawRow(7, "Entry / Invalidation / Target", tradePrices, DASH_SLATE)
drawRow(8, "Reward / Risk", rewardRisk, DASH_SLATE)