# OrdersService

> Placing, reading and cancelling orders.

Source: https://docs.stxapp.io/sdks/go/reference/orders/

## OrdersService

```go
type OrdersService struct {
	// contains filtered or unexported fields
}
```

OrdersService places, lists and cancels orders. Use it through [Client.Orders](/sdks/go/reference/client/#client).

### All

```go
func (s *OrdersService) All(ctx context.Context, q *OrdersQuery) iter.Seq2[Order, error]
```

All returns every order matching q, following the cursor.

### Cancel

```go
func (s *OrdersService) Cancel(ctx context.Context, orderID string) (*Cancellation, error)
```

Cancel requests a cancel (`DELETE /api/v1/orders/{order_id}`). A fill already in flight can still land.

### CancelAll

```go
func (s *OrdersService) CancelAll(ctx context.Context) ([]Cancellation, error)
```

CancelAll cancels every order resting on the account (`DELETE /api/v1/orders/all`).

### CancelBatch

```go
func (s *OrdersService) CancelBatch(ctx context.Context, orderIDs []string) ([]Cancellation, error)
```

CancelBatch cancels the named orders (`DELETE /api/v1/orders/batched`).

### Get

```go
func (s *OrdersService) Get(ctx context.Context, orderID string) (*Order, error)
```

Get returns one of your orders (`GET /api/v1/orders/{order_id}`).

### List

```go
func (s *OrdersService) List(ctx context.Context, q *OrdersQuery) (*Page[Order], error)
```

List returns one page of your orders, newest first (`GET /api/v1/orders`).

### Place

```go
func (s *OrdersService) Place(ctx context.Context, order OrderRequest) (*Order, error)
```

Place places one order (`POST /api/v1/orders`). The exchange validates it: a bad price step or a fractional quantity returns an error matching [ErrBadRequest](/sdks/go/reference/errors/#constants-and-variables) or [ErrRejected](/sdks/go/reference/errors/#constants-and-variables) with the API's message.

A POST is never retried after a 5xx or a dropped connection; set ClientOrderID and look the order up with [OrdersService.List](/sdks/go/reference/orders/#list) if that happens.

With FillMode [PostOnly](/sdks/go/reference/types/#constants-and-variables) or [FillOrKill](/sdks/go/reference/types/#constants-and-variables), Place sends the order and the exchange decides: where the mode is not enabled it rejects the order and Place returns an error matching [ErrNotAvailable](/sdks/go/reference/errors/#constants-and-variables). A market that does not take the mode, such as a combination market, returns an error matching [ErrFillModeNotAllowed](/sdks/go/reference/errors/#constants-and-variables); a post-only order that would have matched returns a \*[PostOnlyWouldMatchError](/sdks/go/reference/errors/#postonlywouldmatcherror).

**Example**

Place a limit order, then cancel it.

```go
ctx := context.Background()
client, err := stx.New()
if err != nil {
	log.Fatal(err)
}
order, err := client.Orders.Place(ctx, stx.OrderRequest{
	MarketID:      "42b861f8-8340-4046-ae06-83a0cec93456",
	Action:        stx.Buy,
	OrderType:     stx.Limit,
	Price:         stx.MustDecimal("0.01"),
	Quantity:      stx.MustDecimal("1"),
	ClientOrderID: "my-order-001",
})
if errors.Is(err, stx.ErrRejected) {
	log.Fatalf("refused by the exchange: %v", err)
} else if err != nil {
	log.Fatal(err)
}
fmt.Println("Placed", order.ID, order.Status)
cancel, err := client.Orders.Cancel(ctx, order.ID)
if err != nil {
	log.Fatal(err)
}
fmt.Println("Cancelled:", cancel.Status)
```

### PlaceBatch

```go
func (s *OrdersService) PlaceBatch(ctx context.Context, orders []OrderRequest) ([]BatchOrderResult, error)
```

PlaceBatch places several orders in one call (`POST /api/v1/orders/batched`). It returns one result per order, in order; a rejected order carries Errors and does not stop the others.

When the exchange rejects the batch because a FillMode ([PostOnly](/sdks/go/reference/types/#constants-and-variables) or [FillOrKill](/sdks/go/reference/types/#constants-and-variables)) is not enabled there, PlaceBatch returns an error matching [ErrNotAvailable](/sdks/go/reference/errors/#constants-and-variables). In a batch a post-only order that would have matched comes back as its result: a cancelled Order (CancellationReason "auto\_matching") or Errors.
