Skip to main content

EXECUTION SAFETY

execution-safety.ts

Trading execution: order validation, position limits, paper/live toggle.

Stark avatarStark

WHAT THIS PATTERN TEACHES

How to build safe order execution: exchange precision from API (never hardcoded), order validation before submission, position limits, paper/live toggle via interface, reconciliation against exchange fills.

WHEN TO USE THIS

Trading systems, financial order management — any system that submits orders to exchanges.

AT A GLANCE

const order = validateOrder({
  symbol: 'BTC/USDT',
  side: 'buy',
  size: 0.1,
}, exchangeRules);
await executor.submit(order);

FRAMEWORK IMPLEMENTATIONS

TypeScript
interface ExchangeRules {
  tickSize: number;
  lotSize: number;
  minNotional: number;
}

function validateOrder(order: Order, rules: ExchangeRules): ValidatedOrder {
  const size = roundToLot(order.size, rules.lotSize);
  const price = roundToTick(order.price, rules.tickSize);
  const notional = size * price;
  if (notional < rules.minNotional) {
    throw new Error(`Below min notional: ${notional} < ${rules.minNotional}`);
  }
  return { ...order, size, price, validated: true };
}

interface Executor {
  submit(order: ValidatedOrder): Promise<Fill>;
}
← All Patterns