本页源码来自 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/
// © balraj_hanmanthugari
//@version=6
indicator("Supply & Demand Zones Finder", overlay = true, max_boxes_count = 500)
// Input sections
string GROUP_PIVOT = "Pivot Detection"
string GROUP_DAYS = "Days to Show"
string GROUP_EXTEND = "Zone Extension & Cleanup"
string GROUP_SUPPLY_VIS = "Individual Supply Zones"
string GROUP_DEMAND_VIS = "Individual Demand Zones"
pivotBars = input.int(5, "Pivot confirmation bars", minval = 1, group = GROUP_PIVOT, display = display.data_window)
showToday = input.bool(true, "Show Today", group = GROUP_DAYS, display = display.data_window)
historicalDays = input.int(1, "Historical calendar days to show", minval = 1, maxval = 365, group = GROUP_DAYS, display = display.data_window)
extensionDays = input.int(1, "Extend zones for calendar days", minval = 0, maxval = 30, group = GROUP_EXTEND, display = display.data_window)
removeBroken = input.bool(true, "Remove zones when price breaks through", group = GROUP_EXTEND, display = display.data_window)
hideSupplyNumbers = input.string("", "Hide supply zones (comma-separated)", group = GROUP_SUPPLY_VIS, tooltip = "Enter zone numbers such as 1,3,5. Number 1 is the newest supply zone.", display = display.data_window)
hideDemandNumbers = input.string("", "Hide demand zones (comma-separated)", group = GROUP_DEMAND_VIS, tooltip = "Enter zone numbers such as 1,3,5. Number 1 is the newest demand zone.", display = display.data_window)
int MAX_ZONES_PER_SIDE = 250
color supplyBorder = color.rgb(210, 70, 75)
color supplyFill = color.new(supplyBorder, 86)
color demandBorder = color.rgb(0, 160, 125)
color demandFill = color.new(demandBorder, 86)
var array<box> supplyZones = array.new<box>()
var array<box> demandZones = array.new<box>()
zoneIsSelected(int zoneTime, int currentDayStart) =>
int zoneDayStart = timestamp("UTC", year(zoneTime, syminfo.timezone), month(zoneTime, syminfo.timezone), dayofmonth(zoneTime, syminfo.timezone), 0, 0)
int zoneAgeDays = int(math.floor((currentDayStart - zoneDayStart) / 86400000))
bool isToday = zoneAgeDays == 0
bool isHistorical = zoneAgeDays >= 1 and zoneAgeDays <= historicalDays
(showToday and isToday) or isHistorical
zoneSlotIsVisible(int slot, bool isSupply) =>
string hiddenNumbers = isSupply ? hideSupplyNumbers : hideDemandNumbers
array<string> numberTokens = str.split(hiddenNumbers, ",")
bool isVisible = true
for token in numberTokens
float selectedNumber = str.tonumber(str.trim(token))
if not na(selectedNumber) and selectedNumber == slot + 1
isVisible := false
isVisible
pivotHigh = ta.pivothigh(high, pivotBars, pivotBars)
pivotLow = ta.pivotlow(low, pivotBars, pivotBars)
// Require the pivot and all bars used to confirm it to belong to the same exchange day.
int pivotDay = time("D")[pivotBars]
int pivotLeftEdgeDay = time("D")[pivotBars * 2]
bool confirmedWithinDay = not na(pivotDay) and pivotDay == time("D") and pivotDay == pivotLeftEdgeDay
int lastBarTime = last_bar_time
int currentDayStart = timestamp("UTC", year(lastBarTime, syminfo.timezone), month(lastBarTime, syminfo.timezone), dayofmonth(lastBarTime, syminfo.timezone), 0, 0)
// Supply zone: pivot candle's upper wick and body.
if not na(pivotHigh) and confirmedWithinDay
int pivotTime = time[pivotBars]
if zoneIsSelected(pivotTime, currentDayStart)
if array.size(supplyZones) >= MAX_ZONES_PER_SIDE
box.delete(array.shift(supplyZones))
float zoneTop = high[pivotBars]
float zoneBottom = math.max(open[pivotBars], close[pivotBars])
box newZone = box.new(left = pivotTime, top = zoneTop, right = pivotTime + extensionDays * 86400000, bottom = zoneBottom, xloc = xloc.bar_time, extend = extend.none, border_color = supplyBorder, bgcolor = supplyFill, text = "Supply", text_color = supplyBorder, text_size = size.tiny)
array.push(supplyZones, newZone)
// Demand zone: pivot candle's lower wick and body.
if not na(pivotLow) and confirmedWithinDay
int pivotTime = time[pivotBars]
if zoneIsSelected(pivotTime, currentDayStart)
if array.size(demandZones) >= MAX_ZONES_PER_SIDE
box.delete(array.shift(demandZones))
float zoneTop = math.min(open[pivotBars], close[pivotBars])
float zoneBottom = low[pivotBars]
box newZone = box.new(left = pivotTime, top = zoneTop, right = pivotTime + extensionDays * 86400000, bottom = zoneBottom, xloc = xloc.bar_time, extend = extend.none, border_color = demandBorder, bgcolor = demandFill, text = "Demand", text_color = demandBorder, text_size = size.tiny)
array.push(demandZones, newZone)
// Optionally remove zones once price closes beyond their far edge.
if removeBroken and array.size(supplyZones) > 0
for i = array.size(supplyZones) - 1 to 0
box zone = array.get(supplyZones, i)
if close > box.get_top(zone)
box.delete(zone)
array.remove(supplyZones, i)
if removeBroken and array.size(demandZones) > 0
for i = array.size(demandZones) - 1 to 0
box zone = array.get(demandZones, i)
if close < box.get_bottom(zone)
box.delete(zone)
array.remove(demandZones, i)
// Numbering is newest first and matches the comma-separated hide fields.
if barstate.islast and array.size(supplyZones) > 0
for i = 0 to array.size(supplyZones) - 1
int slot = array.size(supplyZones) - 1 - i
box zone = array.get(supplyZones, i)
bool visible = zoneSlotIsVisible(slot, true)
box.set_text(zone, "Supply " + str.tostring(slot + 1))
box.set_bgcolor(zone, visible ? supplyFill : color.new(supplyFill, 100))
box.set_border_color(zone, visible ? supplyBorder : color.new(supplyBorder, 100))
box.set_text_color(zone, visible ? supplyBorder : color.new(supplyBorder, 100))
if barstate.islast and array.size(demandZones) > 0
for i = 0 to array.size(demandZones) - 1
int slot = array.size(demandZones) - 1 - i
box zone = array.get(demandZones, i)
bool visible = zoneSlotIsVisible(slot, false)
box.set_text(zone, "Demand " + str.tostring(slot + 1))
box.set_bgcolor(zone, visible ? demandFill : color.new(demandFill, 100))
box.set_border_color(zone, visible ? demandBorder : color.new(demandBorder, 100))
box.set_text_color(zone, visible ? demandBorder : color.new(demandBorder, 100))