The tostring() function converts a numerical value to a string of text (TradingView, n.d.). We can use this function in indicator (study) and strategy scripts.

There are two ways to work with the tostring() function (TradingView, n.d.):

tostring(x, y)
  • x: required argument with the number that tostring() should convert to a string.
  • y: optional format string that sets how the number should be converted to text. This argument has a default value of "#.##########" which converts a numerical value with up to 10 non-zero fractional values. If we’d use "#.##" as the format string, then tostring() converts a value of 1.42345 to the "1.42" string.

Example: convert an input option to a time frame string

The example below uses tostring() to turn a numerical input option to a string of text. That way we can use that input option with the security() function to specify a time frame. But first we set the indicator’s settings with the study() function. Then we use the input() function to make the input option. That custom input option sets the length of the daily time frame for the data we’re going to load. We store its current value in the resValue variable.

Next we load the high and low from that daily time frame with the security() function. Inside that function we specify the instrument to load with tickerid, and use the high and low variables to set the data to return. To get that high data, we use the tostring() function to translate our numerical input option to a certain number of days. For that we use the input variable (resValue) as the first argument, and set the format string to "#D". This converts our input option to "1D", "2D", "3D", and so on when its value if 1, 2, or 3.

To load the daily low data with security(), we convert our input option again to a string. This time we use tostring() with just resValue between its parentheses. Then with the addition operator (+) we add the "D" string to that number so that TradingView Pine understands we want to load daily price data.

After both security() function calls we place the history referencing operator ([]) with a value of 1. This way we get the high and low from the previous day (and not the high and low from the current trading day). We show those previous high and low values on the chart with the plot() function as a green and red solid line.

study(title="tostring() example indicator", overlay=true)

// Inputs
resValue = input(title="Higher time frame days", type=integer, defval=1)

// Get the previous days' higher time frame ('htf') high and low
htfHigh = security(tickerid, tostring(resValue, "#D"), high)[1]
htfLow  = security(tickerid, tostring(resValue) + "D", low)[1]

// Plot values
plot(series=htfHigh, color=green, linewidth=2)
plot(series=htfLow, color=red, linewidth=2)

With the input option set to its default value of 1, the indicator plots the high and low from the preceding day:

Plotting the previous day's high and low in TradingView Pine

If we change the input option to 3, the script shows the highs and lows from the preceding 3 days:

Showing preceding highs and lows in TradingView

Example: plot strategy parameters on the TradingView chart

We can also create order comments that include numerical values with the tostring() function. Unfortunately, those order comments cannot change during the strategy’s backtest period, but they can accurately show values that don’t change during a backtest (like input options).

The example strategy below first sets its properties with the strategy() function. Then we make two input options with input(). Both are numerical input options and set the length of two moving averages. We give them default values of 20 and 10, and store their values in the maLenLong and maLenShort input variables.

Then we set up our long and short trading conditions. The long condition requires that the bar’s open is greater than the previous high, but also that the bar closed above an Exponential Moving Average (EMA). We calculate that EMA value with the ema() function that has a length defined by our input option. For our short condition we want a bar’s open price below the previous bar’s low (low[1]), with the close less than an EMA. That moving average’s length is also set with an input option.

Next we submit entry orders with the strategy.entry() function. Here we set the when argument of that function to our long and short conditions. With the long argument we specify whether the function should submit an enter long (true) or enter short (false) order. We set the order comment to a literal string (like "EL with MA len ") and we combine that (with +) to the value returned by the tostring() function. Inside that function we pass our maLenLong or maLenShort input variables. This way we get our numerical input values to show up in the order comment.

strategy(title="tostring() example strategy", overlay=true)

// Inputs
maLenLong = input(title="MA Length Long", type=integer, defval=20)
maLenShort = input(title="MA Length Short", type=integer, defval=10)

// Trading conditions
enterLong  = (open > high[1]) and (close > ema(close, maLenLong))
enterShort = (open < low[1]) and (close < ema(close, maLenShort))

// Submit orders
strategy.entry(id="long", long=true, when=enterLong, 
     comment="EL with MA len " + tostring(maLenLong))

strategy.entry(id="short", long=false, when=enterShort,
     comment="ES with MA len " + tostring(maLenShort))

Those order comments we set in this example strategy appear in the ‘List of Trades’ in the ‘Strategy Tester’ window:

Showing custom order comments in TradingView's 'Strategy Tester' window

And the order comment text also shows on the chart:

Plotting TradingView order comments on the chart

Notes for tostring()

We currently can use use the tostring() function with arguments from the following built-in TradingView functions:

Function and argument Function description
security() (resolution argument) Loads price data from another instrument and/or resolution.
strategy.entry() (comment argument) Submits an order to open a long or short position.
strategy.exit() (comment argument) Exits part or whole position with a stop or limit order.
strategy.order() (comment argument) Places an order that’s unaffected by the strategy’s pyramiding and risk management settings.

The tostring() function is a promising TradingView feature that could open up a lot of possibilities. Unfortunately, at this time the function is underused since several standard TradingView functions do not accept the dynamic string values created by tostring().

And so we cannot use the tostring() function when we generate an alert with alertcondition(), plot a character on the chart with plotchar(), or show text alongside a shape made by plotshape().

The functions and arguments that tostring() currently does not work with are:

  • alertcondition() (title and message argument)
    • Test code: alertcondition(condition=close > ema(close, 20), message="Price " + tostring(close))
    • Works? No, gives the ‘Cannot call alertcondition with arguments…’ error message.
  • plotchar() (text argument)
    • Test code: plotchar(series=close > ema(close, 10), text="close @ " + tostring(close))
    • Works? No, triggers the ‘argument “text” accepts only constant string values’ error message.
  • plotshape() (text argument)
    • Test code: plotshape(series=close > ema(close, 20), text="Close @" + tostring(close))
    • Works? No, triggers the ‘argument “text” accepts only constant string values’ error message.
  • strategy.entry() (id argument)
    • Test code: strategy.entry(id="long at " + tostring(close), long=true, when=open > high[1])
    • Works? No, gives the ‘Cannot call strategy.entry with arguments…’ error message.
  • strategy.exit() (id argument)
    • Test code: strategy.exit(id="order at bar #" + tostring(n))
    • Works? No, triggers the ‘Cannot call strategy.exit() with arguments…’ error message.
  • strategy.order() (id argument)
    • Test code: strategy.order(id="MA " + tostring(maLenLong), long=true, when=close > sma(close, 10))
    • Works? No, triggers the ‘Cannot call strategy.order() with arguments…’ error message.

Learn more:


Features of tostring()
Name tostring()
Description Converts a numerical value to a string of text optionally based on a certain output format.
Category Text
Type function
Returns A string value
Usable in Indicator (study) and strategy scripts
Available since (beta release date) TradingView Pine Version 2 (October 22, 2015)


TradingView (n.d.). Pine Script Language Reference Manual. Retrieved on October 16, 2017, from