Utilities and constants
Everything here is imported from @kkkermit/trading212.js and works without a client or a key.
Quantities
Section titled “Quantities”truncateQuantity()
Section titled “truncateQuantity()”truncateQuantity(value: number, decimals = 4): numberCuts a quantity to decimals places, always towards zero, using the number’s exact digits rather than floating-point
arithmetic: truncateQuantity(12.09032128) is 12.0903. Selling a truncated quantity can never exceed what you own.
Throws RangeError for a value that is not a finite number.
isValidQuantity()
Section titled “isValidQuantity()”isValidQuantity(value: number): booleantrue when Trading 212 will accept the number as an order quantity: finite, not zero, at most 4 decimal places.
decimalPlaces()
Section titled “decimalPlaces()”decimalPlaces(value: number): numberHow many decimal places a number has as written: decimalPlaces(0.125) is 3.
QUANTITY_DECIMALS
Section titled “QUANTITY_DECIMALS”4: the most decimal places Trading 212 accepts in an order quantity.
Orders
Section titled “Orders”TIME_VALIDITIES
Section titled “TIME_VALIDITIES”["DAY", "GOOD_TILL_CANCEL"]: the values timeValidity takes.
FINAL_ORDER_STATUSES
Section titled “FINAL_ORDER_STATUSES”A set of the statuses an order does not leave: FILLED, CANCELLED, REJECTED and REPLACED. orders.waitFor()
returns when an order reaches one.
if (FINAL_ORDER_STATUSES.has(order.status ?? "")) console.log("Done");Portfolio and markets
Section titled “Portfolio and markets”buildPortfolioSnapshot()
Section titled “buildPortfolioSnapshot()”buildPortfolioSnapshot(summary: AccountSummary, positions: Position[], takenAt?: Date): PortfolioSnapshotThe calculation behind portfolio.snapshot(), on data you already have, with no request.
sessionAt()
Section titled “sessionAt()”sessionAt(events: TimeEvent[], at: Date): { session; since; next; nextOpen }The calculation behind exchanges.status(): where a market is in its day at at, from a working schedule’s
timeEvents.
Errors
Section titled “Errors”isTrading212Error()
Section titled “isTrading212Error()”isTrading212Error(value: unknown): value is Trading212Errortrue for any error this library threw, even from another copy of the package (ESM beside CommonJS). Every error
class is listed in Errors.
parseErrorBody()
Section titled “parseErrorBody()”parseErrorBody(text: string): { apiCode?: string; message?: string; traceId?: string; body?: unknown }Reads Trading 212’s code, the most human sentence and its trace ID from an error body, whatever its shape: problem
details, the { code, errorMessage } a 429 arrives in, { code, clarification }, { code, message }, plain text or
empty. See Error bodies for what demo really sends.
Logging and caching
Section titled “Logging and caching”consoleLogger()
Section titled “consoleLogger()”consoleLogger(options?: { level?: "debug" | "info" | "warn" | "error"; console?: Console }): LoggerWrites one line per event to the console, dropping anything below level (default info). See
Logging and hooks.
fieldsFirst()
Section titled “fieldsFirst()”fieldsFirst(logger: { debug; info; warn; error }): LoggerAdapts a pino- or bunyan-style logger, whose methods take (fields, message).
silentLogger
Section titled “silentLogger”A logger that ignores everything; the default.
MemoryCache
Section titled “MemoryCache”new MemoryCache();The default store for the instrument and exchange lists: a Map with expiry, private to one client. It has get,
set, delete and clear. Pass one as cache to share it between clients in the same process, or write your own
CacheStore to share it between processes; see the cached list.
The API itself
Section titled “The API itself”ENDPOINTS
Section titled “ENDPOINTS”Every Trading 212 endpoint, generated from the official spec and frozen: ENDPOINTS.placeMarketOrder is
{ id, method, path, scope, rateLimit: { requests, periodMs }, kind, deprecated, summary }. kind is "read",
"write" (harmless, such as requesting a report) or "trade" (never retried, and refused while trading is off).
These names are also what server.failNext() in the testing kit and the endpoint log field use.
BASE_URLS
Section titled “BASE_URLS”{ demo: "https://demo.trading212.com", live: "https://live.trading212.com" }.
SPEC_VERSION
Section titled “SPEC_VERSION”The version of Trading 212’s OpenAPI spec this release was generated from.
VERSION
Section titled “VERSION”This package’s version.