如何使用新的 Pine Script V5 创建价格范围工具?使用 input.price 和 input.time 的 2 点之间的百分比差异

How to create Price Range Tool using new Pine Script V5? Percentage difference between 2 points using input.price and input.time

PineScript V5 允许我们通过鼠标单击来使用 input.timeinput.price

我想知道如何编码:

蜡烛高点(用 input.time 选择)和 input.price(用鼠标在蜡烛上方单击选择。

之间的百分比差异

我想使用 pine 脚本创建类似于价格范围工具的东西。

请帮忙!

start_ts = input.time(timestamp("20 Jul 2021 00:00 +0300"), confirm = true, title = "Time of high")
end_price = input.price(defval = 0, confirm = true, title = "Price")


var box myBox = box.new(left = na, top = na, right = na, bottom = na)
var label myLabel = label.new(x = na, y = na, style = label.style_label_right, color = #00000000, textcolor = color.white)

if time == start_ts
    box.set_lefttop(myBox, left = bar_index, top = end_price)
    box.set_rightbottom(myBox, right = bar_index + 1, bottom = high)
    label.set_text(myLabel, text = str.tostring(math.round(end_price / high * 100, 2) - 100) + "%")
    label.set_xy(myLabel, x = bar_index + 1, y = math.avg(high, end_price))
else if time > start_ts
    box.set_right(myBox, right = bar_index + 1)
    label.set_x(myLabel, x = bar_index + 1)
    

感谢@rumpypumpydumpy 的帮助,这部分是我最需要的 不知道该用什么。

(math.round(end_price / high * 100, 2) - 100) + "%")

基于此,我会把我的代码留在这里,以备将来有人需要它:

//I use part of CAGR code in this code.
//@version=5
indicator("Price Range", "Price Range Tool", overlay = true)


color TVBLUE = #1848CC
int   DEFAULT_POINT1 = timestamp("2021-09")
int   DEFAULT_POINT2 = timestamp("2021-10")

// These two time inputs with `confirm = true` launch the interactive input mode     when adding the script to the chart.
var int    entryTimeInput      = input.time(DEFAULT_POINT1, "Point 1", confirm =     true)
var int    exitTimeInput       = input.time(DEFAULT_POINT2, "Point 2", inline = "2", confirm = true)
var float  exitPriceInput      = input.price(10000, "", inline = "2", confirm = true)


getPriceForTime(t)=>
     var float price = na
     if time[1] <= t and time >= t and na(price)
         price := close
     price

int   entryTime     = math.min(entryTimeInput, exitTimeInput)
int   exitTime      = math.max(entryTimeInput, exitTimeInput)
float entryPrice    = getPriceForTime(entryTime)
float exitPrice     = getPriceForTime(exitTime)
Compare =  math.round((exitPriceInput / entryPrice) * 100, 2) - 100

//Plot Line
line.new(entryTime,  entryPrice, exitTime, exitPriceInput, xloc = xloc.bar_time,     color = color.new(TVBLUE, 90), style = line.style_dotted, width = 1)

// Circles
label.new(entryTime, entryPrice, xloc = xloc.bar_time, color = TVBLUE, style =     label.style_circle, size = size.tiny)
//label.new(exitTime,  exitPriceInput,  xloc = xloc.bar_time, color = TVBLUE, style = label.style_circle, size = size.tiny)

// Percentage Label
lblText = str.format("{0, number}%", Compare)
label.new(exitTime, exitPriceInput, text = lblText, xloc = xloc.bar_time, yloc =     yloc.price, style = label.style_label_lower_left, color = TVBLUE, textcolor =     color.white, size = size.normal)