> For AI agents: the complete documentation index is available at [llms.txt](https://docs.snaptrade.com/llms.txt). Markdown versions of documentation pages are available by appending .md to the URL path.

# Place complex order

POST https://api.snaptrade.com/accounts/{accountId}/trading/complex

Places a complex conditional order (OCO, OTO, or OTOCO).
Only supported on certain brokerages.
Please refer to the [brokerage trading support page](https://support.snaptrade.com/brokerages) for details on which brokerages support complex orders and which types they support.

- **OCO** (One Cancels the Other): Two peer orders; when one fills the other is cancelled.
- **OTO** (One Triggers the Other): A trigger order that, when filled, activates a conditional order.
- **OTOCO** (One Triggers a One Cancels the Other): A trigger order that, when filled, activates an OCO pair of two peer orders.


Reference: https://docs.snaptrade.com/reference/Trading/Trading_placeComplexOrder

## Code Examples

### TypeScript

```typescript

import { Snaptrade } from "snaptrade-typescript-sdk";

const snaptrade = new Snaptrade({
  clientId: "PARTNER_CLIENT_ID",
  consumerKey: "CONSUMER_KEY",
});

const response =
  await snaptrade.trading.placeComplexOrder({
    accountId:
      "917c8734-8470-4a3e-a18f-57c3f2ee6631",
    userId: "snaptrade-user-123",
    userSecret:
      "adf2aa34-8219-40f7-a6b3-60156985cc61",
    type: "OTO",
    orders: [
      {
        order_role: "TRIGGER",
        action: "ACTION",
        instrument: {
          symbol: "AAPL",
          type: "TYPE",
        },
        order_type: "Market",
        units: 10.5,
        time_in_force: "Day",
        price: 31.33,
        stop: 29.5,
      },
    ],
  });
console.log(response.data);

```

### Python

```python

from pprint import pprint
from snaptrade_client import SnapTrade

snaptrade = SnapTrade(
    client_id="PARTNER_CLIENT_ID",
    consumer_key="CONSUMER_KEY"
)

response = snaptrade.trading.place_complex_order(
    account_id="917c8734-8470-4a3e-a18f-57c3f2ee6631",
    user_id="snaptrade-user-123",
    user_secret="adf2aa34-8219-40f7-a6b3-60156985cc61",
    type="OTO",
    orders=[
        {
            "order_role": "TRIGGER",
            "action": "ACTION",
            "instrument": {
                "symbol": "AAPL",
                "type": "TYPE"
            },
            "order_type": "Market",
            "units": 10.5,
            "time_in_force": "Day",
            "price": 31.33,
            "stop": 29.5
        }
    ]
)
pprint(response.body)

```

## OpenAPI Specification

```yaml

openapi: 3.0.0
info:
  description: Connect brokerage accounts to your app for live positions and trading
  version: 1.0.0
  title: SnapTrade
  termsOfService: N/A
  contact:
    email: api@snaptrade.com
  x-konfig-ignore:
    potential-incorrect-type: true
  x-readme:
    explorer-enabled: false
paths:
  /accounts/{accountId}/trading/complex:
    post:
      tags:
        - Trading
      summary: Place complex order
      description: >
        Places a complex conditional order (OCO, OTO, or OTOCO).

        Only supported on certain brokerages.

        Please refer to the [brokerage trading support
        page](https://support.snaptrade.com/brokerages) for details on which
        brokerages support complex orders and which types they support.


        - **OCO** (One Cancels the Other): Two peer orders; when one fills the
        other is cancelled.

        - **OTO** (One Triggers the Other): A trigger order that, when filled,
        activates a conditional order.

        - **OTOCO** (One Triggers a One Cancels the Other): A trigger order
        that, when filled, activates an OCO pair of two peer orders.
      operationId: Trading_placeComplexOrder
      parameters:
        - in: path
          name: accountId
          required: true
          description: The ID of the account to execute the trade on.
          schema:
            description: >-
              Unique identifier for the connected brokerage account. This is the
              UUID used to reference the account in SnapTrade.
            type: string
            format: uuid
            example: 917c8734-8470-4a3e-a18f-57c3f2ee6631
        - in: query
          required: true
          name: userId
          schema:
            description: >-
              SnapTrade User ID. This is chosen by the API partner and can be
              any string that is a) unique to the user, and b) immutable for the
              user. It is recommended to NOT use email addresses for this
              property because they are usually not immutable.
            type: string
            example: snaptrade-user-123
        - in: query
          required: true
          name: userSecret
          schema:
            description: >-
              SnapTrade User Secret. This is a randomly generated string and
              should be stored securely. If compromised, please rotate it via
              the [rotate user secret
              endpoint](/reference/Authentication/Authentication_resetSnapTradeUserSecret).
            type: string
            example: adf2aa34-8219-40f7-a6b3-60156985cc61
      requestBody:
        required: true
        content:
          application/json:
            schema:
              description: >-
                Request body for placing a complex conditional order (OCO, OTO,
                or OTOCO).
              type: object
              required:
                - type
                - orders
              properties:
                type:
                  description: >
                    The complex order type.

                    - `OCO`: One Cancels the Other — two peer orders.

                    - `OTO`: One Triggers the Other — a trigger order and a
                    conditional order.

                    - `OTOCO`: One Triggers a One Cancels the Other — a trigger
                    order and two peer orders.
                  type: string
                  enum:
                    - OCO
                    - OTO
                    - OTOCO
                  example: OTO
                orders:
                  description: >
                    The orders that make up the complex order. Required counts
                    and roles per type:

                    - `OCO`: exactly 2 orders, both `PEER`

                    - `OTO`: exactly 2 orders, one `TRIGGER` and one
                    `CONDITIONAL`

                    - `OTOCO`: exactly 3 orders, one `TRIGGER` and two `PEER`
                  type: array
                  items:
                    description: A single leg within a complex order.
                    type: object
                    required:
                      - order_role
                      - action
                      - instrument
                      - order_type
                      - units
                      - time_in_force
                    properties:
                      order_role:
                        description: The role of this leg within the complex order.
                        type: string
                        enum:
                          - TRIGGER
                          - CONDITIONAL
                          - PEER
                        example: TRIGGER
                      action:
                        description: >-
                          The action describes the intent or side of a trade.
                          This is either `BUY` or `SELL`.
                        type: string
                        enum:
                          - BUY
                          - SELL
                      instrument:
                        type: object
                        required:
                          - symbol
                          - type
                        properties:
                          symbol:
                            description: >-
                              The instrument's trading ticker symbol. This
                              currently supports stock symbols and Options
                              symbols in the 21 character OCC format. For
                              example `AAPL  251114C00240000` represents a call
                              option on AAPL expiring on 2025-11-14 with a
                              strike price of $240. For more information on the
                              OCC format, see
                              [here](https://en.wikipedia.org/wiki/Option_symbol#OCC_format)
                            type: string
                            example: AAPL
                          type:
                            description: The instrument's type
                            type: string
                            enum:
                              - EQUITY
                              - OPTION
                              - CRYPTOCURRENCY
                              - CRYPTOCURRENCY_PAIR
                      order_type:
                        description: >
                          The type of order to place.


                          - For `Limit` and `StopLimit` orders, the `price`
                          field is required.

                          - For `Stop` and `StopLimit` orders, the `stop` field
                          is required.
                        example: Market
                        type: string
                        enum:
                          - Limit
                          - Market
                          - StopLimit
                          - Stop
                      units:
                        description: >-
                          Number of shares for the order. This can be a decimal
                          for fractional orders. Must be `null` if
                          `notional_value` is provided.
                        type: number
                        example: 10.5
                      time_in_force:
                        description: >
                          The Time in Force type for the order. This field
                          indicates how long the order will remain active before
                          it is executed or expires. Here are the supported
                          values:
                            - `Day` - Day. The order is valid only for the trading day on which it is placed.
                            - `GTC` - Good Til Canceled. The order is valid until it is executed or canceled.
                            - `FOK` - Fill Or Kill. The order must be executed in its entirety immediately or be canceled completely.
                            - `IOC` - Immediate Or Cancel. The order must be executed immediately. Any portion of the order that cannot be filled immediately will be canceled.
                        type: string
                        enum:
                          - FOK
                          - Day
                          - GTC
                          - IOC
                        example: Day
                      price:
                        description: >-
                          The limit price. Required when `order_type` is `Limit`
                          or `StopLimit`.
                        type: number
                        example: 31.33
                        nullable: true
                      stop:
                        description: >-
                          The stop trigger price. Required when `order_type` is
                          `Stop` or `StopLimit`.
                        type: number
                        example: 29.5
                        nullable: true
                client_order_id:
                  nullable: true
                  allOf:
                    - description: >
                        Optional caller-supplied identifier passed through to
                        the brokerage for idempotent

                        order placement. Must be a canonical 36-character UUID.

                        Idempotency enforcement is brokerage-specific -
                        SnapTrade forwards this value to

                        the broker but does not enforce uniqueness server-side.
                        Refer to per-brokerage

                        documentation for behavior on duplicate submission.
                      nullable: true
                      type: string
                      format: uuid
                      example: 550e8400-e29b-41d4-a716-446655440000
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                description: >
                  Response returned after successfully placing a complex order.
                  AccountOrderRecord rows

                  for the legs are not created synchronously — they're hydrated
                  by the next brokerage sync,

                  and can be queried later using the returned
                  `brokerage_group_order_id`.
                type: object
                properties:
                  type:
                    description: The complex order type that was placed.
                    type: string
                    enum:
                      - OCO
                      - OTO
                      - OTOCO
                    example: OTO
                  brokerage_group_order_id:
                    description: >
                      The brokerage-assigned identifier that links all legs of
                      this complex order together.

                      Each leg will eventually appear as a separate
                      AccountOrderRecord sharing this value.

                      May be null if the brokerage does not return a group
                      identifier.
                    type: string
                    nullable: true
                    example: '1234567890'
        '400':
          description: Trade could not be placed
          content:
            application/json:
              schema:
                description: Example for failed request response
                type: object
                properties:
                  default_detail:
                    example: Unable to verify data sent
                  default_code:
                    example: 1076
        '403':
          description: User does not have permissions to place trades
          content:
            application/json:
              schema:
                description: Example for failed request response
                type: object
                properties:
                  default_detail:
                    example: User does not have permission to access this resource
                  default_code:
                    example: 1066
        '500':
          description: Unexpected Error

```