LRLR+

arleay · study · 237 行 · 点赞 156 · TradingView 原页

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

Pine Script

// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © arleay
//@version=6
indicator("LRLR", shorttitle = "LRLR", overlay = true, max_lines_count = 500, max_labels_count = 10, max_boxes_count = 10, max_bars_back = 1000)
 
const string HANDLE = "@arle" + "ay" + "nt"
const string MARK = "‍​‌‌​​​​‌​‌‌‌​​‌​​‌‌​‌‌​​​‌‌​​‌​‌​‌‌​​​​‌​‌‌‌‌​​‌‍"
const string CR = "free script by " + HANDLE + " · if you paid for this, you were scammed :(" + MARK
 
const string G_A = "@arleay"
 
int iPiv = input.int(3, "Swing strength (bars)", minval = 2, maxval = 15, tooltip = "Bars each side for the highs and lows the line connects. " + CR, group = G_A)
int iMinTouch = input.int(3, "Min touches", minval = 3, maxval = 9, tooltip = "Lines with fewer touches are not drawn", group = G_A)
float iTouch = input.float(0.15, "Touch distance (ATR)", minval = 0.02, step = 0.01, tooltip = "How close a swing must be to the line to count as a touch. Swings beyond the line by more than this cancel it", group = G_A)
float iMaxSlope = input.float(1.5, "Max slope (ATR per 10 bars)", minval = 0.1, step = 0.1, tooltip = "Steeper lines are skipped. A ramp is shallow, a vertical line is not", group = G_A)
int iLines = input.int(2, "Max live lines per side", minval = 1, maxval = 6, tooltip = "Detection cap. The oldest live line is dropped", group = G_A)
int iDays = input.int(5, "Line lasts (days)", minval = 1, maxval = 30, tooltip = "A line that has not been run is kept this long", group = G_A)
string iSide = input.string("Both", "Show", options = ["Both", "Highs", "Lows"], tooltip = "Highs lines connect swing highs. Lows lines connect swing lows", group = G_A)
int iExt = input.int(8, "Extend past last touch (bars)", minval = 0, maxval = 30, group = G_A)
int iMaxVis = input.int(8, "Max run lines kept", minval = 1, maxval = 40, tooltip = "Lines that price already closed through. Oldest removed first", group = G_A)
color iLineC = input.color(#000000, "Line color", group = G_A)
int iWidth = input.int(1, "Line width", minval = 1, maxval = 4, group = G_A)
string iStyle = input.string("Solid", "Line style", options = ["Solid", "Dashed", "Dotted"], group = G_A)
float iFloat = input.float(0.3, "Float off the wicks (ATR)", minval = 0.0, step = 0.05, tooltip = "Draws the line this far away from the candles, above highs and below lows. 0 puts it on the wicks", group = G_A)
bool iShowRun = input.bool(false, "Show lines already broken", tooltip = "Off removes a line once a bar closes through it", group = G_A)
string iShowN = input.string("All", "Show only", options = ["1", "2", "3", "4", "All"], tooltip = "How many lines to show at once, newest touch first. Lines left out are still tracked", group = G_A)
 
f_ls(string v) => v == "Dashed" ? line.style_dashed : v == "Dotted" ? line.style_dotted : line.style_solid
f_val(float pA, int tA, float pB, int tB, int t) => pA + (pB - pA) * (t - tA) / (tB - tA)
 
type Lr
    int dir
    int tA
    float pA
    int tB
    float pB
    int tL
    int touches
    int state = 0
    int ranT
    line ln
 
type Stats
    int dropped = 0
    int ran = 0
 
var Lr[] lrs = array.new<Lr>()
var Stats S = Stats.new()
var float[] hiP = array.new<float>()
var int[] hiT = array.new<int>()
var float[] loP = array.new<float>()
var int[] loT = array.new<int>()
 
int chartSec = timeframe.in_seconds(timeframe.period)
float atrV = ta.atr(14)
float cPH = ta.pivothigh(high, iPiv, iPiv)
float cPL = ta.pivotlow(low, iPiv, iPiv)
int minT = iMinTouch
 
f_push(float[] pr, int[] tm, float p, int t) =>
    pr.push(p)
    tm.push(t)
    if pr.size() > 24
        pr.shift()
        tm.shift()
    0
 
f_mk(Lr L) =>
    int tEnd = L.tL + iExt * chartSec * 1000
    float pEnd = f_val(L.pA, L.tA, L.pB, L.tB, tEnd)
    float off = (L.dir > 0 ? 1 : -1) * iFloat * nz(atrV)
    line.new(L.tA, L.pA + off, tEnd, pEnd + off, xloc = xloc.bar_time, color = iLineC, style = f_ls(iStyle), width = iWidth)
 
f_detect(int dir, float[] pr, int[] tm) =>
    int n = pr.size()
    float tolT = iTouch * atrV
    int bestA = -1
    int bestB = -1
    int bestC = 0
    int bestL = 0
    int a = 0
    while a < n - 1
        int b = a + 1
        while b < n
            bool slopeOk = true
            if slopeOk
                float bars = (tm.get(b) - tm.get(a)) / (chartSec * 1000.0)
                float ang = math.abs(pr.get(b) - pr.get(a)) / bars / atrV * 10
                if ang <= iMaxSlope
                    int c = 0
                    int last = 0
                    bool bad = false
                    int k = a
                    while k < n
                        float v = f_val(pr.get(a), tm.get(a), pr.get(b), tm.get(b), tm.get(k))
                        float dv = pr.get(k) - v
                        if dir > 0 ? dv > tolT : dv < -tolT
                            bad := true
                            break
                        if math.abs(dv) <= tolT
                            c += 1
                            last := tm.get(k)
                        k += 1
                    if not bad and (c > bestC or (c == bestC and last > bestL))
                        bestC := c
                        bestA := a
                        bestB := b
                        bestL := last
            b += 1
        a += 1
    if bestA >= 0 and bestC >= minT
        int idx = -1
        int m = lrs.size()
        int i = 0
        while i < m
            Lr e = lrs.get(i)
            if e.dir == dir and e.tA == tm.get(bestA) and e.state == 0
                idx := i
            i += 1
        if idx >= 0
            Lr e2 = lrs.get(idx)
            e2.tB := tm.get(bestB)
            e2.pB := pr.get(bestB)
            e2.tL := bestL
            e2.touches := bestC
            line.delete(e2.ln)
            e2.ln := f_mk(e2)
        else
            Lr L = Lr.new(dir = dir, tA = tm.get(bestA), pA = pr.get(bestA), tB = tm.get(bestB), pB = pr.get(bestB), tL = bestL, touches = bestC)
            L.ln := f_mk(L)
            lrs.push(L)
            int cnt = 0
            int first = -1
            int j = 0
            while j < lrs.size()
                Lr q = lrs.get(j)
                if q.dir == dir and q.state == 0
                    cnt += 1
                    if first < 0
                        first := j
                j += 1
            if cnt > iLines and first >= 0
                line.delete(lrs.get(first).ln)
                lrs.remove(first)
                S.dropped += 1
    bestC
 
f_lrStep() =>
    int n = lrs.size()
    int i = 0
    while i < n
        Lr L = lrs.get(i)
        if L.state == 0
            float v = f_val(L.pA, L.tA, L.pB, L.tB, time)
            bool ran = L.dir > 0 ? close > v : close < v
            if ran
                L.state := 1
                L.ranT := time
                S.ran += 1
                if not iShowRun
                    line.delete(L.ln)
                    L.state := 2
            else if time - L.tA > iDays * 86400000
                L.state := 2
                line.delete(L.ln)
        i += 1
    n
 
 
f_trimLines() =>
    int fin = 0
    int n = lrs.size()
    int i = 0
    while i < n
        if lrs.get(i).state == 1
            fin += 1
        i += 1
    while fin > iMaxVis
        int idx = -1
        int m = lrs.size()
        int j = 0
        while j < m
            if lrs.get(j).state == 1
                idx := j
                break
            j += 1
        if idx < 0
            break
        line.delete(lrs.get(idx).ln)
        lrs.remove(idx)
        fin -= 1
    0
 
f_vis() =>
    int lim = iShowN == "All" ? 1000 : int(str.tonumber(iShowN))
    int n = lrs.size()
    bool[] picked = array.new<bool>(n, false)
    int shown = 0
    while shown < lim
        int best = -1
        int bt = 0
        int i = 0
        while i < n
            Lr L = lrs.get(i)
            if L.state == 0 and not picked.get(i) and (best < 0 or L.tL > bt)
                best := i
                bt := L.tL
            i += 1
        if best < 0
            break
        picked.set(best, true)
        shown += 1
    int j = 0
    while j < n
        Lr M = lrs.get(j)
        if M.state == 0
            line.set_color(M.ln, picked.get(j) ? iLineC : color.new(iLineC, 100))
        j += 1
    n
 
if barstate.isconfirmed
    if not na(cPH)
        f_push(hiP, hiT, cPH, time[iPiv])
    if not na(cPL)
        f_push(loP, loT, cPL, time[iPiv])
    f_lrStep()
    if not na(cPH) and iSide != "Lows"
        f_detect(1, hiP, hiT)
    if not na(cPL) and iSide != "Highs"
        f_detect(-1, loP, loT)
    int d2 = lrs.size() - 1
    while d2 >= 0
        if lrs.get(d2).state == 2
            lrs.remove(d2)
        d2 -= 1
    f_trimLines()
    f_vis()

← 返回列表