# Markets

> Markets, events, filters, and cursor pagination.

Source: https://docs.stxapp.io/sdks/go/markets/

## Markets

```go
package main

import (
	"context"
	"fmt"
	"log"

	stx "github.com/stxapp/stx-go"
)

func main() {
	ctx := context.Background()
	client, err := stx.New()
	if err != nil {
		log.Fatal(err)
	}

	page, err := client.Markets.List(ctx, &stx.MarketsQuery{Status: []string{"open"}, Limit: 5})
	if err != nil {
		log.Fatal(err)
	}
	for _, m := range page.Items {
		bid, offer := "-", "-"
		if len(m.Bids) > 0 {
			bid = m.Bids[0].Price.String()
		}
		if len(m.Offers) > 0 {
			offer = m.Offers[len(m.Offers)-1].Price.String()
		}
		fmt.Println(m.Symbol, "last", m.LastTradedPrice, "bid", bid, "offer", offer)
	}
	fmt.Println("more pages:", page.HasMore())
}
```

Sample output (two of the five markets):

```text
STXNHL-26OCT101600EDMSJ-GAMEEDM last 0.5800 bid 0.5800 offer 0.6100
STXNHL-26OCT101600EDMSJ-SOGSJCMCDAVID209555-3.5 last 0.5200 bid 0.4900 offer 0.5400
more pages: true
```

`stx.MarketsQuery` fields:

| Field | Meaning |
|---|---|
| `MarketIDs`, `EventIDs` | One id or several |
| `Status` | One or several, lowercase, e.g. `"open"`; see [Market status](/concepts/market-status/) |
| `Trading` | `stx.Bool(true)` for only the markets accepting orders now, `stx.Bool(false)` for only those that are not; nil for both |
| `Sports`, `Competitions` | One or several |
| `SortBy`, `SortDirection` | e.g. `SortBy: "event_start"`, `SortDirection: "desc"` |
| `Limit`, `Cursor` | Page size and the cursor from the previous page |

Filters combine with AND; a list filter matches any of its values. A nil query lists every market. `client.Markets.Get(ctx, marketID)` fetches one market and returns an error matching `stx.ErrNotFound` when there is none.

## Pagination

A list method returns one `*stx.Page[T]`: `page.Items`, `page.Cursor` (empty on the last page) and `page.HasMore()`. To walk every page, use the `All` twin, which follows the cursor for you and stops when you break out of the loop:

```go
var market stx.Market
for m, err := range client.Markets.All(ctx, &stx.MarketsQuery{
	Status:        []string{"open"},
	Trading:       stx.Bool(true),
	SortBy:        "event_start",
	SortDirection: "desc",
}) {
	if err != nil {
		log.Fatal(err)
	}
	if m.EventStatus == "scheduled" {
		market = m
		break
	}
}
fmt.Println(market.MarketID, market.Symbol, market.MaxPrice)
// e00651a3-10cb-4bc1-9e1f-c5ea1796c83b STXNHL-26OCT101600EDMSJ-GAMEEDM 1.0000
```

`Status: []string{"open"}` with `Trading: stx.Bool(true)` returns only the markets that are open and accept orders now. A `pre_open` market also accepts limit orders, but they wait at `accepted` and do not reach the book until it opens. Read the price ceiling from `MaxPrice` rather than assuming one.

Or page by hand:

```go
q := &stx.MarketsQuery{Status: []string{"open"}, Limit: 100}
for {
	page, err := client.Markets.List(ctx, q)
	if err != nil {
		log.Fatal(err)
	}
	for _, m := range page.Items {
		fmt.Println(m.Symbol)
	}
	if !page.HasMore() {
		break
	}
	q.Cursor = page.Cursor
}
```

## Events

```go
events, err := client.Events.List(ctx, &stx.EventsQuery{Limit: 10})
if err != nil {
	log.Fatal(err)
}
for _, e := range events.Items {
	fmt.Println(e.EventID, e.Title, e.Status)
}
// 24636b3d-f566-4dd3-8c8b-c1a6c2747789 Edmonton Oilers at San Jose Sharks scheduled
// a80f6a9d-fcc6-4a02-82d7-82a55e90db1c Seattle Kraken at Detroit Red Wings completed

for e, err := range client.Events.All(ctx, &stx.EventsQuery{Promoted: stx.Bool(true)}) {
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(e.Title)
}
```

`stx.EventsQuery` filters on `EventIDs`, `Sports`, `Competitions`, `EventTypes`, `Title`, `Status` and `Promoted`, and sorts with `SortBy` and `SortDirection`.

## Live prices

`Bids` and `Offers` are a snapshot of the top of the book. To follow prices as they move, stream them; see [WebSockets](/sdks/go/websockets/). An app can also create markets on scheduled events; see [Create markets](/sdks/go/create-markets/).
