本页源码来自 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 Session Odds", "Session Odds", overlay = true, max_boxes_count = 200, max_lines_count = 200, max_labels_count = 300, max_bars_back = 1000)
gS = "Sessions"
tz = input.string("UTC", "Time zone", group = gS, tooltip = "The time zone the session times below are written in, e.g. UTC or America/New_York.")
asiaSess = input.session("0000-0600", "Asia", group = gS, tooltip = "The Asian range: its high and low are the levels the rest of the day is measured against.")
lonSess = input.session("0700-1000", "London", group = gS)
nySess = input.session("1300-1600", "New York", group = gS)
dayEnd = input.int(21, "The day ends at this hour", minval = 1, maxval = 23, group = gS, tooltip = "In the time zone above. A level counts as taken if price trades through it before this hour.")
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 that an Asian level gets taken reach this level. Calls are marked, 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 = "With fewer past days in the same situation, the odds fall back to the hour alone.")
adrLen = input.int(14, "Days in the average range", minval = 5, maxval = 100, group = gO)
gD = "Display"
pastDays = input.int(10, "Past days drawn", minval = 0, maxval = 40, group = gD)
showBoxes = input.bool(true, "Show the sessions", group = gD, tooltip = "The Asian range as a band, London and New York as a light background.")
showCalls = input.bool(false, "Mark the calls on the chart", group = gD, tooltip = "A small triangle on the bar where the odds of a take reached the call level. The calls are always counted in the table and can always send an alert.")
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", "Huge"], group = gD, tooltip = "The size of today's tags at the right of the chart.")
tblSizeIn = input.string("Normal", "Table text size", options = ["Tiny", "Small", "Normal", "Large"], group = gD)
cAsia = input.color(#9575CD, "Asia", group = gD)
cLon = input.color(#42A5F5, "London", group = gD)
cNy = input.color(#FFB74D, "New York", group = gD)
cHi = input.color(#F23645, "Asian high", group = gD)
cLo = input.color(#089981, "Asian low", group = gD)
cMiss = input.color(#787B86, "Not taken", group = gD)
cTxt = chart.fg_color
gA = "Alerts"
aTake = input.bool(true, "Alert when an Asian level is taken", group = gA)
aCall = input.bool(true, "Alert when a take is called", group = gA)
txtSize = txtSizeIn == "Tiny" ? size.tiny : txtSizeIn == "Small" ? size.small : txtSizeIn == "Large" ? size.large : txtSizeIn == "Huge" ? size.huge : size.normal
type Cell
int n = 0
int hits = 0
int last = -1
type Rec
int h
int d
float p
float q
type Side
float lvl
bool taken = false
int takenBar
bool called = false
label mk
line ln
label tg
array<Rec> recs
type Day
int id
int t0
int b0
float hi
float lo
bool asiaDone = false
int asiaEnd
Side up
Side dn
bool lonSeen = false
bool lonHi = false
bool lonLo = false
bool afterHi = false
bool afterLo = false
bool closed = false
box bxA
box bxR
var cells = array.new<Cell>()
if barstate.isfirst
for i = 0 to 287
cells.push(Cell.new())
// bars: model right, hour-alone right, scored; calls: made, right
// days: counted, high taken, low taken, both, london one side, back inside, other side later
var score = array.new_int(12, 0)
var ranges = array.new_float()
var Day cur = na
var int dayN = 0
var drawn = array.new_box()
var drawnL = array.new_line()
var drawnT = array.new_label()
hdIdx(int s, int h, int d) => s * 144 + h * 5 + d
hIdx(int s, int h) => s * 144 + 120 + h
band(float x) => x < 0.1 ? 0 : x < 0.25 ? 1 : x < 0.5 ? 2 : x < 1.0 ? 3 : 4
rate(int i) => cells.get(i).n >= minDays ? cells.get(i).hits * 100.0 / cells.get(i).n : na
learn(int i, bool hit, int id) =>
Cell c = cells.get(i)
if c.last != id
c.last := id
c.n += 1
c.hits += hit ? 1 : 0
bump(int k, bool v) =>
score.set(k, score.get(k) + (v ? 1 : 0))
settleSide(Side s, int sd, int id) =>
for r in s.recs
if not na(r.p) and not na(r.q)
bump(0, (r.p >= 50) == s.taken)
bump(1, (r.q >= 50) == s.taken)
bump(2, true)
learn(hdIdx(sd, r.h, r.d), s.taken, id)
learn(hIdx(sd, r.h), s.taken, id)
if s.called
bump(3, true)
bump(4, s.taken)
if not na(s.mk)
label.set_color(s.mk, s.taken ? (sd == 0 ? cHi : cLo) : color.new(cMiss, 40))
int hr = hour(time, tz)
bool inAsia = not na(time(timeframe.period, asiaSess, tz))
bool inLon = not na(time(timeframe.period, lonSess, tz))
bool inNy = not na(time(timeframe.period, nySess, tz))
bool asiaStart = inAsia and not inAsia[1]
bool endBar = not na(cur) and not cur.closed and cur.asiaDone and (hr >= dayEnd or asiaStart)
// close the day: outcomes, statistics, past drawings
if endBar
Day d = cur
d.closed := true
float fin = close[1]
settleSide(d.up, 0, d.id)
settleSide(d.dn, 1, d.id)
bump(5, true)
bump(6, d.up.taken)
bump(7, d.dn.taken)
bump(8, d.up.taken and d.dn.taken)
if d.lonHi != d.lonLo
bump(9, true)
bump(10, fin <= d.hi and fin >= d.lo)
bump(11, d.lonHi ? d.afterLo : d.afterHi)
for [i, s] in array.from(d.up, d.dn)
if not na(s.ln)
line.set_x2(s.ln, s.taken ? s.takenBar : bar_index - 1)
line.set_color(s.ln, color.new(s.taken ? (i == 0 ? cHi : cLo) : cMiss, s.taken ? 10 : 30))
if not s.taken
line.set_style(s.ln, line.style_dashed)
line.set_width(s.ln, 2)
// the session ranges feed the average range
var float dHi = na
var float dLo = na
if asiaStart and not na(dHi)
ranges.push(dHi - dLo)
if ranges.size() > adrLen
ranges.shift()
if asiaStart
dHi := high
dLo := low
else
dHi := math.max(nz(dHi, high), high)
dLo := math.min(nz(dLo, low), low)
float adr = ranges.size() == adrLen ? ranges.avg() : na
bgcolor(showBoxes and timeframe.isintraday ? (inLon ? color.new(cLon, 84) : inNy ? color.new(cNy, 84) : na) : na, title = "London and New York")
newBox(int x, float top, float bot, color c) =>
box.new(x, top, x, bot, border_color = color.new(c, 30), bgcolor = color.new(c, 70))
// a new day starts with the Asian session
if asiaStart and timeframe.isintraday and timeframe.in_seconds() <= 3600
dayN += 1
cur := Day.new(dayN, time, bar_index, high, low, up = Side.new(recs = array.new<Rec>()), dn = Side.new(recs = array.new<Rec>()))
if showBoxes and pastDays > 0
cur.bxA := newBox(bar_index, high, low, cAsia)
drawn.push(cur.bxA)
if not na(cur) and not cur.closed
Day d = cur
if inAsia and not d.asiaDone
d.hi := math.max(d.hi, high)
d.lo := math.min(d.lo, low)
if not na(d.bxA)
box.set_right(d.bxA, bar_index)
box.set_top(d.bxA, d.hi)
box.set_bottom(d.bxA, d.lo)
else if not d.asiaDone
d.asiaDone := true
d.asiaEnd := bar_index
d.up.lvl := d.hi
d.dn.lvl := d.lo
if pastDays > 0
d.up.ln := line.new(bar_index - 1, d.hi, bar_index, d.hi, color = color.new(cHi, 10), width = 3)
d.dn.ln := line.new(bar_index - 1, d.lo, bar_index, d.lo, color = color.new(cLo, 10), width = 3)
if showBoxes
d.bxR := box.new(bar_index - 1, d.hi, bar_index, d.lo, border_color = color(na), bgcolor = color.new(cAsia, 88))
drawn.push(d.bxR)
d.up.tg := label.new(bar_index, d.hi, "", style = label.style_label_left, color = color(na), textcolor = cHi, size = txtSize)
d.dn.tg := label.new(bar_index, d.lo, "", style = label.style_label_left, color = color(na), textcolor = cLo, size = txtSize)
drawnL.push(d.up.ln)
drawnL.push(d.dn.ln)
drawnT.push(d.up.tg)
drawnT.push(d.dn.tg)
if not na(d.bxR)
box.set_right(d.bxR, bar_index)
while drawn.size() > pastDays * 2
box.delete(drawn.shift())
while drawnL.size() > pastDays * 2
line.delete(drawnL.shift())
label.delete(drawnT.shift())
// after Asia: takes, London breaks, live odds
float pUp = na
float pDn = na
if not na(cur) and not cur.closed and cur.asiaDone and not na(adr)
Day d = cur
int hNow = hour(time_close, tz)
if inLon
d.lonSeen := true
for [i, s] in array.from(d.up, d.dn)
bool through = i == 0 ? high > s.lvl : low < s.lvl
if inLon and through
if i == 0
d.lonHi := true
else
d.lonLo := true
if not inLon and d.lonSeen and through
if i == 0 and d.lonLo
d.afterHi := true
if i == 1 and d.lonHi
d.afterLo := true
if not s.taken and through
s.taken := true
s.takenBar := bar_index
if not na(s.tg)
label.set_xy(s.tg, bar_index, s.lvl)
label.set_style(s.tg, label.style_circle)
label.set_color(s.tg, i == 0 ? cHi : cLo)
label.set_size(s.tg, size.tiny)
if aTake and barstate.isconfirmed
alert("Session Odds: " + syminfo.ticker + " took the Asian " + (i == 0 ? "high " : "low ") + str.tostring(s.lvl, format.mintick), alert.freq_once_per_bar_close)
if not s.taken
float dist = (i == 0 ? s.lvl - close : close - s.lvl) / adr
int db = band(math.max(dist, 0.0))
float p = rate(hdIdx(i, hNow, db))
if na(p)
p := rate(hIdx(i, hNow))
float q = rate(hIdx(i, hNow))
s.recs.push(Rec.new(hNow, db, p, q))
if i == 0
pUp := p
else
pDn := p
if barstate.isconfirmed and not s.called and p >= callAt
s.called := true
if showCalls
s.mk := label.new(bar_index, na, "", yloc = i == 0 ? yloc.abovebar : yloc.belowbar, style = i == 0 ? label.style_triangleup : label.style_triangledown, color = color.new(i == 0 ? cHi : cLo, 20), size = size.tiny, tooltip = "Take of the Asian " + (i == 0 ? "high" : "low") + " called at " + str.tostring(math.round(p)) + "%")
if aCall
alert("Session Odds: " + syminfo.ticker + " take of the Asian " + (i == 0 ? "high" : "low") + " called at " + str.tostring(math.round(p)) + "%", alert.freq_once_per_bar_close)
if not na(s.ln)
line.set_x2(s.ln, s.taken ? s.takenBar : bar_index)
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)) + "%"
share(int k, int n) => score.get(n) < 20 ? "—" : str.tostring(math.round(score.get(k) * 100.0 / score.get(n))) + "%"
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(chart.bg_color, 5)
tblSize = tblSizeIn == "Tiny" ? size.tiny : tblSizeIn == "Small" ? size.small : tblSizeIn == "Large" ? size.large : size.normal
var table odds = table.new(tPos, 3, 18, 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)
note(int r, string v, color tc) =>
table.cell(odds, 0, r, v, text_color = tc, text_halign = text.align_left, text_size = size.small, bgcolor = tblBg)
table.merge_cells(odds, 0, r, 2, r)
statRow(int r, string name, int k, int n) =>
float v = score.get(n) < 20 ? na : score.get(k) * 100.0 / score.get(n)
tcell(0, r, name, cTxt)
tcell(1, r, na(v) ? meter(0) : meter(v), na(v) ? color.new(cTxt, 60) : k == 6 ? cHi : k == 7 ? cLo : cAsia)
tcell(2, r, pctText(v) + " of " + str.tostring(score.get(n)), color.new(cTxt, 15))
var line todayHi = na
var label todayHiTag = na
var line todayLo = na
var label todayLoTag = na
if barstate.islast
line.delete(todayHi)
label.delete(todayHiTag)
line.delete(todayLo)
label.delete(todayLoTag)
if not na(cur) and cur.asiaDone and not cur.closed
Day d = cur
for [i, s] in array.from(d.up, d.dn)
color c = i == 0 ? cHi : cLo
string txt = (i == 0 ? "ASIA HIGH" : "ASIA LOW") + " " + (s.taken ? "✓" : pctText(i == 0 ? pUp : pDn))
line ln = line.new(d.asiaEnd, s.lvl, bar_index + 2, s.lvl, color = color.new(c, 20), width = 2, style = s.taken ? line.style_solid : line.style_dashed)
label lb = label.new(bar_index + 2, s.lvl, txt, style = label.style_label_left, color = color(na), textcolor = c, size = txtSize, tooltip = "Asian " + (i == 0 ? "high " : "low ") + str.tostring(s.lvl, format.mintick) + (s.taken ? ": taken today" : ": odds it gets taken before the close"))
if i == 0
todayHi := ln
todayHiTag := lb
else
todayLo := ln
todayLoTag := lb
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
table.clear(odds, 0, 0, 2, 17)
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, 2, k)
table.cell(odds, 0, off, "◆ NAMBOU SESSION ODDS", text_color = #FFFFFF, text_halign = text.align_left, text_size = tblSize, bgcolor = cAsia)
table.merge_cells(odds, 0, off, 2, off)
if not timeframe.isintraday or timeframe.in_seconds() > 3600
note(off + 1, "Open a chart of 1 hour or less", cTxt)
else if na(adr)
note(off + 1, "Needs " + str.tostring(adrLen + 1) + " full days of history first", cTxt)
else
int r = off + 1
if not na(cur) and cur.asiaDone and not cur.closed
tcell(0, r, "Asian high taken", cHi)
tcell(1, r, cur.up.taken ? "taken ✓" : meter(nz(pUp)) + " " + pctText(pUp), cHi)
tcell(2, r, cur.up.taken ? "" : "today", color.new(cTxt, 40))
r += 1
tcell(0, r, "Asian low taken", cLo)
tcell(1, r, cur.dn.taken ? "taken ✓" : meter(nz(pDn)) + " " + pctText(pDn), cLo)
tcell(2, r, cur.dn.taken ? "" : "today", color.new(cTxt, 40))
else
note(r, not na(cur) and not cur.asiaDone ? "Asian session in progress" : "Waiting for the next Asian session", cTxt)
r += 1
tcell(0, r, "Right on past bars", color.new(cTxt, 40))
tcell(1, r, share(0, 2), cTxt)
tcell(2, r, "hour alone " + share(1, 2), color.new(cTxt, 15))
r += 1
tcell(0, r, "On this chart's days", color.new(cTxt, 40))
tcell(1, r, "", cTxt)
tcell(2, r, "", cTxt)
r += 1
statRow(r, "Asian high taken by the close", 6, 5)
r += 1
statRow(r, "Asian low taken by the close", 7, 5)
r += 1
statRow(r, "Both sides taken", 8, 5)
r += 1
statRow(r, "London took one side, closed back inside", 10, 9)
r += 1
statRow(r, "London took one side, other side taken later", 11, 9)
r += 1
int nc = score.get(3)
note(r, "Take calls at " + str.tostring(callAt) + "%+: " + (nc < 5 ? "too few yet" : str.tostring(math.round(score.get(4) * 100.0 / nc)) + "% right of " + str.tostring(nc)), color.new(cTxt, 35))
r += 1
note(r, tz + ": Asia " + asiaSess + " · London " + lonSess + " · NY " + nySess + " · close " + str.tostring(dayEnd) + ":00", color.new(cTxt, 35))
r += 1
note(r, "each day is scored only on the days before it", color.new(cTxt, 35))