Skip to content

Use confirmed sell release for Firstrade buys#219

Merged
Pigbibi merged 3 commits into
mainfrom
fix/fill-aware-sell-release-budget
Jul 10, 2026
Merged

Use confirmed sell release for Firstrade buys#219
Pigbibi merged 3 commits into
mainfrom
fix/fill-aware-sell-release-budget

Conversation

@Pigbibi

@Pigbibi Pigbibi commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

Summary

  • stop optimistic sell-proceeds reuse and gate buy budgets on confirmed sell releases
  • add Firstrade order-status querying on top of the unofficial API wrapper
  • update the QuantPlatformKit pin, qsl pin, uv lock, and related tests

Dependency

Testing

  • python3 -m pytest /Users/lisiyi/Projects/FirstradePlatform/tests/test_firstrade_client.py /Users/lisiyi/Projects/FirstradePlatform/tests/test_runtime_broker_adapters.py /Users/lisiyi/Projects/FirstradePlatform/tests/test_execution_service.py /Users/lisiyi/Projects/FirstradePlatform/tests/test_rebalance_service.py /Users/lisiyi/Projects/FirstradePlatform/tests/test_dependency_pin_guard.py

Co-Authored-By: Codex <noreply@openai.com>
@github-actions

Copy link
Copy Markdown

🤖 Codex PR Review

🚫 Merge blocked: The Codex review could not be completed.

Codex service request failed: 401 {"status": "error", "error": "OIDC workflow_ref is not allowed"}

The review check fails closed until a valid Codex review is available.

Co-Authored-By: Codex <noreply@openai.com>

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 011baa6f8f

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +698 to +700
confirmed_sell_release_value = compute_confirmed_sell_release_value(
submitted_sell_orders=submitted_sell_orders,
fetch_order_status=fetch_order_status,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Require fetched fills before releasing sell proceeds

Here the release calculation receives the just-submitted sell order, so when get_order_status cannot find the order yet (common immediately after submitting a live limit sell, and also for preview/dry-run payloads), the pinned helper falls back to the submission payload/order dict. Firstrade submission payloads contain the requested shares/limit_price while still pending, which can be credited as released cash and allow the later buy loop to spend proceeds from an unfilled sell; only fetched fill data (or a status explicitly confirmed as filled) should be used for this value.

Useful? React with 👍 / 👎.

Comment on lines +488 to +490
"quantity",
"shares",
"qty",

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Keep requested shares out of executed quantity

Even when get_order_status does find an order-status row, these fallback aliases treat the order's requested quantity/shares as executed_qty. For a live sell that is still open or pending, Firstrade status rows commonly still include the requested share count, and the release helper accepts a positive executed quantity even without a confirmed fill status; this can again credit unfilled sell proceeds and submit buys against cash that has not settled into the account.

Useful? React with 👍 / 👎.

safe_haven_cash_substitute_threshold_usd=settings.safe_haven_cash_substitute_threshold_usd,
cash_only_execution=settings.cash_only_execution,
notional_buy_execution=notional_buy_execution_enabled(settings.strategy_profile),
fetch_order_status=lambda broker_order_id: client.get_order_status(account, broker_order_id),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Handle order-status lookup failures after sells

When client.get_order_status raises (for example, the Firstrade order-status endpoint times out or returns an unexpected payload), that exception bubbles out of execute_value_target_plan after sell orders may already have been submitted. In that scenario the run aborts before producing the submitted/skipped order summary or completed strategy-run state; the safer behavior for an unavailable status lookup is to treat the confirmed release as zero and skip dependent buys instead of crashing the cycle.

Useful? React with 👍 / 👎.

Co-Authored-By: Codex <noreply@openai.com>
@Pigbibi Pigbibi merged commit 212ecac into main Jul 10, 2026
2 of 3 checks passed
@Pigbibi Pigbibi deleted the fix/fill-aware-sell-release-budget branch July 10, 2026 12:37

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: bf81cca684

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +738 to +740
elif float(delta_value) > 0.0 and limit_price > max(0.0, buy_budget):
requires_pending_sell_release = True
if estimated_buy_cost > investable_cash or requires_pending_sell_release:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Allow affordable buys after zero-quantity sells

When a negative delta floors to zero shares, this function still adds that symbol to pending_sell_release_symbols above even though no sell order was submitted. In that state, any later buy whose limit price is above its current budget sets requires_pending_sell_release here, and the condition below clears every buy_delta, so buys that were fully affordable from existing cash are skipped as pending_sell_release even though no sell can ever release funds. Limit this check to actually submitted pending sells, or skip only the buy that needs unconfirmed proceeds.

Useful? React with 👍 / 👎.

Comment on lines +608 to +610
for key, value in flattened.items():
if _sanitize_payload_key(key.rsplit(".", 1)[-1]) in candidates:
return value

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Honor candidate priority when reading order fields

This helper receives candidates in priority order, but it returns whichever matching key appears first in Firstrade's payload. When an order-status row includes both a generic field such as description/limit_price and the real status/avg_price, an earlier generic key shadows the fill field, so confirmed fills can be treated as unfilled or released at the wrong price. Iterate candidate keys by priority instead of payload order so the explicit fill/status fields win.

Useful? React with 👍 / 👎.

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