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, the money and the record of what happened. Contracts and market adapters are declared outside any account and may be shared by several of them.
The shape of that chain is not invented here. It follows Peterson's Developing & Backtesting Systematic Trading Strategies, and the vocabulary below is deliberately the vocabulary of that book.
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 stands on its own: it is created without an account, carries a currency, and may be handed to any number of accounts reporting in that currency. Named, because inputs refer to it by that name; typed, because the record it accepts is fixed when it is created and validated on every push. A contract, likewise, is declared in the library rather than on an account, and everything downstream addresses it by the handle the declaration returns.
| Adapter type | Record pushed |
|---|---|
| OHLCV | candles |
| Bid/ask | top-of-book quotes |
| Trade | prints |
| Executed | trades that happened elsewhere |
| Book | order-book messages |
An input processes every tick
An input is the entry node of the graph and the one place where your own ideas live. It is a named, duration-stamped cache built over a set of contract handles on one adapter: the input hands each arriving tick to your data processor, and whatever the processor stores becomes the series that patterns later observe. The processor may push a value into the storage, which the engine stamps with the timestamp of the tick being processed, and it returns a readiness flag.
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. The builders are
add_input_ohlcv,
add_input_bidask, add_input_trade, add_input_executed,
add_input_book and add_input_book_imbalance.
A pattern watches inputs and fires
A pattern is the decision node. It observes one or more inputs by label and fires whenever its condition holds. What it publishes on firing is an order: the five analytic kinds publish a zeroed one, offering nothing of their own, while a formula hands the order to your callback to fill. A pattern still places nothing by itself — the order becomes tradeable only through a rule that names the pattern, and the rule reads from it exactly the fields its own parameters left undefined.
Six kinds cover the vocabulary of a condition, each demanding an exact number of observed inputs. Threshold, peak and timestamp take one input; comparison and crossover take two; formula takes any number and hands each update to your callback.
A rule turns a firing into an order
A rule is the bridge between observation and action, and every trade it orders is one of four position verbs. A position is short, flat or long, and five moves exist between those states: four verbs name the four a rule may order, one verb per move, and the fifth, flat to flat, is refused because there is nothing to trade.
| Position before | Position after | Verb |
|---|---|---|
| flat | long or short | Entry |
| long or short | the same side, another size | Rebalance |
| long or short | the other side | Flip |
| long or short | flat | Exit |
| flat | flat | none: refused |
| Verb | Requires | Transaction side | Quantity | Position after |
|---|---|---|---|---|
| Entry | a flat book | either | positive | open on the side of the transaction |
| Rebalance | an open position | with the position it grows, against the position it shrinks | against the position, strictly less than it holds | open on the same side |
| Flip | an open position | against the position | strictly more than it holds; “all” is twice the position | open on the other side |
| Exit | an open position | against the position | exactly what it holds; “all” is the position | flat, and the entry is armed again |
The quantity is what keeps the verbs disjoint rather than merely differently named. A quantity that breaks the bound of its verb is refused when the order is admitted, the position is left exactly as it stood, and no quantity is ever reinterpreted into a different verb. The same exactness binds the four protective kinds — stop-loss, trailing stop-loss, take-profit and trailing take-profit — because each of them closes a position: a protective kind always closes the whole position, and the quantity it declares is not honoured. It binds an amendment of a resting order as well: a closing order amended away from the position it closes is refused.
Everything the resulting order will carry — the sides, the quantity, the price form, slippage, fee, time-in-force and priority — is fixed when the rule is built, and none of those ten values has a library-supplied default; three of them — the quantity, the price and the transaction side — may be declared undefined and are then read from the order the signal carries. The firing supplies only the moment. A market rule speaks one verb and is bound to a pattern. The risk rule is the stop-loss and take-profit family and takes no pattern at all — it watches the position. Multileg, bracket and OCO emit several legs as one atomic transaction; cancel, replace and modify amend an order already in flight. An amendment takes effect on the order at once and reaches the venue only after the execution lag, so a single order stays reachable and fills against its amended quantity while the amendment is in flight, where a multileg group freezes until the amendment lands. A rule decides what to send, not what is permitted: risk policies gate orders on position value, position quantity and the trading window.
All four verbs in one chain
The example 07.1 hello world - basic rules speaks all four verbs on one instrument and ends flat. Six timestamp patterns fire one second apart, each drives one rule, every order is at the market, and the position after each step reads straight off the output. The run retains eight trades rather than six, because a flip crosses zero and the engine splits a crossing trade into the half that closes the old position and the half that opens the new one.
| Step | Verb | Quantity | Transaction side | Position it expects | Position after |
|---|---|---|---|---|---|
| 1 | Entry | 4 | long | flat | 4 long |
| 2 | Rebalance | 2 | long | long | 6 long |
| 3 | Flip | 10 | short | long | 4 short |
| 4 | Rebalance | 2 | short | short | 6 short |
| 5 | Flip | 10 | long | short | 4 long |
| 6 | Exit | 4 | short | long | flat |
Source: 07.1 hello world - basic rules on GitHub.
A robot owns a set of rules
A robot is declared over rules that already exist and forwards every instruction they emit under its own label, so results can be read back per robot on an account running many at once. 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. The robot is the unit you start and stop, and the unit the licence counts.
An account owns the robots
The account is the root of every run. It is created first, with no default supplied for its label, storage regime, currency or core id, and it owns the contracts, the adapters, the single execution, the portfolio and the blotter. The execution is the account's outbound edge: the built-in Simulator fills orders as ticks arrive, the book Simulator stands each order in the queue of its price level on a mirror of an order book, and a custom execution hands each order to your callback. Swapping one for the other is the whole distance between a backtest and live trading.
A book Simulator is handed its feed before the account starts. The mirror holds no past: it fills only from the messages that arrive after the feed is attached, so a feed attached later measures the queue only from that moment, and the fill rate of its contract is not comparable with the rest. Once a robot has started, a late book feed is refused.
The book Simulator also hands two things back. The bid-ask quote it derives from the top of its mirror is available as an ordinary market, so the portfolio, an input, a rule and a robot subscribe to it exactly as to a feed of your own making; the rule there is read, do not write, because the Simulator fills that feed itself. And a book is emptied in one call: it returns to the state it was built in, its contract untouched, and takes new messages at once. The one book not emptied that way is the Simulator's own mirror, which is cleared through the execution, because the queue it keeps has to be forgotten in the same action.
The declaration order follows the dependency order
- Create the account, then declare its 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.
- 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.
What comes back
Fills return along the same chain in the opposite direction. Each one moves the portfolio, and each is journaled by the blotter as a retained trade carrying the rule and robot labels and the exposure captured at the moment of the trade. Beyond the summary lies the ex-post layer, which buckets each robot's trades over a time step and scores them.
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.
Version 5.5. Also as Markdown: this page, and the llms.txt map.