仅绘制最后2个相连的摆动高/低(重绘指示器)

时间:2019-09-10 19:32:43

标签: pine-script

我编写了一个指标,该指标在发生移动平均交叉时绘制最高(最低)。

下面的代码很好用,但是存在一个问题,它保持了十字架之后打破的先前高点。

我希望这些线只能从一个最低的低点到一个最高的高点相连,因为交叉像之字形指示器一样。

下图更好地解释了,绿线是指标绘制的。黑线是它应该绘制的。

https://imgur.com/gcblrOA

有没有解决的办法?谢谢


//@version=4
study(title="MA Cross", overlay=true, max_bars_back=3000)

MA1 = sma(close, 20)
MA2 = sma(close, 100)

Highest(src, len) =>
    max = high
    for i = 1 to len
        if src[i] > max
            max := src[i]
    max

Lowest(src, len) =>
    min = src[0]
    for i = 1 to len
        if src[i] < min
            min := src[i]
    min


trend = 0
trend := na(trend[1]) ? 1 : trend[1]

LL = 0.0
HH = 0.0

HH := Highest(high, barssince(cross(MA1, MA2)))
LL := Lowest(low, barssince(cross(MA1, MA2)))

HighLow = 0.0

recent = 0

HL = 0.0

if(MA1 > MA2)

    recent := 1

    for i = 1 to 200
        if(MA1[i] < MA2[i])
            break

        if(high < high[i])
            recent := -1
            break

    HL := na
    if(recent < 0)
        HighLow := na

    else        
        HighLow := high
        HL := high


else
    recent := 1
    HL := na
    for i = 1 to 200
        if(MA1[i] > MA2[i])
            break

        if(low > low[i])
            recent := -1
            break

    if(recent < 0)
        HighLow := na

    else        
        HighLow := low
        HL := low


plot(series= HighLow, color = color.green, linewidth=3)
plot(series=MA1, color = color.aqua, linewidth=1)
plot(series=MA2, color = color.orange, linewidth=1)

1 个答案:

答案 0 :(得分:0)

正如您所说,这不会在所有数据集上绘制线,因为它使用line.new()。尽管如此,以防万一:

//@version=4
study(title="MA Cross", overlay=true, max_bars_back=3000)

MA1 = sma(close, 20)
MA2 = sma(close, 100)

// Highest/lowest hi/lo during up/dn trend.
var hi = 0.
var lo = 10e20
// Bar index of highest/lowest hi/lo.
var hiBar = 0
var loBar = 0
// Crosses.
crossUp = crossover(MA1, MA2)
crossDn = crossunder(MA1, MA2)
upTrend = MA1 > MA2

// Draw line in past when a cross occurs.
if crossUp or crossDn
    line.new(bar_index[bar_index - hiBar], high[bar_index - hiBar], bar_index[bar_index - loBar], low[bar_index - loBar], xloc.bar_index, extend.none, color.black)

// Reset hi/lo and bar index on crosses.
if crossUp
    hi := high
    hiBar := bar_index
else
    if crossDn
        lo := low
        loBar := bar_index

// Update higher/lower hi/lo during trend.
if upTrend and high > hi
    hi := high
    hiBar := bar_index
else
    if not upTrend and low < lo
        lo := low
        loBar := bar_index

plot(series=MA1, color = color.aqua, linewidth=1)
plot(series=MA2, color = color.orange, linewidth=1)

// Debugging.        
// plot(hi, "High", color.green, 1, plot.style_circles)
// plot(lo, "Low", color.red, 1, plot.style_circles)
// plotchar(bar_index, "bar_index", "", location.top)
// plotchar(hiBar, "hiBar", "", location.top, color = color.green)
// plotchar(loBar, "loBar", "", location.top, color = color.red)
// bgcolor(upTrend ? color.green : color.red)

enter image description here