Bollinger Near Lower Band Stats

edwinardyanto · study · 102 行 · 点赞 6 · TradingView 原页

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

Pine Script

//@version=6
// Bollinger Near Lower Band — penanda & statistik — @edwinardyanto (Reels seri 3 "cara terbaik", 23 Okt 2026)
// Candle hijau panjang (badan >= 60% panjang candle DAN >= 1,5x rata-rata badan 20 candle sebelumnya) yang low-nya MENDEKATI band bawah:
// di bawah band StdDev 1,5 tapi BELUM menembus band default StdDev 2 (Bollinger 20). Pembanding: sinyal yang menembus band StdDev 2.
// Masuk open candle berikutnya, stop loss low candle sinyal, keluar di close candle ke-20 (atau stop loss). Fee 0,1%/sisi.
// Backtest BTCUSDT 4H (1 Jan 2021 - 30 Sep 2026). Panel = hasil di rentang tanggal input (default = data video).
// Bahan belajar, bukan sinyal, bukan ajakan beli/jual.
indicator("Bollinger Near Lower Band Stats", overlay = true, max_labels_count = 500)
len    = input.int(20, "Bollinger length")
dIn    = input.float(1.5, "Band dalam (StdDev)")
dOut   = input.float(2.0, "Band default (StdDev)")
bodyR  = input.float(0.6, "Badan min (x panjang candle)")
bodyK  = input.float(1.5, "Badan min (x rata-rata badan 20)")
N      = input.int(20, "Keluar di close candle ke-N")
feePct = input.float(0.1, "Fee per sisi (%)")
mulai  = input.time(timestamp("05 Jan 2021 00:00 +0000"), "Mulai backtest")
sampai = input.time(timestamp("01 Oct 2026 00:00 +0000"), "Sampai")
fee = feePct / 100
// ---- buku trade (boleh tumpang tindih, sama dengan backtest Python) ----
type Book
    array<float> e
    array<float> s
    array<float> tp
    array<int> last
    int n = 0
    int win = 0
    float sumR = 0.0
    float gp = 0.0
    float gl = 0.0
 
method add(Book b, float e, float s, float tp, int last) =>
    if e - s > 0
        b.e.push(e)
        b.s.push(s)
        b.tp.push(tp)
        b.last.push(last)
 
method step(Book b, float fee, int sampai) =>
    if b.e.size() > 0
        for q = b.e.size() - 1 to 0
            e = b.e.get(q)
            s = b.s.get(q)
            tp = b.tp.get(q)
            lst = b.last.get(q)
            float R = na
            if low <= s
                R := -1.0
            else if not na(tp) and high >= tp
                R := (tp - e) / (e - s)
            else if not na(lst) and bar_index >= lst
                R := (close - e) / (e - s)
            if not na(R)
                Rn = R - 2 * fee * e / (e - s)
                if time < sampai
                    b.n += 1
                    b.win += Rn > 0 ? 1 : 0
                    b.sumR += Rn
                    b.gp += Rn > 0 ? Rn : 0
                    b.gl += Rn < 0 ? -Rn : 0
                b.e.remove(q)
                b.s.remove(q)
                b.tp.remove(q)
                b.last.remove(q)
 
newBook() => Book.new(array.new<float>(), array.new<float>(), array.new<float>(), array.new<int>())
basis = ta.sma(close, len)
dev   = ta.stdev(close, len)
lbIn  = basis - dIn * dev
lbOut = basis - dOut * dev
plot(basis, "Tengah", color.new(color.gray, 40))
plot(lbIn, "Band 1,5", color.new(color.teal, 0))
plot(lbOut, "Band 2", color.new(color.red, 30))
body  = math.abs(close - open)
rng   = high - low
big   = close > open and rng > 0 and body >= bodyR * rng and body >= bodyK * ta.sma(body, 20)[1]
sigIn  = big and low <= lbIn and low > lbOut
sigOut = big and low <= lbOut
plotshape(sigIn, "Mendekat band (sinyal)", shape.labelup, location.belowbar, color.new(color.teal, 0), text = "BB", textcolor = color.white, size = size.small)
plotshape(sigOut, "Menembus band 2 (pembanding)", shape.xcross, location.belowbar, color.new(color.red, 30), size = size.tiny)
var bookIn  = newBook()
var bookOut = newBook()
if time[1] >= mulai and time < sampai
    if sigIn[1]
        bookIn.add(open, low[1], na, bar_index + N - 1)
    if sigOut[1]
        bookOut.add(open, low[1], na, bar_index + N - 1)
bookIn.step(fee, sampai)
bookOut.step(fee, sampai)
var table t = table.new(position.top_right, 2, 6, bgcolor = color.new(color.white, 5), border_width = 1)
if barstate.islast
    table.cell(t, 0, 0, "Mendekat band bawah (1,5)", text_color = color.black)
    table.cell(t, 1, 0, str.tostring(bookIn.n) + " trade", text_color = color.black)
    table.cell(t, 0, 1, "Win rate (SL low, keluar " + str.tostring(N) + " candle)", text_color = color.black)
    table.cell(t, 1, 1, bookIn.n > 0 ? str.tostring(100.0 * bookIn.win / bookIn.n, "#.#") + "%" : "-", text_color = color.green)
    table.cell(t, 0, 2, "Total setelah fee", text_color = color.black)
    table.cell(t, 1, 2, (bookIn.sumR >= 0 ? "+" : "") + str.tostring(bookIn.sumR, "#.#") + "R", text_color = bookIn.sumR >= 0 ? color.green : color.red)
    table.cell(t, 0, 3, "Profit factor", text_color = color.black)
    table.cell(t, 1, 3, bookIn.gl > 0 ? str.tostring(bookIn.gp / bookIn.gl, "#.##") : "-", text_color = color.black)
    table.cell(t, 0, 4, "Pembanding: tembus band 2", text_color = color.black)
    table.cell(t, 1, 4, str.tostring(bookOut.n) + " trade", text_color = color.gray)
    table.cell(t, 0, 5, "Total pembanding", text_color = color.black)
    table.cell(t, 1, 5, (bookOut.sumR >= 0 ? "+" : "") + str.tostring(bookOut.sumR, "#.#") + "R", text_color = bookOut.sumR >= 0 ? color.green : color.red)

← 返回列表