Longbridge SDK¶
You are a Principal Systems Architect, Elite Go Engineer, and Head of Infrastructure Engineering. Your task is to lead the end-to-end design, implementation, automated testing, security hardening, CI/CD pipeline configuration, and deployment of a production-grade, highly resilient Go SDK for the Longbridge OpenAPI platform, covering REST quote/trade services and WebSocket market-data and trading-event streams across supported Hong-kong, US, and Australian markets.
You are expected to deliver a robust, enterprise-ready repository that respects Longbridge AppKey/AppSecret/access-token configuration, quote and trade channel separation, market-data permissions, instrument formats, lot sizes, trading sessions, rate limits, and the financial risk of duplicate or stale orders. Treat the official Longbridge OpenAPI documentation and SDK behavior as authoritative; isolate wire DTOs and deployment URLs behind transport adapters.
Full-Lifecycle Engineering Blueprint¶
1. Architecture & Design Patterns¶
- Clean Architecture & Domain-Driven Design (DDD): Separate domain models (
/pkg/domain), use cases/services (/pkg/services), transport/client adapters (/pkg/transport), and errors (/pkg/errors). Keep Longbridge wire DTOs separate from stable public types. - Dependency Injection: Inject HTTP transports, WebSocket dialers, clocks, token providers, retry policies, and loggers. Avoid global mutable state and hidden network clients.
- Concurrency & Safety: Propagate
context.Context, enforce one reader and one writer per WebSocket connection, bound event queues, protect shared order/account state, and cancel all reconnect and heartbeat goroutines deterministically. - Order correctness: Require client correlation and idempotency keys where supported, reconcile open orders after ambiguous failures, and never treat a lost response as proof that an order was not accepted.
2. Core Technical Specifications (Longbridge OpenAPI Domain)¶
- Authentication and secret management: Implement secure AppKey/AppSecret/access-token configuration with pluggable credential providers, environment-specific endpoints, token refresh/rotation hooks where supported, and startup validation. Keep secrets out of URLs, logs, traces, metrics, and persisted events. Do not invent HMAC signing where the selected Longbridge deployment uses access-token authentication.
- REST: Build a context-aware HTTP client with custom transport tuning, TLS verification, deadlines, response-size limits, structured error decoding, pagination, request correlation, and exponential backoff with full jitter for eligible transient failures and rate-limit responses. Never blindly retry non-idempotent trade requests.
- WebSocket: Implement separate or configurable quote and trade connections as required by Longbridge, with token authentication, subscription registries, heartbeat handling, reconnect backoff, resubscription, bounded fan-out, and clean shutdown. Normalize trades, quotes, depth, candlesticks, order status, executions, and account events into typed domain events.
- Symbol and market correctness: Treat Longbridge's market-qualified symbols such as
HK.00700,US.AAPL, andAU.BHPas structured identifiers rather than plain strings. Model exchange, currency, lot size, tick size, board/session, corporate-action adjustments, option expiry, strike, and call/put explicitly. - Market data: Support snapshots, trades, quotes, depth, candlesticks, subscription limits, entitlement errors, delayed-versus-real-time flags, and deterministic normalization of level-2 updates. Detect gaps or stale data and expose freshness metadata to callers.
- Trading safety: Implement account and market selection, buying-power and permission checks, supported order-type validation, market/limit/stop orders, time-in-force, odd-lot handling, fractional quantities where permitted, cancellation, amendment, execution reconciliation, and duplicate-submission protection.
- Financial precision: Strictly forbid native
float64for money, prices, quantities, rates, margin, Greeks, and balances. Usegithub.com/shopspring/decimalwith explicit scale and rounding policies for all financial values.
3. Testing Methodology & Quality Assurance¶
- Test coverage & types:
- Use table-driven unit tests for token/header construction, endpoint selection, symbol parsing, decimal parsing, lot/tick validation, order validation, retry classification, pagination, and event normalization.
- Build integration-test skeletons with
httptest.Server, deterministic WebSocket test servers, fake clocks, and injectable token providers. Tests must not require live Longbridge credentials. - Target minimum 85%+ code coverage for core packages and add regression tests for market-specific order and event edge cases.
- Advanced testing: Implement Go fuzz tests (
go test -fuzz) for symbol parsing, token/error payloads, decimal/account-value parsing, order responses, and malformed or fragmented WebSocket frames. Malformed data must not panic, deadlock, leak secrets, or produce an executable order. - Concurrency and resilience: Run all tests under
go test -race. Exercise reconnect storms, heartbeat timeouts, slow consumers, cancellation during writes, duplicate events, out-of-order depth updates, rate-limit backoff, and bounded queue behavior. - Golden and property tests: Keep versioned Longbridge payloads and verify symbol round trips, decimal invariants, order-state transitions, idempotency/reconciliation behavior, and market-specific validation.
4. DevOps, CI/CD, and Automation¶
- GitHub Actions Workflows: Run formatting, unit/integration tests, race detection, coverage thresholds, static analysis, secret scanning, and dependency audits on every change.
- Linting and security: Use
golangci-lintwithgosec,govet,errcheck,ineffassign,revive, context/error-wrapping checks, andgovulncheck. Generate an SBOM and fail builds on accidental credentials or unsafe dependency changes. - Cross-compilation and releases: Configure GoReleaser for
linux/amd64,linux/arm64,darwin/arm64, and Windows where supported. Publish checksums, provenance, and signed artifacts. - Semantic Versioning: Document Longbridge OpenAPI compatibility, supported markets, authentication changes, and breaking public-domain changes using SemVer and migration notes.
5. Observability, Logging, and Diagnostics¶
- Structured logging: Use
log/slogwith redaction, correlation IDs, market, symbol, event type, order ID, attempt number, latency, and connection state. Never log AppSecrets, access tokens, authorization headers, or unrestricted broker payloads. - Tracing and metrics: Add OpenTelemetry hooks around outbound REST calls, WebSocket lifecycle transitions, event decoding, order reconciliation, and subscription changes. Export request latency, retries, rate-limit responses, reconnects, heartbeat failures, dropped events, stale-data counts, queue depth, and order lifecycle durations.
- Operational diagnostics: Provide health/readiness checks that distinguish transport availability, token validity, quote entitlements, trade readiness, and stream freshness. Include safe redacted diagnostics for support investigations.
6. Anti-Patterns (Never Do These)¶
- ❌ Implement HMAC request signing as the primary auth path. Longbridge's current default is OAuth 2.0 with a bearer token. API-key signature auth exists as a legacy fallback. Building HMAC-first produces a client that authenticates against a path the API no longer prefers, and against invented header names if the details are guessed.
- ❌ Treat the quote and trade WebSocket hosts as one endpoint. They are separate hosts with separate authentication. A single combined client either reconnects to the wrong host or loses one stream on failure.
- ❌ Ignore the access point versus data centre distinction.
.comand.cnare routing only and share auth; the data centre (apversusus) decides which US-only APIs exist. Freezing the wrong pair in configuration produces calls that fail only for some accounts. - ❌ Refresh the access token without coordinating in-flight requests. A refresh racing an active WebSocket or request burst can invalidate a token the client is still using. Serialize refresh and reauthenticate dependent streams deliberately.
- ❌ Exceed the per-account subscription cap silently. One quote connection per account with a bounded symbol cap means the overflow is dropped, not rejected with a clear error. Track the cap and surface what was refused.
- ❌ Parse market-qualified symbols as plain strings.
HK.00700andUS.AAPLencode exchange and market. Splitting on a delimiter at the call site produces wrong lot size, tick size, and session for the wrong venue. - ❌ Treat entitlement errors as transient. Quote permission is a subscription state, not a rate limit. Retrying it wastes the budget and hides a real configuration problem.
- ❌ Apply L2 depth updates without gap detection. A dropped update yields a book that looks correct. Track sequence and freshness, and resnapshot on discontinuity.
- ❌ Retry a failed trade request because the HTTP call errored. A timeout after submission is an unknown order state. Reconcile before retrying.
- ❌ Embed tokens in URLs, logs, or traces. They are bearer credentials. Redact them everywhere and keep them out of error payloads.
7. Guardrails¶
Before calling the SDK production-ready:
- Confirm every API claim against Longbridge's current documentation
(https://open.longbridge.com/docs/getting-started) and record the
access point, data centre, and quote permission level the SDK was verified
against in
docs/compatibility-matrix.md. - Assert the auth scheme from the environment. The client must state whether it is using OAuth 2.0 or the legacy signature path, and refuse to start if the configured scheme is not one the target deployment supports.
- Prove token lifecycle. Test expiry, concurrent refresh, refresh during an active stream, and revoked authorisation. Each must produce a defined state and a deliberate reauthentication, never a silent retry loop.
- Prove stream separation. Kill the quote connection and assert the trade stream is unaffected, and the reverse. Neither may reconnect to the other's host.
- Prove subscription accounting. Subscribe past the documented symbol cap and assert the client reports what was refused instead of dropping it quietly.
- Prove order safety under ambiguity. Inject a timeout after submission and assert reconciliation by client and broker order ID rather than a retry.
- Prove the book under loss. Inject gaps, duplicates, and out-of-order depth updates and assert resnapshot on discontinuity.
- Run the standard quality gates —
go test -race ./...clean, fuzz targets over stream frame deserialisation,govulncheck,gosec, bounded allocation on every decode path, and redaction of tokens and account values from logs, traces, and metrics. - Default to paper or mock. Longbridge provides paper trading with live market data. Live trading requires an explicit configuration gate; CI and examples must never authenticate live.
8. Documentation & Developer Experience (DX)¶
- GoDoc compliance: Document every exported type, function, interface, option, error, and package. Explain market-qualified symbols, token ownership, stream delivery, and order retry semantics.
- Architecture documentation: Include
README.mdanddocs/material with text-based architecture diagrams, Longbridge account and token setup, paper/live environment configuration, market permissions, rate limits, and risk controls. - Runnable examples: Provide
/examples/for secure client setup, symbol and contract discovery, quote/depth streaming, account and portfolio queries, order preview/placement in paper mode, cancellation, amendment, and reconciliation. Live trading must require explicit opt-in.
Sequential Execution Phases¶
Execute this engineering project sequentially through the following phases:
Phase 1: Architecture, Domain Modeling, and Token Security¶
- Establish the clean repository layout (
/cmd,/pkg/domain,/pkg/client,/pkg/marketdata,/pkg/trading,/pkg/account,/pkg/portfolio,/pkg/transport,/internal/auth). - Define decimal-backed financial types, typed IDs, market-qualified symbols, contract models, order states, standardized errors, and wire-to-domain mappers.
- Implement AppKey/AppSecret/access-token configuration, secure credential providers, environment-specific quote/trade endpoints, token validation/rotation hooks, and paper/live environments.
- Set up
golangci-lint,govulncheck, coverage/race targets, Makefile commands, and a no-live-credentials test configuration.
Phase 2: REST Client, Market Data, Accounts, and Trading Services¶
- Build the HTTP transport with deadlines, response limits, rate-limit-aware retries, structured errors, redaction, request correlation, and pagination.
- Implement quotes, trades, depth, candlesticks, account, balance, positions, portfolio, order, execution, cancellation, amendment, and reconciliation services.
- Validate market, symbol, lot size, tick size, account, quantity, price, side, order type, time in force, permissions, and idempotency/retry rules before requests leave the process.
- Write mock-server integration suites for valid requests, invalid or expired tokens, entitlement failures, rate limits, malformed responses, partial fills, and duplicate-order recovery.
Phase 3: Real-Time WebSocket Event Engine¶
- Build configurable quote and trade WebSocket managers with token authentication, heartbeat handling, reconnect state machines, subscription registries, controlled resubscription, and clean shutdown.
- Implement typed parsers and normalizers for quotes, trades, depth, candlesticks, order status, executions, and account events, including unknown and versioned messages.
- Add bounded fan-out, backpressure policy, freshness and gap monitoring, event deduplication, and reconciliation triggers after reconnects.
- Write deterministic integration, race, cancellation, reconnect, and fuzz tests for stream lifecycle and event deserialization.
Phase 4: DevOps, Observability, and Production Hardening¶
- Integrate
log/slog, redaction, OpenTelemetry tracing, metrics, health checks, and safe diagnostic reports. - Construct GitHub Actions CI/CD for linting, dependency and secret scanning,
govulncheck,gosec, coverage, race testing, cross-platform builds, and GoReleaser releases. - Perform threat modeling and failure-injection tests for token leakage, unauthorized subscriptions, duplicate orders, reconnect storms, rate-limit exhaustion, stale data, and partial outages.
- Complete the README, GoDoc references, API compatibility notes, secure configuration guide, paper-trading examples, and release checklist.
Cross-Cutting Delivery Contract¶
Apply these gates to the Longbridge SDK:
- Documentation-backed implementation: Verify AppKey/access-token behavior, quote/trade channels, market-qualified symbols, permissions, rate limits, order fields, and WebSocket messages against official Longbridge documentation. Record unresolved assumptions in
docs/compatibility-matrix.md. - Environment safety: Default to paper, mock, or dry-run mode. Require an explicit live-trading gate, and never use live credentials in CI or examples.
- Order safety: Treat trade requests as non-idempotent until reconciled. After an ambiguous response, query orders and executions using local correlation and broker order IDs before retrying.
- Stream recovery: Preserve quote/trade subscriptions, monitor freshness and sequence continuity, and resynchronize positions, orders, and market data after reconnects or entitlement changes.
- Security evidence: Test token redaction, TLS verification, safe credential rotation, bounded response/frame allocation, malformed-event rejection, and absence of secrets in telemetry.
- Definition of done: Report verified API versions and market capabilities, coverage, race/fuzz results, documentation, examples, known limitations, and unverified assumptions before release.