ctx.order.market()
Place a market order that fills at the next bar's open.
Signature
ctx.order.market(symbol, quantity, metadata?)
Parameters
| Name | Type | Description |
|---|---|---|
symbol | string | Asset symbol (typically 'ASSET') |
quantity | number | Position size: 1 = full position, 0.5 = half, -1 = short |
metadata | object | Optional order metadata |
Quantity
1= Full long position (100% of capital)0.5= Half position (50% of capital)0.25= Quarter position-1= Full short position-0.5= Half short position
Metadata Options
| Option | Type | Description |
|---|---|---|
signal | string | Signal name for analysis ('buy', 'sell', etc.) |
reason | string | Human-readable reason |
type | string | Order type hint ('full_entry', 'partial_entry') |
Examples
Full entry
function onBar(ctx, i) { if (q.crossOver(ctx.ind.fastEma, ctx.ind.slowEma, i)) { ctx.order.market('ASSET', 1, { signal: 'buy', reason: 'ema_crossover' }); } }
Partial entries (scaling in)
function onBar(ctx, i) { const rsi = ctx.ind.rsi[i]; if (rsi < 20) { // Extreme oversold: half position ctx.order.market('ASSET', 0.5, { signal: 'buy', type: 'partial_entry' }); } else if (rsi < 30) { // Oversold: quarter position ctx.order.market('ASSET', 0.25, { signal: 'buy', type: 'partial_entry' }); } }
Short positions
Use negative quantity to open short positions. Your strategy must explicitly handle short entries and exits in its onBar function — the engine does not auto-invert long signals.
function onBar(ctx, i) { const tradingMode = ctx.p.tradingMode || 'LONG_ONLY'; if (tradingMode === 'SHORT_ONLY') { // Enter short on bearish cross if (q.crossUnder(ctx.ind.fastEma, ctx.ind.slowEma, i)) { ctx.order.market('ASSET', -1, { signal: 'enterShort' }); } // Exit short on bullish cross if (q.crossOver(ctx.ind.fastEma, ctx.ind.slowEma, i)) { ctx.order.close('ASSET', { signal: 'exitShort' }); } } }
Important: Strategies must natively implement the trading modes they support. The engine does not auto-invert signals. Unsupported modes are locked in the UI. See the Trading Modes guide under Best Practices for full details.
Fill Timing
Market orders fill on the next bar's open:
Bar i: close = 100, onBar places market order Bar i+1: open = 101 → Order fills at 101 (± slippage)
Slippage
Slippage is applied based on backtest configuration:
Fill Price = Next Bar Open × (1 + slippage × direction)
Where direction is +1 for buys (slippage works against you), -1 for sells.
Related
- ctx.order.close() — Close position
- ctx.order.reduce() — Partial exit
- Market Fill Rules — Engine execution
ordersmarketbuysellentryqsl