本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// CARTOON COYOTE - combined - Pine v6
//
// Zones AND the slope pane in one script. The script is declared overlay = false so it gets
// its own pane for the histogram, and every zone drawing is pushed up to the price chart with
// force_overlay = true. NinjaTrader has no equivalent - IsOverlay is read once at SetDefaults
// and never revisited - which is why those had to be two separate indicators over there.
//
// One set of settings now drives both. Period, lookback and the two percentiles are shared,
// so the pane cannot drift out of sync with the zones the way two scripts could.
//
// WHICH SLOPE DRIVES WHAT
//
// Zones -> always the SLOW period. That is the NT8 behaviour and it is what the zone logic
// was tuned on.
// Pane -> whichever the "Pane shows" toggle is set to. Fast is a read on the same market
// at a shorter horizon; it does not feed the zones.
//
// Each slope gets its own bands, so switching the toggle switches the band lines with it.
//
// HOW A ZONE FORMS
//
// 1. An EVENT starts when |slope| pushes past the inner band, and tracks the running high
// (slope up) or low (slope down) while it lasts.
// 2. The zone PRINTS - yellow circle - when |slope| reached the outer band, has been back
// inside it for N bars, and price has bounced at least X ATR off the extreme.
// 3. The zone CONFIRMS - blue circle - when |slope| falls back inside the inner band.
// 4. The zone CLOSES when price trades through its far side. Closed zones fade and stop
// extending right.
//
// WARMUP: the bands need `Band lookback bars` of history before anything can print. At the
// 2000 default that is about a week of 5 minute bars. If you see no zones at all, that is the
// first thing to check - drop the lookback or load more history.
//@version=6
indicator("CARTOON COYOTE", "COYOTE", overlay = false, max_boxes_count = 500, max_lines_count = 500, max_bars_back = 500)
// ─────────────────────────────────────────────────────────────────────────────
// Inputs
// ─────────────────────────────────────────────────────────────────────────────
var string gS = "Slope"
fastLen = input.int (42, "Fast period", minval = 2, group = gS)
slowLen = input.int (42, "Slow period (zones)",minval = 2, group = gS)
paneShows = input.string("Slow","Pane shows", options = ["Fast", "Slow"], group = gS)
lookback = input.int (2000, "Band lookback bars", minval = 200, group = gS)
outerPct = input.float (90, "Outer percentile", minval = 50, maxval = 100, group = gS)
innerPct = input.float (65, "Inner percentile", minval = 1, maxval = 99, group = gS)
var string gE = "Event"
barsBack = input.int (2, "Bars back inside band", minval = 1, group = gE)
recoveryA = input.float(3.0, "Recovery required (ATR)", minval = 0, step = 0.1, group = gE)
atrLen = input.int (100, "ATR period", minval = 1, group = gE)
var string gZ = "Zones"
priorBars = input.int (1, "Prior candles in zone", minval = 0, maxval = 10, group = gZ)
mergeBars = input.bool (false, "Merge candles into one box", group = gZ)
boundMode = input.string("Wicks", "Zone bounds", options = ["Wicks", "Bodies"], group = gZ)
var string gV = "Zone visuals"
supCol = input.color(color.lime, "Support", group = gV)
resCol = input.color(color.red, "Resistance", group = gV)
priorCol = input.color(color.yellow, "Prior candles", group = gV)
showMid = input.bool (true, "Show 50% line (merged zones only)", group = gV)
showMarks = input.bool (true, "Show print / confirm markers", group = gV)
var string gP = "Pane"
emaLen = input.int (9, "EMA over histogram", minval = 0, group = gP)
normCol = input.color(#DAA520, "Normal", group = gP)
extCol = input.color(#9370DB, "Extreme", group = gP)
// Fill transparency: the NT8 opacities (60 open, 10 closed) inverted for Pine.
TRANSP_OPEN = 40
TRANSP_CLOSED = 90
MAX_ZONES = 50
MAX_BACK = 480 // keep dynamic history reads inside max_bars_back
PRINT_COL = #FFD700 // yellow - zone first drawn
CONFIRM_COL = #1E90FF // blue - zone confirmed
useBodies = boundMode == "Bodies"
useFast = paneShows == "Fast"
// ─────────────────────────────────────────────────────────────────────────────
// Zone record
// ─────────────────────────────────────────────────────────────────────────────
type Zone
box main = na
box prior = na
line mid = na
line wickLn = na
bool isSup = false
float top = na
float bot = na
float yTop = na
float yBot = na
float wick = na
bool closed = false
int exBar = 0
var array<Zone> zones = array.new<Zone>()
// ─────────────────────────────────────────────────────────────────────────────
// Slope and bands
//
// ta.linreg returns the regression VALUE; the difference of two consecutive offsets is the
// slope per bar, which is what NT8's LinRegSlope reports. Both periods are computed every
// bar - ta.* functions are stateful and must be called unconditionally.
// ─────────────────────────────────────────────────────────────────────────────
slopeFast = ta.linreg(close, fastLen, 0) - ta.linreg(close, fastLen, 1)
slopeSlow = ta.linreg(close, slowLen, 0) - ta.linreg(close, slowLen, 1)
outerFast = ta.percentile_linear_interpolation(math.abs(slopeFast), lookback, outerPct)
innerFast = ta.percentile_linear_interpolation(math.abs(slopeFast), lookback, innerPct)
outerSlow = ta.percentile_linear_interpolation(math.abs(slopeSlow), lookback, outerPct)
innerSlow = ta.percentile_linear_interpolation(math.abs(slopeSlow), lookback, innerPct)
// The zones run on the slow slope, always.
slope = slopeSlow
absSlope = math.abs(slopeSlow)
outerBand = outerSlow
innerBand = innerSlow
// The pane follows the toggle.
paneSlope = useFast ? slopeFast : slopeSlow
paneOuter = useFast ? outerFast : outerSlow
paneInner = useFast ? innerFast : innerSlow
atrVal = ta.atr(atrLen)
// ─────────────────────────────────────────────────────────────────────────────
// Geometry helpers
//
// These only READ globals and MUTATE the object handed in - Pine forbids a function
// assigning to a global, which is why the state machine below is written inline.
// ─────────────────────────────────────────────────────────────────────────────
seriesTop(int back) =>
useBodies ? math.max(open[back], close[back]) : high[back]
seriesBot(int back) =>
useBodies ? math.min(open[back], close[back]) : low[back]
// Rebuild a zone's geometry from the extreme bar. Called when the zone first prints, again
// whenever a new extreme replaces the old one, and once more at confirmation.
reshape(Zone z, int anchorBar) =>
back = bar_index - anchorBar
if back >= 0 and back <= MAX_BACK and back + priorBars <= bar_index
exTop = seriesTop(back)
exBot = seriesBot(back)
z.exBar := anchorBar
prTop = -1e15
prBot = 1e15
// Raw wick extremes across every candle in the zone, tracked regardless of bounds
// mode - in Bodies mode they are what the body box leaves out.
rawTop = high[back]
rawBot = low[back]
if priorBars > 0
for i = 1 to priorBars
prTop := math.max(prTop, seriesTop(back + i))
prBot := math.min(prBot, seriesBot(back + i))
rawTop := math.max(rawTop, high[back + i])
rawBot := math.min(rawBot, low[back + i])
z.wick := z.isSup ? rawBot : rawTop
if priorBars < 1
z.top := exTop
z.bot := exBot
z.yTop := na
z.yBot := na
else if mergeBars
z.top := math.max(exTop, prTop)
z.bot := math.min(exBot, prBot)
z.yTop := na
z.yBot := na
else
z.top := exTop
z.bot := exBot
z.yTop := prTop
z.yBot := prBot
zcol = z.isSup ? supCol : resCol
left = anchorBar - priorBars
right = bar_index
// force_overlay sends these to the PRICE chart even though the script lives in a pane.
if na(z.main)
z.main := box.new(left, z.top, right, z.bot, border_color = zcol,
bgcolor = color.new(zcol, TRANSP_OPEN), xloc = xloc.bar_index,
force_overlay = true)
else
box.set_lefttop (z.main, left, z.top)
box.set_rightbottom(z.main, right, z.bot)
if not na(z.yTop)
if na(z.prior)
z.prior := box.new(left, z.yTop, right, z.yBot, border_color = priorCol,
bgcolor = color.new(priorCol, TRANSP_OPEN),
xloc = xloc.bar_index, force_overlay = true)
else
box.set_lefttop (z.prior, left, z.yTop)
box.set_rightbottom(z.prior, right, z.yBot)
if mergeBars and showMid
m = (z.top + z.bot) / 2
if na(z.mid)
z.mid := line.new(left, m, right, m, xloc = xloc.bar_index,
color = color.white, width = 1, force_overlay = true)
else
line.set_xy1(z.mid, left, m)
line.set_xy2(z.mid, right, m)
// Bodies mode only: dashed line at the wick extreme the body box cuts off - the low
// for a support zone, the high for a resistance zone.
if useBodies
if na(z.wickLn)
z.wickLn := line.new(left, z.wick, right, z.wick, xloc = xloc.bar_index,
color = zcol, style = line.style_dashed, width = 1,
force_overlay = true)
else
line.set_xy1(z.wickLn, left, z.wick)
line.set_xy2(z.wickLn, right, z.wick)
0
// ─────────────────────────────────────────────────────────────────────────────
// Event state - inlined, because Pine functions cannot assign to globals
// ─────────────────────────────────────────────────────────────────────────────
var bool inEvent = false
var int eventDir = 0
var bool qualified = false
var bool drawn = false
var int insideOut = 0
var int insideIn = 0
var int exBar = 0
var float exPrice = na
var float maxRecov = 0.0
var Zone prov = na
// Marker flags. These fire on the bar the thing actually happened, which is the point of
// them - the box's left edge is back at the extreme candle, so without these there is
// nothing on the chart saying when the zone appeared.
printedSup = false
printedRes = false
confirmSup = false
confirmRes = false
bandsReady = not na(outerBand) and not na(innerBand) and outerBand > 0 and innerBand > 0
ready = bandsReady and bar_index > math.max(slowLen, atrLen) + 2
if ready
dir = slope < 0 ? -1 : 1
restart = false
if not inEvent
restart := absSlope > innerBand
else if dir != eventDir and absSlope > innerBand
// Flip: confirm whatever is on the chart, then open a fresh event the other way.
if drawn and not na(prov)
reshape(prov, exBar)
if prov.isSup
confirmSup := true
else
confirmRes := true
else if not na(prov)
box.delete(prov.main)
box.delete(prov.prior)
line.delete(prov.mid)
line.delete(prov.wickLn)
if array.size(zones) > 0
array.pop(zones)
inEvent := false
prov := na
restart := true
if restart
inEvent := true
eventDir := dir
qualified := false
drawn := false
insideOut := 0
insideIn := 0
prov := na
exBar := bar_index
exPrice := dir < 0 ? low : high
maxRecov := 0.0
else if inEvent
// Track the running extreme; a new one restarts the recovery clock and reshapes the
// box if one is already on the chart.
newExtreme = eventDir < 0 ? low < exPrice : high > exPrice
if newExtreme
exPrice := eventDir < 0 ? low : high
exBar := bar_index
maxRecov := 0.0
if drawn and not na(prov)
reshape(prov, exBar)
// Recovery is the BEST bounce since the extreme, not the bounce showing on the bar
// where the slope conditions happen to line up.
rec = eventDir < 0 ? close - exPrice : exPrice - close
if rec > maxRecov
maxRecov := rec
if absSlope >= outerBand
qualified := true
insideOut := qualified and absSlope < outerBand ? insideOut + 1 : 0
insideIn := absSlope < innerBand ? insideIn + 1 : 0
recovered = atrVal > 0 and maxRecov >= recoveryA * atrVal
if not drawn and qualified and insideOut >= barsBack and recovered
z = Zone.new()
z.isSup := eventDir < 0
reshape(z, exBar)
if not na(z.main)
array.push(zones, z)
prov := z
drawn := true
if z.isSup
printedSup := true
else
printedRes := true
while array.size(zones) > MAX_ZONES
old = array.shift(zones)
box.delete(old.main)
box.delete(old.prior)
line.delete(old.mid)
line.delete(old.wickLn)
if insideIn >= barsBack
if drawn and not na(prov)
reshape(prov, exBar)
if prov.isSup
confirmSup := true
else
confirmRes := true
else if not na(prov)
box.delete(prov.main)
box.delete(prov.prior)
line.delete(prov.mid)
line.delete(prov.wickLn)
if array.size(zones) > 0
array.pop(zones)
inEvent := false
prov := na
// ─────────────────────────────────────────────────────────────────────────────
// Close test and right-edge extension
// ─────────────────────────────────────────────────────────────────────────────
if array.size(zones) > 0
for i = 0 to array.size(zones) - 1
z = array.get(zones, i)
if not z.closed and not na(z.main)
far = z.isSup ? math.min(z.bot, na(z.yBot) ? z.bot : z.yBot)
: math.max(z.top, na(z.yTop) ? z.top : z.yTop)
through = z.isSup ? low < far : high > far
if through and bar_index > z.exBar + 1
z.closed := true
zcol = z.isSup ? supCol : resCol
box.set_bgcolor (z.main, color.new(zcol, TRANSP_CLOSED))
box.set_border_color(z.main, color.new(zcol, 60))
if not na(z.prior)
box.set_bgcolor (z.prior, color.new(priorCol, TRANSP_CLOSED))
box.set_border_color(z.prior, color.new(priorCol, 60))
if not na(z.wickLn)
line.set_color(z.wickLn, color.new(zcol, 60))
else
box.set_right(z.main, bar_index)
if not na(z.prior)
box.set_right(z.prior, bar_index)
if not na(z.mid)
line.set_x2(z.mid, bar_index)
if not na(z.wickLn)
line.set_x2(z.wickLn, bar_index)
// ─────────────────────────────────────────────────────────────────────────────
// Markers - on the PRICE chart, via force_overlay
//
// Yellow = the zone was first drawn on this bar.
// Blue = the zone confirmed on this bar.
//
// Circles, not arrows - an arrow reads as an entry signal and neither of these is one.
// ─────────────────────────────────────────────────────────────────────────────
plotshape(showMarks and printedSup, "Support print", shape.circle, location.belowbar,
color = PRINT_COL, size = size.small, force_overlay = true)
plotshape(showMarks and printedRes, "Resistance print", shape.circle, location.abovebar,
color = PRINT_COL, size = size.small, force_overlay = true)
plotshape(showMarks and confirmSup, "Support confirm", shape.circle, location.belowbar,
color = CONFIRM_COL, size = size.small, force_overlay = true)
plotshape(showMarks and confirmRes, "Resistance confirm", shape.circle, location.abovebar,
color = CONFIRM_COL, size = size.small, force_overlay = true)
// ─────────────────────────────────────────────────────────────────────────────
// The pane - raw slope, its own scale
// ─────────────────────────────────────────────────────────────────────────────
// ta.ema is stateful - call it unconditionally and gate the PLOT, never the call.
emaFast = ta.ema(slopeFast, math.max(emaLen, 2))
emaSlow = ta.ema(slopeSlow, math.max(emaLen, 2))
emaVal = useFast ? emaFast : emaSlow
isExtreme = not na(paneOuter) and paneOuter > 0 and math.abs(paneSlope) >= paneOuter
plot(paneSlope, "Slope", style = plot.style_columns, color = isExtreme ? extCol : normCol)
plot(emaLen > 1 ? emaVal : na, "Slope EMA", color = color.gray, linewidth = 1)
plot( paneOuter, "Outer +", color = color.new(extCol, 30), linewidth = 1)
plot(-paneOuter, "Outer -", color = color.new(extCol, 30), linewidth = 1)
plot( paneInner, "Inner +", color = color.new(color.gray, 40), linewidth = 1)
plot(-paneInner, "Inner -", color = color.new(color.gray, 40), linewidth = 1)
hline(0, "Zero", color = color.new(color.gray, 20))
// ─────────────────────────────────────────────────────────────────────────────
// Alerts
// ─────────────────────────────────────────────────────────────────────────────
alertcondition(printedSup, "Coyote support zone", "COYOTE support zone printed")
alertcondition(printedRes, "Coyote resistance zone", "COYOTE resistance zone printed")
alertcondition(isExtreme and not isExtreme[1], "Slope extreme", "COYOTE slope reached the outer band")