pine-script - Pinescript - 十进制输入选项
问题描述
我想在设置中选择输入标签上当前价格显示多少小数,但无法使用 label.new 让它工作
所以对于输入选项,它会像
DecOption = input(title="Decimal Option", options=["No Decimal", "One Decimal", "Two Decimal"],
defval="No Decimal")
使用“#.##”静态十进制输出不是问题......但是在设置中选择它作为输入对我来说更难,有什么建议吗?
//@version=4
study("Current Price", overlay=true)
symbolName = input(title="Type Symbol Or Leave Blank", defval="BTC")
sizeOption = input(title="Label Size", type=input.string,options=["Auto", "Huge", "Large", "Normal", "Small", "Tiny"],defval="Large")
labelSize = (sizeOption == "Huge") ? size.huge :
(sizeOption == "Large") ? size.large :
(sizeOption == "Small") ? size.small :
(sizeOption == "Tiny") ? size.tiny :
(sizeOption == "Auto") ? size.auto :
size.normal
l = label.new(bar_index, na, text= symbolName +tostring( close, " $ #"),
color=close >= open ? color.green : color.red,
textcolor=color.white,
style=label.style_labeldown, yloc=yloc.abovebar, size=labelSize)
label.delete(l[1])
解决方案
格式字符串在tostring()函数的参考手册中有说明。
格式化字符串。可选参数,默认值为'#.##########'。
要显示尾随零,请使用 0 而不是“#”符号。例如,“#.000”。
带有 4 位小数的示例:
//@version=4
study("Current Price", overlay=true)
symbolName = input(title="Type Symbol Or Leave Blank", defval="BTC")
sizeOption = input(title="Label Size", type=input.string,options=["Auto", "Huge", "Large", "Normal", "Small", "Tiny"],defval="Large")
labelSize = (sizeOption == "Huge") ? size.huge :
(sizeOption == "Large") ? size.large :
(sizeOption == "Small") ? size.small :
(sizeOption == "Tiny") ? size.tiny :
(sizeOption == "Auto") ? size.auto :
size.normal
l = label.new(bar_index, na, text= symbolName +tostring( close, " $ #.0000"),
color=close >= open ? color.green : color.red,
textcolor=color.white,
style=label.style_labeldown, yloc=yloc.abovebar, size=labelSize)
label.delete(l[1])
编辑 1:响应此评论
此示例为您提供了将小数位数作为整数输入的选项。它还可以通过当前交易品种的最小刻度值来
限制小数位数。
您还可以决定是否为小数显示尾随零。
当您将符号输入留空时,它将默认为图表符号名称。
//@version=4
study("Current Price", "CP", overlay=true)
LS0 = "Auto", LS1 = "Huge", LS2 = "Large", LS3 = "Normal", LS4 = "Small", LS5 = "Tiny"
var string i_symbolName = input("BTC", "Type Symbol Or Leave Blank", type=input.string)
var int i_decimals = input(4, "Decimal option", type=input.integer, minval=0, maxval=10)
var bool i_limit_mintick = input(false, "Decimals limited by mintick", type=input.bool)
var bool i_trailing_zero = input(true, "Show decimal trailing zero's", type=input.bool)
var string i_sizeOption = input(LS2, "Label Size", type=input.string, options=[LS0, LS1, LS2, LS3, LS4, LS5])
var string labelText = na
var color labelColor = na
var string labelSize = (i_sizeOption == LS0) ? size.auto :
(i_sizeOption == LS1) ? size.huge :
(i_sizeOption == LS2) ? size.large :
(i_sizeOption == LS3) ? size.normal :
(i_sizeOption == LS4) ? size.small :
(i_sizeOption == LS5) ? size.tiny :
size.normal
var label l = label.new(na, na, na, yloc=yloc.abovebar, style=label.style_label_down, textcolor=color.white, size=labelSize)
var string strFormatBase = " $ #"
var string strFormat = na
var string symbolName = i_symbolName == "" ? syminfo.ticker : i_symbolName
if barstate.islast
strFormat := strFormatBase + (i_decimals ? "." : "")
for i = 1 to i_limit_mintick ? str.length(tostring(syminfo.mintick))-2 : i_decimals
strFormat := strFormat + (i_trailing_zero ? "0" : "#")
labelText := symbolName + tostring(close, strFormat)
labelColor := close >= open ? color.green : color.red
label.set_x(l, bar_index)
label.set_text(l, labelText)
label.set_color(l, labelColor)
推荐阅读
- node.js - 无法等待 fs.appendFile
- javascript - node.js 将 json 附加到未使用数组的 JSON 文件中
- typescript - 使接口的一部分不是可选的
- python - 为什么某些关键输入在此示例中不起作用?(游戏)
- reactjs - 如何在本机反应中禁用firebase电话号码身份验证reCapcha?
- javascript - 执行时不要再次执行函数
- excel - 复制到另一个工作表中动态创建的行
- docker - Compose up 重新创建其他目录创建的 compose Service
- python - 如何将单词添加到条目框中作为推荐(Tkinter)?
- r - r 中的 cut 方法,breaks 参数只有一个数字