Skip to content

Place a trade ​

ts
export async function buy(symbolId: number, volume: string, seenAsk: string, confirmRequote: (bid: string, ask: string) => Promise<boolean>) {
  const order = {
    request_id: crypto.randomUUID(),
    action: "open" as const,
    symbol_id: symbolId,
    side: "buy" as const,
    volume,
    price: seenAsk,   // the price the trader saw; the fill stays within deviation_pts
    deviation_pts: 20,
  };

  let result;
  try {
    result = await trader.trades.create(order);
  } catch (e) {
    // Outcome unknown: send the same request again. Same request_id never trades twice.
    if (e instanceof NoviqaError && e.code === "trade.timeout") result = await trader.trades.create(order);
    else throw e;
  }

  if (result.status === "requote") {
    const accept = await confirmRequote(result.bid, result.ask);
    result = await trader.trades.requote(order.request_id, { accept });
  }
  if (result.status === "rejected") throw new Error(`refused: ${result.code}`);
  return result; // done, placed, partial or queued; the socket's trade event follows
}
statusMeaning
donefilled
placedpending order accepted
partialsome volume filled
queuedwaiting for a dealer; the answer follows on the socket
requotethe dealer offers bid/ask: accept or decline before the deadline
rejectedcode says why