本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Sōgyo CE1 / CE2 Zone", "Sōgyo", overlay = true, max_boxes_count = 500, max_lines_count = 500, max_labels_count = 500)
// ── Inputs ──────────────────────────────────────────────
var g_S = "Sessions (ET)"
tz = "America/New_York"
rthSess = input.session("0930-1615", "RTH (close = 16:14 bar)", group = g_S)
rth2Sess = input.session("0930-1700", "RTH2 (close = 16:59 bar)", group = g_S)
onSess = input.session("1800-0930", "Overnight", group = g_S)
var g_A = "ATR & context"
length = input.int(14, "RTH sessions to average", minval = 1, group = g_A)
gapRed = input.float(0.68, "Red: gap > x ATR", step = 0.01, group = g_A)
onGreen = input.float(0.45, "Green: overnight range ≤ x ATR", step = 0.01, group = g_A)
wideMin = input.float(0.05, "Wide zone: width ≥ x % of price", step = 0.01, group = g_A)
var g_D = "Drawing"
extMode = input.string("To last candle", "Zone length", options = ["To last candle", "Until next zone"], group = g_D)
maxDays = input.int(10, "Zones to keep", minval = 1, group = g_D)
dateFmt = input.string("HH:mm MM/dd", "Label date format", group = g_D)
showPx = input.bool(true, "Show CE1 / CE2 prices in label", group = g_D)
showFill = input.bool(true, "Show prior closes (full gap fill)", group = g_D)
txtCol = input.color(color.black, "Text colour", group = g_D)
lblSize = input.string("Small", "Text size", options = ["Tiny", "Small", "Normal"], group = g_D)
var g_C = "Colour"
colMode = input.string("By age", "Colour zones", options = ["By age", "By context"], group = g_C)
ageDays = input.int(5, "Fresh for first N trading days", minval = 1, group = g_C)
freshCol = input.color(color.new(color.blue, 70), "Fresh zone", group = g_C)
agedCol = input.color(color.new(color.gray, 75), "Aged zone", group = g_C)
greyText = input.bool(true, "Grey out text of aged zones", group = g_C)
var g_T = "Table"
showTbl = input.bool(true, "Show table", group = g_T)
tblPos = input.string("Top Right", "Table position", options = ["Top Right", "Top Left", "Bottom Right", "Bottom Left"], group = g_T)
if not timeframe.isintraday
runtime.error("Use an intraday chart (1–15 min).")
tSize = lblSize == "Tiny" ? size.tiny : lblSize == "Normal" ? size.normal : size.small
// ── Session flags ───────────────────────────────────────
inRTH = not na(time(timeframe.period, rthSess, tz))
inRTH2 = not na(time(timeframe.period, rth2Sess, tz))
inON = not na(time(timeframe.period, onSess, tz))
rthStart = inRTH and not inRTH[1]
rthEnd = not inRTH and inRTH[1]
onStart = inON and not inON[1]
// ── State ───────────────────────────────────────────────
var array<float> ranges = array.new<float>()
var float rHi = na
var float rLo = na
var float c1 = na
var float c2 = na
var float onHi = na
var float onLo = na
var float atrVal = na
var float O = na
var float ce1 = na
var float ce2 = na
var float gapPts = na
var float onPts = na
var string ctx = "—"
var string fDate = ""
var int dayCount = 0
var array<box> zBoxes = array.new<box>()
var array<label> zLbls = array.new<label>()
var array<line> f1Lines = array.new<line>()
var array<line> f2Lines = array.new<line>()
var array<label> f1Lbls = array.new<label>()
var array<label> f2Lbls = array.new<label>()
var array<int> zDay = array.new<int>()
var array<color> zCtxCol = array.new<color>()
// Overnight range 18:00–09:29
if onStart
onHi := high
onLo := low
else if inON
onHi := math.max(onHi, high)
onLo := math.min(onLo, low)
// ── 09:30: build today's zone from yesterday's closes ───
if rthStart
dayCount += 1
O := open
if not na(c1) and not na(c2)
ce1 := (c1 + O) / 2
ce2 := (c2 + O) / 2
cm = (c1 + c2) / 2
gapPts := math.abs(O - cm)
onPts := onHi - onLo
split = math.sign(O - c1) != math.sign(O - c2)
if not na(atrVal)
ctx := split ? "SPLIT — skip" : gapPts / atrVal > gapRed ? "RED — waypoint to fill" : onPts / atrVal <= onGreen ? "GREEN — valid confluence" : "NEUTRAL — don't weight"
else
ctx := "— (loading ATR)"
wide = math.abs(ce1 - ce2) / O * 100 >= wideMin
fDate := str.format_time(time, dateFmt, tz)
prevDate = str.format_time(time - 86400000, "MM/dd", tz)
ctxCol = str.startswith(ctx, "GREEN") ? color.new(color.green, 70) : str.startswith(ctx, "RED") ? color.new(color.red, 75) : str.startswith(ctx, "SPLIT") ? color.new(color.orange, 75) : color.new(#7E57C2, 70)
// "Until next zone": freeze the previous zone where this one starts
if extMode == "Until next zone" and zBoxes.size() > 0
zBoxes.get(0).set_right(time)
zLbls.get(0).set_x(time)
if showFill and f1Lines.size() > 0
f1Lines.get(0).set_x2(time)
f2Lines.get(0).set_x2(time)
f1Lbls.get(0).set_x(time)
f2Lbls.get(0).set_x(time)
mid = (ce1 + ce2) / 2
txt = fDate + " Sōgyo" + (wide ? " (wide)" : "") + (showPx ? " CE1 " + str.tostring(ce1, format.mintick) + " / CE2 " + str.tostring(ce2, format.mintick) : "")
zBoxes.unshift(box.new(time, math.max(ce1, ce2), time, math.min(ce1, ce2), xloc = xloc.bar_time, border_color = color.new(freshCol, 0), bgcolor = freshCol))
zLbls.unshift(label.new(time, mid, txt, xloc = xloc.bar_time, style = label.style_label_left, color = #ffffff00, textcolor = txtCol, size = tSize))
zDay.unshift(dayCount)
zCtxCol.unshift(ctxCol)
if showFill
f1Lines.unshift(line.new(time, c1, time, c1, xloc = xloc.bar_time, color = color.new(freshCol, 0), style = line.style_dashed))
f2Lines.unshift(line.new(time, c2, time, c2, xloc = xloc.bar_time, color = color.new(freshCol, 0), style = line.style_dotted))
f1Lbls.unshift(label.new(time, c1, "RTH close " + prevDate, xloc = xloc.bar_time, style = label.style_label_left, color = #ffffff00, textcolor = txtCol, size = tSize))
f2Lbls.unshift(label.new(time, c2, "RTH2 close " + prevDate, xloc = xloc.bar_time, style = label.style_label_left, color = #ffffff00, textcolor = txtCol, size = tSize))
// keep only the last N zones
if zBoxes.size() > maxDays
zBoxes.pop().delete()
zLbls.pop().delete()
zDay.pop()
zCtxCol.pop()
if f1Lines.size() > maxDays
f1Lines.pop().delete()
f2Lines.pop().delete()
f1Lbls.pop().delete()
f2Lbls.pop().delete()
rHi := high
rLo := low
else if inRTH
rHi := math.max(rHi, high)
rLo := math.min(rLo, low)
// ── Every bar: grow to the current candle, place text after it, colour by age ──
n = zBoxes.size()
if n > 0
for i = 0 to n - 1
grow = extMode == "To last candle" or i == 0
if grow
zBoxes.get(i).set_right(time)
zLbls.get(i).set_x(time)
if showFill and i < f1Lines.size()
f1Lines.get(i).set_x2(time)
f2Lines.get(i).set_x2(time)
f1Lbls.get(i).set_x(time)
f2Lbls.get(i).set_x(time)
age = dayCount - zDay.get(i) // 0 on the formation day
aged = age >= ageDays
c = colMode == "By context" ? zCtxCol.get(i) : aged ? agedCol : freshCol
zBoxes.get(i).set_bgcolor(c)
zBoxes.get(i).set_border_color(color.new(c, 0))
tc = colMode == "By age" and aged and greyText ? color.gray : txtCol
zLbls.get(i).set_textcolor(tc)
if showFill and i < f1Lines.size()
f1Lines.get(i).set_color(color.new(c, 0))
f2Lines.get(i).set_color(color.new(c, 0))
f1Lbls.get(i).set_textcolor(tc)
f2Lbls.get(i).set_textcolor(tc)
// Closes update after the 09:30 snapshot
if inRTH
c1 := close
if inRTH2
c2 := close
// Session done: store RTH range; ATR = simple average of last N
if rthEnd and not na(rHi)
ranges.push(rHi - rLo)
if ranges.size() > length
ranges.shift()
atrVal := ranges.size() == length ? ranges.avg() : na
plot(atrVal, "RTH ATR (pts)", display = display.data_window)
// ── Table ───────────────────────────────────────────────
pos = tblPos == "Top Left" ? position.top_left : tblPos == "Bottom Right" ? position.bottom_right : tblPos == "Bottom Left" ? position.bottom_left : position.top_right
var table t = table.new(pos, 2, 8, border_width = 1)
f(x) => na(x) ? "—" : str.tostring(x, "#,##0.00")
if showTbl and barstate.islast
gA = na(atrVal) or na(gapPts) ? na : gapPts / atrVal
oA = na(atrVal) or na(onPts) ? na : onPts / atrVal
table.cell(t, 0, 0, "Formed", text_size = size.small, text_color = txtCol)
table.cell(t, 1, 0, fDate, text_size = size.small, text_color = txtCol)
table.cell(t, 0, 1, "RTH ATR(" + str.tostring(length) + ")", text_size = size.small, text_color = txtCol)
table.cell(t, 1, 1, f(atrVal) + " pts", text_size = size.small, text_color = txtCol)
table.cell(t, 0, 2, "Gap", text_size = size.small, text_color = txtCol)
table.cell(t, 1, 2, f(gapPts) + " pts | " + f(gA) + " ATR", text_size = size.small, text_color = txtCol)
table.cell(t, 0, 3, "Overnight range", text_size = size.small, text_color = txtCol)
table.cell(t, 1, 3, f(onPts) + " pts | " + f(oA) + " ATR", text_size = size.small, text_color = txtCol)
table.cell(t, 0, 4, "CE1 / CE2", text_size = size.small, text_color = txtCol)
table.cell(t, 1, 4, f(ce1) + " / " + f(ce2), text_size = size.small, text_color = txtCol)
table.cell(t, 0, 5, "Zone width", text_size = size.small, text_color = txtCol)
table.cell(t, 1, 5, f(math.abs(ce1 - ce2)) + " pts", text_size = size.small, text_color = txtCol)
table.cell(t, 0, 6, "Red if gap >", text_size = size.small, text_color = txtCol)
table.cell(t, 1, 6, f(gapRed * atrVal) + " pts", text_size = size.small, text_color = txtCol)
cbg = str.startswith(ctx, "GREEN") ? color.new(color.green, 70) : str.startswith(ctx, "RED") ? color.new(color.red, 70) : color.new(color.gray, 80)
table.cell(t, 0, 7, "Context", text_size = size.small, text_color = txtCol)
table.cell(t, 1, 7, ctx, bgcolor = cbg, text_size = size.small, text_color = txtCol)