Skip to content

Add Firstrade platform cash reserve policy#23

Merged
Pigbibi merged 3 commits into
mainfrom
firstrade-reserved-cash-policy
May 24, 2026
Merged

Add Firstrade platform cash reserve policy#23
Pigbibi merged 3 commits into
mainfrom
firstrade-reserved-cash-policy

Conversation

@Pigbibi

@Pigbibi Pigbibi commented May 24, 2026

Copy link
Copy Markdown
Contributor

Summary

This PR adds a platform-level cash reserve policy for Firstrade so the platform can keep a minimum amount of cash available without relying only on strategy annotations.

The change introduces FIRSTRADE_MIN_RESERVED_CASH_USD and FIRSTRADE_RESERVED_CASH_RATIO, defaults both to zero, and applies the effective reserve as the maximum of the platform floor, the platform ratio, and any strategy-provided reserve. This preserves current behavior unless the new env vars are explicitly configured.

It also wires the policy through the strategy cycle, updates Cloud Run env sync and .env.example, and documents the behavior in English and Chinese without naming a live strategy.

Validation

  • .venv/bin/python -m pytest tests/test_runtime_config_support.py tests/test_decision_mapper.py tests/test_rebalance_service.py
  • git diff --check

@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: 6047a54a49

ℹ️ 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 thread application/rebalance_service.py Outdated
Comment on lines +171 to +177
runtime_metadata.setdefault(
"firstrade_execution_policy",
{
"reserved_cash_floor_usd": float(settings.reserved_cash_floor_usd or 0.0),
"reserved_cash_ratio": float(settings.reserved_cash_ratio or 0.0),
},
)

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 Enforce platform reserve policy over strategy metadata

_runtime_metadata_with_execution_policy uses setdefault, so any strategy that returns a firstrade_execution_policy key in evaluation.metadata (even {} or zeros) prevents the platform env settings from being applied. In that case the configured FIRSTRADE_MIN_RESERVED_CASH_USD / FIRSTRADE_RESERVED_CASH_RATIO can be silently bypassed, which defeats this change’s platform-level cash safety control for affected strategies.

Useful? React with 👍 / 👎.

@Pigbibi

Pigbibi commented May 24, 2026

Copy link
Copy Markdown
Contributor Author

@codex review

@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: 19b807420f

ℹ️ 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 thread runtime_config_support.py
Comment on lines +191 to +193
if value < 0:
raise ValueError(f"{name} must be non-negative, got {value}")
return float(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.

P1 Badge Reject non-finite reserved cash env values

_resolve_non_negative_float_env only checks < 0, so non-finite inputs like NaN/inf are accepted for FIRSTRADE_MIN_RESERVED_CASH_USD and FIRSTRADE_RESERVED_CASH_RATIO instead of failing fast. In production this can silently break execution policy: e.g., FIRSTRADE_RESERVED_CASH_RATIO=NaN passes validation and is later clamped in decision_mapper to an effective 1.0, which reserves all equity and drives buy budget to zero. Add an explicit finite-number guard before returning.

Useful? React with 👍 / 👎.

@Pigbibi

Pigbibi commented May 24, 2026

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Hooray!

ℹ️ 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".

@Pigbibi Pigbibi merged commit 30af50a into main May 24, 2026
1 check passed
@Pigbibi Pigbibi deleted the firstrade-reserved-cash-policy branch May 24, 2026 12:58
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