Order & Position Types
OrderMeta
Metadata attached to orders for tracking and analysis. All fields are optional.
interface OrderMeta { signal?: string; // Signal name shown in results (e.g., 'buy', 'sell') reason?: string; // Human-readable reason type?: 'full_entry' | 'full_exit' | 'partial_entry' | 'partial_exit'; [key: string]: unknown; // Custom fields }
ctx.order.market('ASSET', 1, { signal: 'buy', reason: 'ema_crossover', type: 'full_entry', confidence: 0.85, // custom field });
Allocation and Partial Orders
Order quantities are fractions of the strategy allocation. Use ctx.order.market() to enter or add to a position, and ctx.order.reduce() to realize part of an open position.
| Order | Fraction | Result |
|---|---|---|
market('ASSET', 1, ...) | 1 | Full long allocation |
market('ASSET', 0.25, ...) | 0.25 | 25% long allocation |
market('ASSET', -0.5, ...) | -0.5 | 50% short allocation |
reduce('ASSET', 0.25, ...) | 0.25 | Close 25% of the current position |
Set the metadata type to describe the order lifecycle. This allows backtests, live validation, and Adaptive Flow to preserve the remaining position after a partial exit and record its realized profit separately from the final close.
// Build a position, take partial profit, then close the remainder. ctx.order.market('ASSET', 0.2, { signal: 'scout', type: 'partial_entry', }); ctx.order.market('ASSET', 1, { signal: 'confirmed_entry', type: 'full_entry', }); ctx.order.reduce('ASSET', 0.25, { signal: 'first_target', type: 'partial_exit', }); ctx.order.close('ASSET', { signal: 'final_exit', type: 'full_exit', });
Position
ctx.pos(symbol) exists (note: ctx.position() is not a real method), but it does not track real
position state during a backtest - it always reports flat (qty: 0), no matter what orders you've
already placed in the run. Never use it to decide whether you currently hold a position.
interface Position { qty: number; // Always 0 during a backtest - do not rely on this avgPrice: number; // Always 0 during a backtest - do not rely on this }
The correct pattern: track position yourself with ctx.state
Every working strategy template tracks its own position with a ctx.state flag, set on entry and
cleared on exit:
function onBar(ctx, i) { // ...indicator readiness checks... if (ctx.state.inPosition) { // Currently holding - only look for exit signals if (/* exit condition */) { ctx.order.close('ASSET', { signal: 'sell' }); ctx.state.inPosition = false; } return; } // Currently flat - only look for entry signals if (/* entry condition */) { ctx.order.market('ASSET', 1, { signal: 'buy' }); ctx.state.inPosition = true; } }
OrderAPI
The full order interface available via ctx.order:
interface OrderAPI { market(symbol: string, qty: number, meta?: OrderMeta): void; close(symbol: string, meta?: OrderMeta): void; reduce(symbol: string, pct: number, meta?: OrderMeta): void; limit(symbol: string, qty: number, price: number, meta?: OrderMeta): void; }
| Method | Description |
|---|---|
market() | Immediate fill at next bar's open |
close() | Close entire position |
reduce() | Reduce position by a percentage (0–1) |
limit() | Fill when price reaches the limit |
See the Strategy API for detailed usage of each method.