Skip to content

fix: preserve Topstep fill metadata and persist Questrade token rotation - #21

Open
aintelegator wants to merge 3 commits into
LuxAlgo:mainfrom
aintelegator:topic/aintelegator/topstep-sdk-fixes
Open

aintelegator wants to merge 3 commits into
LuxAlgo:mainfrom
aintelegator:topic/aintelegator/topstep-sdk-fixes

Conversation

@aintelegator

Copy link
Copy Markdown

Topstep fills currently lose native identity and timestamp precision, omit separately returned commissions, and can turn failed searches into empty positions/history. Questrade can also lose a replacement single-use refresh token when a later account/history request fails. This PR preserves the fill metadata and delivers Questrade rotation before subsequent requests.

Changes

  • Add optional Trade.executionId, instrumentId, executionTieBreak, and reportedGrossPnl. Topstep preserves full contract IDs and all timestamp fraction digits while normalizing offsets to UTC; the parser only passes milliseconds to Date.parse.
  • Include available non-negative fees plus commissions, preserve explicit zero costs, and expose provider-reported gross realized P&L. Omit the whole fee when either component is negative because its meaning is unspecified.
  • Reject Topstep success: false responses at HTTP 200 and malformed non-object/array bodies with typed errors. Failed searches abort the snapshot rather than producing empty history or false position-closure events.
  • Prefer account-scoped native fill IDs in sync. During the Topstep upgrade, match legacy occurrences at millisecond precision after reserving exact matches. Compare mixed-length timestamp fractions correctly in FIFO stats.
  • Add FetchContext.onCredentialsRotated. Questrade awaits it immediately after exchange; connect() updates in-memory credentials before invoking persistence. Callback failures stop further requests. Direct consumers without a callback retain rotatedCredentials on success.

Returned provider accounts remain separate and automatically discovered. The commits separate Topstep/schema work, sync/stats handling, and Questrade rotation. Documentation, the Topstep conformance vector, and synthetic regression tests are included; no generated bundles, source maps, dependencies, or version bump are included.

Compatibility and limits

  • executionTieBreak is a deterministic numeric ID key, not a documented chronological sequence. Provider array order remains intact; stats preserves input order for exact timestamp ties.
  • Offset-less Topstep timestamps now leave executedAt absent. Timestamp-based external consumers must account for the preserved fractional precision. Legacy fills indistinguishable at millisecond precision remain ambiguous during migration.
  • Topstep's published API schema defines fees and optional commissions. Its official fee breakdown lists fees and commissions as separate additive costs, supporting this mapping. The API schema does not explicitly define their accounting relationship; no live fill/statement reconciliation was performed.
  • Questrade tokens remain in memory for later daemon polls, but the CLI does not persist credentials across restarts. Programmatic users can supply a persistence callback. Other adapters retain their existing rotation timing.
  • Stats does not use contract IDs or reported gross P&L to reconstruct futures results. A separate brokerFee field is omitted; combined costs use the existing fee field.

Validation

  • pnpm check: typecheck and 296 tests passed (20 files).
  • pnpm build: ESM, CJS, and declaration builds passed.
  • Dependency license gate: 141 packages passed.
  • git diff --check passed.

Validated locally on Node 26.10.0; the upstream CI matrix is Node 18/20/22. Tests use synthetic provider data and no live broker credentials.

AI assistance

This contribution was AI-assisted using OpenAI Codex for implementation, tests, documentation, and review. The contributor also independently reviewed the changes and re-ran typecheck and tests. All commits include the repository-required DCO sign-off.

Alexey Intelegator added 3 commits October 7, 2026 12:27
Expose native fill and instrument identity, a deterministic tie-break key, and reported gross P&L. Preserve UTC fractional precision, report explicit zero costs, combine fee components, and reject unsuccessful or malformed responses.

Signed-off-by: Alexey Intelegator <alexey.intelegator@email.com>
Prefer account-scoped execution IDs and reserve exact matches before consuming legacy millisecond occurrences. Compare fractional timestamps without losing precision or misordering mixed fraction lengths in FIFO stats.

Signed-off-by: Alexey Intelegator <alexey.intelegator@email.com>
Update connection credentials immediately after single-use refresh-token exchange and await the persistence callback before further IO. Propagate callback failures and retain rotatedCredentials for direct adapter consumers without the callback.

Signed-off-by: Alexey Intelegator <alexey.intelegator@email.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.

1 participant