Comprehensive reference for QuantScript syntax, built-in functions, and namespace APIs.
QuantScript supports the following primitive types:
Integer and floating-point numbers.
42
3.14
-17
2.5e10Logical values true and false.
Text values in single or double quotes: "Hello" or 'Hello'.
Represents the absence of a value: null.
Hex color literals: #FF0000, #00FF00FF.
Use let for mutable variables and const for constants.
let x = 10;
const PI = 3.14159;const [a, b, c] = [1, 2, 3];
let [first, second] = myArray;Supported: +=, -=, *=, /=.
| Operator | Description | Example |
|---|---|---|
+ | Addition | 10 + 5 → 15 |
- | Subtraction | 10 - 5 → 5 |
* | Multiplication | 10 * 5 → 50 |
/ | Division | 10 / 3 → 3.333 |
% | Modulo | 10 % 3 → 1 |
** | Exponentiation | 2 ** 8 → 256 |
==, !=, ===, !==, <, >, <=, >=
&& (AND), || (OR), ! (NOT) — all short-circuit.
let result = condition ? valueIfTrue : valueIfFalse;let value = maybeNull ?? defaultValue;
let prop = obj?.property;let value = object.property;
let first = array[0];
let prev = close[1]; // Previous bar's closeif (condition) {
// code
} else if (other) {
// code
} else {
// code
}for (let i = 0; i < 10; i++) { /* ... */ }
for (const item of myArray) { /* ... */ }while (condition) { /* ... */ }Use break to exit a loop and continue to skip to the next iteration.
function add(a, b) {
return a + b;
}const square = (x) => x * x;
const add = (a, b) => { return a + b; };function calculate(x, y = 10) { return x + y; }
config(name: "setting", value: 42);let numbers = [1, 2, 3, 4, 5];
let config = { period: 14, multiplier: 2.0, enabled: true };Access elements with numbers[0] and properties with config.period or config["period"].
Global functions available without a namespace prefix.
Display an alert message.
Trigger an alert when a condition is true.
Replace null values with a default value (default: 0).
Convert a value to its string representation.
Convert a string to number. Returns null if invalid.
Check if a value is null. Returns true if null.
Print values to console output.
Print error to console output.
Simple Moving Average.
Returns: SMA value
Exponential Moving Average.
Returns: EMA value
Weighted Moving Average.
Returns: WMA value
Recursive Moving Average (Wilder's smoothing).
Returns: RMA value
Sine-Weighted Moving Average.
Returns: SWMA value
Hull Moving Average.
Returns: HMA value
Arnaud Legoux Moving Average.
Returns: ALMA value
Linear Regression.
Returns: Linear regression value
Relative Strength Index.
Returns: RSI value (0-100)
Moving Average Convergence Divergence.
Returns: Array of [macdLine, signalLine, histogram]
Stochastic Oscillator.
Returns: Stochastic value (0-100)
Rate of Change.
Returns: ROC percentage
Momentum.
Returns: Momentum value
Standard Deviation.
Variance.
Mean Deviation.
Bollinger Bands. mult defaults to 2.
Returns: Array of [lower, middle, upper]
Keltner Channels. mult defaults to 2.
Returns: Array of [lower, middle, upper]
Check if source1 crosses over source2.
Returns: true if crossover occurred
Check if source1 crosses under source2.
Returns: true if crossunder occurred
Highest value over a period.
Lowest value over a period.
Bar index of highest value. Returns negative bar offset.
Bar index of lowest value. Returns negative bar offset.
Change in value over a period.
Check if series is falling.
Check if series is rising.
Number of bars since condition was true.
Value of source when condition was last true.
Cumulative sum of a series.
Median value over a period.
Percentile rank (0-100).
Pivot high point. Returns value or null.
Pivot low point. Returns value or null.
Correlation between two series (-1 to 1).
Absolute value.
Maximum of multiple values.
Minimum of multiple values.
Power function.
Square root.
Sign of a number (1, -1, or 0).
Round to nearest integer or specified precision.
Round up to nearest integer.
Round down to nearest integer.
Natural logarithm or with specified base.
Base-10 logarithm.
Base-2 logarithm.
Exponential function (e^x).
Sine, cosine, tangent. Input in radians.
Inverse trig functions. Returns radians.
Arc tangent of y/x using signs for quadrant.
Angle conversion.
Average of multiple values.
Sum of series over a period.
Random number in [0, 1).
Mathematical constants: π, e, φ.
Get string length.
Check if string contains substring.
Check if string starts with prefix.
Check if string ends with suffix.
Find position of substring. Returns index or null.
Check if string matches regex.
Convert to lowercase / uppercase.
Remove leading/trailing whitespace.
Reverse the string.
Replace all occurrences of a substring.
Extract a substring.
Repeat string multiple times.
Pad string to specified length.
Split string into array.
Join array into string.
Type conversion.
Format string with {0}, %s, %d, %f placeholders.
Create array of integers.
Create array of floats.
Create array of booleans.
Create array of strings.
Create array of colors.
Create array from a value or copy an array.
Add element to end.
Add element to beginning.
Remove and return last element.
Remove and return first element.
Insert element at index.
Remove element at index.
Remove all elements.
Fill array with a value.
Get/set element at index.
Get first/last element.
Get size or check if empty.
Search for value.
Extract a portion of array.
Concatenate or copy arrays.
Sort array. 0 = ascending (default), 1 = descending.
Aggregate functions.
Statistical functions.
Join elements into string.
Plot a data series on the chart.
Plot a shape marker on the chart.
Draw a horizontal line at a price level.
Set background color.
Create or switch to a named pane.
Enter a position.
Close a position.
Close all open positions.
Set exit conditions for a position.
Place an order. Same parameters as strategy.entry.
Cancel pending orders.
| Property | Description |
|---|---|
strategy.position_size | Current position size |
strategy.position_avg_price | Average entry price |
strategy.equity | Current equity |
strategy.cash | Available cash |
strategy.openprofit | Open position profit |
strategy.netprofit | Net profit |
strategy.opentrades | Number of open trades |
strategy.closedtrades | Number of closed trades |
strategy.wintrades | Winning trades count |
strategy.losstrades | Losing trades count |
| Constant | Description |
|---|---|
strategy.long | Long direction |
strategy.short | Short direction |
strategy.fixed | Fixed quantity type |
strategy.cash | Cash quantity type |
strategy.percent_of_equity | Percent of equity type |
Iterate over each frame (bar) in the DataFrame.
df.forEach((frame) => {
let closePrice = frame.close;
let highPrice = frame.high;
});| Property | Description |
|---|---|
bar_index | Current bar index (0-based) |
last_bar_index | Index of the last bar |
barstate.isfirst | true if first bar |
barstate.islast | true if last bar |
barstate.isnew | true if new bar |
barstate.isconfirmed | true if bar is confirmed |
frame.hl2 | (high + low) / 2 |
frame.hlc3 | (high + low + close) / 3 |
frame.hlcc4 | (high + low + close + close) / 4 |
frame.ohlc4 | (open + high + low + close) / 4 |
let prevClose = close[1]; // Previous bar's close
let twoAgo = high[2]; // High from 2 bars ago// Indicator script
indicator("My Indicator");
// Strategy script
strategy("My Strategy");
Comments & Semicolons#