May 17, 2017
More possibilities with compile time constants

Pine Script used to have 3 types of constants – literal, non-literal and serial. Now there’s one more, but let’s look at existing ones first.

The literal type includes fixed values such as 15.5, -2.
The non-literal type has a single value on all bars but isn’t a literal e.g. a ticker, interval, or sin(1).
The serial type constant represents numbers that have different values on different bars such as close, and time[1]

Some functions (input, color) can only accept literals as certain arguments. We realized that it could be more convenient, so we added a new type of constants that can be calculated during compilation. In addition to literals, they include mathematical expressions that use other literals and constants.

Any type of Pine Script literal (integer, float, boolean, string) is the most specific type while the serial type is the most general one. The compilation-time constants take a spot between the literal and non-literal types. All of the functions that accepted literal as an argument will now accept compilation-time constants as well.

There are several ways to call functions, as shown below.

pi = acos(-1)
r = input(pi)

mins = input(24*60)
la = barmerge.lookahead_on
daily = security(tickerid, 'D', close, lookahead=la)
daily_low = security(tickerid, 'D', low, lookahead=la)
in_sma='SMA', in_ema='EMA', in_mixed='1/2 SMA + 1/2 EMA'
smoothing = input(in_sma, options=[in_sma, in_ema, in_mixed])

smoothed = na
if smoothing == in_sma
     smoothed := sma(close, 7)

if smoothing == in_ema
     smoothed := ema(close, 7)

if smoothing == in_mixed
     smoothed := 0.5*sma(close, 7) + 0.5*ema(close, 7)

The fastest way to follow markets

Launch Chart