Add Firstrade platform cash reserve policy#23
Conversation
There was a problem hiding this comment.
💡 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".
| 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), | ||
| }, | ||
| ) |
There was a problem hiding this comment.
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 👍 / 👎.
|
@codex review |
There was a problem hiding this comment.
💡 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".
| if value < 0: | ||
| raise ValueError(f"{name} must be non-negative, got {value}") | ||
| return float(value) |
There was a problem hiding this comment.
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 👍 / 👎.
|
@codex review |
|
Codex Review: Didn't find any major issues. Hooray! ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
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_USDandFIRSTRADE_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.pygit diff --check