Built-ins

Introduction

Lipi Script includes hundreds of built-in variables and functions that provide valuable information and perform calculations, so you don’t have to code them from scratch. The more familiar you are with these built-ins, the more you can accomplish with your Lipi Script scripts.

This section provides an overview of some of Lipi Script’s built-in variables and functions. Detailed explanations are available in other sections of this manual, organized by specific topics.

Variables and Functions in Lipi Script

Variables and functions within the same group are organized under the same namespace, which serves as a prefix in the function’s name. For example, the talib.sma() function belongs to the ta namespace, representing technical analysis. A namespace can contain both variables and functions.

Some variables also have function versions. For instance:

  • The talib.tr variable provides the True Range of the current bar. The talib.tr(true) function also returns the True Range but calculates high - low if the previous close value needed is na.
  • The time variable gives the time at the opening of the current bar. The time(timeframe) function returns the time of the bar’s open for the specified timeframe, regardless of the chart’s timeframe. Additional function variations include time(timeframe, session) for returning the time only within a particular session and time(timeframe, session, timezone) to include a specific timezone.

Built-in Variables

Built-in variables provide information for various purposes. Here are a few categories:

  • Price and volume variables: open, high, low, close, hl2, hlc3, ohlc4, and volume.
  • Symbol-specific information in the syminfo namespace: syminfo.basecurrency, syminfo.currency, syminfo.description, syminfo.mintick, syminfo.pointvalue, syminfo.prefix, syminfo.root, syminfo.session, syminfo.ticker, syminfo.tickerid, syminfo.timezone, and syminfo.type.
  • Timeframe (or interval) variables in the timeframe namespace: timeframe.isseconds, timeframe.isminutes, timeframe.isintraday, timeframe.isdaily, timeframe.isweekly, timeframe.ismonthly, timeframe.isdwm, timeframe.multiplier, and timeframe.period.
  • Bar states in the barstate namespace: barstate.isconfirmed, barstate.isfirst, barstate.ishistory, barstate.islast, barstate.islastconfirmedhistory, barstate.isnew, and barstate.isrealtime.

Built-in Functions

Many built-in functions are used for the results they return. Here are a few examples:

  • Math functions in the math namespace: math.abs(), math.log(), math.max(), math.random(), math.round_to_mintick(), etc.
  • Technical indicators in the ta namespace: talib.sma(), talib.ema(), talib.macd(), talib.rsi(), talib.supertrend(), etc.
  • Helper functions in the ta namespace for indicator calculations: talib.barssince(), talib.crossover(), talib.highest(), etc.
  • String functions in the str namespace: str.format(), str.length(), str.tonumber(), str.tostring(), etc.
  • Input functions in the input namespace for user-modifiable values in the “Settings/Inputs” tab: input(), input.color(), input.int(), input.session(), input.symbol(), etc.
  • Color functions in the color namespace: color.from_gradient(), color.new(), color.rgb(), etc.

Some functions do not return values but are executed for their side effects, meaning they perform actions without returning a result:

  • Declaration functions that define a script type and properties, such as indicator() at the script’s start.
  • Plotting functions: bgcolor(), plotbar(), plotcandle(), plotchar(), plotshape(), fill().

All built-in functions are documented in the Lipi Script v1 Reference Manual. Each function’s entry includes the function’s signature with:

  • Parameter names
  • The qualified type of each parameter value (parameters are the values passed to a function upon calling)
  • Whether the parameter is required

Let’s take the talib.vwma() function, which returns the volume-weighted moving average of a source value, as an example.

This line of code calls the talib.vwma() function, creating a variable named myVwma and assigning it the result of talib.vwma(close, 20):

myVwma = talib.vwma(close, 20)

Note that:

  • The built-in variable close is used as the argument for the source parameter.
  • The value 20 is provided as the argument for the length parameter.
  • If this line is placed in the global scope (i.e., at the start of a line), the Lipi Script runtime will execute it on each bar of the chart.
  • Parameter names can also be specified when calling the function. When used in a function call, these are called keyword arguments:
myVwma = talib.vwma(source=close, length=20)

When using keyword arguments, you can reorder the arguments, but only if all arguments are specified as keywords. For functions with many parameters, such as indicator(), you may omit keyword arguments for the initial parameters as long as none are skipped. However, if any parameters are skipped, keyword arguments must be used for the remaining ones so the Lipi Script compiler can determine their respective parameters. For example:

indicator("Example", "Ex", true, max_bars_back = 100)

Mixing things up this way is not allowed:

indicator(precision = 3, "Example") // Compilation error!

When calling built-in functions, it’s essential to make sure the arguments match the required qualified type for each parameter, as these types can vary.

To understand this process, familiarity with the Lipi Script type system is necessary.