Detect paper environment from api key PK prefix when Alpaca is only a data provider - #73
Open
AlexCatarino wants to merge 1 commit into
Open
Conversation
…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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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-tradingnever reachesjob.BrokerageDataandSetJobdefaulted every client to the live endpoint — which rejects paper credentials withrequest is not authorized.SetJobnow derives the default environment from the API key itself: paper keys are detected by theirPKprefix. An explicit parseablealpaca-paper-tradingvalue 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 viabool.TryParse— locally the job packet always carries the key (as an empty string when unconfigured), which previously would have thrownFormatExceptioninConvert.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
PKprefixThe prefix is a convention used by Alpaca's own tooling rather than a documented contract: Alpaca's example-hftish selects
paper-api.alpaca.marketsviakey_id.startswith('PK'), and the official Alpaca CLI usesALPACA_API_KEY=PK...throughout its paper examples. Verified empirically: a paper key returns 200 onpaper-api.alpaca.markets/v2/accountand 401 onapi.alpaca.markets. Because the prefix is undocumented, the explicit setting keeps precedence.Testing
DetectsPaperEnvironmentFromKeyPrefixWhenOnlyDataProvidertest inAlpacaBrokerageAdditionalTests:SetJobwith a paper key and noalpaca-paper-tradinginitializes and connects — routing the paper key to the live endpoint would throwrequest is not authorizedduring the symbol mapper's eager trading-API call, so success asserts the routing. Passed against the paper endpoint.PaperBrokerageas the brokerage + Alpaca as data-queue-handler only, paper key, no environment setting — SPY streamed via IEX andBasicTemplateAlgorithmtraded.🤖 Generated with Claude Code