本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This work is licensed under Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) https://creativecommons.org/licenses/by-nc-sa/4.0/
// © Ordinary-Trader
// Swing detection follows the method in Smart Money Concepts (SMC) [LuxAlgo] (CC BY-NC-SA 4.0). Credit to LuxAlgo.
// ══════════════════════════════════════════════════════════════════════════════
// Zones [Ordinary Trader] v2.0.1
// See CHANGELOG.md for full version history.
// ══════════════════════════════════════════════════════════════════════════════
//@version=6
indicator("Zones [Ordinary Trader] v2.0.1", shorttitle = "Zones [Ordinary Trader]", overlay = true,
max_lines_count = 500,
max_labels_count = 500,
max_boxes_count = 500)
max_bars_back(time, 5000)
// ─────────────────────────────────────────────────────────────────────────────
// SECTION 1 — INPUTS
// ─────────────────────────────────────────────────────────────────────────────
GRP_MAIN = "────── Master Settings · v2.0.1 ──────"
i_extLen = input.int(20, "Ext. Structure", minval = 2, group = GRP_MAIN, inline = "se",
tooltip = "Sensitivity of the external swing detection. External swings define the major structural breaks that produce the bigger zones.\n\nHigher values produce fewer but more significant swings — increase to filter out noise on volatile symbols. Lower values produce more frequent swings.", display = display.data_window)
i_intLen = input.int(4, "Int. Structure", minval = 1, group = GRP_MAIN, inline = "se",
tooltip = "Sensitivity of the internal swing detection. Internal swings produce the smaller, more frequent zones.\n\nSmaller values pick up more turning points but produce more noise. Larger values lag but are more reliable.", display = display.data_window)
i_dirFilter = input.string("Both", "Direction", options = ["Both", "Bull Only", "Bear Only"], group = GRP_MAIN,
tooltip = "Controls which direction of zones are drawn.\n\nBoth: show bullish (demand) and bearish (supply) zones.\nBull Only: only bullish zones.\nBear Only: only bearish zones.", display = display.data_window)
i_lastEnabled = input.bool(true, "Last", group = GRP_MAIN, inline = "last",
tooltip = "Limit the number of zones kept visible on the chart at any one time.\n\nWhen checked (default): only the most recent N zones are shown, where N is the value to the right. Older zones are removed as new ones form, keeping the chart clean.\n\nWhen unchecked: every zone ever detected stays on the chart regardless of age. Can slow chart loading on active symbols.\n\nThe limit is shared across external and internal zones — oldest goes first regardless of type.")
i_lastN = input.int(20, "", minval = 1, maxval = 100, group = GRP_MAIN, inline = "last", display = display.data_window)
GRP_ZONES = "────── Zones ──────"
i_extZones = input.bool(true, "External", group = GRP_ZONES, inline = "ext",
tooltip = "Draw zones at major (external) market structure breaks. These tend to be larger, more significant zones — strong areas of likely price reaction.")
i_extBullClr = input.color(color.new(#81b29a, 70), "Bull", group = GRP_ZONES, inline = "ext", display = display.data_window)
i_extBearClr = input.color(color.new(#e07a5f, 70), "Bear", group = GRP_ZONES, inline = "ext", display = display.data_window)
i_intZones = input.bool(true, "Internal", group = GRP_ZONES, inline = "int",
tooltip = "Draw zones at minor (internal) market structure breaks. More frequent than external zones — useful for finer-grained analysis. Defaults to a higher transparency than external so externals stand out.")
i_intBullClr = input.color(color.new(#81b29a, 85), "Bull", group = GRP_ZONES, inline = "int", display = display.data_window)
i_intBearClr = input.color(color.new(#e07a5f, 85), "Bear", group = GRP_ZONES, inline = "int", display = display.data_window)
i_showBorder = input.bool(false, "Border", group = GRP_ZONES, inline = "bdr",
tooltip = "Draw a coloured border around each zone box. Off by default — zones use a transparent fill only.\n\nWhen on, choose the line style and bull/bear border colours to the right.")
i_borderStyle = input.string("Solid", "", options = ["Solid", "Dashed", "Dotted"], group = GRP_ZONES, inline = "bdr", display = display.data_window)
i_borderBullClr = input.color(#81b29a, "Bull", group = GRP_ZONES, inline = "bdr", display = display.data_window)
i_borderBearClr = input.color(#e07a5f, "Bear", group = GRP_ZONES, inline = "bdr", display = display.data_window)
i_showMid = input.bool(true, "Midline", group = GRP_ZONES, inline = "mid",
tooltip = "Draw a horizontal line at the midpoint of each zone. The midpoint often acts as a sub-level of support / resistance and gives you an additional reference inside the zone.\n\nLine style and bull / bear colours are configurable to the right.")
i_midStyle = input.string("Dashed", "", options = ["Solid", "Dashed", "Dotted"], group = GRP_ZONES, inline = "mid", display = display.data_window)
i_midBullClr = input.color(#81b29a, "Bull", group = GRP_ZONES, inline = "mid", display = display.data_window)
i_midBearClr = input.color(#e07a5f, "Bear", group = GRP_ZONES, inline = "mid", display = display.data_window)
// ─────────────────────────────────────────────────────────────────────────────
// SECTION 2 — HELPERS
// ─────────────────────────────────────────────────────────────────────────────
getLineStyle(string s) =>
s == "Solid" ? line.style_solid :
s == "Dashed" ? line.style_dashed :
line.style_dotted
borderStyle = getLineStyle(i_borderStyle)
bullBorder = i_showBorder ? i_borderBullClr : color.new(color.white, 100)
bearBorder = i_showBorder ? i_borderBearClr : color.new(color.white, 100)
borderWidth = i_showBorder ? 1 : 0
midStyle = getLineStyle(i_midStyle)
// ─────────────────────────────────────────────────────────────────────────────
// SECTION 3 — EXTERNAL SWING DETECTION
// ─────────────────────────────────────────────────────────────────────────────
// Swing test (high[len] > ta.highest(high, len)) follows Smart Money Concepts
// (SMC) [LuxAlgo], CC BY-NC-SA 4.0. Credit to LuxAlgo.
eUpper = ta.highest(high, i_extLen)
eLower = ta.lowest(low, i_extLen)
var int eSwDir = 0
int _eSwDirPrev = eSwDir
float eSwHiPrice = na
int eSwHiBar = na
if _eSwDirPrev >= 0 and high[i_extLen] > eUpper
eSwDir := -1
eSwHiPrice := high[i_extLen]
eSwHiBar := bar_index - i_extLen
float eSwLoPrice = na
int eSwLoBar = na
if _eSwDirPrev <= 0 and low[i_extLen] < eLower
eSwDir := 1
eSwLoPrice := low[i_extLen]
eSwLoBar := bar_index - i_extLen
bool eNewPH = not na(eSwHiPrice)
bool eNewPL = not na(eSwLoPrice)
var float eLastPH = na, var int eLastPH_bar = na
var float eLastPL = na, var int eLastPL_bar = na
var bool eBosUpChk = false, var bool eBosDownChk = false
if eNewPH
eLastPH := eSwHiPrice
eLastPH_bar := eSwHiBar
eBosUpChk := true
if eNewPL
eLastPL := eSwLoPrice
eLastPL_bar := eSwLoBar
eBosDownChk := true
// ─────────────────────────────────────────────────────────────────────────────
// SECTION 4 — INTERNAL SWING DETECTION
// ─────────────────────────────────────────────────────────────────────────────
iUpper = ta.highest(high, i_intLen)
iLower = ta.lowest(low, i_intLen)
var int iSwDir = 0
int _iSwDirPrev = iSwDir
float iSwHiPrice = na
int iSwHiBar = na
if _iSwDirPrev >= 0 and high[i_intLen] > iUpper
iSwDir := -1
iSwHiPrice := high[i_intLen]
iSwHiBar := bar_index - i_intLen
float iSwLoPrice = na
int iSwLoBar = na
if _iSwDirPrev <= 0 and low[i_intLen] < iLower
iSwDir := 1
iSwLoPrice := low[i_intLen]
iSwLoBar := bar_index - i_intLen
bool iNewPH = not na(iSwHiPrice)
bool iNewPL = not na(iSwLoPrice)
var float iLastPH = na, var int iLastPH_bar = na
var float iLastPL = na, var int iLastPL_bar = na
var bool iBosUpChk = false, var bool iBosDownChk = false
if iNewPH
iLastPH := iSwHiPrice
iLastPH_bar := iSwHiBar
iBosUpChk := true
if iNewPL
iLastPL := iSwLoPrice
iLastPL_bar := iSwLoBar
iBosDownChk := true
// ─────────────────────────────────────────────────────────────────────────────
// SECTION 5 — LAST N TRACKING
// ─────────────────────────────────────────────────────────────────────────────
// Each zone bundles its box and (optional) midline so the Last-N trim deletes
// both together. midline is na when i_showMid is off.
type Zone
box bx
line ml
var array<Zone> zoneArr = array.new<Zone>()
addZone(box _b, line _m) =>
array.push(zoneArr, Zone.new(_b, _m))
if i_lastEnabled
while array.size(zoneArr) > i_lastN
Zone _old = array.shift(zoneArr)
box.delete(_old.bx)
line.delete(_old.ml)
// ─────────────────────────────────────────────────────────────────────────────
// SECTION 6 — ZONE DETECTION + DRAWING
// ─────────────────────────────────────────────────────────────────────────────
bool extBullZone = false
bool extBearZone = false
bool intBullZone = false
bool intBearZone = false
// External zones
if i_extZones
bool _extZoneUp = eBosUpChk and not na(eLastPH) and close > eLastPH
if _extZoneUp
eBosUpChk := false
if (i_dirFilter == "Bull Only" or i_dirFilter == "Both")
float _top = eLastPH
float _bot = eSwDir == -1 ? eLower : eLastPL
if not na(_top) and not na(_bot)
box _newBox = box.new(eLastPH_bar, _top, bar_index, _bot,
border_color = bullBorder, bgcolor = i_extBullClr,
border_width = borderWidth, border_style = borderStyle)
line _newMid = na
if i_showMid
float _mid = (_top + _bot) / 2
_newMid := line.new(eLastPH_bar, _mid, bar_index, _mid,
color = i_midBullClr, style = midStyle, width = 1)
addZone(_newBox, _newMid)
extBullZone := true
bool _extZoneDown = eBosDownChk and not na(eLastPL) and close < eLastPL
if _extZoneDown
eBosDownChk := false
if (i_dirFilter == "Bear Only" or i_dirFilter == "Both")
float _top = eSwDir == 1 ? eUpper : eLastPH
float _bot = eLastPL
if not na(_top) and not na(_bot)
box _newBox = box.new(eLastPL_bar, _top, bar_index, _bot,
border_color = bearBorder, bgcolor = i_extBearClr,
border_width = borderWidth, border_style = borderStyle)
line _newMid = na
if i_showMid
float _mid = (_top + _bot) / 2
_newMid := line.new(eLastPL_bar, _mid, bar_index, _mid,
color = i_midBearClr, style = midStyle, width = 1)
addZone(_newBox, _newMid)
extBearZone := true
// Internal zones
if i_intZones
bool _intZoneUp = iBosUpChk and not na(iLastPH) and close > iLastPH
if _intZoneUp
iBosUpChk := false
if (i_dirFilter == "Bull Only" or i_dirFilter == "Both")
float _top = iLastPH
float _bot = iSwDir == -1 ? iLower : iLastPL
if not na(_top) and not na(_bot)
box _newBox = box.new(iLastPH_bar, _top, bar_index, _bot,
border_color = bullBorder, bgcolor = i_intBullClr,
border_width = borderWidth, border_style = borderStyle)
line _newMid = na
if i_showMid
float _mid = (_top + _bot) / 2
_newMid := line.new(iLastPH_bar, _mid, bar_index, _mid,
color = i_midBullClr, style = midStyle, width = 1)
addZone(_newBox, _newMid)
intBullZone := true
bool _intZoneDown = iBosDownChk and not na(iLastPL) and close < iLastPL
if _intZoneDown
iBosDownChk := false
if (i_dirFilter == "Bear Only" or i_dirFilter == "Both")
float _top = iSwDir == 1 ? iUpper : iLastPH
float _bot = iLastPL
if not na(_top) and not na(_bot)
box _newBox = box.new(iLastPL_bar, _top, bar_index, _bot,
border_color = bearBorder, bgcolor = i_intBearClr,
border_width = borderWidth, border_style = borderStyle)
line _newMid = na
if i_showMid
float _mid = (_top + _bot) / 2
_newMid := line.new(iLastPL_bar, _mid, bar_index, _mid,
color = i_midBearClr, style = midStyle, width = 1)
addZone(_newBox, _newMid)
intBearZone := true
// ─────────────────────────────────────────────────────────────────────────────
// SECTION 7 — ALERTS
// ─────────────────────────────────────────────────────────────────────────────
alertcondition(extBullZone, "External Bullish Zone", "New external bullish zone formed")
alertcondition(extBearZone, "External Bearish Zone", "New external bearish zone formed")
alertcondition(intBullZone, "Internal Bullish Zone", "New internal bullish zone formed")
alertcondition(intBearZone, "Internal Bearish Zone", "New internal bearish zone formed")
// ══════════════════════════════════════════════════════════════════════════════
// END OF SCRIPT
// ══════════════════════════════════════════════════════════════════════════════