# Candles (OHLC) Stream

The `candle` channel streams the Open, High, Low and Close (OHLC) data for the specific interval period. The updates are generated on trade events.

## Update Speed

* Pushed on each block that is at least 2 seconds since the last push

## Subscribe Request

* Subscribe Schema

See [`WsCandleSubscriptionParams`](https://sodex.com/documentation/rest-v1/schema#wscandlesubscriptionparams) in [Schema](https://sodex.com/documentation/api/rest-v1/schema).

```typescript
interface SubscribeRequest {
  op: "subscribe";
  id: number | null; // Optional client originated request identifier sent as acknowledgment in the response.
  params: WsCandleSubscriptionParams;
}
```

* Subscribe Ack Schema

See [`WsCandleSubscriptionResult`](https://sodex.com/documentation/rest-v1/schema#wscandlesubscriptionresult) in [Schema](https://sodex.com/documentation/api/rest-v1/schema).

```typescript
interface SubscriptionResult {
  op: "subscribe";
  id: number | null; // Optional client originated request identifier sent as acknowledgment in the response.
  result: WsCandleSubscriptionResult | null;
  success: boolean; // Indicates if the request was successfully processed by the engine.
  connID: string;
  error: string | null; // Condition: If success is false. Error message.
  time_in: number; // The timestamp when the subscription was received on the wire, just prior to parsing data, in milliseconds.
  time_out: number; // The timestamp when the acknowledgement was sent on the wire, just prior to transmitting data, in milliseconds.
}
```

* Example: Subscribe

```json
{
  "op": "subscribe",
  "params": {
    "channel": "candle",
    "symbol": "BTC-USD",
    "interval": "1m"
  }
}
```

* Example: Subscribe Ack

```json
{
  "op": "subscribe",
  "result": {
    "channel": "candle",
    "symbol": "BTC-USD",
    "interval": "1m"
  },
  "success": true,
  "connID": "0xb0922dfe2b14dadb1195ea4db2b45508",
  "time_in": 1672515782130,
  "time_out": 1672515782136
}
```

## Unsubscribe Request

* Unsubscribe Schema

See [`WsCandleSubscriptionParams`](https://sodex.com/documentation/rest-v1/schema#wscandlesubscriptionparams) in [Schema](https://sodex.com/documentation/api/rest-v1/schema).

```typescript
interface UnsubscribeRequest {
  op: "unsubscribe";
  id: number | null; // Optional client originated request identifier sent as acknowledgment in the response.
  params: WsCandleSubscriptionParams;
}
```

* Unsubscribe Ack Schema

See [`WsCandleSubscriptionResult`](https://sodex.com/documentation/rest-v1/schema#wscandlesubscriptionresult) in [Schema](https://sodex.com/documentation/api/rest-v1/schema).

```typescript
interface UnsubscriptionResult {
  op: "unsubscribe";
  id: number | null; // Optional client originated request identifier sent as acknowledgment in the response.
  result: WsCandleSubscriptionResult | null;
  success: boolean; // Indicates if the request was successfully processed by the engine.
  connID: string;
  error: string | null; // Condition: If success is false. Error message.
  time_in: number; // The timestamp when the subscription was received on the wire, just prior to parsing data, in milliseconds.
  time_out: number; // The timestamp when the acknowledgement was sent on the wire, just prior to transmitting data, in milliseconds.
}
```

* Example: Unsubscribe

```json
{
  "op": "unsubscribe",
  "params": {
    "channel": "candle",
    "symbol": "BTC-USD",
    "interval": "1m"
  }
}
```

* Example: Unsubscribe Ack

```json
{
  "op": "unsubscribe",
  "result": {
    "channel": "candle",
    "symbol": "BTC-USD",
    "interval": "1m"
  },
  "success": true,
  "connID": "0xb0922dfe2b14dadb1195ea4db2b45508",
  "time_in": 1672515782130,
  "time_out": 1672515782136
}
```

## Update Response

* Update Schema

See [`Candle`](https://sodex.com/documentation/rest-v1/schema#candle) in [Schema](https://sodex.com/documentation/api/rest-v1/schema).

```typescript
interface CandleResponse {
  channel: "candle";
  type: "update";
  data: Candle;
}
```

* Example: Update

```json
{
  "channel": "candle",
  "type": "update",
  "data": {
    "t": 1767972900000,
    "T": 1767972938724,
    "s": "BTC-USD",
    "i": "1m",
    "o": "91869",
    "h": "91982",
    "l": "91869",
    "c": "91976",
    "v": "4.12298",
    "q": "379148.6798",
    "n": 0,
    "x": false
  }
}
```
