Skip to content

Detect paper environment from api key PK prefix when Alpaca is only a data provider - #73

Open
AlexCatarino wants to merge 1 commit into
QuantConnect:masterfrom
AlexCatarino:bug-72-data-provider-paper-endpoint
Open

Detect paper environment from api key PK prefix when Alpaca is only a data provider#73
AlexCatarino wants to merge 1 commit into
QuantConnect:masterfrom
AlexCatarino:bug-72-data-provider-paper-endpoint

Conversation

@AlexCatarino

Copy link
Copy Markdown
Member

Description

Fixes #72. When Alpaca is used only as a secondary data provider, the Live Deployment Wizard exposes no Paper/Live selector, so alpaca-paper-trading never reaches job.BrokerageData and SetJob defaulted every client to the live endpoint — which rejects paper credentials with request is not authorized.

SetJob now derives the default environment from the API key itself: paper keys are detected by their PK prefix. An explicit parseable alpaca-paper-trading value still takes precedence, so existing deployments that set the flag behave exactly as before (a trace log records when the setting overrides the detection). Unset, empty, or invalid values fall back to the detection via bool.TryParse — locally the job packet always carries the key (as an empty string when unconfigured), which previously would have thrown FormatException in Convert.ToBoolean.

OAuth-only deployments are unaffected: access tokens carry no environment prefix and authenticate against both trading endpoints, and the market-data endpoints are environment-independent.

Source of the PK prefix

The prefix is a convention used by Alpaca's own tooling rather than a documented contract: Alpaca's example-hftish selects paper-api.alpaca.markets via key_id.startswith('PK'), and the official Alpaca CLI uses ALPACA_API_KEY=PK... throughout its paper examples. Verified empirically: a paper key returns 200 on paper-api.alpaca.markets/v2/account and 401 on api.alpaca.markets. Because the prefix is undocumented, the explicit setting keeps precedence.

Testing

  • New DetectsPaperEnvironmentFromKeyPrefixWhenOnlyDataProvider test in AlpacaBrokerageAdditionalTests: SetJob with a paper key and no alpaca-paper-trading initializes and connects — routing the paper key to the live endpoint would throw request is not authorized during the symbol mapper's eager trading-API call, so success asserts the routing. Passed against the paper endpoint.
  • End-to-end local deployment reproducing the issue: PaperBrokerage as the brokerage + Alpaca as data-queue-handler only, paper key, no environment setting — SPY streamed via IEX and BasicTemplateAlgorithm traded.

🤖 Generated with Claude Code

…ndler

When Alpaca is only a secondary data provider the live deployment wizard
sets no alpaca-paper-trading, so SetJob routed paper credentials to the
live endpoint which rejects them. Detect paper api keys by their PK
prefix as the default; an explicit parseable alpaca-paper-trading value
still takes precedence.

Fixes QuantConnect#72

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Alpaca data provider always uses live endpoint; paper API credentials rejected when Alpaca is used only as secondary data provider

1 participant