Add Hedging Functionality to the portfolio management tool#163
Open
manu49 wants to merge 4 commits into
Open
Conversation
Adds a new agent tool that analyses a portfolio's risk factors — sector concentration, regional exposure, asset class distribution, and high-risk positions — then maps them to specific hedging instruments (inverse ETFs, commodities, bonds, defensive equities, options strategies). Changes: - tools.py: tool schema, Oracle SQL implementation (_suggest_portfolio_hedge), pure-Python hedge catalogue + recommendation engine (_build_hedge_recommendations), dispatcher routing for both converged and sprawl modes - sprawl_tools.py: PostgreSQL equivalent (suggest_portfolio_hedge_sprawl) Supports risk_focus parameter: market | sector | regional | currency | all https://claude.ai/code/session_014SVg1DDKFLVDTfAfQ3mGuR
Sets up testing infrastructure from scratch (no prior tests existed) and adds 97 tests covering the feature end-to-end. Backend (pytest): - pytest.ini + pytest/pytest-mock added to requirements.txt - tests/conftest.py: shared row fixtures (tech-heavy, ESG, balanced, intl-heavy portfolios) used across all backend test files - test_hedge_recommendations.py (39 tests): pure-unit tests for _build_hedge_recommendations — output structure, market/sector/ regional/currency risk detection, ESG filtering, risk_focus scoping, edge cases (empty rows, None PCTs, malformed JSON) - test_suggest_portfolio_hedge_integration.py (8 tests): integration tests for _suggest_portfolio_hedge with a mock execute_query — verifies JSON output, error messages, default args, query_logger invocation, account_id SQL param binding - test_tool_schema.py (11 tests): schema registration, PRELOADED_TOOLS inclusion, required/optional params, enum values, dispatcher routing Frontend (vitest + @testing-library/react): - vite.config.js: vitest config (jsdom environment, coverage) - package.json: test / test:watch / test:coverage scripts - src/test/setup.js: @testing-library/jest-dom matchers - src/test/hedgeFixtures.js: shared ToolCall fixtures (success/running/error) - ToolCallBubble.test.jsx (22 tests): running spinner, success checkmark, error icon, elapsed time, expand/collapse toggle, pretty-printed JSON output, args rendering edge cases (null/empty args) - chatReducer.test.js (17 tests): ADD_TOOL_CALL_START → UPDATE_TOOL_CALL → AGENT_COMPLETE lifecycle — status transitions, output attachment to assistant messages, isLoading, multi-tool-call isolation All 97 tests pass (58 backend, 39 frontend). https://claude.ai/code/session_014SVg1DDKFLVDTfAfQ3mGuR
Runs Ruff (linter + formatter) on all changed Python files and Prettier on all changed JS/JSX files to comply with the repository's pre-commit hook standards defined in .pre-commit-config.yaml. Changes: import ordering, unused import removal, line-length normalisation in tests/conftest.py, test_hedge_recommendations.py, test_tool_schema.py, test_suggest_portfolio_hedge_integration.py, tools.py, sprawl_tools.py, chatReducer.test.js, and hedgeFixtures.js. All 97 tests still pass. https://claude.ai/code/session_014SVg1DDKFLVDTfAfQ3mGuR
Add portfolio hedge recommendation tool with comprehensive risk analysis
|
Thank you for your pull request and welcome to our community! To contribute, please sign the Oracle Contributor Agreement (OCA).
To sign the OCA, please create an Oracle account and sign the OCA in Oracle's Contributor Agreement Application. When signing the OCA, please provide your GitHub username. After signing the OCA and getting an OCA approval from Oracle, this PR will be automatically updated. If you are an Oracle employee, please make sure that you are a member of the main Oracle GitHub organization, and your membership in this organization is public. |
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.
Summary
This PR adds a new
suggest_portfolio_hedgeagent tool to the Agentic Financial Service Assistant (AFSA) demo app. The tool analyses a portfolio's risk factors and recommends specific hedging instruments, giving financial advisors an AI-assisted starting point for downside protection conversations.It also establishes the project's first testing infrastructure — from scratch — covering the full stack from the backend recommendation engine through to the React frontend component rendering.
What's New
New Agent Tool:
suggest_portfolio_hedgeUsers can now ask the agent questions like:
How it works:
A single Oracle SQL query (Relational + JSON + window-function analytics) fetches the full risk picture for an account — sector breakdown, regional exposure, asset class distribution, high-risk positions (rating ≥ 7), and ESG mandate from JSON metadata — in one statement. A pure-Python recommendation engine then maps detected risk factors to a curated catalogue of hedging instruments.
Risk dimensions covered (controlled by the optional
risk_focusparameter):marketsectorregionalcurrencyallESG-aware: accounts with an ESG mandate have leveraged and high-risk instruments automatically filtered out of recommendations.
Both architecture modes supported: Oracle converged mode (
tools.py) and PostgreSQL sprawl mode (sprawl_tools.py).Test Suite (first tests in this project)
Sets up testing infrastructure from scratch and adds 97 tests across the full stack.
Backend — pytest (
backend/tests/)test_hedge_recommendations.py_build_hedge_recommendations: output structure, all four risk dimensions, ESG filtering,risk_focusscoping, edge cases (empty rows,NonePCTs, malformed JSON)test_suggest_portfolio_hedge_integration.py_suggest_portfolio_hedgepipeline with a mockexecute_query— JSON output, error messages, default args, query logger invocation, SQL param bindingtest_tool_schema.pyTOOL_SCHEMASandPRELOADED_TOOLS, required/optional params, enum values, dispatcher routingRun with:
cd backend pip install -r requirements.txt pytest tests/ -vFrontend — Vitest + @testing-library/react (
frontend/src/test/)ToolCallBubble.test.jsxchatReducer.test.jsADD_TOOL_CALL_START→UPDATE_TOOL_CALL→AGENT_COMPLETE, tool calls attached to assistant messages,isLoadingcleared, multi-tool isolationRun with:
Files Changed
Checklist
ruff checkandprettier --check