本页源码来自 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/
// © MAlphaCore
//@version=6
// M·Alpha·Session Edge — when does THIS market really move, and does any hour lean up or down? For every hour of the day
// the script measures, over the whole chart history, the average range of that hour and how often it closed above its open.
// The coming hours (12 by default, up to 24) are listed to the right of price, the next hour on top: the longer and brighter the bar, the more
// that hour usually moves. A direction is only
// printed when it is stronger than chance (three standard errors away from 50 %, strict on purpose because 24 hours
// are tested at once and one of them would look special by luck alone); everything else is marked "no lean".
// Past hours can be tinted the same way. Closed hours only, nothing repaints. Works on 1 hour and lower timeframes.
indicator("M·Alpha·Session Edge", shorttitle = "M·Alpha·Session", overlay = true, max_boxes_count = 30)
// ───────────────────────────── Inputs ─────────────────────────────
grSt = "Statistics"
tz = input.string("UTC", "Time zone", options = ["UTC", "America/New_York", "Europe/London", "Europe/Berlin", "Asia/Tokyo", "Asia/Singapore", "Australia/Sydney"], group = grSt, display = display.none)
zLimit = input.float(3.0, "Lean needs (standard errors)", minval = 0, maxval = 5, step = 0.5, group = grSt, display = display.none)
minHours = input.int(100, "Minimum hours per slot", minval = 10, maxval = 5000, group = grSt, display = display.none)
grShow = "Display"
showAhead = input.bool(true, "Show the coming hours", group = grShow, display = display.none)
nAhead = input.int(12, "Coming hours shown", minval = 1, maxval = 24, group = grShow, display = display.none)
showBg = input.bool(false, "Tint past hours by activity", group = grShow, display = display.none)
width = input.int(60, "Column width (bars)", minval = 10, maxval = 300, group = grShow, display = display.none)
gap = input.int(5, "Distance from price (bars)", minval = 0, maxval = 100, group = grShow, display = display.none)
grStyle = "Style"
colAct = input.color(#7c4dff, "Activity", group = grStyle, display = display.none)
colUp = input.color(#ffeb3b, "Leans up", group = grStyle, display = display.none)
colDown = input.color(#b2b5be, "Leans down", group = grStyle, display = display.none)
colText = input.color(#e8eaf0, "Text", group = grStyle, display = display.none)
textSizeIn = input.string("Normal", "Text size", options = ["Tiny", "Small", "Normal"], group = grStyle, display = display.none)
textSize = textSizeIn == "Tiny" ? size.tiny : textSizeIn == "Normal" ? size.normal : size.small
if timeframe.in_seconds() > 3600
runtime.error("M·Alpha·Session Edge works on 1 hour and lower timeframes.")
// ───────────────────────────── Statistics ─────────────────────────────
var array<int> cnt = array.new<int>(24, 0)
var array<int> ups = array.new<int>(24, 0)
var array<float> rng = array.new<float>(24, 0.0)
var float hOpen = na
var float hHigh = na
var float hLow = na
hr = hour(time, tz)
newHour = na(time[1]) or hr != hour(time[1], tz)
if newHour
hOpen := open
hHigh := high
hLow := low
else
hHigh := math.max(hHigh, high)
hLow := math.min(hLow, low)
hourEnd = minute(time_close, tz) == 0 // this bar closes the hour
if barstate.isconfirmed and hourEnd and not na(hOpen)
cnt.set(hr, cnt.get(hr) + 1)
if close > hOpen
ups.set(hr, ups.get(hr) + 1)
rng.set(hr, rng.get(hr) + (hHigh - hLow) / hOpen * 100)
upShare(int h) => cnt.get(h) > 0 ? 100.0 * ups.get(h) / cnt.get(h) : na
avgRange(int h) => cnt.get(h) > 0 ? rng.get(h) / cnt.get(h) : na
strength(int h) => cnt.get(h) > 0 ? (upShare(h) - 50) / (50 / math.sqrt(cnt.get(h))) : 0.0
// activity 0..1: this hour's average range between the quietest and the busiest hour
activity(int h) =>
lo = 1e10
hi = 0.0
for i = 0 to 23
r = avgRange(i)
if not na(r)
lo := math.min(lo, r)
hi := math.max(hi, r)
r = avgRange(h)
na(r) or hi <= lo ? 0.0 : (r - lo) / (hi - lo)
leanText(int h) =>
z = strength(h)
cnt.get(h) < minHours ? "few data" : math.abs(z) >= zLimit ? (z > 0 ? "leans up " : "leans down ") + str.tostring(upShare(h), "#") + " %" : "no lean"
bgcolor(showBg and cnt.get(hr) >= minHours ? color.new(colAct, 95 - int(activity(hr) * 20)) : na, title = "Hour activity")
// ───────────────────────────── The coming hours ─────────────────────────────
var array<box> ahead = array.new<box>()
top = ta.highest(high, 100)
bot = ta.lowest(low, 100)
if barstate.islast and showAhead
for b in ahead
b.delete()
ahead.clear()
t0 = time_close - (time_close % 3600000)
t0 := t0 < time_close ? t0 + 3600000 : t0
span = (top - bot) * 1.2 // column taller than the recent candles, so every row has room
colTop = close + span
rowH = 2 * span / nAhead
x0 = bar_index + gap
for k = 0 to nAhead - 1 // one row per hour, the next hour on top
ts = t0 + k * 3600000
h = hour(ts, tz)
z = strength(h)
lean = cnt.get(h) >= minHours and math.abs(z) >= zLimit
y1 = colTop - k * rowH
y0 = y1 - rowH * 0.9
w = math.max(1, int(math.round(width * (0.25 + 0.75 * activity(h)))))
txt = str.format("{0,number,00}:00 ±{1} % {2}", h, str.tostring(avgRange(h), "#.##"), leanText(h))
ahead.push(box.new(x0, y1, x0 + w, y0, border_color = lean ? (z > 0 ? colUp : colDown) : na, border_width = 2,
bgcolor = color.new(colAct, 75 - int(activity(h) * 55)), text = txt, text_color = colText,
text_size = textSize, text_halign = text.align_left))