언어
Standard library (language 0.1)
이 페이지는 영어입니다. 하나의 언어로 유지된 사양은 하나의 내용만 말합니다.
Every module here is pure: no host, no clock, no files, no network, no
permission. A module is used after import std.<name>; and its functions
are called as <name>.<function>(…). Host data (charts, datasets,
backtests, research) is not in the standard library: it comes from host
contracts (docs/api/host-api.md), declared with requires and granted by
a person.
Rules that hold for every function:
- No placeholder results. A function returns its documented value or
fails with the code listed; "no data" is
None, never0. - Errors are stable codes (
docs/language/diagnostics.md). A runtime failure ends the execution with that code and the call's location. - Limits apply. A list a function builds may hold at most
max_listelements and a string at mostmax_stringbytes (runtime limits,docs/runtime/runtime-sessions-security.md§2); past them the execution fails withE-RT-LIMIT-COLLECTION, checked before the value is built. What a function builds also counts against the execution's memory budget (E-RT-LIMIT-MEMORY), refused before building whenever the result's size is known. - Indices count elements (lists) or Unicode scalar values (strings), from 0.
The prelude (no import) — price, qty, money, ratio, percent,
bps, currency, timestamp, to_float, to_decimal,
decimal_from_float, round, trunc, error, print, to_string, the
currency constants and the methods of the core types — is specified in
specification.md §7.5. std.math and std.stats are in §7.6.
std.text#
| function | result | behaviour | fails with |
|---|---|---|---|
contains(s: String, part: String) |
Bool |
part occurs in s (the empty string occurs everywhere) |
— |
starts_with(s, prefix) / ends_with(s, suffix) |
Bool |
— | |
to_upper(s) / to_lower(s) |
String |
Unicode case mapping (may lengthen the text) | E-RT-LIMIT-COLLECTION |
trim(s) |
String |
removes leading and trailing Unicode whitespace | — |
split(s, separator) |
List<String> |
every piece, empty pieces included ("a,,b" → ["a", "", "b"]) |
E-NUM-DOMAIN for an empty separator; E-RT-LIMIT-COLLECTION |
join(parts: List<String>, separator) |
String |
E-RT-LIMIT-COLLECTION |
|
replace(s, pattern, with) |
String |
every non-overlapping occurrence, left to right | E-NUM-DOMAIN for an empty pattern; E-RT-LIMIT-COLLECTION |
slice(s, from: Int, to: Int) |
Option<String> |
characters from..to; None if from > to, either is negative, or to is past the end |
— |
parse_int(s) |
Option<Int> |
optional -, decimal digits, fits i64; no +, spaces or underscores |
— |
std.list#
Generic over the element type T.
| function | result | behaviour | fails with |
|---|---|---|---|
range(from: Int, to: Int) |
List<Int> |
from, from+1, …, to−1; empty when to <= from |
E-RT-LIMIT-COLLECTION |
reverse(xs) |
List<T> |
— | |
concat(xs, ys) |
List<T> |
E-RT-LIMIT-COLLECTION |
|
slice(xs, from, to) |
Option<List<T>> |
elements from..to; None as for text.slice |
— |
contains(xs, x) |
Bool |
by the language's equality (§6.3) | the equality's own errors (e.g. E-FIN-CURRENCY-MISMATCH comparing amounts in two currencies) |
sort(xs) |
List<T> |
ascending, stable; T must be ordered (§6.3), checked at compile time |
the ordering's own errors |
take(xs, n) / drop(xs, n) |
List<T> |
the first n / all but the first n (n past the end gives the whole / empty list) |
E-NUM-DOMAIN for n < 0 |
Methods on every list (no import): len(), is_empty(), get(i) -> Option<T>, first(), last(), and push(x) as a statement on a
let mut list.
std.decimal#
| function | result | behaviour | fails with |
|---|---|---|---|
parse(s) |
Option<Decimal> |
the decimal literal grammar (§2): optional -, digits, optional .digits, at most 18 fractional digits |
— |
scale(d) |
Int |
the number of fractional digits as stored (1.50 → 2) |
— |
floor(d, places) / ceil(d, places) |
Decimal |
rounds toward −∞ / +∞ to places fractional digits; a value already that short is unchanged |
E-NUM-SCALE for places outside 0..=18 |
round_half_up(d, places) |
Decimal |
ties away from zero (2.5 → 3, −2.5 → −3). The prelude's round is half-even. |
E-NUM-SCALE |
rescale(d, places) |
Decimal |
the same value written with places digits: adds zeros, never rounds |
E-NUM-DOMAIN if digits would be lost; E-NUM-SCALE |
std.time#
Timestamps are nanoseconds since the Unix epoch, UTC, no leap seconds (§8). There is no "now": a program reads time only from its data.
| function | result | behaviour | fails with |
|---|---|---|---|
from_unix_ns(n) |
Timestamp |
— | |
from_unix_ms(n) |
Timestamp |
n × 10⁶ ns |
E-NUM-OVERFLOW |
unix_ms(t) |
Int |
floor division (−1 ns → −1 ms) |
— |
weekday(t) |
Int |
the UTC day of the week: 0 Monday .. 6 Sunday | — |
minute_of_day(t) |
Int |
the minute of the UTC day, 0 .. 1439 | — |
floor(t, step: Duration) |
Timestamp |
the start of t's bucket of width step, buckets aligned to the epoch (floor, also before 1970) |
E-NUM-DOMAIN for step <= 0 |
utc_offset_minutes(zone: String, t) |
Int |
how far the wall clock of the IANA time zone zone ("America/New_York", "Europe/London", "UTC") is from UTC at t, in whole minutes, east positive: New York is -300 in winter and -240 in summer time. From the IANA database bundled in the binary (the same on every machine). The zone's wall clock in minutes is unix_ms(t) / 60000 + offset: its minute of the day is that % 1440, and a session that turns over at a wall-clock time is (that - the minute of the turn-over) / 1440 |
E-NUM-DOMAIN for a name that is not in the database or an instant outside its range |
seconds(d) |
Decimal |
exact: d / 10⁹ |
— |
from_seconds(x: Decimal) |
Duration |
exact | E-NUM-DOMAIN if x has digits below 1 ns; E-NUM-OVERFLOW |
std.fin#
| function | result | behaviour | fails with |
|---|---|---|---|
round_to_tick(p: Price, tick: Price) |
Price |
the nearest multiple of tick (ties to even) |
E-FIN-CURRENCY-MISMATCH; E-NUM-DOMAIN for tick <= 0 |
floor_to_step(q: Quantity, step: Quantity) |
Quantity |
the largest multiple of step not above q (lot sizing never rounds up) |
E-NUM-DOMAIN for step <= 0 |
pct_change(from: Price, to: Price) |
Ratio |
(to − from) / from |
E-FIN-CURRENCY-MISMATCH; E-NUM-DIV-ZERO when from is 0 |
std.ind — causal indicators over List<Float>#
Each returns one entry per input, None until it has enough data. The
entry at index i reads only inputs 0..=i (causal: computing over a
whole history never lets a later value into an earlier result). Any
non-finite intermediate fails with E-NUM-NONFINITE instead of
propagating.
The SMA and EMA are TTT's own definitions (strategy_core::Features),
so a value computed here equals the one a strategy sees in a backtest:
the SMA sums its window left to right; the EMA's first value is the SMA of
the first n inputs, then ema = α·x + (1 − α)·ema, α = 2 / (n + 1).
| function | result | first value at index | fails with |
|---|---|---|---|
sma(xs, n) |
List<Option<Float>> |
n − 1 |
E-NUM-DOMAIN for n < 1 |
ema(xs, n) |
List<Option<Float>> |
n − 1 |
E-NUM-DOMAIN for n < 1 |
rolling_stdev(xs, n) |
List<Option<Float>> |
n − 1; sample standard deviation (n − 1) |
E-NUM-DOMAIN for n < 2 |
returns(xs) |
List<Option<Float>> |
1; x[i] / x[i−1] − 1 |
E-NUM-NONFINITE after a zero value |
log_returns(xs) |
List<Option<Float>> |
1; ln(x[i] / x[i−1]) |
E-NUM-DOMAIN for a non-positive ratio |
atr(high, low, close, n) |
List<Option<Float>> |
n − 1; Wilder: mean of the first n true ranges, then (prev × (n − 1) + tr) / n |
E-NUM-DOMAIN for unequal lengths or n < 1 |
vwap(prices, volumes) |
List<Option<Float>> |
first index with positive cumulative volume; cumulative from the first element | E-NUM-DOMAIN for unequal lengths or a negative volume |
latest(values) |
Option<Float> |
the last entry if it is known | — |
known(values) |
List<Float> |
the known entries, in order | — |
std.ind works on Float on purpose: indicators are statistics, not
amounts. To use one with prices, convert explicitly
(to_float(p.amount())) and back with decimal_from_float(x, places)
when a decimal is needed; see decisions/0005-numeric-model.md,
"Decimal ↔ f64 boundaries".
What is deliberately not here#
- Reading files, the network, environment variables or the clock.
- Randomness (a strategy's seed belongs to the backtest engine).
- Charts, datasets, backtests, orders: host contracts.
- Regular expressions and string formatting templates (not designed yet).