# Settlements

> Query settlement history for your account.

Source: https://docs.stxapp.io/sdks/csharp/settlements/

A settlement is the final payout when a position closes. Query them with `STXSettlementService`.

## Fetch settlement history

```csharp
using STX.Sdk.Enums;
using STX.Sdk.Services;

var settlements = serviceProvider.GetRequiredService<STXSettlementService>();

var history = await settlements.GetMySettlementsAsync(
    settlementTypes:         new[] { STXSettlementType.CLOSED_LONG, STXSettlementType.CLOSED_SHORT },
    rollingWindowIncrement:  STXRollingWindowIncrement.DAYS,
    rollingWindowValue:      7,
    page:                    1,
    pageSize:                50);

Console.WriteLine($"{history.Settlements.Count} of {history.TotalCount} settlements");

foreach (var s in history.Settlements)
{
    Console.WriteLine($"{s.Id}  {s.MarketId}  {s.Type}  P&L: {s.RealizedPnl}c");
}
```

`settlementTypes` is required. The rolling window bounds how far back to look, so the call above means "the last 7 days".

### Settlement types

| | |
|---|---|
| `CLOSED_LONG` | A long position closed by an opposing trade |
| `CLOSED_SHORT` | A short position closed by an opposing trade |
| `EXPIRED_LONG` | A long position held to market expiry |
| `EXPIRED_SHORT` | A short position held to market expiry |

The type describes **how** the position closed, not whether it made money. Use `RealizedPnl` for that.

### `STXSettlement`

| | |
|---|---|
| `Id`, `MarketId`, `AccountId` | Identifiers |
| `Type` | One of the four above |
| `RealizedPnl`, `GrossPnl`, `Fee` | Amounts in cents |
| `Quantity` | Contracts settled |
| `OpeningPrice`, `ClosingPrice` | Prices in cents |
| `OpeningTradeId`, `ClosingTradeId` | The trades that opened and closed the position |
| `InsertedAt`, `InsertedAtIso` | Unix time, and the same value as a `DateTime` |

## Real-time

Subscribe for settlements as they land instead of polling:

```csharp
var channel = serviceProvider.GetRequiredService<STXActiveSettlementsChannel>();

channel.SetOnReceiveAction(s =>
    _logger.LogInformation("Settled {MarketId}: {Type} {Pnl}c", s.MarketId, s.Type, s.RealizedPnl));

await channel.StartAsync();
```

The channel topic is keyed on the user id, so under API-key authentication call `STXIdentityService.GetMeAsync()` once at startup before connecting. See [WebSockets](/sdks/csharp/websockets/).

`STXActiveSettlementsChannelWrapper` wraps the same channel with a bounded queue if you would rather drain settlements yourself than handle a callback.

## See also

- [Trading](/sdks/csharp/trading/): the fills that lead up to a settlement
- [WebSockets](/sdks/csharp/websockets/): live settlement stream
- [Account reference](/sdks/csharp/reference/account/): full signatures
