Make the Assumptions Executable
A paper order filled at the last traded price can hide the cost of crossing a spread. Filling every request in full also hides what happens when the displayed quantity is smaller than the order.
A local executor should state the model that produces its fills. The following example uses a synthetic instrument with integer price ticks and integer quantity units. It tests ledger behaviour; it does not reproduce an exchange matching engine.
The model fills a buy at the supplied ask plus two price ticks, charges one tick of fee per unit, and fills no more than the supplied available quantity or cash can cover. Any unfilled remainder is cancelled immediately. Each quote represents fresh, independently supplied liquidity; reusing the same quote does not model depletion across calls.
Save this as paper-broker.mjs:
paper-broker.mjs
export function createPaperBroker(initialCash) {
if (typeof initialCash !== 'bigint' || initialCash < 0n) throw new Error('Invalid cash');
let cash = initialCash;
let position = 0n;
return {
buy({ requested, ask, available }) {
for (const value of [requested, ask, available]) {
if (typeof value !== 'bigint' || value < 0n) throw new Error('Invalid fixture unit');
}
if (ask === 0n) throw new Error('Ask must be positive');
const price = ask + 2n;
const feePerUnit = 1n;
const affordable = cash / (price + feePerUnit);
const filled = [requested, available, affordable].reduce((a, b) => (a < b ? a : b));
const fee = filled * feePerUnit;
cash -= filled * price + fee;
position += filled;
return { filled, cancelled: requested - filled, price, fee, cash, position };
},
};
}
BigInt keeps these synthetic units exact. A real instrument needs a deliberate conversion from its decimal quantity and price increments; assigning arbitrary ticks to an exchange symbol would change the model's meaning.
Check the Ledger
Save this beside the module and run node --test paper-broker.test.mjs:
paper-broker.test.mjs
import test from 'node:test';
import assert from 'node:assert/strict';
import { createPaperBroker } from './paper-broker.mjs';
test('partial execution charges only filled units', () => {
const broker = createPaperBroker(1_000n);
assert.deepEqual(broker.buy({ requested: 10n, ask: 100n, available: 3n }), {
filled: 3n,
cancelled: 7n,
price: 102n,
fee: 3n,
cash: 691n,
position: 3n,
});
});
test('cash limits execution after price adjustment and fees', () => {
const broker = createPaperBroker(205n);
const result = broker.buy({ requested: 5n, ask: 100n, available: 5n });
assert.equal(result.filled, 1n);
assert.equal(result.cash, 102n);
});
test('an empty quote produces no fill', () => {
const result = createPaperBroker(1_000n).buy({ requested: 2n, ask: 100n, available: 0n });
assert.equal(result.filled, 0n);
assert.equal(result.cash, 1_000n);
});
Connect Data Without Changing the Model Silently
An SDK can supply public quotes to an adapter. That adapter must establish quantity units, quote age, and the available side of the book before supplying a model input. A last-trade ticker does not establish executable ask depth.
The public-data-first program starts the observation path separately. Recorded observations make it possible to compare model changes using the same inputs.
Latency, queue position, multi-level depth, sell orders, resting orders, and funding are outside this small executor. Add one assumption at a time with a fixture that demonstrates its effect. An exchange demo environment is a separate test surface and should not be presented as equivalent to this local model.
Related articles
Continue from here