T-Invest Node SDK Node.js

Track order state

orderStateStream reports changes to all orders on the selected account. To follow one order, filter by orderRequestId, the UUID supplied in orderId when placing it. The first orderState.orderId may still be an internal order number.

The snippet uses an existing sdk connected to the sandbox and a signal to stop reading. The streaming example shows how to create them, handle cancellation, and close the SDK. Supply an existing sandbox account ID and the saved order key.

order-state-stream.ts
import { ResultSubscriptionStatus } from '@woodger/t-invest-node-sdk';

const accountId = 'YOUR_SANDBOX_ACCOUNT_ID';
const orderRequestId = 'YOUR_ORDER_REQUEST_ID';
const responses = sdk.ordersStream.orderStateStream({
  accounts: [accountId]
}, { signal });

for await (const response of responses) {
  if (response.subscription) {
    if (response.subscription.status !== ResultSubscriptionStatus.RESULT_SUBSCRIPTION_STATUS_OK) {
      throw new Error('Order-state subscription was rejected');
    }

    console.log('Подписка на состояния заявок подтверждена');
  }

  const state = response.orderState;

  if (state?.orderRequestId === orderRequestId) {
    console.log(state);
  }
}

For a new order, open the stream and wait for subscription confirmation before placing it. The sandbox order lifecycle explains how to set up an account and order. If you plan to keep using the account, close it at the end of your scenario.

Check the current state of an existing order, including after a disconnect, using getOrderState or getSandboxOrderState. The stream skips events missed during disconnection.

After a partial fill, a canceled order may still have the PARTIALLYFILL stream status. Reading continues until signal aborts or the stream ends. See the provider documentation for identifier and status behavior.