扫描指示器 - 如何仅在被触摸时显示线条

时间:2021-07-26 16:11:53

标签: pine-script

所以我有一个扫描指示器,它从图表上的分形到最后一个触及该高度的灯芯绘制线条。但现在我想要的是只有在它被触摸时才显示这条线,因为我不想要一个凌乱的图表。我可以通过禁用线条来做到这一点,但我想看看哪个被触摸了。

// First it plots the fractals on the chart, then it draws lines from them
// to the last wick that touched that height. You can add sweep alerts, once
// per bar close, so that you get notified when a wick was touched.
// {TradingView limits the max nr of drawings to 50 so I've limited the
// historical lines to 20 (20 for the bottom and 20 for the top fractals).}
// Updated to 50 historical lines.
// Updated to show only recent swings(possibly sweeps)

//@version=4


// Define our title and that this script will overlay the candles.
study("Sweep Hunter",shorttitle="Sweep", overlay=true, max_bars_back=144)

n = input(title="Periods", defval=2, minval=2, type=input.integer)
fractalBars = input(title="3 or 5 Bar Fractal", defval="3", options=["3", "5"])
arrayLength = input(title="Max nr of historical fractals", defval=20, minval=1, maxval=50, type=input.integer)
plotActiveLines = input(title="Plot active lines", defval=true)

var upFractalArray = array.new_int(arrayLength), var dnFractalArray = array.new_int(arrayLength)

var isAlertLow = bool(na), var isAlertHigh = bool(na)

// Fractal Code
dnFractal = if fractalBars == "5"
    dnFractal5 = (high[n-2] < high[n]) and (high[n-1] < high[n]) and (high[n+1] < high[n]) and (high[n+2] < high[n])
else
    if fractalBars == "3"
        dnFractal3 = (high[n-1] < high[n]) and (high[n+1] < high[n]) 

upFractal = if fractalBars == "5"
    upFractal5 = (low[n-2] > low[n]) and (low[n-1] > low[n]) and (low[n+1] > low[n]) and (low[n+2] > low[n])
else
    if fractalBars == "3"
        upFractal3 = (low[n-1] > low[n]) and (low[n+1] > low[n]) 

// Active fractals
if dnFractal
    array.push(dnFractalArray, bar_index[n])
    array.shift(dnFractalArray)
if upFractal
    array.push(upFractalArray, bar_index[n])
    array.shift(upFractalArray)

if barstate.islast
    for i = 0 to array.size(dnFractalArray) - 1
        dnBarIndex = array.get(dnFractalArray, i)
        l = line.new(color=color.red, x1=dnBarIndex, y1=high[bar_index-dnBarIndex], x2=bar_index, y2=high[bar_index-dnBarIndex])
        wickFound = false
        limitCount = 0
        // Find out if we have a higher fractal
        for t = i to array.size(dnFractalArray) - 1
            nextFractalIndex = array.get(dnFractalArray, t)
            if high[bar_index - dnBarIndex] < high[bar_index-nextFractalIndex]
                limitCount := bar_index-nextFractalIndex
                break
        // Delete line if it will only hit a closed candle
        // Else plot the line up to the last wick
        for j = bar_index - dnBarIndex - 1 to limitCount
            if high[bar_index - dnBarIndex] < high[j]
                if high[bar_index - dnBarIndex] < close[j]
                    if not wickFound
                        line.delete(l)
                    break
                else
                    wickFound := true
                    line.set_x2(l, bar_index - j)
        if not plotActiveLines and line.get_x2(l) == bar_index
            line.set_color(l, color.new(color.red, 100))
        // Alert condition
        isAlertHigh := line.get_x2(l) == bar_index and high[bar_index-dnBarIndex] < high
        
    for i = 0 to array.size(upFractalArray) - 1
        upBarIndex = array.get(upFractalArray, i)
        l = line.new(color=color.red, x1=upBarIndex, y1=low[bar_index-upBarIndex], x2=bar_index, y2=low[bar_index-upBarIndex])
        wickFound = false
        limitCount = 0
        // Find out if we have a lower fractal
        for t = i to array.size(upFractalArray) - 1
            nextFractalIndex = array.get(upFractalArray, t)
            if low[bar_index - upBarIndex] > low[bar_index-nextFractalIndex]
                limitCount := bar_index-nextFractalIndex
                break
        // Delete line if it will only hit a closed candle
        // Else plot the line up to the last wick
        for j = bar_index - upBarIndex - 1 to limitCount
            if low[bar_index - upBarIndex] > low[j]
                if low[bar_index - upBarIndex] > close[j]
                    if not wickFound
                        line.delete(l)
                    break
                else
                    wickFound := true
                    line.set_x2(l, bar_index - j)
        if not plotActiveLines and line.get_x2(l) == bar_index
            line.set_color(l, color.new(color.red, 100))
        // Alert condition
        isAlertLow := low[bar_index-upBarIndex] > low and line.get_x2(l) == bar_index

alertcondition(isAlertHigh, title="Sweep High", message="Sweep Hunter found a higher sweep")
alertcondition(isAlertLow, title="Sweep Low", message="Sweep Hunter found a lower sweep")
alertcondition(isAlertLow or isAlertHigh, title="Sweep", message="Sweep Hunter found a sweep")

//plotshape(dnFractal, style=shape.triangledown, location=location.abovebar,  offset=-n, color=color.new(color.blue, 15))
//plotshape(upFractal, style=shape.triangleup, location=location.belowbar,  offset=-n, color=color.new(color.gray, 15))

0 个答案:

没有答案