本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("ANAX | Shark & Kill Zones MTF v4", "ANAX S/K MTF v4", overlay=true, max_boxes_count=150, max_labels_count=150, dynamic_requests=true)
// Derived from the supplied ANAX Heikin-Ashi Execution Engine.
// Only active high-volume OBs (Sharks) and same-direction OB/FVG overlaps (Kill Zones).
// All detection runs separately inside each requested Heikin-Ashi timeframe.
grp = "Detection (original ANAX defaults)"
swingLen = input.int(5, "Swing pivot length", minval=2, maxval=50, group=grp)
obSearchBack = input.int(80, "Opposite candle search", minval=2, maxval=250, group=grp)
volMultiplier = input.float(1.5, "Shark volume multiplier", minval=1.0, step=0.1, group=grp)
obMode = input.string("Body", "OB range", options=["Body", "Full"], group=grp)
mitiMethod = input.string("Wick", "OB mitigation", options=["Wick", "Close", "Avg"], group=grp)
useEmaFilter = input.bool(false, "EMA 200 filter", group=grp)
maxStored = input.int(20, "Max active OB/FVG per direction", minval=3, maxval=40, group=grp)
grpVis = "Display"
showShark = input.bool(true, "Show Shark zones", group=grpVis)
showKZ = input.bool(true, "Show Kill Zones", group=grpVis)
showBull = input.bool(true, "Bullish zones", group=grpVis)
showBear = input.bool(true, "Bearish zones", group=grpVis)
sharkBullColor = input.color(color.new(#00E676, 76), "Bull Shark", group=grpVis)
sharkBearColor = input.color(color.new(#FF1744, 76), "Bear Shark", group=grpVis)
killBullColor = input.color(color.new(#9C27B0, 60), "Bull Kill Zone", group=grpVis)
killBearColor = input.color(color.new(#7B1FA2, 70), "Bear Kill Zone", group=grpVis)
labelSizeOpt = input.string("Small", "TF label size", options=["Tiny", "Small", "Normal", "Large"], group=grpVis)
zoneForwardBars = input.int(80, "Label offset (chart bars)", minval=1, maxval=450, group=grpVis)
// Set any timeframe OFF without changing the others.
grpTF = "Timeframes"
tf1 = input.bool(true, "1m", group=grpTF, inline="a")
tf3 = input.bool(true, "3m", group=grpTF, inline="a")
tf5 = input.bool(true, "5m", group=grpTF, inline="a")
tf15 = input.bool(true, "15m", group=grpTF, inline="b")
tf30 = input.bool(true, "30m", group=grpTF, inline="b")
tf60 = input.bool(true, "1H", group=grpTF, inline="b")
tf120 = input.bool(true, "2H", group=grpTF, inline="c")
tf180 = input.bool(true, "3H", group=grpTF, inline="c")
tf240 = input.bool(true, "4H", group=grpTF, inline="c")
tfD = input.bool(true, "1D", group=grpTF, inline="d")
tf3D = input.bool(true, "3D", group=grpTF, inline="d")
tfW = input.bool(true, "1W", group=grpTF, inline="d")
tfM = input.bool(true, "1M", group=grpTF, inline="e")
type Zone
bool bull
float top
float bottom
int born
bool highVol
type Gap
bool bull
float top
float bottom
int born
type ZonePack
float sharkBullTop = na
float sharkBullBottom = na
int sharkBullBorn = na
float sharkBearTop = na
float sharkBearBottom = na
int sharkBearBorn = na
float killBullTop = na
float killBullBottom = na
int killBullBorn = na
float killBearTop = na
float killBearBottom = na
int killBearBorn = na
f_engine() =>
var array<Zone> bullOBs = array.new<Zone>()
var array<Zone> bearOBs = array.new<Zone>()
var array<Gap> bullGaps = array.new<Gap>()
var array<Gap> bearGaps = array.new<Gap>()
var float lastHigh = na
var float lastLow = na
var int trend = 0
float vavg = ta.sma(volume, 50)
float ema200 = ta.ema(close, 200)
float ph = ta.pivothigh(high, swingLen, swingLen)
float pl = ta.pivotlow(low, swingLen, swingLen)
if not na(ph)
lastHigh := ph
if not na(pl)
lastLow := pl
// Mitigate older zones first; a freshly formed zone is not immediately killed.
if array.size(bullOBs) > 0
for i = array.size(bullOBs) - 1 to 0
Zone z = array.get(bullOBs, i)
bool hit = mitiMethod == "Wick" ? low < z.bottom : mitiMethod == "Close" ? math.min(open, close) < z.bottom : low < math.avg(z.top, z.bottom)
if hit
array.remove(bullOBs, i)
if array.size(bearOBs) > 0
for i = array.size(bearOBs) - 1 to 0
Zone z = array.get(bearOBs, i)
bool hit = mitiMethod == "Wick" ? high > z.top : mitiMethod == "Close" ? math.max(open, close) > z.top : high > math.avg(z.top, z.bottom)
if hit
array.remove(bearOBs, i)
if array.size(bullGaps) > 0
for i = array.size(bullGaps) - 1 to 0
if low < array.get(bullGaps, i).bottom
array.remove(bullGaps, i)
if array.size(bearGaps) > 0
for i = array.size(bearGaps) - 1 to 0
if high > array.get(bearGaps, i).top
array.remove(bearGaps, i)
if bar_index >= 2
if low > high[2]
array.unshift(bullGaps, Gap.new(true, low, high[2], time[2]))
if high < low[2]
array.unshift(bearGaps, Gap.new(false, low[2], high, time[2]))
bool bullBreak = not na(lastHigh) and close > lastHigh
bool bearBreak = not na(lastLow) and close < lastLow
if bullBreak or bearBreak
bool isBull = bullBreak and not bearBreak ? true : bearBreak and not bullBreak ? false : trend != 1
bool passTrend = not useEmaFilter or (isBull ? close > ema200 : close < ema200)
if passTrend
int found = na
int back = math.min(obSearchBack, bar_index - 1)
if back >= 1
for i = 1 to back
if isBull ? close[i] < open[i] : close[i] > open[i]
found := i
break
if not na(found)
float ztop = obMode == "Full" ? high[found] : math.max(open[found], close[found])
float zbottom = obMode == "Full" ? low[found] : math.min(open[found], close[found])
bool highVol = not na(vavg[found]) and volume[found] > vavg[found] * volMultiplier
Zone z = Zone.new(isBull, ztop, zbottom, time[found], highVol)
if isBull
array.unshift(bullOBs, z)
else
array.unshift(bearOBs, z)
trend := isBull ? 1 : -1
// Consume crossed pivot to avoid repeatedly generating the same OB.
if isBull
lastHigh := na
else
lastLow := na
while array.size(bullOBs) > maxStored
array.pop(bullOBs)
while array.size(bearOBs) > maxStored
array.pop(bearOBs)
while array.size(bullGaps) > maxStored
array.pop(bullGaps)
while array.size(bearGaps) > maxStored
array.pop(bearGaps)
ZonePack p = ZonePack.new()
// Find most recently formed surviving Shark of each direction.
if array.size(bullOBs) > 0
for i = 0 to array.size(bullOBs) - 1
Zone ob = array.get(bullOBs, i)
if ob.highVol and na(p.sharkBullBorn)
p.sharkBullTop := ob.top
p.sharkBullBottom := ob.bottom
p.sharkBullBorn := ob.born
if array.size(bullGaps) > 0
for j = 0 to array.size(bullGaps) - 1
Gap g = array.get(bullGaps, j)
float bot = math.max(ob.bottom, g.bottom)
float top = math.min(ob.top, g.top)
if bot < top and (na(p.killBullBorn) or math.max(ob.born, g.born) > p.killBullBorn)
p.killBullTop := top
p.killBullBottom := bot
p.killBullBorn := math.max(ob.born, g.born)
if array.size(bearOBs) > 0
for i = 0 to array.size(bearOBs) - 1
Zone ob = array.get(bearOBs, i)
if ob.highVol and na(p.sharkBearBorn)
p.sharkBearTop := ob.top
p.sharkBearBottom := ob.bottom
p.sharkBearBorn := ob.born
if array.size(bearGaps) > 0
for j = 0 to array.size(bearGaps) - 1
Gap g = array.get(bearGaps, j)
float bot = math.max(ob.bottom, g.bottom)
float top = math.min(ob.top, g.top)
if bot < top and (na(p.killBearBorn) or math.max(ob.born, g.born) > p.killBearBorn)
p.killBearTop := top
p.killBearBottom := bot
p.killBearBorn := math.max(ob.born, g.born)
p
f_draw(array<box> boxes, array<label> labels, float top, float bot, int born, string caption, color col, int labelBar, labelTextSize) =>
if not na(top) and not na(bot) and not na(born) and top > bot
box b = box.new(left=born, right=time, top=top, bottom=bot, xloc=xloc.bar_time, bgcolor=col, border_color=color.new(col, 0), border_width=1, extend=extend.right)
array.push(boxes, b)
// Anchor to future CHART BARS, not calendar timestamps. This puts labels to the right of candles even across market holidays and non-trading sessions.
label lb = label.new(x=labelBar, y=bot, xloc=xloc.bar_index, text=caption, style=label.style_label_left, textcolor=color.white, color=color.new(col, 10), size=labelTextSize, tooltip=caption)
array.push(labels, lb)
string[] tfs = array.from("1", "3", "5", "15", "30", "60", "120", "180", "240", "D", "3D", "W", "M")
string[] tfNames = array.from("1m", "3m", "5m", "15m", "30m", "1H", "2H", "3H", "4H", "1D", "3D", "1W", "1M")
bool[] enabled = array.from(tf1, tf3, tf5, tf15, tf30, tf60, tf120, tf180, tf240, tfD, tf3D, tfW, tfM)
var array<box> drawnBoxes = array.new<box>()
var array<label> drawnLabels = array.new<label>()
string haTicker = ticker.heikinashi(syminfo.tickerid)
int labelBar = bar_index + zoneForwardBars
labelTextSize = labelSizeOpt == "Tiny" ? size.tiny : labelSizeOpt == "Small" ? size.small : labelSizeOpt == "Normal" ? size.normal : size.large
if barstate.islast
for b in drawnBoxes
box.delete(b)
for l in drawnLabels
label.delete(l)
array.clear(drawnBoxes)
array.clear(drawnLabels)
for idx = 0 to array.size(tfs) - 1
if array.get(enabled, idx)
string tf = array.get(tfs, idx)
ZonePack p = request.security(haTicker, tf, f_engine(), gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_off, calc_bars_count=3000)
if barstate.islast
string nm = array.get(tfNames, idx)
if showShark and showBull
f_draw(drawnBoxes, drawnLabels, p.sharkBullTop, p.sharkBullBottom, p.sharkBullBorn, "SHARK BULL | " + nm, sharkBullColor, labelBar, labelTextSize)
if showShark and showBear
f_draw(drawnBoxes, drawnLabels, p.sharkBearTop, p.sharkBearBottom, p.sharkBearBorn, "SHARK BEAR | " + nm, sharkBearColor, labelBar, labelTextSize)
if showKZ and showBull
f_draw(drawnBoxes, drawnLabels, p.killBullTop, p.killBullBottom, p.killBullBorn, "KILL BULL | " + nm, killBullColor, labelBar, labelTextSize)
if showKZ and showBear
f_draw(drawnBoxes, drawnLabels, p.killBearTop, p.killBearBottom, p.killBearBorn, "KILL BEAR | " + nm, killBearColor, labelBar, labelTextSize)