All reference chapters

2. How a robot works

The engine does not execute a program you write; it runs a graph you declare. Market data enters at an adapter, an input turns every tick into a number, a pattern decides when that number means something, a rule turns the decision into a fully specified order, a robot owns the rules and is the thing you start and stop, and an account owns the robots together with everything they touch except the market adapters, which stand on their own and can feed several accounts at once. This chapter walks that chain once, stage by stage, and explains what each stage is for and why it exists at all. It contains no code by design — the names of the calls appear where they belong to the explanation, and the dedicated chapters that follow give every stage its complete surface on all three languages.

Two properties hold along the whole chain. Each stage is declared under a unique label and is referred to by that label from the stage above — a pattern names its inputs, a rule names its pattern, a robot names its rules — so the graph is assembled bottom-up out of names rather than out of pointers. And declaring is not running: nothing in the graph touches data until the robot is started.

Market data arrives at an adapter

A market adapter is the single place where the outside world ends and the engine begins. It is a named, typed channel that belongs to no account: named, because inputs and portfolio subscriptions refer to it by that name; typed, because the record it accepts is fixed when it is created and is validated on every push. The handle that creates an adapter and every account the adapter is handed to share it, so one adapter of a currency can feed any number of accounts of that currency, and it lives until the last of them lets go of it. You may create as many adapters as you need — one per feed, per venue, per vendor, per resolution — and the same contract may live on several of them at once.

The five adapter types are the five shapes market data takes in this engine.

Adapter typeRecord pushedPush entry points
tse_md_ohlcvTseTickOHLCVtse_market_push_ohlcv
tse_md_bidaskTseTickBidAsktse_market_push_bidask
tse_md_tradeTseTickTradetse_market_push_trade
tse_md_executedTseRetainedtse_market_push_executed
tse_md_bookTseBookMessagetse_market_push_book

Data is fed tick by tick, and each push addresses exactly one contract, by the contract handle that tse_add_contract returned when the contract was declared. The handle is the contract: it costs no lookup, so a high-rate replay pays nothing for addressing. A contract becomes known to an adapter through wiring only: by binding an input to it, or by subscribing it with tse_portfolio_subscribe. There is no standalone call that adds a contract to an adapter, and pushing to a contract the adapter does not carry is an error.

Two of the five types are pure input sources. The executed feed carries trades that happened elsewhere, and the book feed carries order-book messages; neither offers a price the portfolio could mark a position against, nor a venue a simulated fill could occur on. A simulator therefore cannot be attached to them, and a portfolio subscription on them is refused with a diagnostic. An adapter is opened with tse_market_create, which is given the label the rest of the graph will refer to it by, the TseMdType that fixes its record shape for good, and the currency of the accounts it may feed; tse_market_destroy releases the share held by the handle, and a refused push leaves its reason on the adapter, readable with tse_market_last_error. The order-book chapter covers the book feed in full; the environment chapter covers the loaders that turn CSV files into tick arrays ready to be pushed.

An Input processes every tick

An input is the entry node of the graph and the one place in the whole pipeline where your own ideas live. It is a named, duration-stamped cache built over a set of contracts on one adapter: raw ticks arrive at the adapter, the input hands each tick to your data processor, and whatever the processor stores becomes the series that patterns later observe.

What crosses the boundary is deliberately minimal. The processor receives the input's storage handle, the contract the tick belongs to, the tick itself and the opaque userData pointer it was registered with. It may push a timestamped value into the storage, and it returns a readiness flag — non-zero once the input has enough material to be observed.

The engine never inspects the callable — it holds a function pointer of a fixed signature and a pointer it does not interpret, and it calls them. A three-line moving average, a gradient-boosted model, a neural network scoring news sentiment and a call out to an external service are consequently the same object to the engine, and swapping one for another changes no wiring at all. The builders tse_add_input_ohlcv, tse_add_input_bidask, tse_add_input_trade and tse_add_input_executed cover the four tick feeds; tse_add_input_book and tse_add_input_book_imbalance cover the book feed. Each takes the adapter it reads from, the list of contract handles it covers — binding registers those contracts on the adapter — a cache length, an explicit duration and a core id. The callable itself has one type per feed — TseOhlcvInputProcessor for candles, and its siblings for the other four — and it stores a value by calling tse_storage_push with that number alone, because the element takes the timestamp of the tick being processed. The Input chapter details every argument and gives each signature.

A Pattern watches Inputs and fires

A pattern is the decision node. It observes one or more inputs by label and emits a signal whenever its condition holds, carrying the timestamp of the input update that produced it and an order — a quantity, a price and a transaction side. The five analytic kinds leave that order zeroed, making no proposal of their own; a formula processor fills in whatever it wants the rule to use. A pattern produces nothing tradeable by itself: a signal becomes an order only through a rule that names the pattern.

Six kinds cover the vocabulary of a condition, and each demands an exact number of observed inputs.

  • tse_add_pattern_threshold — one input: fires while the value compares against a constant as asked.
  • tse_add_pattern_peak — one input: fires when the series forms a local peak.
  • tse_add_pattern_timestamp — one input: fires once the input timestamp reaches a checkpoint, then every cool-down thereafter.
  • tse_add_pattern_comparison — two inputs: fires while one compares against the other at the same timestamp.
  • tse_add_pattern_crossover — two inputs: fires on the bar where that comparison flips from false to true.
  • tse_add_pattern_formula — any number of inputs: your callback receives each update, decides, and fills the order the signal will carry.

The comparison itself is a TseCmp value — tse_cmp_ge, tse_cmp_lt, tse_cmp_gt, tse_cmp_le, tse_cmp_eq, tse_cmp_ne. Every builder takes an explicit TseDuration: the engine never derives a pattern's duration from the inputs it observes, exactly as it never derives an input's own. Every builder also takes a mandatory core id, which pins the pattern's worker thread or asks for no separate thread at all. The Pattern chapter gives each kind its exact firing semantics.

A Rule turns a firing into an order

A rule is the bridge between observation and action. Everything the resulting order will carry is fixed when the rule is built — the transaction side, the position side that must hold, the quantity mode and quantity, the price form and limit price, slippage, fee, time-in-force and the rule's priority among the observers of the same pattern. Those ten values travel together in TseRuleParams, and none of them has a library-supplied default. The contract is not one of them: it is named separately, as the trailing contract handle argument of the builder. Three of the ten may be left undefined — the quantity mode, the price form and the transaction side — and each one left undefined is taken from the order the signal carries, on every firing.

  • tse_add_rule_market — the market channel proper: an entry, an exit, a rebalance or a flip, selected by TseRuleType and bound to a pattern.
  • tse_add_rule_risk — the stop-loss and take-profit families, fixed or trailing. This rule takes no pattern at all: it watches the position and fires when the unrealized-P&L ratio crosses its threshold.
  • tse_add_rule_multileg, tse_add_rule_bracket, tse_add_rule_oco — several legs emitted as one atomic transaction, an entry with venue-resting protective legs, and a one-cancels-other pair.
  • tse_add_rule_cancel, tse_add_rule_replace, tse_add_rule_modify — amendments of an order already in flight.

A rule decides what to send; it does not decide what is permitted. Permission is a separate layer: risk policies declared with tse_add_risk_policy and tse_add_risk_policy_time_period gate orders on position value, on position quantity and on the trading window before anything reaches the execution. The risk management chapter treats that layer on its own, and the Rule chapter documents every field of TseRuleParams.

A Robot owns a set of Rules

A robot is the execution node that closes the graph at the top, and the unit at which the engine is operated. It is declared over rules that already exist, it receives every instruction those rules emit, and it forwards each one into the account's order management under its own label. Because that label travels onward with every reservation, fill and journaled trade, results can be read back per robot on an account running many robots at once.

A robot is declared with tse_add_robot, which is handed the labels of the rules it is to own. Since a rule names its pattern and a pattern names its inputs, adding a robot transitively takes in a whole dependency component whose leaves are inputs. Wiring alone changes nothing: tse_start is what sets the component in motion, tse_stop and tse_halt bring it to rest, and the bulk actions tse_cancel_all, tse_sale_all, tse_halt_and_cancel_all, tse_halt_and_sale_all and tse_halt_and_cancel_sale_all combine halting with withdrawing resting orders and flattening positions.

The robot is also the unit the licence counts. Starting one takes a seat keyed by the account and the robot label; stopping or halting it returns that seat. There is a single pool of seats: a robot that backtests, a robot that trades live and a robot started by a grid search all count against the same purchased number of robots, and the applied ceiling, whether the licence is unlimited, and the observed peak are readable at any time through tse_protection_status.

An Account owns the robots

The account is the root of every run. It is created first with tse_account_create — which asks for a label, a storage regime, a currency and a core id, and supplies no default for any of them — everything else except the market adapters is declared on it, and every result is read back through it.

The account owns the single execution, the books, the portfolio that carries the positions, and the blotter that journals the trades — the storage regime chosen at creation selects whether that blotter lives in memory, in a database, or in both. A contract belongs to no account: tse_add_contract declares it in the library itself, and the handle it returns stays good for the whole life of the process, so the same contract may be wired into any number of accounts. Handles taken from an account are borrowed views into it, so the account is destroyed last. The market adapters are not among them: an adapter handed to an account is held by it jointly with the handle that created the adapter and with every other account the adapter feeds, so the adapter and the accounts may be destroyed in any order.

The execution is the account's outbound edge, and there is exactly one of it. tse_exec_create_simulator builds the engine's own fill model, which is connected to market data and fills orders as ticks arrive. tse_exec_create_simulator_book builds the second fill model the engine ships: it is connected to a book feed, keeps a mirror of that book per traded contract and stands the account's own order in the queue of its price level. tse_exec_create_custom builds an execution that is not connected to market data at all: it hands each order to your callback, which reports fills back with tse_exec_apply_fill. Swapping one for the other is the whole distance between a backtest and live trading — the graph above it does not change.

The Simulator's own behaviour is declared in one aggregate. fillRatio and probFill are percentages that decide how much of an order a tick executes; the four preference fields name which field of a bar or of a quote a fill prices against, per side. The last two carry the broker's execution lag as a count and the unit that count is measured in: the Simulator acts on an order, a cancel, a modify or a replace only at the first market event later than the moment that order or message arrived plus the lag, so a lag longer than the gap between ticks holds the order back for as many ticks as it spans. The lag governs when the Simulator acts on the message, not when the order itself changes: the order management applies an amended quantity and price to the order the moment it admits the amendment and re-reserves against them, while the Simulator learns of that amendment only once the lag has run. An order with a modify or a replace in flight is therefore not held back — it stays reachable on its own arrival moment plus the lag — and the Simulator fills it against the amended quantity, because the quantity it reads is the one the order already carries. The book Simulator measures that fill differently: its queue takes the quantity from the order management at the moment the order reaches the book and then keeps a remainder of its own until the amendment lands, so an order already standing in the queue fills against the quantity it was admitted with. A cancel is the exception in both fill models, because an order awaiting a cancel is never filled, and under the book Simulator such an order never reaches the queue at all. A zero execLagValue means no lag and leaves execLagDuration immaterial, which is how an aggregate written with only its six leading fields behaves. A negative count, a unit outside TseDuration, or a lag too large to hold in nanoseconds is refused, and the simulator is not created. The settings can be read back from the execution with tse_exec_get_simulator_config, field for field as they were given.

FieldType
fillRatioint32_t
probFillint32_t
sellPreferenceTseOhlcvField
buyPreferenceTseOhlcvField
sellPreferenceBidAskTseBidAskField
buyPreferenceBidAskTseBidAskField
execLagValueint32_t
execLagDurationTseDuration

The declaration order follows the dependency order.

  • Declare the contracts an already-open position names, if there is one.
  • Create the account, seeding those positions into it if there is one, then declare the remaining contracts.
  • Create the market adapters the data will arrive on.
  • Create the execution, simulated or custom.
  • Declare the inputs, naming their adapter and contracts.
  • Declare the patterns over the input labels.
  • Declare the rules over the pattern labels, and the risk policies that gate them.
  • Declare the robot over the rule labels.
  • Start the robot, then push data.

The order is also a deadline: an account's composition is fixed by its first start. Contracts, market adapters, the execution, inputs, patterns, rules, robots and the feeds they need are all declared before it, and the account takes no further declarations once a robot has started.

Every contract a running robot trades is marked to market automatically; tse_portfolio_subscribe adds further contracts you want marked without trading them. The Account chapter covers the portfolio, the booking calls and the resets in full.

Positions a client already holds enter at the very top of that order, not partway down it. The seeding form of account creation takes them as an array and builds them into the portfolio while the account is being built, which is before any rule, any robot and any tick, so the robots that are declared afterwards see a book that is already populated. This is the reason the step belongs here rather than among the booking calls: booking reports a transaction and moves a position, while seeding states the position the run begins with.

What comes back

Fills return along the same chain in the opposite direction. Each one moves the portfolio, so the position and the account aggregate are current at any instant: tse_get_position_state snapshots one contract into TsePositionState, tse_get_portfolio_state snapshots the whole book into TsePortfolioState.

Each fill is also journaled by the blotter as a retained trade. tse_get_trades reads them back as TseTrade records, filtered by time range and optionally by robot. A retained trade is deliberately wide: besides price, quantity, fee and booked P&L it carries both order identifiers, the rule and robot labels that produced it, and the contract and portfolio exposure captured at the moment of the trade — which is what makes the journal joinable against whatever your own processor logged.

Aggregates come from the same store. tse_get_summary folds the whole account into a TseSummary; tse_get_robot_summary and tse_get_summaries do the same per robot. Beyond the summary lies the ex-post layer, which buckets each robot's trades over a time step and scores them: tse_ex_post_create builds a live object on as many threads as it is given, its feature matrices are read through tse_ex_post_feature_momentum, tse_ex_post_feature_ewma and tse_ex_post_feature_level_crossings — the projections a model uses to choose among strategy candidates — tse_ex_post_save_compact and tse_ex_post_save_complete write what the object holds into an SQLite database without scoring again, and tse_ex_post_open and tse_ex_post_rebuild_from_file read such a database back — the first for its features alone, the second to carry the run forward over the trades that have arrived since. Trades executed outside the engine can be folded into the same picture with tse_book_trade and tse_book_trade_with_exposure.

The whole assembly at a glance

Engine architecture: market data, model output and derived series are pushed into a named market adapter, which stands on its own and may be shared by several accounts; the chain then runs Input, where your data processor lives, then Pattern, which fires on a condition, then Rule, which turns a firing into one fully specified order, then Execution, the simulator in a backtest and your callback when live; a robot is a named set of rules and is the unit that starts and stops; contracts are declared in the library and reach the chain as handles; pre-trade risk is computed at the rule, and bracket and OCO orders are left resting at the venue; the account owns the money and remembers what happened; ExPost reports the summary, every retained trade and the duration-bucketed scores.

Every robot is assembled by the same twelve declarations, in the same order, and none of them is optional. The account is created; an adapter is opened for the shape of market data that will arrive; an execution is attached; the traded contracts are declared; an Input is given its processor; a Pattern is pointed at that Input; a Rule is pointed at that Pattern; a Robot is given its rules; the robot is started; data is pushed tick by tick; the summary is read; the account is destroyed and the adapter handle released. The three surfaces differ in spelling alone — the same twelve steps in the same order on each.

Read the diagram in that order and the chain explains itself: the outside world stops at the adapter, your own thinking lives in the input and nowhere else, the pattern is where thinking becomes a decision, the rule is where a decision becomes a fully specified instruction, the robot is the thing you switch on, and the account is what owns the money and remembers what happened. Nothing above the input knows what your processor computed, and nothing below the rule knows why it was asked to trade — which is what lets you replace either end without disturbing the other.

This chapter has deliberately shown no code: it is the map, not the manual. The chapters that follow give each stage its complete surface on all three languages, and the examples chapter opens with this same assembly written out end to end, from a moving-average crossover to a book-imbalance market maker.

Version 6.1.0.0. Also as Markdown: this chapter, the whole reference in one file, and the llms.txt map.