Skip to content

Trading

Place orders, manage positions, and execute trades.

Place an Order

typescript
// Market buy order
const response = await client.trading.placeOrder({
  marketId: 'BTC-USD',
  side: 'buy',
  type: 'market',
  quantity: 1000000n, // 0.01 BTC (8 decimals)
})

// Limit sell order
const response = await client.trading.placeOrder({
  marketId: 'BTC-USD',
  side: 'sell',
  type: { limit: 50000_00000000n }, // $50,000 (8 decimals)
  quantity: 1000000n,
  timeInForce: 'GTC',
})

Convenience Methods

typescript
// Market orders
await client.trading.marketBuy('BTC-USD', 1000000n)
await client.trading.marketSell('BTC-USD', 1000000n)

// Limit orders
await client.trading.limitBuy('BTC-USD', 50000_00000000n, 1000000n)
await client.trading.limitSell('BTC-USD', 55000_00000000n, 1000000n)

Order Parameters

ParameterTypeRequiredDescription
marketIdstringYesMarket symbol (e.g., 'BTC-USD')
side'buy' | 'sell'YesOrder side
type'market' | { limit: bigint }YesOrder type with price for limit
quantitybigintYesOrder quantity (8 decimals)
timeInForceTIFNoTime in force (default: 'GTC')
slippageTolerancenumberNoSlippage in basis points (default: 100 = 1%)
selfTradePreventionSTPNoSelf-trade prevention mode

Time in Force

OptionDescription
'GTC'Good Till Cancelled - remains until filled or cancelled
'IOC'Immediate or Cancel - fill immediately, cancel remainder
'FOK'Fill or Kill - fill entirely or cancel
{ GTD: bigint }Good Till Date - expires at timestamp

Self-Trade Prevention

typescript
await client.trading.placeOrder({
  marketId: 'BTC-USD',
  side: 'buy',
  type: { limit: 50000_00000000n },
  quantity: 1000000n,
  selfTradePrevention: 'cancelNewest',
})
OptionDescription
'none'No prevention
'decrementAndCancel'Reduce quantity, cancel remainder
'cancelOldest'Cancel the older order
'cancelNewest'Cancel the incoming order
'cancelBoth'Cancel both orders

Cancel Orders

typescript
// Cancel single order
const result = await client.trading.cancelOrder('BTC-USD', orderId)
console.log(`Cancelled ${result.cancelled_quantity} units`)

// Cancel all orders in a market
const result = await client.trading.cancelAll('BTC-USD')
console.log(`Cancelled ${result.total_cancelled} orders`)

Update Orders

typescript
// Update price
await client.trading.updateOrder('BTC-USD', orderId, {
  price: 51000_00000000n,
})

// Update both price and quantity
await client.trading.updateOrder('BTC-USD', orderId, {
  price: 51000_00000000n,
  quantity: 2000000n,
})

Get Orders

typescript
// Get open orders
const orders = await client.trading.getOrders('BTC-USD')

for (const order of orders) {
  console.log(`${order.order_id}: ${order.side} ${order.quantity} @ ${order.price}`)
}

// Get specific order
const order = await client.trading.getOrder('BTC-USD', orderId)

// Get order history
const history = await client.trading.getOrderHistory('BTC-USD', {
  limit: 50n,
  offset: 0n,
})

Positions

typescript
// Get position info
const position = await client.trading.getPosition('BTC-USD')
console.log('Position:', position.position)
console.log('Unrealized PnL:', position.unrealized_pnl)

// Get risk limits
const limits = await client.trading.getRiskLimits('BTC-USD')
console.log('Max position:', limits.max_position)

Simulate Orders

Preview execution before placing:

typescript
const simulation = await client.trading.simulateOrder({
  marketId: 'BTC-USD',
  side: 'buy',
  quantity: 1000000n,
})

console.log('Expected price:', simulation.expected_price)
console.log('Slippage:', simulation.estimated_slippage_bps, 'bps')
console.log('Fees:', simulation.estimated_fees)

Decimal Precision

All prices and quantities use 8 decimal places represented as bigint:

typescript
// $50,000.00 = 50000_00000000n
const price = 50000_00000000n

// 0.01 BTC = 1000000n (0.01 * 10^8)
const quantity = 1000000n