Complete reference for supported Pine Script functions, operators, and built-in variables in TrainBard.
Introduction
Pine Script Reference
Welcome to the Muuned Pine Script documentation. This reference covers all supported functions, operators, and built-in variables available in the script editor.
Scripts follow Pine Script v6-style syntax. Every strategy begins with a version directive and strategy declaration:
Define parameters with input.* functions, compute indicators with ta.* functions, and generate trade signals with strategy.* functions:
fast = input.int(10, "Fast MA")
slow = input.int(20, "Slow MA")
fastMA = ta.sma(close, fast)
slowMA = ta.sma(close, slow)
if ta.crossover(fastMA, slowMA)
strategy.entry("Long", strategy.long)
if ta.crossunder(fastMA, slowMA)
strategy.close("Long")
Built-in Variables
The following price and time series are available on every bar:
Variable
Description
open
Opening price
high
Highest price
low
Lowest price
close
Closing price
volume
Bar volume
hl2
(high + low) / 2
hlc3
(high + low + close) / 3
ohlc4
(open + high + low + close) / 4
hlcc4
(high + low + close + close) / 4
time
Bar timestamp (Unix ms)
bar_index
Current bar number (0-based)
Symbol Variables
Variable
Description
syminfo.tickerid
Current symbol identifier
syminfo.ticker
Current symbol
syminfo.basecurrency
Base asset
syminfo.currency
Quote asset
syminfo.mintick
Symbol tick size used by tick-based exits and math.round_to_mintick()
Backtesting Behavior
Muuned evaluates strategies on the selected candle data. It does not replay intrabar ticks.
Simple entry/close strategies use a fast signal path. Strategies with entry-specific exits, partial exits, stop/limit/profit/loss exits, trailing exits, multiple entry or exit IDs, or trade introspection use advanced mode so strategy.* state is available while the script runs.
Market entries and market closes fill on the next candle. If a candle touches both stop and limit for an open trade, Muuned uses the conservative fill.
Time Variables
Variable
Description
year
UTC year
month
UTC month (1-12)
dayofmonth
UTC day of month
dayofweek
UTC day of week (1=Sun)
hour
UTC hour
minute
UTC minute
second
UTC second
Strategy Constants
Constant
Description
strategy.long
Long direction
strategy.short
Short direction
Language Features
Variables: myVar = expression
Persistent variables: var myVar = initialValue (retains value across bars)
History operator: close[1] accesses previous bar values
Input Functions
Input Functions
Input functions define user-configurable parameters for your strategy. When running a backtest, parameter values can be set individually or as ranges for optimization.
input.int
Declares an integer input parameter.
Syntax: input.int(defval, title)
Parameter
Type
Description
defval
int
Default value
title
string
Display name in the parameter form
length = input.int(14, "RSI Length")
Use range notation in the Test tab to sweep values: 10_50:5 tests 10, 15, 20, … 50.
input.float
Declares a floating-point input parameter.
Syntax: input.float(defval, title)
Parameter
Type
Description
defval
float
Default value
title
string
Display name
factor = input.float(1.5, "Multiplier")
input.bool
Declares a boolean input parameter.
Syntax: input.bool(defval, title)
Parameter
Type
Description
defval
bool
Default value (true or false)
title
string
Display name
useLong = input.bool(true, "Enable Long")
input.string
Declares a string input parameter.
Syntax: input.string(defval, title)
Parameter
Type
Description
defval
string
Default value
title
string
Display name
maType = input.string("SMA", "MA Type")
input.source
Declares a price source input parameter.
Syntax: input.source(defval, title)
Parameter
Type
Description
defval
series
Default source (e.g. close, hl2)
title
string
Display name
src = input.source(close, "Source")
Moving Averages
Moving Averages
Moving average functions smooth price data over a specified period. All functions return a series (one value per bar).
ta.sma
Simple Moving Average. The unweighted arithmetic mean of the last length bars.
Syntax: ta.sma(source, length)
Parameter
Type
Description
source
series
Input series (e.g. close)
length
int
Number of bars
sma20 = ta.sma(close, 20)
ta.ema
Exponential Moving Average. Gives more weight to recent values. Seeded with SMA of the first length values.
Syntax: ta.ema(source, length)
Parameter
Type
Description
source
series
Input series
length
int
Number of bars
ema50 = ta.ema(close, 50)
ta.wma
Weighted Moving Average. Linearly weights values so the most recent bar has the highest weight.
Syntax: ta.wma(source, length)
Parameter
Type
Description
source
series
Input series
length
int
Number of bars
wma10 = ta.wma(close, 10)
ta.hma
Hull Moving Average. A fast, smooth MA that reduces lag using WMA of WMA differences.
Syntax: ta.hma(source, length)
Parameter
Type
Description
source
series
Input series
length
int
Number of bars
hma20 = ta.hma(close, 20)
ta.rma
Relative Moving Average (Wilder’s smoothing). Used internally by RSI and ATR. Equivalent to an EMA with alpha = 1/length.
Syntax: ta.rma(source, length)
Parameter
Type
Description
source
series
Input series
length
int
Number of bars
rma14 = ta.rma(close, 14)
ta.vwma
Volume-Weighted Moving Average. Weights each bar’s value by its volume.
Syntax: ta.vwma(source, length)
Parameter
Type
Description
source
series
Input series
length
int
Number of bars
vwma20 = ta.vwma(close, 20)
ta.alma
Arnaud Legoux Moving Average. Uses a Gaussian distribution for weighting, controlled by offset and sigma.
Syntax: ta.alma(source, length, offset, sigma)
Parameter
Type
Default
Description
source
series
Input series
length
int
Window size
offset
float
0.85
Gaussian offset (0–1)
sigma
float
6
Gaussian sigma
alma = ta.alma(close, 20, 0.85, 6)
ta.swma
Symmetrically Weighted Moving Average. A fixed 4-bar weighted average with weights [1, 2, 2, 1] / 6.
Syntax: ta.swma(source)
Parameter
Type
Description
source
series
Input series
sw = ta.swma(close)
ta.dema
Double Exponential Moving Average. 2 * EMA - EMA(EMA). Reduces lag compared to a standard EMA.
Syntax: ta.dema(source, length)
Parameter
Type
Description
source
series
Input series
length
int
Number of bars
dema20 = ta.dema(close, 20)
ta.tema
Triple Exponential Moving Average. 3 * EMA - 3 * EMA(EMA) + EMA(EMA(EMA)). Further reduces lag versus DEMA.
Syntax: ta.tema(source, length)
Parameter
Type
Description
source
series
Input series
length
int
Number of bars
tema20 = ta.tema(close, 20)
ta.kama
Kaufman Adaptive Moving Average. Adjusts smoothing speed based on market noise.
Syntax: ta.kama(source, length)
Parameter
Type
Description
source
series
Input series
length
int
Efficiency ratio period
kama10 = ta.kama(close, 10)
ta.t3
Tillson T3 Moving Average. A smooth, low-lag MA built from six cascaded EMAs with a volume factor.
Syntax: ta.t3(source, length, factor)
Parameter
Type
Default
Description
source
series
Input series
length
int
EMA period
factor
float
0.7
Volume factor (0–1)
t3val = ta.t3(close, 5, 0.7)
Oscillators
Oscillators
Oscillator functions measure momentum and overbought/oversold conditions. They typically return bounded or centered values.
ta.rsi
Relative Strength Index. Measures the speed and magnitude of price changes on a 0–100 scale.
Syntax: ta.rsi(source, length)
Parameter
Type
Description
source
series
Input series
length
int
Lookback period
rsi14 = ta.rsi(close, 14)
ta.stoch
Stochastic oscillator. Returns the position of the source relative to the high-low range over length bars.
Syntax: ta.stoch(source, high, low, length)
Parameter
Type
Description
source
series
Source (typically close)
high
series
High series
low
series
Low series
length
int
Lookback period
k = ta.stoch(close, high, low, 14)
ta.cci
Commodity Channel Index. Measures the deviation of price from its statistical mean.
Syntax: ta.cci(source, length)
Parameter
Type
Description
source
series
Input series
length
int
Lookback period
cci20 = ta.cci(close, 20)
ta.cmo
Chande Momentum Oscillator. Measures momentum on a -100 to +100 scale using the difference between gains and losses.
Functions for measuring volatility and computing price bands/channels.
ta.atr
Average True Range. The RMA of the true range over length bars.
Syntax: ta.atr(length)
Parameter
Type
Description
length
int
Lookback period
atr14 = ta.atr(14)
ta.natr
Normalized Average True Range. ATR expressed as a percentage of the closing price.
Syntax: ta.natr(length)
Parameter
Type
Description
length
int
Lookback period
natr14 = ta.natr(14)
ta.tr
True Range. The greatest of: current high minus low, absolute(high minus previous close), absolute(low minus previous close). Also available as the built-in variable ta.tr.
Syntax: ta.tr
No parameters — available as a series variable.
trueRange = ta.tr
ta.bb
Bollinger Bands. Returns three series: middle band (SMA), upper band, and lower band.
Plot functions render visual indicators, overlays, and markers on the chart. They execute during backtesting and their output is displayed alongside candlestick data.
plot
Draws a line, area, histogram, or other series on the chart.
Creates a color from red, green, and blue components.
Syntax: color.rgb(red, green, blue)
Parameter
Type
Range
Description
red
int
0–255
Red channel
green
int
0–255
Green channel
blue
int
0–255
Blue channel
plot(close, color=color.rgb(255, 165, 0))
color.new
Creates a color with transparency applied.
Syntax: color.new(base_color, transp)
Parameter
Type
Description
base_color
color
Base color
transp
int
Transparency (0 = opaque, 100 = invisible)
Color Constants
Constant
Hex
color.red
#EF4444
color.green
#10B981
color.blue
#3B82F6
color.orange
#F97316
color.purple
#8B5CF6
color.yellow
#EAB308
color.white
#FFFFFF
color.black
#000000
color.gray
#6B7280
color.lime
#84CC16
color.aqua
#06B6D4
color.fuchsia
#D946EF
color.teal
#14B8A6
color.navy
#1E3A8A
color.maroon
#881337
color.olive
#84CC16
color.silver
#E5E7EB
Strategy Functions
Strategy Functions
Strategy functions control position entry, exit, and risk management.
How Orders Execute
Muuned uses two strategy execution paths.
Fast mode is used for simple strategy.entry() plus strategy.close() strategies. The script emits a signal series and the trusted backtester runs the portfolio simulation.
Advanced mode is used when Pine code needs trade state while it runs. This includes strategy.exit() with from_entry, qty, qty_percent, profit, loss, stop, limit, or trailing parameters; multiple entry or exit IDs; and strategy.closedtrades.* or strategy.opentrades.* calls. Advanced mode tracks open entries while the script executes, then the trusted backtester replays the resulting fills for the final summary.
Market entries and market closes are placed when the script calls them and fill on the next candle. strategy.position_size, strategy.position_avg_price, and trade counters update after that fill, so exits that depend on position state begin from the candle where the entry is actually open.
Muuned simulates over the available OHLC candles, such as 5m or 15m bars. It does not use tick-level replay. If a candle touches both a stop and a limit for an open trade, Muuned uses the conservative fill for that trade. For a long, the stop wins; for a short, the stop wins.
Backtests use a default combined fee/slippage assumption of 0.1% unless parameters override it.
strategy
Declares the script as a strategy. Must appear at the top of the script.
Syntax: strategy(title, overlay)
Parameter
Type
Default
Description
title
string
Strategy name
overlay
bool
true
Plot on the price chart
initial_capital
float
Starting capital
strategy("My Strategy", overlay=true)
strategy.entry
Opens a new position or adds to an existing one.
Syntax: strategy.entry(id, direction, qty)
Parameter
Type
Default
Description
id
string
Order identifier
direction
const
strategy.long or strategy.short
qty
float
Asset quantity
In advanced mode, explicit qty values are asset units. For high-priced symbols, qty=1 means one full coin/share/contract. If qty is omitted, Muuned sizes the entry from available starting capital. In fast mode, portfolio sizing follows the backtest position size setting.
Tick-based exits use syminfo.mintick. Absolute stop and limit prices are rounded to valid tick increments before orders are evaluated.
Repeated calls with the same id and from_entry update the same pending exit order. Once that exit ID has filled for an entry, it will not keep closing the remaining quantity of that same entry on later bars. Use a different exit ID or strategy.close() to close the remainder.
trend = close > ta.sma(close, 30)
if trend and strategy.position_size == 0
strategy.entry("Long", strategy.long, qty=2)
if strategy.position_size > 0
strategy.exit("Half Off", "Long", qty_percent=50, profit=100, loss=50)
if not trend and strategy.position_size > 0
strategy.close("Long")
math.round_to_mintick() uses the selected market’s tick size. The same tick size is used when strategy.exit() converts profit, loss, trail_points, and trail_offset into prices.
Trigonometry
Function
Description
math.sin(x)
Sine (radians)
math.cos(x)
Cosine (radians)
math.tan(x)
Tangent (radians)
math.asin(x)
Arcsine
math.acos(x)
Arccosine
math.atan(x)
Arctangent
math.atan2(y, x)
Two-argument arctangent
math.sinh(x)
Hyperbolic sine
math.cosh(x)
Hyperbolic cosine
math.tanh(x)
Hyperbolic tangent
math.todegrees(x)
Convert radians to degrees
math.toradians(x)
Convert degrees to radians
Constants
Constant
Value
Description
math.pi
3.14159…
Pi
math.e
2.71828…
Euler’s number
math.phi
1.61803…
Golden ratio
math.rphi
0.61803…
Reciprocal golden ratio
Other
Function
Description
math.random()
Random number between 0 and 1
String Functions
String Functions
Functions for working with string values.
Conversion
Function
Description
str.tostring(value, format)
Convert value to string. Optional format for decimals.
str.tonumber(string)
Parse a string to a number. Returns NaN if invalid.
label_text = str.tostring(close, "#.##")
val = str.tonumber("3.14")