> ## Documentation Index
> Fetch the complete documentation index at: https://takeprofit.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Feed protocol and limits

> The wire contract for feed servers: WebSocket and SSE record formats, heartbeats, timestamp rules, the three history requests, replay_done and platform limits.

This page is for the author of the feed server. The Indie side — declaring the source and what the script sees — is
in [Live data from a feed](https://takeprofit.com/docs/indie/External-data/Live-data-from-a-feed.md).

A feed is a service you host at a public address. The platform connects to it, reads records and delivers them to
every subscribed indicator. One address can serve many indicators of many users: the platform shares connections
and fans records out on its side, so your server sees connections, never the audience. Do not assume exactly one
connection: platform replicas and redeploys can hold several at a time.

There are two capability levels:

* **Level 1, required:** accept a connection and send records.
* **Level 2, optional:** answer history requests. They fill the platform's record buffer on a cold start, close
  the hole a reconnect leaves, and are how a run launched from a date, or over a date window, gets that range.

## Transports

The URL scheme in the source descriptor selects the transport:

* **WebSocket** (`wss://`). The connection itself is the subscription. The platform sends no greeting; the only
  frame it can ever send is a replay request (level 2).
* **SSE** (`https://`). A GET request with `Accept: text/event-stream`; the response body is the stream.

Requirements for both: a secure scheme (`ws://` and `http://` are rejected), a valid certificate, and a publicly
reachable address — private and internal networks are refused. Credentials cannot be embedded in the URL
(`wss://user:pass@...` is rejected); to restrict access, use a signed token in the query string. On ordinary
connections the URL is used unchanged. A connection that carries a replay request (level 2) re-assembles the
query to add the `replay_*` parameters, which can reorder your parameters and normalize their encoding — so sign
parameter values, not the raw query string, and do not use query parameter names starting with `replay_` for your
own purposes.

The platform reads your HTTP answer on the SSE response and on the WebSocket handshake. A 404 or 410 means the
stream is gone for good: subscribed indicators stop with `external_feed_gone`. A 401 or 403 means unauthorized:
they stop with `external_feed_auth_failed`. A 429 makes the platform back off and retry. Any other failure is
retried with increasing delays and never reaches the indicator as an error.

## Records

Over WebSocket, every frame is a JSON object with a `type` field. Frames without `type`, and types other than the
ones below, are ignored.

```json One record (WebSocket) theme={null}
{"type": "record", "time": 1723456789, "data": {"price": 101.5}}
```

* `time` is the record's timestamp: Unix seconds, UTC. A fractional part is allowed.
* `data` holds the record fields as one flat object. Names match the dataclass schema declared in the script, and
  a field of a nested dataclass uses a dotted name, exactly like a CSV column:
  `"data": {"signal": 0.42, "risk.beta": 1.1}`. A record that does not match the schema — an unknown field, a
  missing non-optional field, a nested object, a wrong value type — is dropped at intake, and the stream
  continues. An optional field may be absent or `null`.
* An optional `id` field is read only for the platform's diagnostics. When present, it must be a JSON string;
  any other type makes the whole record undecodable, and it is dropped.

Two other frame types exist:

* `{"type": "heartbeat"}` resets the silence clock and does nothing else. Send heartbeats when the gaps between
  records can exceed the `stale_after` your consumers declare.
* `{"type": "error", "code": "...", "message": "..."}` refuses the stream. On a live connection the platform
  stops serving this feed and subscribed indicators fail with `external_feed_gone`; use it when you want the
  platform to stop trying — for example, for a stream that no longer exists. On a short window connection
  (level 2) it only ends that window request.

Over SSE, each event carries the same JSON in `data:`, without the `type` wrapper:

```txt One record (SSE) theme={null}
data: {"time": 1723456789, "data": {"price": 101.5}}

```

* The blank line after each event is required: it terminates the event.
* Comment lines (starting with `:`) count as heartbeats.
* The `id:` and `retry:` fields are ignored. The `event:` field is ignored too, with one exception:
  `event: replay_done` (level 2, below).

## Two rules about timestamps

The platform orders, resumes and deduplicates the stream by the record timestamp, so two rules are hard
requirements:

1. **Time does not decrease.** Each record's `time` is equal to or later than the previous one. A record older
   than the previous one is dropped.
2. **An equal timestamp is a revision.** A record with the same `time` as the previous one replaces it. It is
   treated as a new version of the same logical record, not as a separate event.

Both rules cover everything one connection sends: live records, and replayed records together with the live
records that follow them.

## Answering history requests (level 2)

The platform asks for recent past in exactly three ways. A feed that ignores them is a valid level-1 feed:
everything works, indicators just get less history. The one hard compatibility rule: never close the connection
because of an unfamiliar frame or query parameter.

| Request | What to send | Then |
| - | - | - |
| **Live from a point** — `from_time` | the stored records with `time` at or after `from_time`, oldest first, then the done frame | keep the connection open and continue with live records |
| **Last records before a moment** — `count` + `to_time` | the last `count` records with `time` before `to_time`, oldest first, then the done frame | the connection closes; no live records follow |
| **A time window** — `from_time` + `to_time` | the records with `from_time` ≤ `time` \< `to_time`, oldest first, then the done frame | the connection closes; no live records follow |

A connection that carries none of them is a plain live subscription: send live records right away.

Timestamps are integer Unix seconds, UTC. `from_time` is inclusive, `to_time` is exclusive. After the done frame
of a request with `to_time`, either side may close the connection.

Over WebSocket, a request is the one frame the platform sends right after connecting — without a request it sends
nothing:

```json Requests (WebSocket) theme={null}
{"replay": {"from_time": 1723450000}}
{"replay": {"count": 100, "to_time": 1723460000}}
{"replay": {"from_time": 1723450000, "to_time": 1723460000}}
```

Over SSE, the same requests arrive as query parameters added to your URL:

```txt Requests (SSE) theme={null}
?replay_from_time=1723450000
?replay_count=100&replay_to_time=1723460000
?replay_from_time=1723450000&replay_to_time=1723460000
```

### The done frame

Finish every answer with a done frame that repeats the request it answers: the same parameters, with the same
values, as integers, without the `replay_` prefix. Leave out the parameters that were not sent.

```json End of an answer (WebSocket) theme={null}
{"type": "replay_done", "replay": {"count": 100, "to_time": 1723460000}}
```

```txt End of an answer (SSE) theme={null}
event: replay_done
data: {"replay": {"count": 100, "to_time": 1723460000}}

```

The done frame is how the platform tells a whole answer from a cut-off one, and the repeated request is how it
knows the answer is to the question it asked. Without the done frame a warm-up waits out the silence timeout, and
a run over a date window ends with an error instead of computing. A done frame naming another request is not
taken as an answer to this one.

One window from start to finish, over WebSocket:

```txt A date window, start to finish theme={null}
platform → {"replay": {"from_time": 1723450000, "to_time": 1723460000}}
server   → {"type": "record", "time": 1723451200, "data": {"signal": 0.4}}
server   → {"type": "record", "time": 1723455900, "data": {"signal": 0.7}}
server   → {"type": "replay_done", "replay": {"from_time": 1723450000, "to_time": 1723460000}}
           the connection closes, and no live records follow
```

### Rules around the answer

* Send what you have; nothing is a valid answer. With nothing to send, send the done frame right away.
* A done-framed answer is final — an empty answer with a done frame means you hold no records in that range, and
  the platform does not substitute its own buffer. Without a done frame, the records that arrived are used as they
  are; for a window the platform falls back to its buffer only when nothing usable arrived at all.
* The done frame is a signal, not a guarantee: the platform also ends the answer on its own silence timeout,
  overall deadline and size caps.
* Records outside the requested bounds are dropped by the platform. Send no more than `count` records: the
  platform keeps the newest `count` of what it read, but it stops reading at the record limit below, so a long
  oversend leaves it with the oldest ones.
* `count` is never more than the replay record limit below: a larger history is asked for as that limit.

### What your server actually sees

The platform turns user launches into a small set of requests, and the users themselves are invisible to you:

* **The first connection** the platform opens to your feed asks for the last records before the present moment,
  as many as its record buffer holds. They fill the buffer, which then serves live subscribers without asking you
  again. When you finish that answer with the done frame, the platform closes the connection and opens the live
  one from the last record it got — a live-from-a-point request — so nothing you published in between is lost.
  Finish that one with the done frame too: without it the platform cannot tell whether anything was missed in
  between, and reports a gap to every subscriber. A feed that ignores the first request and just streams live
  keeps that first connection as its live one.
* **A reconnect** after a dropped live connection asks for live from the last record the platform received. Answer
  it, and the outage leaves no hole.
* **A launch from a date** arrives as a separate short connection asking for a window, where `to_time` is the
  present moment.
* **A run over a date window** arrives the same way, carrying the user's own two dates.
* **A trading strategy's live run** asks for the last records before the moment it starts, on a short connection.
* **A launch by history depth** is served from the platform's buffer and never reaches your server.

The whole level-2 server logic fits in a few lines:

```py One connection, one answer (pseudocode) theme={null}
def on_connection(conn):
    ask = conn.first_frame_or_query_params()      # there may be no request at all
    if ask:
        if ask.count:                             # last records before a moment
            records = my_storage.last(ask.count, before=ask.to_time)
        else:                                     # a window, or live from a point
            records = my_storage.range(ask.from_time, ask.to_time)
        for record in records:                    # oldest first
            conn.send(record)
        conn.send({"type": "replay_done", "replay": ask.as_sent()})
        if ask.to_time:                           # a closed request: answer, then close
            conn.close()
            return
    stream_live(conn)                             # then just send new records
```

## Limits

These are platform settings, not constants of the protocol; treat them as current defaults.

| Limit | Current value |
| - | - |
| Maximum record size | 64 KiB; larger records are dropped |
| Clock skew | A record timestamped more than 5 minutes into the future is dropped |
| Silence | A stream quiet for longer than `stale_after` is marked stale; the connection is not closed |
| History answer | Ends at your done frame, or at the first of: 10 seconds of silence, a 60-second deadline, 20,000 records, 32 MiB received |
| Record buffer | The last 1,000 records or 4 MiB per feed, whichever fills first; the first connection asks for 1,000 |


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.