无法在松脚本公式中为标签添加百分比

时间:2020-03-25 06:11:29

标签: label percentage pine-script

我对pinescript很陌生,在将向上和向下百分比添加到公式标签上时遇到很多麻烦。我想在每个下脚底部的标签上绘制百分比下降,在每个上脚顶部的标签上绘制百分比增长。 我真的很感谢您的帮助。

//@version=4
study("ZigZag", overlay=true)

useclose = input(true, title="Use close to confirm", type=input.bool)

var bool uptrend = na

var float tophigh = na(uptrend[1]) ? high : na
var float toplow = na(uptrend[1]) ? low : na

var float bothigh = na(uptrend[1]) ? high : na
var float botlow = na(uptrend[1]) ? low : na

newtop = na(uptrend[1])
newbot = na(uptrend[1])
changed = na(uptrend[1])

if na(uptrend[1])
    uptrend := close > close[1]
else

    if uptrend[1]
        if na(tophigh[1]) or high > tophigh[1]
            tophigh := high
            toplow := low
            newtop := true

        if na(toplow[1]) or ((close < toplow[1] and useclose) or (low < toplow[1] and not useclose))
            bothigh := high
            botlow := low
            newbot := true
            changed := true
            uptrend := false
    else
        if na(botlow[1]) or low < botlow[1]
            bothigh := high
            botlow := low
            newbot := true

        if na(bothigh[1]) or ((close > bothigh[1] and useclose) or (high > bothigh[1] and not useclose))
            tophigh := high
            toplow := low
            changed := true
            newtop := true
            uptrend := true


// labels

float l = 0.0
float oldl = 0.0
float h = 0.0
float oldh = 0.0

float perc = 0.0
float perc2 = 0.0
float perc3 = 0.0


if changed and not barstate.isrealtime
    if uptrend
        for i = 0 to 100
            if newbot[i]
                l := low [i]

                oldl := valuewhen(changed and uptrend, l, 1)

                perc := (l - oldl) * 100 / oldl    

                label.new(bar_index[i], na,tostring(low[i], "#,###,###.##") + "\n" + (perc > 0 ? "+" : "") + tostring(round(perc))+"%" ,
                  color=#AD2B4E,
                  textcolor=color.white,
                  style=label.style_labelup, yloc=yloc.belowbar)

                break
    else
        for i = 0 to 100
            if newtop[i]
                h := high [i]

                oldh := valuewhen(changed and not uptrend, h, 1)
                perc2 := (h - oldh) * 100 / oldh

                label.new(bar_index[i], na, tostring(high[i], "#,###,###.##") + " \n" + (perc2 > 0 ? "+" : "") + tostring(round(perc2)) + "%",
                  color=#67E300,
                  textcolor=color.white,
                  style=label.style_labeldown, yloc=yloc.abovebar)
                break

// zig zag
getBot() =>
    int index = na
    x = not uptrend ? 1 : 0
    for i = x to 100
        if newbot[i]
            index := i
            break
    index

getTop() =>
    int index = na
    x = uptrend ? 1 : 0
    for i = x to 100
        if newtop[i]
            index := i
            break
    index

ziglow = getBot()
zighigh = getTop()

if changed[1]
    if uptrend[1]
        l = line.new(bar_index[ziglow[1]+1], low[ziglow[1]+1], bar_index[zighigh[1]+1], high[zighigh[1]+1], width=2, color=#f0f0ff)
    else
        l = line.new(bar_index[ziglow[1]+1], low[ziglow[1]+1], bar_index[zighigh[1]+1], high[zighigh[1]+1], width=2, color=#f0f0ff)

zig zag image

0 个答案:

没有答案