Robot examples

The examples are ordered as a course, and every one of them is the same twelve declarations in the same order: what differs between a moving-average crossover and a book-imbalance market maker is the body of one callable and the parameters on the rules. Each example is a directory holding a C++ and a Python version of the same strategy.

All examples on GitHub

Hello world

Brings a robot up from nothing: the process-wide configuration, the full assembly, a parameter sweep, several feeds, the saved recipe, the swap that takes it live, and the four position verbs a rule can speak.

01 — Initial parameters

No robot — process-wide configuration only; each account captures the data folder at construction.

Sets the data folder, the log folder and the log level before the first account exists. An account resolves its data folder when it is built, so the example builds one account, switches the folder, builds a second, and each blotter file sits in the folder configured at its own construction time.

Source on GitHub

02 — MACD

Input: MACD of the close → Patterns: thresholds against zero → Rules: market entry and exit.

The Input computes a MACD, the distance between a twelve-day and a twenty-six-day exponential average of the close, and returns readiness only once the slow average has seen enough days. Threshold Patterns compare that distance against zero, and the Rules bound to them enter and leave.

Source on GitHub

02.1 — Built-in inputs

Inputs: two shipped simple moving averages → Pattern: crossover → Rules: market entry and exit.

The same robot as example 02, with one line changed in kind: the indicator is declared rather than written. Nine processors ship with the library, and a built-in input names which of them it wants, which field of the record it reads and over how long a window. The processor is engine code called directly, so no tick crosses the language boundary, and the robot is savable with nothing registered, because the recipe describes a shipped processor completely.

Source on GitHub

03 — Grid search

One robot built four times by the grid runner — a fresh account per lookback, the same history.

The same robot built four times, over lookbacks of 10, 20, 50 and 100 days against a 200-day trend. The grid runner takes a builder callback that assembles a fresh account for every parameter value and feeds it the same history, and the independent grid points run side by side on a pool of worker threads.

Source on GitHub

03.1 — Grid search over several parameters

A grid over a table of period pairs: three fast against two slow, six independent runs.

Example 03 searches one parameter against a fixed trend; this one searches both ends of the crossover. The grid builder is handed exactly one number per run, so a pair of periods cannot pass through it directly: the combinations are tabulated before the run and the number becomes an index into that table. The same number comes back with the result, which is how the printed line knows which pair produced it. The technique scales to a grid of any width, because the number of parameters changes the table and not the signature.

Source on GitHub

04 — Multiple contracts

Inputs: average AAPL close, average BTC mid → Patterns: threshold and peak → Rules: market entry and limit exit on WTI.

Three feeds of three different shapes on three adapters: daily AAPL bars, BTC/USDT quotes and WTI trade prints. Two Inputs average the Apple close and the Bitcoin mid, a threshold Pattern and a peak Pattern watch them, and the Rules trade a third contract, WTI, which no Input watches at all.

Source on GitHub

05 — Multiple adapters

Inputs: 50-day and 200-day AAPL averages → Pattern: crossover → Rules: entry and exit; BTC/USDT is subscribed for marking only.

One account, two market-data adapters: daily bars for the equity and a quote stream for the crypto pair. AAPL is traded off the golden cross of its fifty-day and two-hundred-day average Inputs, while BTC/USDT carries no Input and no Rule and is subscribed to the quote adapter only so the portfolio can mark it.

Source on GitHub

06 — Save / Load — Research → Production

One graph, two processes: Python saves the recipe, C++ loads it, binds each Input to a fresh adapter and starts.

The Python leg registers its two processors under keys, builds a WTI crossover from Inputs declared by those keys, runs the backtest and saves the recipe: contracts, inputs, patterns, rules and the robot travel, adapters and execution do not. The C++ leg registers the same processor keys, loads the robot, binds each restored Input to today's adapter, and starts.

Source on GitHub

07 — Live trading

Input: MACD → Patterns: two thresholds at zero → Rules: market entry and exit; both ends swapped — a market-data client replays into the adapter, a broker callback replaces the Simulator.

The MACD robot of example 02, carried over line for line, with only its two ends replaced — which is the whole subject. The IN end is a schematic market-data client that subscribes to a feed and pushes each bar into the market adapter. The OUT end is a custom execution impersonating a broker: its transmit callback receives every order the engine emits, fills it in full at the last price it saw, charges a flat fee after a millisecond of latency, and reports the fill back. The execution is started before the replay and stopped after it, because that is the caller's duty. Comparing the two files shows that nothing between the ends was rewritten.

Source on GitHub

07.1 — Basic rules

Input: last trade price → Patterns: six timestamps one second apart → Rules: one per step — entry, rebalance, flip, rebalance, flip, exit — every order at the market; no indicator, no file of history, no selection criterion.

Four verbs partition every move a position can make, and a rule speaks exactly one of them: an entry opens from flat, a rebalance moves the position without leaving the side it is on, a flip crosses to the other side, and an exit closes it to exactly zero. This example speaks all four in one chain and ends flat, so the whole contract is visible in a single run. The quantity is what keeps the four disjoint rather than merely differently named: a flip must strictly exceed the open position, an exit must equal it exactly, a rebalance against its own position must stay strictly below it, and a quantity that breaks the bound of its verb is refused with the position left exactly as it stood. Every rule also names the position side it expects to find, so a flip silences the rules of the side it has just left and hands the chain to the rules of the side it has arrived on — which is why the example declares a rebalance and a flip for each of the two sides. The run prints the position after each verb — four long, six long, four short, six short, four long, flat — and retains eight trades rather than six, because a flip crosses zero and the engine splits the crossing trade into the half that closes the old position and the half that opens the new one.

Source on GitHub

Robots

Builds strategies with that assembly, changing the code inside one callable and the parameters on the rules and nothing else.

08 — Gradient-boosted model

Input: model forecast from mid, spread and quote imbalance → Patterns: thresholds at zero → Rules: market entry and exit.

The MACD robot with the processor replaced. A gradient-boosted model trained before the run lives inside a bid/ask Input that derives mid price, spread and quote imbalance from every tick and pushes the model's forecast as the Input's value, and two thresholds at zero put the robot long while the forecast is positive and flat once it turns.

Source on GitHub

Putting an XGBoost model inside a trading strategy, on one page of Python

09 — Rebalance

Patterns: two timestamps → Rules: one market entry and one rebalance that adds to the open position.

Two timestamp Patterns drive an entry Rule and a rebalance Rule: the entry Pattern fires once, the rebalance Pattern rearms one second after firing and fires twice. The rebalance adjusts a position that is already open, where an entry opens one from flat and an exit flattens it — it chains an adjustment order onto the open position, additively, and does not bring the position to a target size; it stays on the side it tends, and an adjustment that would leave that side is refused, because leaving it is the exit's business.

Source on GitHub

10 — Market maker

Inputs: two book feeds splitting client flow → Patterns: eight formulas reading the live position → Rules: eight orders whose size comes from the signal, four per side — entry, reduce, close and top-up — the two top-ups taking their price and side from the signal too.

Two book Inputs split client order flow — one keeps the client sells the maker buys against, the other the client buys it sells against — and eight formula Patterns whose closures read the live position through get_position_state decide which side to quote: below the upper inventory band the maker quotes both sides, at the band it stops adding and trades only the side that unwinds, until the inventory is back to zero. The eight Rules are the four position verbs as a two-sided book needs them: an entry opens from flat, and unwinding an inventory takes two Rules per side rather than one, because while more than a single quote's worth is still on the book the reduction keeps the side and is a rebalance, and only the last quote's worth leaves through an exit, which has to close the whole position. Every Rule declares its quantity undefined and reads it from the order the signal carries, so each order the maker sends is exactly the size of the client order it absorbs. The two top-up Rules, rebalances that grow the inventory, declare their price form and their transaction side undefined as well and take all three from the signal — a limit at the price the client asked rather than a market cross, and whichever direction the inventory currently has — the one place in the examples where a single firing supplies size, price and direction at once.

Source on GitHub

11 — Market maker with amendments

Input: recorded prints → Patterns: outsized trade, same-side run, quiet gap → Rules: cancel, replace and modify on the one resting quote.

One measurement pass over each recorded print produces the three facts the maker reacts to, each of them a Pattern: an outsized trade relative to the running average size, a third trade in a row on one side, and a silence longer than ten seconds. One quote rests at a time and one amendment Rule acts on it per fact — a cancel pulls it, a replace steps it away and shrinks it, a modify restores it to full size.

Source on GitHub

12 — Voting group

Input: signed sum of three robots' fills on an executed-trade adapter → Patterns: two thresholds → Rules: entry and exit for a fourth robot.

Four robots on one account: three vote by a seeded coin flip on every WTI bar, and the fourth trades their behaviour. The voter's Input over an executed-trade adapter keeps the signed sum of the traders' fills, and two thresholds over that net put the voter long while the group is net long and flat when the net returns to zero.

Source on GitHub

13 — Order-book imbalance

Input: built-in book imbalance over an L3 book → Patterns: thresholds at +0.2 and −0.2 → Rules: entry and exit on each side.

The account owns a real L3 book replayed from recorded quote snapshots, and the built-in imbalance Input reads it: whose resting size is bigger, the buyers' or the sellers', +1 when only bids rest and -1 when only offers do. Two thresholds at plus and minus a fifth drive the robot both ways, with four Rules covering entry and exit on each side.

Source on GitHub

14 — The same signal on L1, L2 and L3

The graph of 13 unchanged; the only line that differs is the book's construction — L1, L2 or L3.

The imbalance robot of the previous example, run unchanged on all three book depths a venue may publish: the only line that differs between the three variants is the book's construction as L1, L2 or L3. The feed adds at each step exactly the size that drags the book onto the next target imbalance, so the sequence of signals is known before the run starts and the three variants can be compared field by field.

Source on GitHub

15 — Cross-venue book arbitrage

Inputs: imbalance on an L2 book and on an L3 book → Patterns: formulas comparing the two → Rules: entry and exit in a third contract.

The same asset is quoted on two venues: an L2 book tracks the first, an L3 book tracks the second, and each feeds its own imbalance Input. Each formula Pattern remembers the last value seen from each Input, refuses to answer until both books have spoken, and fires when the two disagree by more than 0.3; the Rules take the position in a third, liquid contract that neither book observes.

Source on GitHub

16 — Multileg butterfly

Patterns: two timestamps → Rule: one multileg transaction of three option legs, filled together or not at all.

Two timestamp Patterns supply the two moments, and the Rules submit a SPY call butterfly — long one 440 call, short two 450 calls, long one 460 call — as a single multileg transaction, each leg a descriptor carrying its own contract, side, quantity, limit price and transaction kind. No leg fills until every leg's market has reached its price, and the exit reverses every leg in one all-or-nothing transaction.

Source on GitHub

17 — Multileg strangle hedge

Patterns: two timestamps → Rule: one multileg transaction of a future and three option legs.

The same atomic mechanics, now mixing instrument types inside one transaction: a long CLF26 future carries the directional view, two long out-of-the-money puts at 45 and 44 insure it against a fall, and a far out-of-the-money 60 call buys the upside back. Four legs, one hedged position, filled together or not at all, so the hedge never exists half built.

Source on GitHub

Risk

Adds risk control, in the form the engine computes inside itself and in the form that rests at the venue.

18 — Internal risk calculation

Risk policies gate the order before it is sent; risk rules watch the position and fire on the unrealized-P&L ratio. Neither takes a Pattern.

Risk the engine computes inside itself, measured against a baseline run of the same strategy. Risk policies are pre-trade gates tested against the position an order would leave you holding — five thousand on the market value of the whole book, a hundred shares on one name, a repeating window of local time in a named zone — while stop-loss and take-profit rules, each in a fixed and a trailing variant, watch the position tick by tick.

Source on GitHub

19 — Fixed bracket

Pattern: three-over-ten crossover → Rule: one bracket — an entry leg plus a fixed stop and a fixed target resting at the venue.

The Pattern is a WTI three-over-ten crossover and one bracket Rule submits the entry leg together with a venue risk specification holding a fixed stop-loss leg and a fixed take-profit leg, both resting at the venue good-till-cancelled. The entry is a 580-lot market order guarded two percent below the fill and three percent above it, and whichever side the market touches first closes the position and cancels its sibling.

Source on GitHub

20 — Trailing bracket

Pattern: fifty-over-two-hundred crossover → Rule: one bracket with a trailing stop and a fixed target.

The fixed bracket with one leg changed: a fifty-over-two-hundred golden cross on AAPL daily bars, protected by a five percent trailing stop and a fixed ten percent target. The kind field of the risk leg selects the behaviour — a fixed stop is priced once off the entry, a trailing stop is re-priced off the best price the position has seen.

Source on GitHub

21 — OCO and amendments

Patterns: timestamps → Rules: cancel, modify and replace on a resting limit order, and one OCO pair on an open position.

Four scenarios, each on its own rig and each fired by a timestamp Pattern so the moment of the action is exact. Three act on a limit entry already resting at the venue — a cancel Rule withdraws it and releases its reservation, a modify Rule shrinks it in place from ten to four, a replace Rule pulls it and posts a fresh one at a different price and size — and the fourth arms a stop and a target on an open position as one pair.

Source on GitHub

Statistics

Reads the statistics back: the blotter of retained fills, the per-period scores, and a model that ranks candidates on them.

22 — Transaction and robot statistics

One crossover robot; the statistics are read back off the account afterwards, not declared in the graph.

One WTI crossover run, read back at two levels. The transaction level is the blotter: one row per retained fill, with symbol, price, quantity, booked profit and loss and side; the duration level cuts the same run into day buckets and scores each one, and the example asks for the parameter count and the bucket count, then saves the scores and loads them back.

Source on GitHub

22.1 — Multithreaded ex-post

Eight crossover robots, each on a contract of its own fed the same WTI bars; the scores are computed on every core.

Example 22 with eight robots instead of one, differing only in how far back the slow average looks, from 10 to 45 bars in steps of five. The ex_post object is created on every core of the machine: the day buckets of different robots are built in parallel, and at each day step the robots whose scoring window is complete are scored in parallel. The number of threads changes the time, not the result — the scores are the same, bit for bit, as with one thread, except thompson_sampling, which is random by design. The scoring runs once, when the object is created; saving writes what the object already holds, and the database is loaded back.

Source on GitHub

22.2 — Resuming ex-post

The WTI run of example 22, saved and then re-entered three ways.

Saving comes in two forms: the complete form keeps the inference record and every absent score, the compact form drops both, which makes it smaller and fit for SQL but unfit for being read back. Opening is the cheap way in, reading the features out of the file without touching a trade or scoring anything, and such a handle refuses both saves rather than writing half a file. Rebuilding is the resuming way in and the one a daily cycle uses: the series, the ranking ring and the journal come back from the file, the new trades extend them, and the scoring pass runs from one window behind the newest stored bucket. Money is stored in floating-point columns, so a resumed result is very close to a full one rather than identical.

Source on GitHub

23 — Candidate selection

Five robots from the grid runner, each scored through ex_post; a booster ranks the score rows and picks one.

Five candidates differing only in their fast lookback are swept with the grid runner, and each builder opens an ex_post object on its own finished account and extracts the score matrix. The feature vector is the last bucket's row, a booster is trained on every candidate but the one held back, and the pick is the highest predicted profit rather than the highest realized one.

Source on GitHub

A hundred candidates, re-chosen every hour

23.1 — Candidate selection, multithreaded

The five candidates of example 23; each finished account is scored twice, on one thread and on every core.

The candidate selection of example 23 with every candidate scored twice. On each finished account of the grid the builder creates two ex_post objects, one on a single thread and one on every core of the machine, and compares their momentum matrices row by row: bit for bit in every column except thompson_sampling, which must agree only in where it is NaN. The model is trained on the all-core scores. Each account holds a single robot, and ex_post never uses more threads than there are robots, so the example shows the API and the agreement of the results, not a gain in time.

Source on GitHub

Everything else

Collects the facilities that surround a running engine: cores, currency, storage, manual booking, bulk controls, the data toolkit and the book Simulator.

24 — Core affinity

One AAPL trend follower run twice, once unpinned and once with the inputs and the blotter nailed to cores.

Every worker in the export surface takes a core identifier last: the account, each execution, each input, each pattern and the robot. A non-negative value pins that worker, minus one leaves the choice to the operating system, and cores zero and one stay reserved, so two is the lowest a worker may be nailed to. Both runs agree, and that is the claim: affinity buys latency stability under load, never a different trade. The layout follows one rule, that a chain of input, pattern, rule and robot takes a thread only once, so the pinned arrangement here needs a processor with at least five cores.

Source on GitHub

25 — Multicurrency

Patterns: two timestamps → Rules: one entry and one exit; the only variable is the account's currency.

An account denominated in euro: the currency is the third argument of account construction, and prices, cash, booked profit and loss, the summary and the portfolio mark all inherit it. The strategy is a single round trip driven by two timestamp Patterns, ten units bought at 100 and sold at 110, because the subject is the unit and not the edge; the engine never converts between currencies. Note what this example is: the engine is validated on a single currency, the US dollar — the euro value and cross-currency portfolios are a feature in the process of shipping, so the interface here runs ahead of its cross-currency arithmetic.

Source on GitHub

26 — Storage regimes

The same crossover graph twice; the only variable is the account's storage regime.

The same WTI crossover backtest run twice, with the blotter in memory and with the blotter on disk in the configured data folder. The regime is the second argument of account construction and decides where the statistics live, never what the strategy does, and both runs agree on net profit and trade count.

Source on GitHub

27 — Manual booking

No graph at all — a contract registered straight with the portfolio, and fills booked by hand.

The portfolio without an adapter and without an execution: a contract is registered directly with the portfolio and fills the engine never placed are booked by hand. The plain mode runs the average-cost engine and computes the profit and loss while the caller only reports what traded; the exposure-parameterized mode prices the fill against exposure snapshots the caller supplies and leaves the live position exactly as it was.

Source on GitHub

27.1 — Seeded position

An account created already holding ten of a contract at a hundred, with nothing traded and nothing journaled.

A desk that is already long does not start from nothing. The whole of it is one extra argument to account construction, an array of seeded positions, each naming the contract by its handle and carrying the quantity, the acquisition price with its stamp, the market price with its stamp, and the side. The highest and lowest prices seen are deliberately not seedable, so a trailing protective order measures from the first price that arrives. A seeded position is a real position: a closing, reducing or rebalancing rule is accepted against it, and an entry rule is refused for the same reason it would be refused after a genuine fill. The set is taken whole or not at all, and nothing of it reaches the journal or the save recipe.

Source on GitHub

28 — Bulk actions

Rules: one market entry and one resting limit exit, then the bulk actions addressed by robot label.

One rig — an entry that fills at the market and an exit that rests as a limit far above it — acted on by robot label, so there is always both a position and a resting order. Cancelling the resting orders and flattening the position cleans the desk without stopping the robot; halting plus a cancel keeps the position open; halting plus a market close leaves a flat desk; the composite of halt, cancel and flatten is the kill switch.

Source on GitHub

29 — Timeserie tools

No robot — the CSV reader, the slicing transforms and the writer run as one pipeline.

A CSV of daily Apple closes is read with an explicit date format, separator and header setting, and the index and one named column come back as a timestamp series and a value series. The series is cut to calendar year 2010 by nanosecond bounds taken as midnight UTC, written back out with formatted timestamps, read again, and windowed with head and tail, which hand back a begin-and-length view rather than a copy.

Source on GitHub

30 — Statistics

A field of the bar projected into a flat array, then the measures asked directly.

The moments in one pass, the median, the ninetieth percentile, the interquartile range, and two robust measures of scale that barely move when a few observations do. The trend is read by two tests that assume nothing about the shape of the series, the Theil–Sen slope and the Mann–Kendall statistic, both asked over the last sixty days because the slope is quadratic in the number of observations. The interval for the mean is built on the Student distribution. The correlogram closes the run, one value per lag with lag zero included. Every one of these calls may legitimately have no answer, which is why each returns an optional in C++ and None in Python rather than a number and a flag.

Source on GitHub

31 — Book Simulator

Input: built-in book imbalance over an L3 book → Pattern: a formula that fires at +0.2 or more while the account has nothing resting, naming the best bid as the price → Rule: one entry, a limit order at that price; the execution is the book Simulator, and there is no price feed.

The second execution the engine ships fills an order against an order book rather than a quote: it mirrors the book feed for every traded contract, stands the account's own order in the queue of its price level and fills it only as the feed's executions reach it. The recorded order flow is replayed as the orders each snapshot implies, the price paid between snapshots arrives as an execution, and the portfolio marks the position by a quote the execution derives from the top of its mirror, so the example carries no price feed. What the run prints is the place in the queue — the orders ahead, the size they hold and what is left of the account's own order — for the order taken from the admitted listing. Two more capabilities close the example: the derived quote is taken as an ordinary market and the portfolio is subscribed to it by name, and the mirror is emptied through the execution, which reports every order still resting there as cancelled.

Source on GitHub