0

In the example below pine script converts str.tonumber('15min') correctly to 15.00 as it should. However if we provide the argument as a function parameter it fails to convert. Bug has been reported.

//@version=5
indicator("My script", "m", true)

convert() =>
    foo = '15min'
    str.tostring(str.tonumber(foo))

convert(simple string foo) => str.tostring(str.tonumber(foo))

if barstate.islast
    label.new(bar_index, high * 1.001, 'Correct result: ' + convert()) // result correctly 15
    foo = '15min'
    label.new(bar_index, high, 'Inconsistent result: ' + convert(foo)) // result NaN
elod008
  • 1,227
  • 1
  • 5
  • 15

2 Answers2

0

if you code like this, it works :

convert(message) => (str.tonumber(message))

if barstate.islast
    foo = '15min'
    label.new(bar_index, high, 'Inconsistent result: ' + str.tostring(convert('15min')) ) 
G.Lebret
  • 2,826
  • 2
  • 16
  • 27
  • Thank you very much for your response! Unfortunately my logic is more complex and relies on dynamic state changes so I cannot use this version. I need to be able to pass a variable as an argument (which is still of type simple string. just mentioning this not to get into confusion here) – elod008 Nov 26 '22 at 16:38
0

Answer from the pine team (2022-11-28):

The function call automatically removes the 'min' part and returns the correct value of int(15), but this explicit check is done in the script's global scope but not inside a local function block - in that case, the str.tostring() receives 'na' value.

So it seems it is designed just like that. Ok.

elod008
  • 1,227
  • 1
  • 5
  • 15