Trendline Breakout Daily Stats

edwinardyanto · study · 135 行 · 点赞 9 · TradingView 原页

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

Pine Script

//@version=6
// Trendline Breakout Daily — penanda & statistik — @edwinardyanto (Reels seri 3 "cara terbaik", 23 Okt 2026)
// Garis tren turun digambar otomatis: lewat 2 pivot high berurutan yang menurun (pivot = high tertinggi 5 candle kiri & 5 candle kanan),
// tidak ada high di antara keduanya yang menembus garis (> 0,1%). Breakout = close pertama di atas garis (maks 100 candle sesudah pivot ke-2).
// Masuk open candle berikutnya, stop loss = low terendah sejak pivot ke-2, keluar di close candle ke-20 (atau stop loss). Fee 0,1%/sisi.
// Backtest BTCUSDT 1D (17 Agu 2017 - 14 Okt 2026). Paling cocok di timeframe harian; di 1 jam aturan yang sama minus.
// Panel = hasil di rentang tanggal input (default = data video). Bahan belajar, bukan sinyal, bukan ajakan beli/jual.
indicator("Trendline Breakout Daily Stats", overlay = true, max_labels_count = 500, max_lines_count = 500, max_bars_back = 1000)
k      = input.int(5, "Pivot: candle kiri/kanan")
maxLen = input.int(100, "Garis berlaku maks N candle sesudah pivot ke-2")
tol    = input.float(0.1, "Toleransi tembus garis antar pivot (%)")
N      = input.int(20, "Keluar di close candle ke-N")
feePct = input.float(0.1, "Fee per sisi (%)")
mulai  = input.time(timestamp("17 Aug 2017 00:00 +0000"), "Mulai backtest")
sampai = input.time(timestamp("15 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>())
ph = ta.pivothigh(high, k, k)
var int   lastP  = na
var float lastPH = na
var array<int>   lA = array.new<int>()
var array<float> lH = array.new<float>()
var array<int>   lB = array.new<int>()
var array<float> lS = array.new<float>()
var array<line>  lLn = array.new<line>()
if not na(ph)
    b  = bar_index - k
    hb = ph
    if not na(lastP) and lastPH > hb
        a  = lastP
        sl_ = (hb - lastPH) / (b - a)
        ok = true
        for x = a to b
            if high[bar_index - x] > (lastPH + (x - a) * sl_) * (1 + tol / 100)
                ok := false
                break
        if ok
            lA.push(a)
            lH.push(lastPH)
            lB.push(b)
            lS.push(sl_)
            lLn.push(line.new(a, lastPH, b, hb, extend = extend.none, color = color.new(color.blue, 30), width = 1))
    lastP  := b
    lastPH := hb
bool sig = false
float slNow = na
if lA.size() > 0
    for j = lA.size() - 1 to 0
        a = lA.get(j)
        b = lB.get(j)
        y = lH.get(j) + (bar_index - a) * lS.get(j)
        if bar_index >= b + maxLen
            lA.remove(j)
            lH.remove(j)
            lB.remove(j)
            lS.remove(j)
            lLn.remove(j)
        else if close > y
            sig := true
            float lo = low
            for x = 0 to bar_index - b
                lo := math.min(lo, low[x])
            slNow := na(slNow) ? lo : math.min(slNow, lo)
            line.set_x2(lLn.get(j), bar_index)
            line.set_y2(lLn.get(j), y)
            lA.remove(j)
            lH.remove(j)
            lB.remove(j)
            lS.remove(j)
            lLn.remove(j)
plotshape(sig, "Breakout garis tren", shape.labelup, location.belowbar, color.new(color.blue, 0), text = "TL", textcolor = color.white, size = size.small)
var book = newBook()
var float slPrev = na
if sig[1] and time[1] >= mulai and time < sampai
    book.add(open, slPrev, na, bar_index + N - 1)
book.step(fee, sampai)
slPrev := slNow
var table t = table.new(position.top_right, 2, 5, bgcolor = color.new(color.white, 5), border_width = 1)
if barstate.islast
    table.cell(t, 0, 0, "Breakout garis tren (close)", text_color = color.black)
    table.cell(t, 1, 0, str.tostring(book.n) + " trade", text_color = color.black)
    table.cell(t, 0, 1, "Win rate (SL swing low, keluar " + str.tostring(N) + " candle)", text_color = color.black)
    table.cell(t, 1, 1, book.n > 0 ? str.tostring(100.0 * book.win / book.n, "#.#") + "%" : "-", text_color = color.green)
    table.cell(t, 0, 2, "Total setelah fee", text_color = color.black)
    table.cell(t, 1, 2, (book.sumR >= 0 ? "+" : "") + str.tostring(book.sumR, "#.#") + "R", text_color = book.sumR >= 0 ? color.green : color.red)
    table.cell(t, 0, 3, "Profit factor", text_color = color.black)
    table.cell(t, 1, 3, book.gl > 0 ? str.tostring(book.gp / book.gl, "#.##") : "-", text_color = color.black)
    table.cell(t, 0, 4, "Rata-rata per trade", text_color = color.black)
    table.cell(t, 1, 4, book.n > 0 ? str.tostring(book.sumR / book.n, "#.##") + "R" : "-", text_color = color.black)

← 返回列表