本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Seasonality - Day of Week & Hour of Day Statistics [Dots3Red]",
shorttitle = "Seasonality [D3R]",
overlay = true)
// Inputs
C_TXT = color.new(#f8fafc, 0)
C_DIM = color.new(#94a3b8, 0)
C_BG = color.new(#131722, 5)
C_BORD = color.new(#334155, 0)
C_AMBER = color.new(#ffb700, 0)
GRP_ST = "📅 Seasonality"
GRP_VIS = "🎨 Visuals"
GRP_HUD = "🖥️ Dashboard"
show_mode = input.string("Both", "Show", options=["Day of Week", "Hour of Day", "Both"], group=GRP_ST)
tz_opt = input.string("Exchange", "Hour Timezone", options=["Exchange", "UTC", "America/New_York", "Europe/London", "Europe/Berlin", "Asia/Dubai", "Asia/Tokyo"], group=GRP_ST,
tooltip="Timezone used to bucket hours. Day-of-week buckets always follow the exchange's own trading day.")
min_n = input.int(10, "Min Samples per Bucket", minval=3, group=GRP_ST,
tooltip="A bucket's numbers are only shown once it has at least this many samples.")
col_pos = input.color(#00f0ff, "Positive Color", group=GRP_VIS)
col_neg = input.color(#ff00aa, "Negative Color", group=GRP_VIS)
show_bg = input.bool(true, "Highlight Strong / Weak Hours", group=GRP_VIS,
tooltip="Tints the chart background during hours whose up-rate so far is at or above the threshold (strong) or at or below 100 minus the threshold (weak).")
bg_th = input.float(58, "Strong Hour Threshold (Up %)", minval=50, maxval=90, step=1, group=GRP_VIS)
bg_tr = input.int(90, "Highlight Transparency", minval=60, maxval=98, group=GRP_VIS)
show_hud = input.bool(true, "Show Dashboard", group=GRP_HUD)
hud_pos = input.string("Top Right", "Position",
options=["Top Right", "Top Left", "Bottom Right", "Bottom Left"], group=GRP_HUD)
txt_opt = input.string("Tiny", "Text Size", options=["Tiny", "Small", "Normal"], group=GRP_HUD)
layout = input.string("Compact", "Layout", options=["Compact", "Tall"], group=GRP_HUD,
tooltip="Compact: days across one block, hours in three rows of eight, so the whole table fits on screen. Tall: one row per bucket with sample counts. Hover a compact cell to see its sample count.")
string tz = tz_opt == "Exchange" ? syminfo.timezone : tz_opt
bool hr_ok = timeframe.isintraday and timeframe.in_seconds() <= 3600
bool nd = timeframe.change("D")
int hr_now = hour(time, tz)
bool new_hr = hr_now != hr_now[1] or time - time[1] >= 3600000
var float[] dow_n = array.new<float>(7, 0.0)
var float[] dow_sum = array.new<float>(7, 0.0)
var float[] dow_up = array.new<float>(7, 0.0)
var float[] hr_n = array.new<float>(24, 0.0)
var float[] hr_sum = array.new<float>(24, 0.0)
var float[] hr_up = array.new<float>(24, 0.0)
var float last_dc = na
var float hr_open = na
if nd
if barstate.isconfirmed and not na(last_dc) and last_dc != 0 and not na(close[1])
int di = dayofweek(time[1]) - 1
float ret = (close[1] / last_dc - 1.0) * 100.0
array.set(dow_n, di, array.get(dow_n, di) + 1.0)
array.set(dow_sum, di, array.get(dow_sum, di) + ret)
if ret > 0
array.set(dow_up, di, array.get(dow_up, di) + 1.0)
last_dc := close[1]
if hr_ok and new_hr
if barstate.isconfirmed and not na(hr_open) and hr_open != 0 and not na(close[1])
int hi = hour(time[1], tz)
float ret = (close[1] / hr_open - 1.0) * 100.0
array.set(hr_n, hi, array.get(hr_n, hi) + 1.0)
array.set(hr_sum, hi, array.get(hr_sum, hi) + ret)
if ret > 0
array.set(hr_up, hi, array.get(hr_up, hi) + 1.0)
hr_open := open
f_up_pct(float[] n_arr, float[] up_arr, int i) =>
float n = array.get(n_arr, i)
n >= min_n ? array.get(up_arr, i) / n * 100.0 : na
f_avg(float[] n_arr, float[] sum_arr, int i) =>
float n = array.get(n_arr, i)
n >= min_n ? array.get(sum_arr, i) / n : na
// Dashboard
f_hud_pos(string s) =>
switch s
"Top Right" => position.top_right
"Top Left" => position.top_left
"Bottom Right" => position.bottom_right
=> position.bottom_left
txt_sz = txt_opt == "Tiny" ? size.tiny : txt_opt == "Normal" ? size.normal : size.small
var table hud = table.new(f_hud_pos(hud_pos), 9, 36,
bgcolor=C_BG, border_color=C_BORD,
border_width=1, frame_color=C_BORD, frame_width=2)
f_cell_bg(float avg) =>
na(avg) ? color.new(#0f172a, 40) : avg > 0 ? color.new(col_pos, 78) : avg < 0 ? color.new(col_neg, 78) : color.new(#0f172a, 40)
f_bucket_row(int row, string name, bool is_now, float n, float avg, float up) =>
color bg = f_cell_bg(avg)
table.cell(hud, 0, row, (is_now ? "▶ " : "") + name, text_color = is_now ? C_AMBER : C_TXT, bgcolor=bg, text_size=txt_sz)
table.cell(hud, 1, row, na(avg) ? "—" : (avg > 0 ? "+" : "") + str.tostring(avg, "#.###") + "%",
text_color = na(avg) ? C_DIM : C_TXT, bgcolor=bg, text_size=txt_sz, text_halign=text.align_center)
table.cell(hud, 2, row, na(up) ? "—" : str.tostring(math.round(up)) + "%",
text_color = na(up) ? C_DIM : C_TXT, bgcolor=bg, text_size=txt_sz, text_halign=text.align_center)
table.cell(hud, 3, row, str.tostring(int(n)), text_color=C_DIM, bgcolor=bg, text_size=txt_sz, text_halign=text.align_center)
f_header(int row, string title) =>
color hb = color.new(#1e293b, 20)
table.cell(hud, 0, row, title, text_color=C_DIM, bgcolor=hb, text_size=txt_sz)
table.cell(hud, 1, row, "Avg", text_color=C_DIM, bgcolor=hb, text_size=txt_sz, text_halign=text.align_center,
tooltip="Average return of this bucket, in percent.")
table.cell(hud, 2, row, "Up %", text_color=C_DIM, bgcolor=hb, text_size=txt_sz, text_halign=text.align_center,
tooltip="Share of samples in this bucket that closed higher than they started.")
table.cell(hud, 3, row, "n", text_color=C_DIM, bgcolor=hb, text_size=txt_sz, text_halign=text.align_center)
f_cc(int col, int row, string txt, color bg, color tc, string tip) =>
table.cell(hud, col, row, txt, text_color=tc, bgcolor=bg, text_size=txt_sz, text_halign=text.align_center, tooltip=tip)
f_compact_block(int row, string[] labels, bool[] nows, float[] ns, float[] avgs, float[] ups, string title) =>
color hb = color.new(#1e293b, 20)
f_cc(0, row, title, hb, C_DIM, "")
f_cc(0, row + 1, "Avg", hb, C_DIM, "Average return of this bucket, in percent.")
f_cc(0, row + 2, "Up %", hb, C_DIM, "Share of samples in this bucket that closed higher than they started.")
for j = 0 to array.size(labels) - 1
float avg = array.get(avgs, j)
float up = array.get(ups, j)
bool now = array.get(nows, j)
string tip = "n = " + str.tostring(int(array.get(ns, j)))
color bg = f_cell_bg(avg)
f_cc(j + 1, row, (now ? "▶" : "") + array.get(labels, j), hb, now ? C_AMBER : C_TXT, tip)
f_cc(j + 1, row + 1, na(avg) ? "—" : (avg > 0 ? "+" : "") + str.tostring(avg, "#.##"), bg, na(avg) ? C_DIM : C_TXT, tip)
f_cc(j + 1, row + 2, na(up) ? "—" : str.tostring(math.round(up)), bg, na(up) ? C_DIM : C_TXT, tip)
if show_hud and barstate.islast and layout == "Compact"
table.clear(hud, 0, 0, 8, 35)
table.cell(hud, 0, 0, "SEASONALITY [D3R]", text_color=C_TXT,
bgcolor=color.new(#1e293b, 0), text_size=txt_sz, text_halign=text.align_center)
table.merge_cells(hud, 0, 0, 8, 0)
int crow = 1
if show_mode != "Hour of Day"
string[] dl = array.from("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun")
int[] od = array.from(1, 2, 3, 4, 5, 6, 0)
int td = dayofweek(time) - 1
bool[] dnow = array.new<bool>()
float[] dn = array.new<float>()
float[] da = array.new<float>()
float[] du = array.new<float>()
for k = 0 to 6
int di = array.get(od, k)
array.push(dnow, di == td)
array.push(dn, array.get(dow_n, di))
array.push(da, f_avg(dow_n, dow_sum, di))
array.push(du, f_up_pct(dow_n, dow_up, di))
f_compact_block(crow, dl, dnow, dn, da, du, "Day")
crow += 3
if show_mode != "Day of Week"
if hr_ok
for b = 0 to 2
string[] hl = array.new<string>()
bool[] hnow = array.new<bool>()
float[] hn = array.new<float>()
float[] ha = array.new<float>()
float[] hu = array.new<float>()
for j = 0 to 7
int h = b * 8 + j
array.push(hl, (h < 10 ? "0" : "") + str.tostring(h))
array.push(hnow, h == hr_now)
array.push(hn, array.get(hr_n, h))
array.push(ha, f_avg(hr_n, hr_sum, h))
array.push(hu, f_up_pct(hr_n, hr_up, h))
f_compact_block(crow, hl, hnow, hn, ha, hu, b == 0 ? "Hour" : "")
crow += 3
table.cell(hud, 0, crow, "Hours in " + (tz_opt == "Exchange" ? "exchange time" : tz_opt) + " · hover a cell for n",
text_color=C_DIM, bgcolor=color.new(#0f172a, 40), text_size=txt_sz)
table.merge_cells(hud, 0, crow, 8, crow)
else
table.cell(hud, 0, crow, "Hour stats need a chart of 1h or lower", text_color=C_DIM, bgcolor=color.new(#0f172a, 40), text_size=txt_sz)
table.merge_cells(hud, 0, crow, 8, crow)
if show_hud and barstate.islast and layout == "Tall"
table.clear(hud, 0, 0, 8, 35)
table.cell(hud, 0, 0, "SEASONALITY [D3R]", text_color=C_TXT,
bgcolor=color.new(#1e293b, 0), text_size=txt_sz, text_halign=text.align_center)
table.merge_cells(hud, 0, 0, 3, 0)
int row = 1
if show_mode != "Hour of Day"
f_header(row, "Day")
row += 1
string[] dnames = array.from("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat")
int[] order = array.from(1, 2, 3, 4, 5, 6, 0)
int today = dayofweek(time) - 1
for k = 0 to 6
int di = array.get(order, k)
f_bucket_row(row, array.get(dnames, di), di == today, array.get(dow_n, di), f_avg(dow_n, dow_sum, di), f_up_pct(dow_n, dow_up, di))
row += 1
if show_mode != "Day of Week"
if hr_ok
f_header(row, "Hour (" + (tz_opt == "Exchange" ? "exch" : tz_opt) + ")")
row += 1
for h = 0 to 23
string hn = (h < 10 ? "0" : "") + str.tostring(h) + ":00"
f_bucket_row(row, hn, h == hr_now, array.get(hr_n, h), f_avg(hr_n, hr_sum, h), f_up_pct(hr_n, hr_up, h))
row += 1
else
table.cell(hud, 0, row, "Hour stats need a chart of 1h or lower", text_color=C_DIM, bgcolor=color.new(#0f172a, 40), text_size=txt_sz)
table.merge_cells(hud, 0, row, 3, row)
// Visual
float cur_up = hr_ok ? f_up_pct(hr_n, hr_up, hr_now) : na
bool strong_h = not na(cur_up) and cur_up >= bg_th
bool weak_h = not na(cur_up) and cur_up <= 100 - bg_th
bgcolor(show_bg and strong_h ? color.new(col_pos, bg_tr) : show_bg and weak_h ? color.new(col_neg, bg_tr) : na, title="Strong / Weak Hour")
// Alert
alertcondition(hr_ok and new_hr and strong_h, "Strong Hour Started", "D3R Seasonality: a historically strong hour just started on {{ticker}}")
alertcondition(hr_ok and new_hr and weak_h, "Weak Hour Started", "D3R Seasonality: a historically weak hour just started on {{ticker}}")