本页源码来自 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/
// © Nambou_Signals
//@version=6
indicator("Nambou Day Odds", "Day Odds", overlay = true, max_labels_count = 500, max_lines_count = 100, max_boxes_count = 200, max_bars_back = 1000)
gO = "Odds"
callAt = input.int(80, "Call it when the odds reach", minval = 50, maxval = 99, group = gO, tooltip = "A call is the first bar of the day whose odds reach this level. Calls are marked on the chart, counted under the table and can send an alert.")
minDays = input.int(20, "Fewest past days behind a number", minval = 5, maxval = 200, group = gO, tooltip = "When fewer past days were this far back from the high (or low) at this hour, the odds fall back to the hour alone.")
adrLen = input.int(14, "Days in the average daily range", minval = 5, maxval = 100, group = gO)
gD = "Display"
showLevels = input.bool(true, "Show today's high and low with their odds", group = gD)
showReach = input.bool(true, "Show how far the rest of the day usually reaches", group = gD, tooltip = "Two dashed lines above the high and two below the low: half of the past days in the same situation got as far as the inner line, and only one in five got past the outer one.")
showMarks = input.bool(true, "Mark the calls", group = gD, tooltip = "▼ over the bar where the high was called, ▲ under the bar where the low was called, with the odds at that moment. Once the day is over, a right call takes the side's colour and a wrong one turns grey.")
pastDays = input.int(10, "Past days drawn as boxes", minval = 0, maxval = 100, group = gD, tooltip = "A faint box around each past day's range, so you can see where the calls landed against the day's real high and low.")
showTable = input.bool(true, "Show the odds table", group = gD)
tablePosIn = input.string("Auto", "Table position", options = ["Auto", "Top center", "Bottom center", "Top right", "Bottom right", "Bottom left", "Top left"], group = gD, tooltip = "Auto looks at the left, middle and right of the visible chart and puts the table where the candles leave the most room. It never picks the bottom left, where TradingView puts its logo.")
txtSizeIn = input.string("Normal", "Chart text size", options = ["Tiny", "Small", "Normal", "Large"], group = gD, tooltip = "The odds at today's high and low are drawn one size larger, and the zone texts one size smaller at Large.")
tblSizeIn = input.string("Normal", "Table text size", options = ["Tiny", "Small", "Normal", "Large"], group = gD)
extBars = input.int(12, "Lines run this many bars past the last bar", minval = 2, maxval = 100, group = gD)
cHi = input.color(#FF3D71, "High", group = gD)
cLo = input.color(#00E5A0, "Low", group = gD)
cWrong = input.color(#787B86, "Wrong calls", group = gD)
cTxt = input.color(#D1D4DC, "Neutral text", group = gD)
gA = "Alerts"
aHi = input.bool(true, "Alert when the high is called", group = gA)
aLo = input.bool(true, "Alert when the low is called", group = gA)
txtSize = txtSizeIn == "Tiny" ? size.tiny : txtSizeIn == "Small" ? size.small : txtSizeIn == "Large" ? size.large : size.normal
bigSize = txtSizeIn == "Tiny" ? size.small : txtSizeIn == "Small" ? size.normal : txtSizeIn == "Large" ? size.huge : size.large
zoneSize = txtSizeIn == "Large" ? size.normal : txtSize
tblSize = tblSizeIn == "Tiny" ? size.tiny : tblSizeIn == "Small" ? size.small : tblSizeIn == "Large" ? size.large : size.normal
type Cell
int n = 0
int hits = 0
int last = -1
array<float> ext
type Rec
int h
int dh
int dl
float hi
float lo
float pHi
float qHi
float pLo
float qLo
type Call
bool made = false
float lvl
int h
label mk
var cellsHi = array.new<Cell>()
var cellsLo = array.new<Cell>()
if barstate.isfirst
for i = 0 to 119
cellsHi.push(Cell.new(ext = array.new_float()))
cellsLo.push(Cell.new(ext = array.new_float()))
// bars: high right, hour-alone high right, high scored, low right, hour-alone low right, low scored
// calls: high made, right, hour sum, low made, right, hour sum
var score = array.new_int(12, 0)
var recs = array.new<Rec>()
var ranges = array.new_float()
var bool started = false
var int dayN = 0
var int dOpen = na
var float adr = na
var float runHi = na
var float runLo = na
var int hiBar = na
var int loBar = na
var int d0 = na
var dayBoxes = array.new_box()
var Call mHi = Call.new()
var Call mLo = Call.new()
backIdx(int h, int d) => h * 4 + d
hourIdx(int h) => 96 + h
pick(array<Cell> cs, int h, int d) =>
int i = backIdx(h, d)
if cs.get(i).n < minDays
i := hourIdx(h)
if cs.get(i).n < minDays
i := -1
i
odds(array<Cell> cs, int i) => i < 0 ? na : cs.get(i).hits * 100.0 / cs.get(i).n
learn(array<Cell> cs, int i, bool hit, float e) =>
Cell c = cs.get(i)
if c.last != dayN
c.last := dayN
c.n += 1
c.hits += hit ? 1 : 0
c.ext.push(e)
if c.ext.size() > 500
c.ext.shift()
backBand(float x) => x < 0.1 ? 0 : x < 0.25 ? 1 : x < 0.5 ? 2 : 3
settle(Call c, int k, bool isHi, float fin, color cRight) =>
if c.made
bool right = isHi ? c.lvl >= fin - syminfo.mintick / 2 : c.lvl <= fin + syminfo.mintick / 2
score.set(k, score.get(k) + 1)
score.set(k + 1, score.get(k + 1) + (right ? 1 : 0))
score.set(k + 2, score.get(k + 2) + c.h)
if not na(c.mk)
label.set_textcolor(c.mk, right ? cRight : cWrong)
label.set_tooltip(c.mk, (isHi ? "High" : "Low") + " called " + str.tostring(c.h) + "h into the day: " + (right ? "right" : "wrong"))
bool newDay = timeframe.change("D")
if newDay
if started and not na(adr) and recs.size() > 0
for r in recs
bool hin = r.hi >= runHi - syminfo.mintick / 2
bool lin = r.lo <= runLo + syminfo.mintick / 2
float up = (runHi - r.hi) / adr
float dn = (r.lo - runLo) / adr
if not na(r.pHi) and not na(r.qHi)
score.set(0, score.get(0) + ((r.pHi >= 50) == hin ? 1 : 0))
score.set(1, score.get(1) + ((r.qHi >= 50) == hin ? 1 : 0))
score.set(2, score.get(2) + 1)
if not na(r.pLo) and not na(r.qLo)
score.set(3, score.get(3) + ((r.pLo >= 50) == lin ? 1 : 0))
score.set(4, score.get(4) + ((r.qLo >= 50) == lin ? 1 : 0))
score.set(5, score.get(5) + 1)
learn(cellsHi, backIdx(r.h, r.dh), hin, up)
learn(cellsHi, hourIdx(r.h), hin, up)
learn(cellsLo, backIdx(r.h, r.dl), lin, dn)
learn(cellsLo, hourIdx(r.h), lin, dn)
settle(mHi, 6, true, runHi, cHi)
settle(mLo, 9, false, runLo, cLo)
if started and not na(runHi) and pastDays > 0 and timeframe.isintraday
dayBoxes.push(box.new(math.max(d0, bar_index - 500), runHi, bar_index - 1, runLo, border_color = color.new(cTxt, 82), border_style = line.style_dotted, bgcolor = color.new(cTxt, 96)))
if dayBoxes.size() > pastDays
box.delete(dayBoxes.shift())
if started and not na(runHi)
ranges.push(runHi - runLo)
if ranges.size() > adrLen
ranges.shift()
adr := ranges.size() == adrLen ? ranges.avg() : na
started := true
dayN += 1
dOpen := time
d0 := bar_index
runHi := high
runLo := low
hiBar := bar_index
loBar := bar_index
recs.clear()
mHi := Call.new()
mLo := Call.new()
else if started
if high > runHi
runHi := high
hiBar := bar_index
if low < runLo
runLo := low
loBar := bar_index
bool live = started and not na(adr) and timeframe.isintraday
int hNow = live ? math.min(23, int(math.floor((time_close - dOpen) / 3600000.0))) : na
float backHi = live ? (runHi - close) / adr : na
float backLo = live ? (close - runLo) / adr : na
int dhNow = live ? backBand(backHi) : na
int dlNow = live ? backBand(backLo) : na
int iHi = live ? pick(cellsHi, hNow, dhNow) : -1
int iLo = live ? pick(cellsLo, hNow, dlNow) : -1
float pHi = odds(cellsHi, iHi)
float pLo = odds(cellsLo, iLo)
int iHr = live and cellsHi.get(hourIdx(hNow)).n >= minDays ? hourIdx(hNow) : -1
float qHi = odds(cellsHi, iHr)
float qLo = odds(cellsLo, iHr)
if live
recs.push(Rec.new(hNow, dhNow, dlNow, runHi, runLo, pHi, qHi, pLo, qLo))
if live and barstate.isconfirmed
if not mHi.made and pHi >= callAt
mHi.made := true
mHi.lvl := runHi
mHi.h := hNow
if showMarks
mHi.mk := label.new(bar_index, high, str.tostring(math.round(pHi)) + "%\n▼", style = label.style_label_down, color = color(na), textcolor = color.new(cTxt, 30), size = txtSize, tooltip = "High called " + str.tostring(hNow) + "h into the day")
if aHi
alert("Day Odds: " + syminfo.ticker + " HIGH IN " + str.tostring(math.round(pHi)) + "% at " + str.tostring(runHi, format.mintick), alert.freq_once_per_bar_close)
if not mLo.made and pLo >= callAt
mLo.made := true
mLo.lvl := runLo
mLo.h := hNow
if showMarks
mLo.mk := label.new(bar_index, low, "▲\n" + str.tostring(math.round(pLo)) + "%", style = label.style_label_up, color = color(na), textcolor = color.new(cTxt, 30), size = txtSize, tooltip = "Low called " + str.tostring(hNow) + "h into the day")
if aLo
alert("Day Odds: " + syminfo.ticker + " LOW IN " + str.tostring(math.round(pLo)) + "% at " + str.tostring(runLo, format.mintick), alert.freq_once_per_bar_close)
meter(float pct) =>
int n = int(math.round(math.max(0.0, math.min(100.0, pct)) / 10.0))
string s = ""
for k = 1 to 10
s += k <= n ? "▰" : "▱"
s
pctText(float v) => na(v) ? "—" : str.tostring(math.round(v)) + "%"
tPos = switch tablePosIn
"Top center" => position.top_center
"Bottom center" => position.bottom_center
"Top right" => position.top_right
"Bottom right" => position.bottom_right
"Bottom left" => position.bottom_left
"Top left" => position.top_left
=> position.top_center
tblBg = color.new(#131722, 8)
var table odds = table.new(tPos, 4, 10, border_width = 0, frame_width = 0)
tcell(int c, int r, string v, color tc) =>
table.cell(odds, c, r, v, text_color = tc, text_halign = text.align_left, text_size = tblSize, bgcolor = tblBg)
share(int right, int n) => n < 50 ? "—" : str.tostring(math.round(right * 100.0 / n)) + "%"
callText(int k, string mark) =>
int n = score.get(k)
mark + " " + (n < 5 ? "too few calls yet" : str.tostring(math.round(score.get(k + 1) * 100.0 / n)) + "% right of " + str.tostring(n) + ", on average " + str.tostring(math.round(score.get(k + 2) * 1.0 / n)) + "h into the day")
var line lHi = na
var line lLo = na
var label tHi = na
var label tLo = na
var zones = array.new_box()
var box today = na
levelLine(line l, label t, int x1, float y, string txt, color c) =>
line ln = l
label lb = t
if na(ln)
ln := line.new(x1, y, bar_index + extBars, y, color = c, width = 2)
lb := label.new(bar_index + extBars, y, txt, style = label.style_label_left, color = color.new(c, 80), textcolor = c, size = bigSize)
else
line.set_xy1(ln, x1, y)
line.set_xy2(ln, bar_index + extBars, y)
label.set_xy(lb, bar_index + extBars, y)
label.set_text(lb, txt)
[ln, lb]
if barstate.islast
float vHi = high
float vLo = low
int vj = 0
while vj < 999 and time[vj] >= chart.left_visible_bar_time
vHi := math.max(vHi, high[vj])
vLo := math.min(vLo, low[vj])
vj += 1
float gap = math.max(vHi - vLo, syminfo.mintick) * (txtSizeIn == "Large" ? 0.04 : txtSizeIn == "Normal" ? 0.04 : txtSizeIn == "Small" ? 0.032 : 0.025)
for z in zones
box.delete(z)
zones.clear()
box.delete(today)
if live and pastDays > 0
today := box.new(math.max(d0, bar_index - 500), runHi, bar_index, runLo, border_color = color.new(cTxt, 70), border_style = line.style_dotted, bgcolor = color.new(cTxt, 94))
if live and showLevels
[a1, b1] = levelLine(lHi, tHi, math.max(hiBar, bar_index - 500), runHi, "HIGH IN " + pctText(pHi), cHi)
lHi := a1
tHi := b1
[a2, b2] = levelLine(lLo, tLo, math.max(loBar, bar_index - 500), runLo, "LOW IN " + pctText(pLo), cLo)
lLo := a2
tLo := b2
if live and showReach
for side = 0 to 1
int ci = side == 0 ? iHi : iLo
if ci < 0
continue
array<float> ext = side == 0 ? cellsHi.get(ci).ext : cellsLo.get(ci).ext
float base = side == 0 ? runHi : runLo
color col = side == 0 ? cHi : cLo
float prev = base
int x1 = math.max(side == 0 ? hiBar : loBar, bar_index - 500)
for q = 0 to 1
float e = array.percentile_nearest_rank(ext, q == 0 ? 50 : 80)
float y = side == 0 ? base + e * adr : base - e * adr
if e > 0 and y != prev
string txt = math.abs(y - prev) >= gap ? (q == 0 ? "half the days got here " : "4 in 5 stopped before ") + str.tostring(y, format.mintick) + " " : ""
zones.push(box.new(x1, math.max(y, prev), bar_index + extBars, math.min(y, prev), border_color = color.new(col, q == 0 ? 55 : 75), border_style = q == 0 ? line.style_dashed : line.style_dotted, bgcolor = color.new(col, q == 0 ? 82 : 91), text = txt, text_color = color.new(col, 10), text_size = zoneSize, text_halign = text.align_right, text_valign = text.align_center))
prev := y
table.clear(odds, 0, 0, 3, 9)
bool atTop = tablePosIn == "Top center" or tablePosIn == "Top left"
if tablePosIn == "Auto"
float span = math.max(vHi - vLo, syminfo.mintick)
float best = -1.0
string spot = position.top_center
for part = 0 to 2
int a = int(vj * part / 3.0)
int b = math.max(a, math.min(999, int(vj * (part + 1) / 3.0) - 1))
float sHi = high[a]
float sLo = low[a]
for j = a to b
sHi := math.max(sHi, high[j])
sLo := math.min(sLo, low[j])
float bonus = part == 1 ? 0.03 : 0.0
float upRoom = (vHi - sHi) / span + bonus
float dnRoom = (sLo - vLo) / span + bonus
if upRoom > best
best := upRoom
spot := part == 0 ? position.top_right : part == 1 ? position.top_center : position.top_left
if dnRoom > best and part < 2
best := dnRoom
spot := part == 0 ? position.bottom_right : part == 1 ? position.bottom_center : position.bottom_left
atTop := spot == position.top_center or spot == position.top_left
table.set_position(odds, spot)
if showTable
int off = atTop ? 2 : 0
if off > 0
for k = 0 to off - 1
table.cell(odds, 0, k, " ", text_size = tblSize)
table.merge_cells(odds, 0, k, 3, k)
table.cell(odds, 0, off, "◆ NAMBOU DAY ODDS", text_color = #FFFFFF, text_halign = text.align_left, text_size = tblSize, bgcolor = tblBg)
table.merge_cells(odds, 0, off, 3, off)
if not timeframe.isintraday
table.cell(odds, 0, off + 1, "Open an intraday chart (1 minute to 4 hours)", text_color = cTxt, text_halign = text.align_left, text_size = tblSize, bgcolor = tblBg)
table.merge_cells(odds, 0, off + 1, 3, off + 1)
else if not live
table.cell(odds, 0, off + 1, "Needs " + str.tostring(adrLen + 1) + " full days of history first", text_color = cTxt, text_halign = text.align_left, text_size = tblSize, bgcolor = tblBg)
table.merge_cells(odds, 0, off + 1, 3, off + 1)
else
tcell(0, off + 1, "Right now", color.new(cTxt, 40))
tcell(1, off + 1, "", cTxt)
tcell(2, off + 1, "right on past bars", color.new(cTxt, 40))
tcell(3, off + 1, "hour alone", color.new(cTxt, 40))
tcell(0, off + 2, "High in", cHi)
tcell(1, off + 2, meter(nz(pHi)) + " " + pctText(pHi), na(pHi) ? color.new(cHi, 60) : #FFFFFF)
tcell(2, off + 2, share(score.get(0), score.get(2)), #FFFFFF)
tcell(3, off + 2, share(score.get(1), score.get(2)), color.new(cTxt, 15))
tcell(0, off + 3, "Low in", cLo)
tcell(1, off + 3, meter(nz(pLo)) + " " + pctText(pLo), na(pLo) ? color.new(cLo, 60) : #FFFFFF)
tcell(2, off + 3, share(score.get(3), score.get(5)), #FFFFFF)
tcell(3, off + 3, share(score.get(4), score.get(5)), color.new(cTxt, 15))
string basis = str.tostring(hNow) + "h into the day · " + str.tostring(backHi, "#.##") + " ADR below the high · " + str.tostring(backLo, "#.##") + " ADR above the low"
array<string> notes = array.from(basis, callText(6, "▼ high calls at " + str.tostring(callAt) + "%+:"), callText(9, "▲ low calls at " + str.tostring(callAt) + "%+:"), "right = the side the odds favoured happened · each day is scored only on the days before it")
for [k, t] in notes
table.cell(odds, 0, off + 4 + k, t, text_color = color.new(cTxt, k == 0 ? 15 : 35), text_halign = text.align_left, text_size = size.small, bgcolor = tblBg)
table.merge_cells(odds, 0, off + 4 + k, 3, off + 4 + k)