alpacasse

Alpacasse:用于可靠消费 Alpaca Broker API 实时事件流的技能,通过服务器发送事件(SSE)接收账户状态、日记账、转账/入金、交易和非交易活动事件,涵盖连接、身份验证、重放游标(since/since_id)、心跳、重连/退避、排序与幂等处理,适用于在任何语言中构建 Alpaca 生命周期事件消费者。

安装方式

先用 Prompt 核对来源与依赖。也可下载安装包。

用 AI 助手安装

复制提示词,让助手先核对固定版本和依赖。

查看 Prompt收起 Prompt
先检查当前 Agent 是否已安装 SkillPI Skill Finder。如果尚未安装,请按官方仓库的说明安装并启用:https://github.com/Findskills/skillpi。然后使用 Skill Finder 打开此 SkillPI 详情页并定位 "alpacasse":https://skillpi.com/zh/skills/deepskill-c4f4e541d7ed34feee247001b5907aa110aea8a359e50e3fc9b374bf216c4bcb。安装前核对上游来源和版本:https://skillhub.cn/skills/user_3c6cb52e/alpaca-broker-sse-events。来源不一致时停止并说明差异。核对通过后,用 Skill Finder 安装目标 Skill,并报告安装路径和来源版本。

来源与配置

skillhub.cn

配置方式待核实

请查看原始来源中的适用工具、依赖和权限要求。

安装实测待补充

Alpacasse:用于可靠消费 Alpaca Broker API 实时事件流的技能,通过服务器发送事件(SSE)接收账户状态、日记账、转账/入金、交易和非交易活动事件,涵盖连接、身份验证、重放游标(since/since_id)、心跳、重连/退避、排序与幂等处理,适用于在任何语言中构建 Alpaca 生命周期事件消费者。

0 下载

介绍来源: third_party · user_3c6cb52e · 1.0.0 参考来源

SkillPi 解读

能完成的任务
翻译待完成,请参阅来源文档。
实测状态
未测试

文件浏览

在原站浏览文件 ↗

原始 SKILL.md

打开原文 ↗
Frontmatter
name: "Alpaca券商SSE事件"
slug: alpaca-broker-sse-events
version: "1.0.0"
displayName: "Alpaca券商SSE事件"
summary: "Consume Alpaca Broker API real-time event streams over Server-Sent Events (SSE) — account status, journal, transfer/funding, trade, and non-trade-activity events — reliably. Covers connection, auth, r..."
license: "MIT"
description: "Consume Alpaca Broker API real-time event streams over Server-Sent Events (SSE) — account status, journal, transfer/funding, trade, and non-trade-activity events — reliably. Covers connection, auth, replay cursors (since/since_id), heartbeats, reconnection/backoff, ordering, and idempotent processing. Use when building an event consumer for Alpaca lifecycle events in any language."
category: "Quantitative Trading"
framework: "Multi-Framework"

Alpaca Broker API — Real-Time Events (SSE)

Alpaca pushes brokerage lifecycle events over Server-Sent Events: a long-lived HTTP GET that streams text/event-stream. This is not the market-data WebSocket (alpaca-broker-market-data) — different transport, different auth, different reliability model.

Read alpaca-broker-integration first. SSE uses the Broker API host + HTTP Basic auth (same credential as Broker REST).

Reference

  • Guide: https://docs.alpaca.markets/docs/sse-events
  • Live: alpaca-docs MCP → search "SSE Events", then fetch us/sse-events

1. Why SSE (and why it's simpler than it looks)

SSE is plain HTTP. You don't need a special client: open a GET, keep the connection open, and read the body line-by-line. Each event is a data: line containing a JSON object. It is replayable — you can ask for events from a point in the past and seamlessly catch up to live, which makes it far better than naive polling for lifecycle state.

2. Event streams

Stream Path Carries
Account status GET /v1/events/accounts/status Account-property changes: status/crypto_status (SUBMITTED→ACTIVE, ACTION_REQUIRED, REJECTED), plus kyc_results, account_blocked, trading_blocked, cash_interest, options
Journal status GET /v2/events/journals/status JNLC/JNLS lifecycle (queued→executed, correct…)
Funding/transfer status GET /v2/events/funding/status Unified: Transfer, BankRelationship, WireBank, FundingWallet entities (switch on entity_type)
Trade updates GET /v2/events/trades Order events in the event field: new, fill, partial_fill, canceled, rejected, held, trade_bust, trade_correct… (richer than order status)
Non-trade activities GET /v1/events/nta Dividends, interest, fees, splits, ACATs, cash disbursements. entry_type e.g. JNLC/FEE/INT/DIVNRA/CSD; status ∈ executed/correct/canceled

Paths & versions are NOT uniform — verify each. This is exactly the kind of cross-stream inconsistency Alpaca's docs under-communicate:

  • /v2 streams (trades, journals/status, funding/status) use a ULID event_id directly; /v1/events/trades and /v1/events/journals/status are legacy (existing partners only — migrate to v2). /v2/events/trades was previously /v2beta1, now redirected.
  • /v1 streams (accounts/status, nta) are current, not deprecated — there is no v2 yet. Each event carries both an integer event_id and a ULID event_ulid.

Every event carries at (timestamp), account_id, and status_from/status_to (account/journal/funding) or event+order (trades).

3. Connection

GET /v2/events/journals/status?since_id=<last-ulid-you-saw> HTTP/1.1
Host: broker-api.alpaca.markets
Authorization: Basic <base64(key:secret)>
Accept: text/event-stream

Read the response stream and parse data: {…} frames as they arrive. In most languages an off-the-shelf EventSource/SSE client works — just make sure it lets you set the Authorization header on the initial request (the browser EventSource API famously does not; use a server-side SSE library instead).

4. Replay cursors — the feature that prevents data loss

Every stream supports point-in-time replay:

Param Meaning
since / until Date or RFC3339 timestamps. URL-encode + in offsets as %2B.
since_id / until_id ID cursors. On v2 streams the ID is a ULID. On v1 streams it's the integer event_id.
since_ulid / until_ulid v1 streams only (accounts, nta) — ULID-based cursors, since v1 events carry both an int event_id and a event_ulid.

Rules: since is required if until is set; since_id required if until_id set (same for since_ulid/until_ulid); you can't mix since, since_id, and since_ulid. Without any since cursor, no history is returned — you only get live pushes from now on. Reaching the until bound ends the stream with a 200.

This is the single most important reliability lesson: persist the ID of the last event you successfully processed. On every (re)connect, pass it as your since cursor (since_id on v2; since_ulid or since_id on v1) so Alpaca replays anything you missed during the gap. A consumer that reconnects without a cursor silently drops every event that occurred while it was down.

5. Ordering caveat

Within a millisecond, ULIDs contain a random component, so two events in the same millisecond can sort either way. Alpaca's own guidance: for reconciliation, restart the stream from a since a few minutes before your last event and rely on idempotent processing to absorb the overlap. Don't assume strict total ordering — assume approximate ordering plus dedup.

6. Reliability patterns (hard-won)

SSE connections drop — networks, load balancers, deploys, and Alpaca-side resets all happen. A production consumer needs:

  1. Heartbeat / silence detection. SSE has no application heartbeat by default. Track lastMessageAt on every frame; if the stream is silent past a threshold (e.g. 5 min), proactively tear down and reconnect — a dead socket often looks "open."
  2. Reconnect with exponential backoff + cap. On error/close, reconnect after a delay that doubles up to a ceiling (e.g. start 1s, cap 60s). Reset the delay on a successful connect.
  3. A single-reconnect guard. Use a flag so an error storm doesn't spawn many concurrent reconnect attempts racing each other.
  4. Always reconnect with since_id = last processed event (see §4).
  5. Process idempotently (see §7) — overlap from replay is expected, not exceptional.
  6. Don't let a side-effect failure kill the stream. Wrap per-event processing in try/catch; log and continue. One bad event (or a downstream outage) must not stop you consuming the rest.

Note: OpenAPI can't fully model SSE, so generated API clients often hang on these endpoints (waiting for a response that never ends). Use a real streaming HTTP/SSE client, not a codegen'd one.

7. Idempotent processing pipeline

The robust shape for each event:

parse → persist a raw event snapshot (keyed on event_id, skip-if-exists)
      → match the local record by Alpaca ID (account_id / journal_id / order_id / transfer_id)
      → update local state under a row lock / guarded by current status
      → fire side effects (notifications, downstream transfers)
      → advance the stored cursor to this event_id
  • Snapshot-first, keyed on event_id. Insert the raw event with an upsert/skip-duplicate on event_id. A duplicate (from replay or an at-least-once redelivery) is then a no-op. This is your dedup boundary.
  • Lock the target row (SELECT … FOR UPDATE or equivalent) when mutating a transfer/order so two events for the same record can't race.
  • Guard transitions by current status — e.g. only act on a transfer that isn't already in a terminal state, so a late/duplicate "executed" doesn't re-trigger a payout.
  • Advance the cursor only after successful processing, so a crash mid-event replays it rather than skipping it (at-least-once, which idempotency makes safe).

8. Per-stream notes

  • Account status: drive onboarding UI and "enable trading" off status_to == ACTIVE. Reject sandbox/paper account IDs in live handlers.
  • Trade updates: new/accepted/pending_new are pre-fill; update local order state on fill/partial_fill/canceled/rejected. Invalidate any cached portfolio/holdings on fills.
  • Journals: remember executed isn't final and correct spawns a new journal ID (see alpaca-broker-journals). Idempotency + ID-keyed snapshots absorb both.
  • Funding/transfer: unified stream across 4 entity types; switch on entity_type. Funding-wallet per-transfer status may still need polling (alpaca-broker-funding-transfers).
  • NTA: dividends/fees/interest/corporate-actions — persist as activity snapshots; these feed balance/portfolio reconciliation.

9. SSE is necessary but not sufficient

Even a perfect consumer can miss events (extended downtime beyond retention, a bug, an un-handled type). Always pair SSE with a periodic reconciliation/heal pass that re-pulls authoritative state (activities, journals, transfers) from Alpaca and upserts it. SSE is for low latency; reconciliation is for correctness. See alpaca-broker-reconciliation-idempotency.

Related skills: correctness backstop → alpaca-broker-reconciliation-idempotency; dedup/idempotency mechanics → alpaca-broker-reconciliation-idempotency; backoff details → alpaca-broker-rate-limits-resilience; market-data streaming (WS, not SSE) → alpaca-broker-market-data.

当前收录版本

1.0.0

核对来源版本 ↗

安全与评测信息

质量评测

满分100 · 来自 SkillPi LAB 实验室的评测报告。

场景价值92/100

亮点

  • 提供Alpaca券商SSE事件流的完整消费指南,涵盖连接、认证、重放游标、心跳、重连/退避、排序和幂等处理等关键可靠性模式
  • 对构建券商生命周期事件消费者具有极高实用价值,解决实时事件流消费的痛点问题
  • 详细说明5种事件流类型及其用途,提供生产级别的可靠性最佳实践
触发精准95/100

亮点

  • 触发词精准完整,明确描述通过SSE消费Alpaca Broker API实时事件流
  • 覆盖账户状态、日记账、转账/入金、交易和非交易活动事件等多种事件类型
  • 边界清晰,与WebSocket市场数据流明确区分
结构规范90/100

亮点

  • 结构清晰,分9个章节详细说明SSE事件流消费的各个方面
  • 包含端到端示例、连接代码、可靠性模式、幂等处理流程等实用内容
  • frontmatter完整,包含名称、版本、许可证等必要信息
执行可靠92/100

亮点

  • 涵盖主要异常处理、重连机制、幂等处理、心跳检测等关键可靠性模式
  • 提供生产级别的可靠性最佳实践,包括重放游标、排序注意事项、幂等处理流水线
  • 详细说明SSE连接的可靠性挑战和解决方案,工具来源明确
证据可验 (前四项均值)92/100

取前四项原始分值的算术平均,四舍五入取整。可通过收录报告核对各项评分与说明,运行记录需单独查证。

正文静态检测

100 / 100 · skillpi-20261123-no-r3-v1

正文未命中当前规则。

分数仅反映正文中的规则命中,供使用前参考。附件与运行行为不在此评分范围内。

权限与使用前检查

根据当前主文档自动整理,命中内容可能是示例或防护说明;不是实际权限声明,也未覆盖附件、外部依赖或运行行为。

  • 账号、密钥或授权

    使用专用、最小权限账号,不把密钥粘贴到公开内容或命令日志;核对接收数据的服务与租户。

    主文档提及 · 第 18 行
文档引用的域名或地址

这些地址可能用于文档链接或示例,不是已批准的联网清单。使用前核对实际目的地及发送的数据。

  • docs.alpaca.markets

本站展示提示不会授予权限。涉及文件改动、密钥、外部发送或费用时,仍由你使用的工具按需确认。

安全评级与访问范围

safe · 静态证据核验,不代表运行安全保证。

使用前请由宿主核对并授权实际文件读写、外部服务和联网目的地;本站的核验不会授予任何运行权限。