Initial Balance Base Rates

comhamm1 · study · 167 行 · 点赞 13 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

//@version=6
indicator("Initial Balance Base Rates", "IB Stats", overlay = true, max_labels_count = 500, max_boxes_count = 500)
 
// Counts over the loaded history how often the Initial Balance (first hour of the session)
// is broken and how far the break extends. Statistics only, no trading advice.
 
lang      = input.string("English", "Language / Sprache", options = ["English", "Deutsch"], display = display.none)
sessInput = input.session("0930-1600", "Session (exchange time)", display = display.none)
tzInput   = input.string("America/New_York", "Session time zone", display = display.none)
ibMin     = input.int(60, "IB length in minutes", minval = 5, step = 5, display = display.none)
showLv    = input.bool(true, "Show IB range and extension levels", display = display.none)
lblMode   = input.string("Latest session", "Level labels", options = ["Latest session", "All sessions", "Off"], display = display.none)
lw        = input.int(2, "Line width", minval = 1, maxval = 4, display = display.none)
tblPos    = input.string(position.top_right, "Table position", options = [position.top_right, position.bottom_right, position.top_left, position.bottom_left], display = display.none)
 
color cIB = #d1d4dc
color cUp = #26a69a
color cDn = #ef5350
 
bool en = lang == "English"
L(string de, string eng) =>
    en ? eng : de
 
if not timeframe.isintraday or timeframe.in_seconds() > ibMin * 60
    runtime.error("Please use an intraday timeframe no longer than the IB length.")
 
inSess  = not na(time(timeframe.period, sessInput, tzInput))
dayChg  = ta.change(dayofmonth(time, tzInput)) != 0
newSess = inSess and (not inSess[1] or dayChg)
endSess = not inSess and inSess[1]
 
var int   sessStart = na
var int   sessStartBar = na
var float ibH       = na
var float ibL       = na
var float postH     = na
var float postL     = na
var float lastClose = na
var bool  ibDone    = false
var bool  counted   = true
 
var int   nDays     = 0
var int   nUpOnly   = 0
var int   nDnOnly   = 0
var int   nBoth     = 0
var int   nNone     = 0
var int   nExt50    = 0
var int   nExt100   = 0
var int   nBackIn   = 0
var float sumRange  = 0.0
 
// Evaluate the finished session (before resetting for the new one)
if (endSess or newSess) and ibDone and not counted
    float r = ibH - ibL
    if r > 0
        bool  bu    = postH > ibH
        bool  bd    = postL < ibL
        float extUp = bu ? (postH - ibH) / r : 0.0
        float extDn = bd ? (ibL - postL) / r : 0.0
        float ext   = math.max(extUp, extDn)
        nDays    += 1
        sumRange += r
        if bu and bd
            nBoth += 1
        else if bu
            nUpOnly += 1
        else if bd
            nDnOnly += 1
        else
            nNone += 1
        if ext >= 0.5
            nExt50 += 1
        if ext >= 1.0
            nExt100 += 1
        if (bu or bd) and lastClose <= ibH and lastClose >= ibL
            nBackIn += 1
    counted := true
 
if newSess
    sessStart := time
    sessStartBar := bar_index
    ibH       := high
    ibL       := low
    postH     := na
    postL     := na
    ibDone    := false
    counted   := false
else if inSess and not na(sessStart)
    if time < sessStart + ibMin * 60000
        ibH := math.max(ibH, high)
        ibL := math.min(ibL, low)
    else
        ibDone := true
        postH  := na(postH) ? high : math.max(postH, high)
        postL  := na(postL) ? low : math.min(postL, low)
 
if inSess
    lastClose := close
 
// Range and extension levels, drawn from the end of the IB to the session close
bool  drawLv = showLv and inSess and ibDone
float rNow   = ibH - ibL
plot(drawLv ? ibH : na, "IB high", cIB, lw, plot.style_linebr)
plot(drawLv ? ibL : na, "IB low", cIB, lw, plot.style_linebr)
// Shaded box over the IB period itself (drawn once the IB is complete)
if drawLv and not drawLv[1]
    box.new(sessStartBar, ibH, bar_index, ibL, border_color = color.new(cIB, 40), bgcolor = color.new(cIB, 82))
plot(drawLv ? ibH + 0.5 * rNow : na, "+0.5 x IB", color.new(cUp, 20), lw, plot.style_linebr, linestyle = plot.linestyle_dashed)
plot(drawLv ? ibH + rNow : na, "+1 x IB", cUp, lw, plot.style_linebr)
plot(drawLv ? ibL - 0.5 * rNow : na, "-0.5 x IB", color.new(cDn, 20), lw, plot.style_linebr, linestyle = plot.linestyle_dashed)
plot(drawLv ? ibL - rNow : na, "-1 x IB", cDn, lw, plot.style_linebr)
 
// Light labels at the right end of the levels; they move along while the session runs
var label[] cur = array.new<label>()
mk(float y, string txt, color c) =>
    label.new(bar_index + 1, y, txt, style = label.style_label_left, color = color.new(color.black, 100), textcolor = color.new(c, 25), size = size.small)
bool lvStart = drawLv and not drawLv[1]
if lvStart and lblMode != "Off"
    if lblMode == "Latest session"
        for lb in cur
            label.delete(lb)
    array.clear(cur)
    array.push(cur, mk(ibH + rNow, "+1 × IB", cUp))
    array.push(cur, mk(ibH + 0.5 * rNow, "+0.5 × IB", cUp))
    array.push(cur, mk(ibH, L("IB-Hoch", "IB high"), cIB))
    array.push(cur, mk(ibL, L("IB-Tief", "IB low"), cIB))
    array.push(cur, mk(ibL - 0.5 * rNow, "−0.5 × IB", cDn))
    array.push(cur, mk(ibL - rNow, "−1 × IB", cDn))
else if drawLv
    for lb in cur
        label.set_x(lb, bar_index + 1)
 
// Raw counts in the data window (for checking)
plot(nDays, "Days", display = display.data_window)
plot(nUpOnly, "Days broke high only", display = display.data_window)
plot(nDnOnly, "Days broke low only", display = display.data_window)
plot(nBoth, "Days broke both", display = display.data_window)
plot(nNone, "Days no break", display = display.data_window)
plot(nExt50, "Days ext >= 0.5 x IB", display = display.data_window)
plot(nExt100, "Days ext >= 1 x IB", display = display.data_window)
plot(nBackIn, "Days closed back inside", display = display.data_window)
 
pct(int a, int b) =>
    b > 0 ? str.tostring(100.0 * a / b, "#.0") + " %" : "–"
 
row(table tb, int r, string label, string value) =>
    table.cell(tb, 0, r, label, text_color = color.silver, text_halign = text.align_left)
    table.cell(tb, 1, r, value, text_color = color.white, text_halign = text.align_right)
 
var table t = table.new(tblPos, 2, 11, bgcolor = color.new(color.black, 20), border_width = 1, border_color = color.new(color.gray, 70))
 
if barstate.islast
    int nBreak = nUpOnly + nDnOnly + nBoth
    table.cell(t, 0, 0, L("IB-Basisraten", "IB base rates"), text_color = color.white, text_halign = text.align_left)
    table.cell(t, 1, 0, str.tostring(nDays) + L(" Tage", " days"), text_color = color.white, text_halign = text.align_right)
    row(t, 1, L("Ausbruch nur oben", "Broke high only"), pct(nUpOnly, nDays))
    row(t, 2, L("Ausbruch nur unten", "Broke low only"), pct(nDnOnly, nDays))
    row(t, 3, L("Beide Seiten gebrochen", "Broke both sides"), pct(nBoth, nDays))
    row(t, 4, L("Kein Ausbruch", "No break"), pct(nNone, nDays))
    row(t, 5, L("Ausdehnung ≥ 0,5 × IB", "Extension ≥ 0.5 × IB"), pct(nExt50, nDays))
    row(t, 6, L("Ausdehnung ≥ 1 × IB", "Extension ≥ 1 × IB"), pct(nExt100, nDays))
    row(t, 7, L("Schluss zurück in der IB*", "Closed back inside IB*"), pct(nBackIn, nBreak))
    row(t, 8, L("Ø IB-Spanne", "Avg IB range"), nDays > 0 ? str.tostring(sumRange / nDays, format.mintick) : "–")
    table.cell(t, 0, 9, L("* Anteil der Ausbruchstage", "* share of days with a break"), text_color = color.gray, text_halign = text.align_left, text_size = size.small)
    table.cell(t, 1, 9, "")
    table.cell(t, 0, 10, L("Nur geladene Historie. Keine Empfehlung.", "Loaded history only. Not advice."), text_color = color.gray, text_halign = text.align_left, text_size = size.small)
    table.cell(t, 1, 10, "")

← 返回列表