本页源码来自 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/
// © dagram_trader
//@version=6
indicator("Gold Clock: Usual vs Today Hourly Range", shorttitle = "Gold Clock", overlay = false, format = format.price, precision = 2)
// Gold Clock
// For every clock hour of the day it compares how far price has travelled in that hour today
// with how far it usually travels in the same clock hour on earlier days.
// Usual range = median (or average) of the same clock hour on the last N days before today.
// The hour that is still forming never enters any usual value.
// ---------- Inputs ----------
string TZ = input.string("Asia/Tokyo", "Clock time zone", options = ["Asia/Tokyo", "Asia/Singapore", "Asia/Hong_Kong", "Asia/Dubai", "Europe/London", "Europe/Berlin", "America/New_York", "America/Chicago", "UTC"])
int DAYS = input.int(20, "Days used for the usual range", minval = 5, maxval = 250)
string STAT = input.string("Median", "Usual range =", options = ["Median", "Average"])
float HOT = input.float(1.5, "Strong when today / usual is at least", minval = 1.0, step = 0.1)
bool SHOW_TBL = input.bool(true, "Show the 24-hour table", group = "Table")
string LANG = input.string("English", "Table language", options = ["English", "日本語"], group = "Table")
string POS = input.string("Top right", "Table position", options = ["Top right", "Middle right", "Bottom right", "Top left", "Bottom left"], group = "Table")
string TXT = input.string("Small", "Table text size", options = ["Tiny", "Small", "Normal"], group = "Table")
int DEC = input.int(2, "Decimals in the table", minval = 0, maxval = 5, group = "Table")
color C_LOW = input.color(color.new(color.gray, 35), "Range below usual", group = "Colors")
color C_UP = input.color(color.new(#D4AF37, 0), "Range at or above usual", group = "Colors")
color C_HOT = input.color(color.new(#FF6D00, 0), "Range at or above the strong level", group = "Colors")
color C_LINE = input.color(color.new(#2962FF, 0), "Usual range line", group = "Colors")
const int MIN_SAMPLES = 5
// samples older than this are not used (N trading days plus weekends and a few holidays)
int MAX_AGE_MS = int((DAYS * 7.0 / 5.0 + 5.0) * 86400000.0)
// ---------- Timeframe check ----------
int tfSec = timeframe.in_seconds()
bool tfOk = timeframe.isintraday and tfSec <= 3600 and 3600 % tfSec == 0
// ---------- Storage: one history per clock hour ----------
type Hist
array<float> v // range of the hour
array<int> d // calendar day of that hour (yyyymmdd in the clock time zone)
array<int> t // start time of that hour
var array<Hist> hist = array.new<Hist>()
var array<float> today = array.new<float>(24, na)
if barstate.isfirst
for i = 0 to 23
hist.push(Hist.new(array.new<float>(), array.new<int>(), array.new<int>()))
// usual range of clock hour h, from days before dayLimit only, newest first, at most DAYS days
usualOf(int h, int dayLimit, int nowTime) =>
Hist hs = hist.get(h)
int n = hs.v.size()
array<float> pick = array.new<float>()
if n > 0
for i = n - 1 to 0
if pick.size() >= DAYS
break
if hs.d.get(i) < dayLimit and hs.t.get(i) >= nowTime - MAX_AGE_MS
pick.push(hs.v.get(i))
int cnt = pick.size()
float u = cnt >= MIN_SAMPLES ? (STAT == "Median" ? pick.median() : pick.avg()) : na
[u, cnt]
// ---------- The clock hour of this bar ----------
int hr = hour(time, TZ)
int dkey = year(time, TZ) * 10000 + month(time, TZ) * 100 + dayofmonth(time, TZ)
var float bHi = na
var float bLo = na
var int bHour = na
var int bDay = na
var int bStart = na
var int started = 0
float rangeNow = na
float usualNow = na
if tfOk
bool newBucket = na(bHour) or hr != bHour or dkey != bDay
if newBucket
// store the hour that just ended (the first hour on the chart may be partial, so it is skipped)
if not na(bHour) and started > 1
Hist hs = hist.get(bHour)
hs.v.push(bHi - bLo)
hs.d.push(bDay)
hs.t.push(bStart)
if hs.v.size() > DAYS + 1
hs.v.shift()
hs.d.shift()
hs.t.shift()
if dkey != bDay
for i = 0 to 23
today.set(i, na)
bHi := high
bLo := low
bHour := hr
bDay := dkey
bStart := time
started += 1
else
bHi := math.max(bHi, high)
bLo := math.min(bLo, low)
rangeNow := bHi - bLo
today.set(hr, rangeNow)
[u0, n0] = usualOf(hr, dkey, time)
usualNow := u0
float ratioNow = usualNow > 0 ? rangeNow / usualNow : na
color colNow = na(ratioNow) ? C_LOW : ratioNow >= HOT ? C_HOT : ratioNow >= 1 ? C_UP : C_LOW
plot(rangeNow, "Range so far in this clock hour", color = colNow, style = plot.style_columns)
plot(usualNow, "Usual range for this clock hour", color = C_LINE, style = plot.style_stepline, linewidth = 2)
// ---------- 24-hour table (two blocks of 12 hours) ----------
posOf(string p) =>
switch p
"Middle right" => position.middle_right
"Bottom right" => position.bottom_right
"Top left" => position.top_left
"Bottom left" => position.bottom_left
=> position.top_right
sizeOf(string s) =>
switch s
"Tiny" => size.tiny
"Normal" => size.normal
=> size.small
string FMT = DEC == 0 ? "0" : "0." + str.repeat("0", DEC)
fmt(float x) =>
na(x) ? "-" : str.tostring(x, FMT)
hh(int h) =>
(h < 10 ? "0" : "") + str.tostring(h) + ":00"
bool JA = LANG == "日本語"
color BG = color.new(#131722, 15)
color BG_HEAD = color.new(#2A2E39, 0)
color BG_NOW = color.new(#2962FF, 45)
color FG = color.new(color.white, 0)
color FG_DIM = color.new(color.white, 45)
var table tbl = table.new(posOf(POS), 8, 14, bgcolor = BG, border_width = 1, border_color = color.new(color.white, 85))
if SHOW_TBL and barstate.islast
string sz = sizeOf(TXT)
if not tfOk
table.cell(tbl, 0, 0, JA ? "1時間を割り切れる時間足(1分・5分・15分・30分・1時間)で使ってください" : "Use a timeframe that divides one hour (1m, 5m, 15m, 30m, 1h)", text_color = FG, text_size = sz, bgcolor = BG_HEAD)
else
int minN = 1000000
int maxN = 0
for b = 0 to 1
int c0 = b * 4
table.cell(tbl, c0 + 0, 0, JA ? "時刻" : "Hour", text_color = FG, text_size = sz, bgcolor = BG_HEAD)
table.cell(tbl, c0 + 1, 0, JA ? "ふだん" : "Usual", text_color = FG, text_size = sz, bgcolor = BG_HEAD)
table.cell(tbl, c0 + 2, 0, JA ? "今日" : "Today", text_color = FG, text_size = sz, bgcolor = BG_HEAD)
table.cell(tbl, c0 + 3, 0, JA ? "今日÷ふだん" : "Today/Usual", text_color = FG, text_size = sz, bgcolor = BG_HEAD)
for k = 0 to 11
int h = b * 12 + k
[u, n] = usualOf(h, dkey, time)
float t = today.get(h)
float r = u > 0 and not na(t) ? t / u : na
if n > 0
minN := math.min(minN, n)
maxN := math.max(maxN, n)
color rowBg = h == hr ? BG_NOW : BG
color tCol = na(r) ? FG_DIM : r >= HOT ? C_HOT : r >= 1 ? C_UP : FG
string rTxt = na(r) ? "" : JA ? str.tostring(r, "0.0") + "倍" : "x" + str.tostring(r, "0.00")
table.cell(tbl, c0 + 0, k + 1, hh(h), text_color = FG, text_size = sz, bgcolor = rowBg)
table.cell(tbl, c0 + 1, k + 1, fmt(u), text_color = FG, text_size = sz, bgcolor = rowBg)
table.cell(tbl, c0 + 2, k + 1, na(t) ? "" : fmt(t), text_color = tCol, text_size = sz, bgcolor = rowBg)
table.cell(tbl, c0 + 3, k + 1, rTxt, text_color = tCol, text_size = sz, bgcolor = rowBg)
string nTxt = maxN == 0 ? "0" : minN == maxN ? str.tostring(maxN) : str.tostring(minN) + "-" + str.tostring(maxN)
string stEn = STAT == "Median" ? "median" : "average"
string stJa = STAT == "Median" ? "中央値" : "平均"
string foot = JA ? "ふだん=同じ時刻の、今日より前の" + nTxt + "日分の" + stJa + "(" + TZ + ")" : "Usual = " + stEn + " of the same hour on the last " + nTxt + " days before today (" + TZ + ")"
table.cell(tbl, 0, 13, foot, text_color = FG_DIM, text_size = size.tiny, bgcolor = BG)
table.merge_cells(tbl, 0, 13, 7, 13)