SDKs
JavaScript/Typescript
User Methods
Trades
Order

Orders

Orders are orders that are placed at a specific price. They are used to buy or sell a stock at a specific price or better. For example, if you want to buy a stock at $10.00 or better, you can place a limit order at $10.00.

If the stock is trading at $10.00 or lower, your order will be filled. If the stock is trading higher than $10.00, your order will not be filled.

Fetching Orders

To list orders the list() method under the orders namespace in the SDK should be used.

import { PaginatedResponse, Order } from '@tradrapi/trading-sdk';
 
const orders: PaginatedResponse<Order> = await tradrApi.orders.list({ accountId: 1 });

The list() method accepts a parameter of type ListOrdersDto which has the following interface:

interface ListOrdersDto {
  /**
   * The current page being returned.
   * @min 1
   * @default 1
   */
  page?: number;
  /**
   * Number of records per page.
   * @min 1
   * @default 10
   */
  limit?: number;
  /**
   * The sort direction of the records being returned
   * @default "DESC"
   */
  sortDir?: SortDir;
  /**
   * Filter the results by account id
   * @min 1
   */
  accountId?: number;
  /** Filter the results by order type */
  type?: OrderType;
  /** Filter the results by symbol */
  symbol?: string;
  /** Fetch only orders created after, included ISO 8610 */
  from?: string;
  /** Fetch only orders created before, included ISO 8610 */
  to?: string;
  /** Sort the results by the given property */
  sortBy?: OrderSortby;
}

The response of the list() method is a PaginatedResponse of type Order.

Order Model

interface Order {
  /** The TradrAPI ID of the entity */
  id: number;
  /**
   * The date the entity was created
   * @format date-time
   */
  createdAt: string;
  /**
   * The date the entity was last updated
   * @format date-time
   */
  updatedAt: string;
  /** The ID of the order on the exchange */
  exchangeOperationId: string;
  /** The symbol name the order was placed on */
  symbol: string;
  /** The price at which the order will be opened */
  openPrice: number;
  /** The take profit amount to set on the order */
  takeProfit: number;
  /** The stop loss amount to set on the order */
  stopLoss: number;
  /** The number of lots to open the order with */
  lots: number;
  /** The contract size of the instrument the order is for */
  contractSize: number;
  /** The swap amount of the order */
  swap?: number;
  /** The swap amount of the order in USD */
  swapUSD?: number;
  /** The comment present on the order */
  comment: string;
  /** The type of order to open */
  type: OrderType;
  /** The status of the order */
  status?: OrderStatus;
  /** The IP address of the user who placed the order */
  ip?: string;
  /** The country of the user who placed the order */
  country?: string;
  /**
   * The date and time the order will expire
   * @format date-time
   */
  expiresAt?: string;
  /** The ID of the brand the order belongs to */
  brandId: number;
  /** The ID of the account the order belongs to */
  accountId: number;
  /** The ID of the group the order belongs to */
  groupId: number;
  /** The ID of the exchange the order belongs to */
  exchangeId: number;
  /** The ID of the exchange rate which is valid for the order */
  exchangeRateId?: number;
}

Opening An Order

To open a new order the open() method should be used

import { Order, OrderType } from '@tradrapi/trading-sdk';
 
const order: Order = await tradrApi.orders.open({
  accountId: 1,
  lots: 0.01,
  operation: OrderType.BUY_LIMIT,
  symbol: 'EURBTC',
  triggerPrice: 450,
});

This method accepts a parameter of type OpenOrderDto which has the following interface:

interface OpenOrderDto {
  /** The type of order to open */
  type: OrderType;
  /**
   * The price at which to open the order
   * @min 1
   */
  triggerPrice: number;
  /** The symbol to open the order for */
  symbol: string;
  /**
   * The number of lots to open
   * @min 1
   */
  lots: number;
  /**
   * A take profit amount to set on the position once it is opened
   * @min 1
   */
  takeProfit?: number;
  /**
   * A stop loss amount to set on the position once it is opened
   * @min 1
   */
  stopLoss?: number;
  /** A comment for the order */
  comment?: string;
  /** The date at which the order expires */
  expiresAt?: string;
  /**
   * The ID of the account to open the order for
   * @min 1
   */
  accountId: number;
}

Cancelling An Order

To cancel an existing order the cancel() method should be used:

const response: boolean = await tradrApi.orders.cancel(1);

Updating An Existing Order

The update an order the update() method should be used:

import { Order } from '@tradrapi/trading-sdk';
 
const order: Order = await tradrApi.orders.update(1, { stopLoss: 250.0 });

The update method accepts a single parameter of type UpdateOrderDto which has the following interface:

interface UpdateOrderDto {
  /**
   * The price at which to trigger the order
   * @min 1
   */
  triggerPrice?: number;
  /**
   * A take profit amount to set on the position once it is opened
   * @min 1
   */
  takeProfit?: number;
  /**
   * A stop loss amount to set on the position once it is opened
   * @min 1
   */
  stopLoss?: number;
  /** The date at which the order expires */
  expiresAt?: string;
}