Pinescript - Decimal Input Option

Viewed 488

I want to have the option in the settings to input how many Decimals show for current price on a label, but cant get it to work using label.new

So for the input option, it would be something like

DecOption = input(title="Decimal Option", options=["No Decimal", "One Decimal", "Two Decimal"],
     defval="No Decimal")

Statically decimal output is not a problem using "#.##" ... but having it selectable as a input in the settings is harder for me, any suggestions ?

//@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])
1 Answers

The format string is explained in the reference manual for the tostring() function.
Format string. Optional argument, default value is '#.##########'.
To display trailing zeros use 0 instead of a '#' symbol. For example, '#.000'.

Example with 4 decimals:

//@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])

Edit 1 : in response to this comment
This example gives you the option to input the number of decimals as an integer.
It can also limit the number of decimals by the Min tick value for the current symbol.
You can also decide wether or not to show trailing zero's for the decimals.
When you leave the symbol input blank, it will default to the charted symbol name.

//@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)
Related