WIP: local TradingAgents customizations through 2026-04-13

Bulk commit of accumulated local changes on the dtarkent2-sys fork.
Spans agents, dataflows, llm_clients, graph orchestration, CLI, and
docs. Primary work areas:

- llm_clients/ — multi-LLM client layer (anthropic, google, openai,
  factory, base, validators) for swappable provider support
- dataflows/alpaca_data.py — Alpaca integration alongside existing
  alpha_vantage and y_finance flows
- agents/structured/ — portfolio, scoring, and tier1/2/3 layers
- agents/analysts, researchers, risk_mgmt — local prompt and logic
  customizations
- graph/ — orchestration tweaks (parallel_analysts, propagation,
  reflection, signal_processing, trading_graph)
- alembic scaffolding inherited from prior commit
- chainlit web UI design notes in docs/plans/

This is a single WIP snapshot to preserve work before any upstream
merge. History can be cleaned up with interactive rebase later.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
dtarkent2-sys 2026-04-13 22:01:00 -04:00
parent b863d0939c
commit 8c48c3cffd
77 changed files with 17628 additions and 17628 deletions

View File

@ -1,8 +1,8 @@
.git .git
eval_results eval_results
nvda_output*.txt nvda_output*.txt
docs docs
uv.lock uv.lock
__pycache__ __pycache__
.env .env
.env.example .env.example

View File

@ -1,6 +1,6 @@
# LLM Providers (set the one you use) # LLM Providers (set the one you use)
OPENAI_API_KEY= OPENAI_API_KEY=
GOOGLE_API_KEY= GOOGLE_API_KEY=
ANTHROPIC_API_KEY= ANTHROPIC_API_KEY=
XAI_API_KEY= XAI_API_KEY=
OPENROUTER_API_KEY= OPENROUTER_API_KEY=

440
.gitignore vendored
View File

@ -1,221 +1,221 @@
# Byte-compiled / optimized / DLL files # Byte-compiled / optimized / DLL files
__pycache__/ __pycache__/
*.py[codz] *.py[codz]
*$py.class *$py.class
# C extensions # C extensions
*.so *.so
# Distribution / packaging # Distribution / packaging
.Python .Python
build/ build/
develop-eggs/ develop-eggs/
dist/ dist/
downloads/ downloads/
eggs/ eggs/
.eggs/ .eggs/
lib/ lib/
lib64/ lib64/
parts/ parts/
sdist/ sdist/
var/ var/
wheels/ wheels/
share/python-wheels/ share/python-wheels/
*.egg-info/ *.egg-info/
.installed.cfg .installed.cfg
*.egg *.egg
MANIFEST MANIFEST
# PyInstaller # PyInstaller
# Usually these files are written by a python script from a template # Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it. # before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest *.manifest
*.spec *.spec
# Installer logs # Installer logs
pip-log.txt pip-log.txt
pip-delete-this-directory.txt pip-delete-this-directory.txt
# Unit test / coverage reports # Unit test / coverage reports
htmlcov/ htmlcov/
.tox/ .tox/
.nox/ .nox/
.coverage .coverage
.coverage.* .coverage.*
.cache .cache
nosetests.xml nosetests.xml
coverage.xml coverage.xml
*.cover *.cover
*.py.cover *.py.cover
.hypothesis/ .hypothesis/
.pytest_cache/ .pytest_cache/
cover/ cover/
# Translations # Translations
*.mo *.mo
*.pot *.pot
# Django stuff: # Django stuff:
*.log *.log
local_settings.py local_settings.py
db.sqlite3 db.sqlite3
db.sqlite3-journal db.sqlite3-journal
# Flask stuff: # Flask stuff:
instance/ instance/
.webassets-cache .webassets-cache
# Scrapy stuff: # Scrapy stuff:
.scrapy .scrapy
# Sphinx documentation # Sphinx documentation
docs/_build/ docs/_build/
# PyBuilder # PyBuilder
.pybuilder/ .pybuilder/
target/ target/
# Jupyter Notebook # Jupyter Notebook
.ipynb_checkpoints .ipynb_checkpoints
# IPython # IPython
profile_default/ profile_default/
ipython_config.py ipython_config.py
# pyenv # pyenv
# For a library or package, you might want to ignore these files since the code is # For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in: # intended to run in multiple environments; otherwise, check them in:
# .python-version # .python-version
# pipenv # pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies # However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not # having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies. # install all needed dependencies.
# Pipfile.lock # Pipfile.lock
# UV # UV
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control. # Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more # This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries. # commonly ignored for libraries.
# uv.lock # uv.lock
# poetry # poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more # This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries. # commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
# poetry.lock # poetry.lock
# poetry.toml # poetry.toml
# pdm # pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
# pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python. # pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
# https://pdm-project.org/en/latest/usage/project/#working-with-version-control # https://pdm-project.org/en/latest/usage/project/#working-with-version-control
# pdm.lock # pdm.lock
# pdm.toml # pdm.toml
.pdm-python .pdm-python
.pdm-build/ .pdm-build/
# pixi # pixi
# Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control. # Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
# pixi.lock # pixi.lock
# Pixi creates a virtual environment in the .pixi directory, just like venv module creates one # Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
# in the .venv directory. It is recommended not to include this directory in version control. # in the .venv directory. It is recommended not to include this directory in version control.
.pixi .pixi
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/ __pypackages__/
# Celery stuff # Celery stuff
celerybeat-schedule celerybeat-schedule
celerybeat.pid celerybeat.pid
# Redis # Redis
*.rdb *.rdb
*.aof *.aof
*.pid *.pid
# RabbitMQ # RabbitMQ
mnesia/ mnesia/
rabbitmq/ rabbitmq/
rabbitmq-data/ rabbitmq-data/
# ActiveMQ # ActiveMQ
activemq-data/ activemq-data/
# SageMath parsed files # SageMath parsed files
*.sage.py *.sage.py
# Environments # Environments
.env .env
.envrc .envrc
.venv .venv
env/ env/
venv/ venv/
ENV/ ENV/
env.bak/ env.bak/
venv.bak/ venv.bak/
# Spyder project settings # Spyder project settings
.spyderproject .spyderproject
.spyproject .spyproject
# Rope project settings # Rope project settings
.ropeproject .ropeproject
# mkdocs documentation # mkdocs documentation
/site /site
# mypy # mypy
.mypy_cache/ .mypy_cache/
.dmypy.json .dmypy.json
dmypy.json dmypy.json
# Pyre type checker # Pyre type checker
.pyre/ .pyre/
# pytype static type analyzer # pytype static type analyzer
.pytype/ .pytype/
# Cython debug symbols # Cython debug symbols
cython_debug/ cython_debug/
# PyCharm # PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear # and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder. # option (not recommended) you can uncomment the following to ignore the entire idea folder.
# .idea/ # .idea/
# Abstra # Abstra
# Abstra is an AI-powered process automation framework. # Abstra is an AI-powered process automation framework.
# Ignore directories containing user credentials, local state, and settings. # Ignore directories containing user credentials, local state, and settings.
# Learn more at https://abstra.io/docs # Learn more at https://abstra.io/docs
.abstra/ .abstra/
# Visual Studio Code # Visual Studio Code
# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore # Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
# that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore # that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
# and can be added to the global gitignore or merged into this file. However, if you prefer, # and can be added to the global gitignore or merged into this file. However, if you prefer,
# you could uncomment the following to ignore the entire vscode folder # you could uncomment the following to ignore the entire vscode folder
# .vscode/ # .vscode/
# Ruff stuff: # Ruff stuff:
.ruff_cache/ .ruff_cache/
# PyPI configuration file # PyPI configuration file
.pypirc .pypirc
# Marimo # Marimo
marimo/_static/ marimo/_static/
marimo/_lsp/ marimo/_lsp/
__marimo__/ __marimo__/
# Streamlit # Streamlit
.streamlit/secrets.toml .streamlit/secrets.toml
# Cache # Cache
**/data_cache/ **/data_cache/
eval_results/ eval_results/
.env.railway .env.railway

View File

@ -1,4 +1,4 @@
.git .git
__pycache__ __pycache__
*.pyc *.pyc
.env .env

402
LICENSE
View File

@ -1,201 +1,201 @@
Apache License Apache License
Version 2.0, January 2004 Version 2.0, January 2004
http://www.apache.org/licenses/ http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions. 1. Definitions.
"License" shall mean the terms and conditions for use, reproduction, "License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document. and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by "Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License. the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all "Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition, control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the "control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity. outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity "You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License. exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications, "Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation including but not limited to software source code, documentation
source, and configuration files. source, and configuration files.
"Object" form shall mean any form resulting from mechanical "Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation, not limited to compiled object code, generated documentation,
and conversions to other media types. and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or "Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work copyright notice that is included in or attached to the work
(an example is provided in the Appendix below). (an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object "Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of, separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof. the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including "Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted" the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems, communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution." designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity "Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work. subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of 2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual, this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of, copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form. Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of 3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual, this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made, (except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work, use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s) Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate granted to You under this License for that Work shall terminate
as of the date such litigation is filed. as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the 4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You modifications, and in Source or Object form, provided that You
meet the following conditions: meet the following conditions:
(a) You must give any other recipients of the Work or (a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices (b) You must cause any modified files to carry prominent notices
stating that You changed the files; and stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works (c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work, attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of excluding those notices that do not pertain to any part of
the Derivative Works; and the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its (d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or, documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed that such additional attribution notices cannot be construed
as modifying the License. as modifying the License.
You may add Your own copyright statement to Your modifications and You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use, for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License. the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise, 5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions. this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions. with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade 6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor, names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file. origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or 7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS, Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License. risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory, 8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise, whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special, liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill, Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages. has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing 9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer, the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity, and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify, of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability. of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work. APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]" boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier same "printed page" as the copyright notice for easier
identification within third-party archives. identification within third-party archives.
Copyright [yyyy] [name of copyright owner] Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
You may obtain a copy of the License at You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0 http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. limitations under the License.

438
README.md
View File

@ -1,219 +1,219 @@
<p align="center"> <p align="center">
<img src="assets/TauricResearch.png" style="width: 60%; height: auto;"> <img src="assets/TauricResearch.png" style="width: 60%; height: auto;">
</p> </p>
<div align="center" style="line-height: 1;"> <div align="center" style="line-height: 1;">
<a href="https://arxiv.org/abs/2412.20138" target="_blank"><img alt="arXiv" src="https://img.shields.io/badge/arXiv-2412.20138-B31B1B?logo=arxiv"/></a> <a href="https://arxiv.org/abs/2412.20138" target="_blank"><img alt="arXiv" src="https://img.shields.io/badge/arXiv-2412.20138-B31B1B?logo=arxiv"/></a>
<a href="https://discord.com/invite/hk9PGKShPK" target="_blank"><img alt="Discord" src="https://img.shields.io/badge/Discord-TradingResearch-7289da?logo=discord&logoColor=white&color=7289da"/></a> <a href="https://discord.com/invite/hk9PGKShPK" target="_blank"><img alt="Discord" src="https://img.shields.io/badge/Discord-TradingResearch-7289da?logo=discord&logoColor=white&color=7289da"/></a>
<a href="./assets/wechat.png" target="_blank"><img alt="WeChat" src="https://img.shields.io/badge/WeChat-TauricResearch-brightgreen?logo=wechat&logoColor=white"/></a> <a href="./assets/wechat.png" target="_blank"><img alt="WeChat" src="https://img.shields.io/badge/WeChat-TauricResearch-brightgreen?logo=wechat&logoColor=white"/></a>
<a href="https://x.com/TauricResearch" target="_blank"><img alt="X Follow" src="https://img.shields.io/badge/X-TauricResearch-white?logo=x&logoColor=white"/></a> <a href="https://x.com/TauricResearch" target="_blank"><img alt="X Follow" src="https://img.shields.io/badge/X-TauricResearch-white?logo=x&logoColor=white"/></a>
<br> <br>
<a href="https://github.com/TauricResearch/" target="_blank"><img alt="Community" src="https://img.shields.io/badge/Join_GitHub_Community-TauricResearch-14C290?logo=discourse"/></a> <a href="https://github.com/TauricResearch/" target="_blank"><img alt="Community" src="https://img.shields.io/badge/Join_GitHub_Community-TauricResearch-14C290?logo=discourse"/></a>
</div> </div>
<div align="center"> <div align="center">
<!-- Keep these links. Translations will automatically update with the README. --> <!-- Keep these links. Translations will automatically update with the README. -->
<a href="https://www.readme-i18n.com/TauricResearch/TradingAgents?lang=de">Deutsch</a> | <a href="https://www.readme-i18n.com/TauricResearch/TradingAgents?lang=de">Deutsch</a> |
<a href="https://www.readme-i18n.com/TauricResearch/TradingAgents?lang=es">Español</a> | <a href="https://www.readme-i18n.com/TauricResearch/TradingAgents?lang=es">Español</a> |
<a href="https://www.readme-i18n.com/TauricResearch/TradingAgents?lang=fr">français</a> | <a href="https://www.readme-i18n.com/TauricResearch/TradingAgents?lang=fr">français</a> |
<a href="https://www.readme-i18n.com/TauricResearch/TradingAgents?lang=ja">日本語</a> | <a href="https://www.readme-i18n.com/TauricResearch/TradingAgents?lang=ja">日本語</a> |
<a href="https://www.readme-i18n.com/TauricResearch/TradingAgents?lang=ko">한국어</a> | <a href="https://www.readme-i18n.com/TauricResearch/TradingAgents?lang=ko">한국어</a> |
<a href="https://www.readme-i18n.com/TauricResearch/TradingAgents?lang=pt">Português</a> | <a href="https://www.readme-i18n.com/TauricResearch/TradingAgents?lang=pt">Português</a> |
<a href="https://www.readme-i18n.com/TauricResearch/TradingAgents?lang=ru">Русский</a> | <a href="https://www.readme-i18n.com/TauricResearch/TradingAgents?lang=ru">Русский</a> |
<a href="https://www.readme-i18n.com/TauricResearch/TradingAgents?lang=zh">中文</a> <a href="https://www.readme-i18n.com/TauricResearch/TradingAgents?lang=zh">中文</a>
</div> </div>
--- ---
# TradingAgents: Multi-Agents LLM Financial Trading Framework # TradingAgents: Multi-Agents LLM Financial Trading Framework
## News ## News
- [2026-02] **TradingAgents v0.2.0** released with multi-provider LLM support (GPT-5.x, Gemini 3.x, Claude 4.x, Grok 4.x) and improved system architecture. - [2026-02] **TradingAgents v0.2.0** released with multi-provider LLM support (GPT-5.x, Gemini 3.x, Claude 4.x, Grok 4.x) and improved system architecture.
- [2026-01] **Trading-R1** [Technical Report](https://arxiv.org/abs/2509.11420) released, with [Terminal](https://github.com/TauricResearch/Trading-R1) expected to land soon. - [2026-01] **Trading-R1** [Technical Report](https://arxiv.org/abs/2509.11420) released, with [Terminal](https://github.com/TauricResearch/Trading-R1) expected to land soon.
<div align="center"> <div align="center">
<a href="https://www.star-history.com/#TauricResearch/TradingAgents&Date"> <a href="https://www.star-history.com/#TauricResearch/TradingAgents&Date">
<picture> <picture>
<source media="(prefers-color-scheme: dark)" srcset="https://api.star-history.com/svg?repos=TauricResearch/TradingAgents&type=Date&theme=dark" /> <source media="(prefers-color-scheme: dark)" srcset="https://api.star-history.com/svg?repos=TauricResearch/TradingAgents&type=Date&theme=dark" />
<source media="(prefers-color-scheme: light)" srcset="https://api.star-history.com/svg?repos=TauricResearch/TradingAgents&type=Date" /> <source media="(prefers-color-scheme: light)" srcset="https://api.star-history.com/svg?repos=TauricResearch/TradingAgents&type=Date" />
<img alt="TradingAgents Star History" src="https://api.star-history.com/svg?repos=TauricResearch/TradingAgents&type=Date" style="width: 80%; height: auto;" /> <img alt="TradingAgents Star History" src="https://api.star-history.com/svg?repos=TauricResearch/TradingAgents&type=Date" style="width: 80%; height: auto;" />
</picture> </picture>
</a> </a>
</div> </div>
> 🎉 **TradingAgents** officially released! We have received numerous inquiries about the work, and we would like to express our thanks for the enthusiasm in our community. > 🎉 **TradingAgents** officially released! We have received numerous inquiries about the work, and we would like to express our thanks for the enthusiasm in our community.
> >
> So we decided to fully open-source the framework. Looking forward to building impactful projects with you! > So we decided to fully open-source the framework. Looking forward to building impactful projects with you!
<div align="center"> <div align="center">
🚀 [TradingAgents](#tradingagents-framework) | ⚡ [Installation & CLI](#installation-and-cli) | 🎬 [Demo](https://www.youtube.com/watch?v=90gr5lwjIho) | 📦 [Package Usage](#tradingagents-package) | 🤝 [Contributing](#contributing) | 📄 [Citation](#citation) 🚀 [TradingAgents](#tradingagents-framework) | ⚡ [Installation & CLI](#installation-and-cli) | 🎬 [Demo](https://www.youtube.com/watch?v=90gr5lwjIho) | 📦 [Package Usage](#tradingagents-package) | 🤝 [Contributing](#contributing) | 📄 [Citation](#citation)
</div> </div>
## TradingAgents Framework ## TradingAgents Framework
TradingAgents is a multi-agent trading framework that mirrors the dynamics of real-world trading firms. By deploying specialized LLM-powered agents: from fundamental analysts, sentiment experts, and technical analysts, to trader, risk management team, the platform collaboratively evaluates market conditions and informs trading decisions. Moreover, these agents engage in dynamic discussions to pinpoint the optimal strategy. TradingAgents is a multi-agent trading framework that mirrors the dynamics of real-world trading firms. By deploying specialized LLM-powered agents: from fundamental analysts, sentiment experts, and technical analysts, to trader, risk management team, the platform collaboratively evaluates market conditions and informs trading decisions. Moreover, these agents engage in dynamic discussions to pinpoint the optimal strategy.
<p align="center"> <p align="center">
<img src="assets/schema.png" style="width: 100%; height: auto;"> <img src="assets/schema.png" style="width: 100%; height: auto;">
</p> </p>
> TradingAgents framework is designed for research purposes. Trading performance may vary based on many factors, including the chosen backbone language models, model temperature, trading periods, the quality of data, and other non-deterministic factors. [It is not intended as financial, investment, or trading advice.](https://tauric.ai/disclaimer/) > TradingAgents framework is designed for research purposes. Trading performance may vary based on many factors, including the chosen backbone language models, model temperature, trading periods, the quality of data, and other non-deterministic factors. [It is not intended as financial, investment, or trading advice.](https://tauric.ai/disclaimer/)
Our framework decomposes complex trading tasks into specialized roles. This ensures the system achieves a robust, scalable approach to market analysis and decision-making. Our framework decomposes complex trading tasks into specialized roles. This ensures the system achieves a robust, scalable approach to market analysis and decision-making.
### Analyst Team ### Analyst Team
- Fundamentals Analyst: Evaluates company financials and performance metrics, identifying intrinsic values and potential red flags. - Fundamentals Analyst: Evaluates company financials and performance metrics, identifying intrinsic values and potential red flags.
- Sentiment Analyst: Analyzes social media and public sentiment using sentiment scoring algorithms to gauge short-term market mood. - Sentiment Analyst: Analyzes social media and public sentiment using sentiment scoring algorithms to gauge short-term market mood.
- News Analyst: Monitors global news and macroeconomic indicators, interpreting the impact of events on market conditions. - News Analyst: Monitors global news and macroeconomic indicators, interpreting the impact of events on market conditions.
- Technical Analyst: Utilizes technical indicators (like MACD and RSI) to detect trading patterns and forecast price movements. - Technical Analyst: Utilizes technical indicators (like MACD and RSI) to detect trading patterns and forecast price movements.
<p align="center"> <p align="center">
<img src="assets/analyst.png" width="100%" style="display: inline-block; margin: 0 2%;"> <img src="assets/analyst.png" width="100%" style="display: inline-block; margin: 0 2%;">
</p> </p>
### Researcher Team ### Researcher Team
- Comprises both bullish and bearish researchers who critically assess the insights provided by the Analyst Team. Through structured debates, they balance potential gains against inherent risks. - Comprises both bullish and bearish researchers who critically assess the insights provided by the Analyst Team. Through structured debates, they balance potential gains against inherent risks.
<p align="center"> <p align="center">
<img src="assets/researcher.png" width="70%" style="display: inline-block; margin: 0 2%;"> <img src="assets/researcher.png" width="70%" style="display: inline-block; margin: 0 2%;">
</p> </p>
### Trader Agent ### Trader Agent
- Composes reports from the analysts and researchers to make informed trading decisions. It determines the timing and magnitude of trades based on comprehensive market insights. - Composes reports from the analysts and researchers to make informed trading decisions. It determines the timing and magnitude of trades based on comprehensive market insights.
<p align="center"> <p align="center">
<img src="assets/trader.png" width="70%" style="display: inline-block; margin: 0 2%;"> <img src="assets/trader.png" width="70%" style="display: inline-block; margin: 0 2%;">
</p> </p>
### Risk Management and Portfolio Manager ### Risk Management and Portfolio Manager
- Continuously evaluates portfolio risk by assessing market volatility, liquidity, and other risk factors. The risk management team evaluates and adjusts trading strategies, providing assessment reports to the Portfolio Manager for final decision. - Continuously evaluates portfolio risk by assessing market volatility, liquidity, and other risk factors. The risk management team evaluates and adjusts trading strategies, providing assessment reports to the Portfolio Manager for final decision.
- The Portfolio Manager approves/rejects the transaction proposal. If approved, the order will be sent to the simulated exchange and executed. - The Portfolio Manager approves/rejects the transaction proposal. If approved, the order will be sent to the simulated exchange and executed.
<p align="center"> <p align="center">
<img src="assets/risk.png" width="70%" style="display: inline-block; margin: 0 2%;"> <img src="assets/risk.png" width="70%" style="display: inline-block; margin: 0 2%;">
</p> </p>
## Installation and CLI ## Installation and CLI
### Installation ### Installation
Clone TradingAgents: Clone TradingAgents:
```bash ```bash
git clone https://github.com/TauricResearch/TradingAgents.git git clone https://github.com/TauricResearch/TradingAgents.git
cd TradingAgents cd TradingAgents
``` ```
Create a virtual environment in any of your favorite environment managers: Create a virtual environment in any of your favorite environment managers:
```bash ```bash
conda create -n tradingagents python=3.13 conda create -n tradingagents python=3.13
conda activate tradingagents conda activate tradingagents
``` ```
Install dependencies: Install dependencies:
```bash ```bash
pip install -r requirements.txt pip install -r requirements.txt
``` ```
### Required APIs ### Required APIs
TradingAgents supports multiple LLM providers. Set the API key for your chosen provider: TradingAgents supports multiple LLM providers. Set the API key for your chosen provider:
```bash ```bash
export OPENAI_API_KEY=... # OpenAI (GPT) export OPENAI_API_KEY=... # OpenAI (GPT)
export GOOGLE_API_KEY=... # Google (Gemini) export GOOGLE_API_KEY=... # Google (Gemini)
export ANTHROPIC_API_KEY=... # Anthropic (Claude) export ANTHROPIC_API_KEY=... # Anthropic (Claude)
export XAI_API_KEY=... # xAI (Grok) export XAI_API_KEY=... # xAI (Grok)
export OPENROUTER_API_KEY=... # OpenRouter export OPENROUTER_API_KEY=... # OpenRouter
export ALPHA_VANTAGE_API_KEY=... # Alpha Vantage export ALPHA_VANTAGE_API_KEY=... # Alpha Vantage
``` ```
For local models, configure Ollama with `llm_provider: "ollama"` in your config. For local models, configure Ollama with `llm_provider: "ollama"` in your config.
Alternatively, copy `.env.example` to `.env` and fill in your keys: Alternatively, copy `.env.example` to `.env` and fill in your keys:
```bash ```bash
cp .env.example .env cp .env.example .env
``` ```
### CLI Usage ### CLI Usage
You can also try out the CLI directly by running: You can also try out the CLI directly by running:
```bash ```bash
python -m cli.main python -m cli.main
``` ```
You will see a screen where you can select your desired tickers, date, LLMs, research depth, etc. You will see a screen where you can select your desired tickers, date, LLMs, research depth, etc.
<p align="center"> <p align="center">
<img src="assets/cli/cli_init.png" width="100%" style="display: inline-block; margin: 0 2%;"> <img src="assets/cli/cli_init.png" width="100%" style="display: inline-block; margin: 0 2%;">
</p> </p>
An interface will appear showing results as they load, letting you track the agent's progress as it runs. An interface will appear showing results as they load, letting you track the agent's progress as it runs.
<p align="center"> <p align="center">
<img src="assets/cli/cli_news.png" width="100%" style="display: inline-block; margin: 0 2%;"> <img src="assets/cli/cli_news.png" width="100%" style="display: inline-block; margin: 0 2%;">
</p> </p>
<p align="center"> <p align="center">
<img src="assets/cli/cli_transaction.png" width="100%" style="display: inline-block; margin: 0 2%;"> <img src="assets/cli/cli_transaction.png" width="100%" style="display: inline-block; margin: 0 2%;">
</p> </p>
## TradingAgents Package ## TradingAgents Package
### Implementation Details ### Implementation Details
We built TradingAgents with LangGraph to ensure flexibility and modularity. The framework supports multiple LLM providers: OpenAI, Google, Anthropic, xAI, OpenRouter, and Ollama. We built TradingAgents with LangGraph to ensure flexibility and modularity. The framework supports multiple LLM providers: OpenAI, Google, Anthropic, xAI, OpenRouter, and Ollama.
### Python Usage ### Python Usage
To use TradingAgents inside your code, you can import the `tradingagents` module and initialize a `TradingAgentsGraph()` object. The `.propagate()` function will return a decision. You can run `main.py`, here's also a quick example: To use TradingAgents inside your code, you can import the `tradingagents` module and initialize a `TradingAgentsGraph()` object. The `.propagate()` function will return a decision. You can run `main.py`, here's also a quick example:
```python ```python
from tradingagents.graph.trading_graph import TradingAgentsGraph from tradingagents.graph.trading_graph import TradingAgentsGraph
from tradingagents.default_config import DEFAULT_CONFIG from tradingagents.default_config import DEFAULT_CONFIG
ta = TradingAgentsGraph(debug=True, config=DEFAULT_CONFIG.copy()) ta = TradingAgentsGraph(debug=True, config=DEFAULT_CONFIG.copy())
# forward propagate # forward propagate
_, decision = ta.propagate("NVDA", "2026-01-15") _, decision = ta.propagate("NVDA", "2026-01-15")
print(decision) print(decision)
``` ```
You can also adjust the default configuration to set your own choice of LLMs, debate rounds, etc. You can also adjust the default configuration to set your own choice of LLMs, debate rounds, etc.
```python ```python
from tradingagents.graph.trading_graph import TradingAgentsGraph from tradingagents.graph.trading_graph import TradingAgentsGraph
from tradingagents.default_config import DEFAULT_CONFIG from tradingagents.default_config import DEFAULT_CONFIG
config = DEFAULT_CONFIG.copy() config = DEFAULT_CONFIG.copy()
config["llm_provider"] = "openai" # openai, google, anthropic, xai, openrouter, ollama config["llm_provider"] = "openai" # openai, google, anthropic, xai, openrouter, ollama
config["deep_think_llm"] = "gpt-5.2" # Model for complex reasoning config["deep_think_llm"] = "gpt-5.2" # Model for complex reasoning
config["quick_think_llm"] = "gpt-5-mini" # Model for quick tasks config["quick_think_llm"] = "gpt-5-mini" # Model for quick tasks
config["max_debate_rounds"] = 2 config["max_debate_rounds"] = 2
ta = TradingAgentsGraph(debug=True, config=config) ta = TradingAgentsGraph(debug=True, config=config)
_, decision = ta.propagate("NVDA", "2026-01-15") _, decision = ta.propagate("NVDA", "2026-01-15")
print(decision) print(decision)
``` ```
See `tradingagents/default_config.py` for all configuration options. See `tradingagents/default_config.py` for all configuration options.
## Contributing ## Contributing
We welcome contributions from the community! Whether it's fixing a bug, improving documentation, or suggesting a new feature, your input helps make this project better. If you are interested in this line of research, please consider joining our open-source financial AI research community [Tauric Research](https://tauric.ai/). We welcome contributions from the community! Whether it's fixing a bug, improving documentation, or suggesting a new feature, your input helps make this project better. If you are interested in this line of research, please consider joining our open-source financial AI research community [Tauric Research](https://tauric.ai/).
## Citation ## Citation
Please reference our work if you find *TradingAgents* provides you with some help :) Please reference our work if you find *TradingAgents* provides you with some help :)
``` ```
@misc{xiao2025tradingagentsmultiagentsllmfinancial, @misc{xiao2025tradingagentsmultiagentsllmfinancial,
title={TradingAgents: Multi-Agents LLM Financial Trading Framework}, title={TradingAgents: Multi-Agents LLM Financial Trading Framework},
author={Yijia Xiao and Edward Sun and Di Luo and Wei Wang}, author={Yijia Xiao and Edward Sun and Di Luo and Wei Wang},
year={2025}, year={2025},
eprint={2412.20138}, eprint={2412.20138},
archivePrefix={arXiv}, archivePrefix={arXiv},
primaryClass={q-fin.TR}, primaryClass={q-fin.TR},
url={https://arxiv.org/abs/2412.20138}, url={https://arxiv.org/abs/2412.20138},
} }
``` ```

984
app.py
View File

@ -1,492 +1,492 @@
"""FastAPI SSE backend for the structured equity ranking engine.""" """FastAPI SSE backend for the structured equity ranking engine."""
from pathlib import Path from pathlib import Path
from dotenv import load_dotenv from dotenv import load_dotenv
load_dotenv(Path(__file__).parent / ".env") load_dotenv(Path(__file__).parent / ".env")
import logging import logging
import os import os
import re import re
import time import time
import uuid import uuid
import asyncio import asyncio
import json import json
import traceback as _tb import traceback as _tb
from datetime import date from datetime import date
from fastapi import FastAPI, HTTPException, Request, Depends from fastapi import FastAPI, HTTPException, Request, Depends
logging.basicConfig( logging.basicConfig(
level=logging.INFO, level=logging.INFO,
format="%(asctime)s %(levelname)s %(name)s %(message)s", format="%(asctime)s %(levelname)s %(name)s %(message)s",
) )
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
from fastapi.middleware.cors import CORSMiddleware from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel from pydantic import BaseModel
from sse_starlette.sse import EventSourceResponse from sse_starlette.sse import EventSourceResponse
# If using Groq (or other OpenAI-compatible), set OPENAI_API_KEY for langchain # If using Groq (or other OpenAI-compatible), set OPENAI_API_KEY for langchain
if not os.environ.get("OPENAI_API_KEY"): if not os.environ.get("OPENAI_API_KEY"):
groq_key = os.environ.get("GROQ_API_KEY", "") groq_key = os.environ.get("GROQ_API_KEY", "")
if groq_key: if groq_key:
os.environ["OPENAI_API_KEY"] = groq_key os.environ["OPENAI_API_KEY"] = groq_key
from tradingagents.graph.trading_graph import TradingAgentsGraph from tradingagents.graph.trading_graph import TradingAgentsGraph
from tradingagents.default_config import DEFAULT_CONFIG from tradingagents.default_config import DEFAULT_CONFIG
app = FastAPI(title="TradingAgents Structured Pipeline") app = FastAPI(title="TradingAgents Structured Pipeline")
# --- CORS --- # --- CORS ---
_cors_env = os.getenv("CORS_ORIGINS", "") _cors_env = os.getenv("CORS_ORIGINS", "")
_cors_origins = [o.strip() for o in _cors_env.split(",") if o.strip()] if _cors_env else ["*"] _cors_origins = [o.strip() for o in _cors_env.split(",") if o.strip()] if _cors_env else ["*"]
app.add_middleware( app.add_middleware(
CORSMiddleware, CORSMiddleware,
allow_origins=_cors_origins, allow_origins=_cors_origins,
allow_methods=["*"], allow_methods=["*"],
allow_headers=["*"], allow_headers=["*"],
) )
# --- Auth --- # --- Auth ---
_API_KEY = os.getenv("AGENTS_API_KEY", "") _API_KEY = os.getenv("AGENTS_API_KEY", "")
async def verify_api_key(request: Request): async def verify_api_key(request: Request):
if not _API_KEY: if not _API_KEY:
return return
auth = request.headers.get("Authorization", "") auth = request.headers.get("Authorization", "")
if auth != f"Bearer {_API_KEY}": if auth != f"Bearer {_API_KEY}":
raise HTTPException(401, "Invalid or missing API key") raise HTTPException(401, "Invalid or missing API key")
# --- Concurrency --- # --- Concurrency ---
MAX_CONCURRENT = int(os.getenv("MAX_CONCURRENT_ANALYSES", "3")) MAX_CONCURRENT = int(os.getenv("MAX_CONCURRENT_ANALYSES", "3"))
_semaphore = asyncio.Semaphore(MAX_CONCURRENT) _semaphore = asyncio.Semaphore(MAX_CONCURRENT)
# --- Event buffer cap --- # --- Event buffer cap ---
MAX_EVENTS_PER_ANALYSIS = 5000 MAX_EVENTS_PER_ANALYSIS = 5000
analyses: dict[str, dict] = {} analyses: dict[str, dict] = {}
def _append_event(state: dict, evt: dict): def _append_event(state: dict, evt: dict):
"""Append an event to the analysis state, enforcing the buffer cap.""" """Append an event to the analysis state, enforcing the buffer cap."""
events = state["events"] events = state["events"]
events.append(evt) events.append(evt)
if len(events) > MAX_EVENTS_PER_ANALYSIS: if len(events) > MAX_EVENTS_PER_ANALYSIS:
# Drop oldest events, keep the last MAX_EVENTS_PER_ANALYSIS # Drop oldest events, keep the last MAX_EVENTS_PER_ANALYSIS
state["events"] = events[-MAX_EVENTS_PER_ANALYSIS:] state["events"] = events[-MAX_EVENTS_PER_ANALYSIS:]
class AnalyzeRequest(BaseModel): class AnalyzeRequest(BaseModel):
ticker: str ticker: str
date: str | None = None date: str | None = None
def build_config(): def build_config():
"""Build TradingAgents config from env vars.""" """Build TradingAgents config from env vars."""
config = DEFAULT_CONFIG.copy() config = DEFAULT_CONFIG.copy()
config["llm_provider"] = os.getenv("LLM_PROVIDER", "openai") config["llm_provider"] = os.getenv("LLM_PROVIDER", "openai")
config["deep_think_llm"] = os.getenv("DEEP_THINK_MODEL", "deepseek-v3.1:671b-cloud") config["deep_think_llm"] = os.getenv("DEEP_THINK_MODEL", "deepseek-v3.1:671b-cloud")
config["quick_think_llm"] = os.getenv("QUICK_THINK_MODEL", "deepseek-v3.1:671b-cloud") config["quick_think_llm"] = os.getenv("QUICK_THINK_MODEL", "deepseek-v3.1:671b-cloud")
config["backend_url"] = os.getenv("LLM_BASE_URL", "https://ollama.com/v1") config["backend_url"] = os.getenv("LLM_BASE_URL", "https://ollama.com/v1")
config["max_debate_rounds"] = 1 config["max_debate_rounds"] = 1
config["max_risk_discuss_rounds"] = 1 config["max_risk_discuss_rounds"] = 1
config["data_vendors"] = { config["data_vendors"] = {
"core_stock_apis": "yfinance", "core_stock_apis": "yfinance",
"technical_indicators": "yfinance", "technical_indicators": "yfinance",
"fundamental_data": "yfinance", "fundamental_data": "yfinance",
"news_data": "yfinance", "news_data": "yfinance",
} }
logger.info( logger.info(
"config_built provider=%s deep=%s quick=%s url=%s", "config_built provider=%s deep=%s quick=%s url=%s",
config['llm_provider'], config['deep_think_llm'], config['llm_provider'], config['deep_think_llm'],
config['quick_think_llm'], config['backend_url'], config['quick_think_llm'], config['backend_url'],
) )
return config return config
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# Stage/agent mapping for SSE events # Stage/agent mapping for SSE events
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# Maps state field → (agent display name, pipeline stage) # Maps state field → (agent display name, pipeline stage)
FIELD_AGENT_MAP = { FIELD_AGENT_MAP = {
"validation": ("Validation", "validation"), "validation": ("Validation", "validation"),
"company_card": ("Company Card", "validation"), "company_card": ("Company Card", "validation"),
"macro": ("Macro Regime", "tier1"), "macro": ("Macro Regime", "tier1"),
"liquidity": ("Liquidity", "tier1"), "liquidity": ("Liquidity", "tier1"),
"business_quality": ("Business Quality", "tier2"), "business_quality": ("Business Quality", "tier2"),
"institutional_flow": ("Institutional Flow", "tier2"), "institutional_flow": ("Institutional Flow", "tier2"),
"valuation": ("Valuation", "tier2"), "valuation": ("Valuation", "tier2"),
"entry_timing": ("Entry Timing", "tier2"), "entry_timing": ("Entry Timing", "tier2"),
"earnings_revisions": ("Earnings Revisions", "tier2"), "earnings_revisions": ("Earnings Revisions", "tier2"),
"sector_rotation": ("Sector Rotation", "tier2"), "sector_rotation": ("Sector Rotation", "tier2"),
"backlog": ("Backlog / Order Momentum", "tier2"), "backlog": ("Backlog / Order Momentum", "tier2"),
"crowding": ("Narrative Crowding", "tier2"), "crowding": ("Narrative Crowding", "tier2"),
"archetype": ("Archetype", "scoring"), "archetype": ("Archetype", "scoring"),
"master_score": ("Master Score", "scoring"), "master_score": ("Master Score", "scoring"),
"theme_substitution": ("Theme Substitution", "portfolio"), "theme_substitution": ("Theme Substitution", "portfolio"),
"position_replacement": ("Position Replacement", "portfolio"), "position_replacement": ("Position Replacement", "portfolio"),
"bull_case": ("Bull Researcher", "debate"), "bull_case": ("Bull Researcher", "debate"),
"bear_case": ("Bear Researcher", "debate"), "bear_case": ("Bear Researcher", "debate"),
"debate": ("Debate Referee", "debate"), "debate": ("Debate Referee", "debate"),
"risk": ("Risk / Invalidation", "decision"), "risk": ("Risk / Invalidation", "decision"),
"final_decision": ("Final Decision", "decision"), "final_decision": ("Final Decision", "decision"),
} }
ALL_AGENTS = [name for name, _ in FIELD_AGENT_MAP.values()] ALL_AGENTS = [name for name, _ in FIELD_AGENT_MAP.values()]
ALL_STAGES = ["validation", "tier1", "tier2", "scoring", "portfolio", "debate", "decision"] ALL_STAGES = ["validation", "tier1", "tier2", "scoring", "portfolio", "debate", "decision"]
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# Analysis runner # Analysis runner
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
async def _run_analysis_inner(analysis_id: str, ticker: str, trade_date: str): async def _run_analysis_inner(analysis_id: str, ticker: str, trade_date: str):
"""Core analysis logic — streams structured pipeline state changes as SSE.""" """Core analysis logic — streams structured pipeline state changes as SSE."""
state = analyses[analysis_id] state = analyses[analysis_id]
q = state["queue"] q = state["queue"]
config = build_config() config = build_config()
try: try:
graph = TradingAgentsGraph(debug=False, config=config) graph = TradingAgentsGraph(debug=False, config=config)
logger.info( logger.info(
"analysis_init_ok deep_llm=%s quick_llm=%s analysis_id=%s", "analysis_init_ok deep_llm=%s quick_llm=%s analysis_id=%s",
type(graph.deep_thinking_llm).__name__, type(graph.deep_thinking_llm).__name__,
type(graph.quick_thinking_llm).__name__, type(graph.quick_thinking_llm).__name__,
analysis_id, analysis_id,
) )
except Exception as e: except Exception as e:
logger.error("analysis_init_failed analysis_id=%s error=%s\n%s", analysis_id, e, _tb.format_exc()) logger.error("analysis_init_failed analysis_id=%s error=%s\n%s", analysis_id, e, _tb.format_exc())
await q.put({"type": "error", "message": f"Init failed: {e}"}) await q.put({"type": "error", "message": f"Init failed: {e}"})
await q.put(None) await q.put(None)
return return
init_state = graph._create_initial_state(ticker, trade_date) init_state = graph._create_initial_state(ticker, trade_date)
start_time = time.time() start_time = time.time()
emitted_fields = set() emitted_fields = set()
prev_agent_statuses = {} prev_agent_statuses = {}
final_state = None final_state = None
# Emit initial status: all agents pending # Emit initial status: all agents pending
for field, (agent_name, stage) in FIELD_AGENT_MAP.items(): for field, (agent_name, stage) in FIELD_AGENT_MAP.items():
prev_agent_statuses[field] = "pending" prev_agent_statuses[field] = "pending"
evt = { evt = {
"type": "agent_update", "type": "agent_update",
"agent": agent_name, "agent": agent_name,
"stage": stage, "stage": stage,
"status": "pending", "status": "pending",
"stats": _stats(start_time, emitted_fields), "stats": _stats(start_time, emitted_fields),
} }
_append_event(state, evt) _append_event(state, evt)
await q.put(evt) await q.put(evt)
try: try:
async for chunk in graph.graph.astream( async for chunk in graph.graph.astream(
init_state, init_state,
stream_mode="values", stream_mode="values",
config={"recursion_limit": 25}, config={"recursion_limit": 25},
): ):
final_state = chunk final_state = chunk
# Detect newly populated fields # Detect newly populated fields
for field, (agent_name, stage) in FIELD_AGENT_MAP.items(): for field, (agent_name, stage) in FIELD_AGENT_MAP.items():
if field in emitted_fields: if field in emitted_fields:
continue continue
value = chunk.get(field) value = chunk.get(field)
if value is None: if value is None:
continue continue
emitted_fields.add(field) emitted_fields.add(field)
st = _stats(start_time, emitted_fields) st = _stats(start_time, emitted_fields)
# Mark this agent completed # Mark this agent completed
prev_agent_statuses[field] = "completed" prev_agent_statuses[field] = "completed"
evt = { evt = {
"type": "agent_update", "type": "agent_update",
"agent": agent_name, "agent": agent_name,
"stage": stage, "stage": stage,
"status": "completed", "status": "completed",
"stats": st, "stats": st,
} }
_append_event(state, evt) _append_event(state, evt)
await q.put(evt) await q.put(evt)
# Emit report data for key fields # Emit report data for key fields
if field in ("validation", "company_card"): if field in ("validation", "company_card"):
evt = { evt = {
"type": "report", "type": "report",
"agent": agent_name, "agent": agent_name,
"stage": stage, "stage": stage,
"field": field, "field": field,
"report": _format_report(field, value), "report": _format_report(field, value),
"stats": st, "stats": st,
} }
_append_event(state, evt) _append_event(state, evt)
await q.put(evt) await q.put(evt)
elif field == "debate": elif field == "debate":
bull = chunk.get("bull_case") or {} bull = chunk.get("bull_case") or {}
bear = chunk.get("bear_case") or {} bear = chunk.get("bear_case") or {}
evt = { evt = {
"type": "debate", "type": "debate",
"stage": "debate", "stage": "debate",
"bull": bull.get("thesis", ""), "bull": bull.get("thesis", ""),
"bear": bear.get("thesis", ""), "bear": bear.get("thesis", ""),
"judge": (value or {}).get("reasoning", ""), "judge": (value or {}).get("reasoning", ""),
"winner": (value or {}).get("winner", ""), "winner": (value or {}).get("winner", ""),
"stats": st, "stats": st,
} }
_append_event(state, evt) _append_event(state, evt)
await q.put(evt) await q.put(evt)
elif field == "master_score": elif field == "master_score":
evt = { evt = {
"type": "score", "type": "score",
"stage": "scoring", "stage": "scoring",
"master_score": value, "master_score": value,
"adjusted_score": chunk.get("adjusted_score"), "adjusted_score": chunk.get("adjusted_score"),
"position_role": chunk.get("position_role"), "position_role": chunk.get("position_role"),
"stats": st, "stats": st,
} }
_append_event(state, evt) _append_event(state, evt)
await q.put(evt) await q.put(evt)
# Mark in-progress agents for upcoming stages # Mark in-progress agents for upcoming stages
await _update_in_progress(chunk, emitted_fields, prev_agent_statuses, state, q, start_time) await _update_in_progress(chunk, emitted_fields, prev_agent_statuses, state, q, start_time)
except Exception as e: except Exception as e:
logger.error("analysis_stream_error analysis_id=%s error=%s\n%s", analysis_id, e, _tb.format_exc()) logger.error("analysis_stream_error analysis_id=%s error=%s\n%s", analysis_id, e, _tb.format_exc())
evt = {"type": "error", "message": str(e)} evt = {"type": "error", "message": str(e)}
_append_event(state, evt) _append_event(state, evt)
await q.put(evt) await q.put(evt)
state["done"] = True state["done"] = True
await q.put(None) await q.put(None)
return return
# Final decision event # Final decision event
if final_state: if final_state:
decision = final_state.get("final_decision") or {} decision = final_state.get("final_decision") or {}
st = _stats(start_time, emitted_fields) st = _stats(start_time, emitted_fields)
# Mark all remaining as completed # Mark all remaining as completed
for field in FIELD_AGENT_MAP: for field in FIELD_AGENT_MAP:
if prev_agent_statuses.get(field) != "completed": if prev_agent_statuses.get(field) != "completed":
agent_name, stage = FIELD_AGENT_MAP[field] agent_name, stage = FIELD_AGENT_MAP[field]
prev_agent_statuses[field] = "completed" prev_agent_statuses[field] = "completed"
evt = { evt = {
"type": "agent_update", "type": "agent_update",
"agent": agent_name, "agent": agent_name,
"stage": stage, "stage": stage,
"status": "completed", "status": "completed",
"stats": st, "stats": st,
} }
_append_event(state, evt) _append_event(state, evt)
await q.put(evt) await q.put(evt)
evt = { evt = {
"type": "decision", "type": "decision",
"stage": "decision", "stage": "decision",
"signal": decision.get("action", "AVOID"), "signal": decision.get("action", "AVOID"),
"decision_text": decision.get("narrative", ""), "decision_text": decision.get("narrative", ""),
"master_score": final_state.get("master_score"), "master_score": final_state.get("master_score"),
"adjusted_score": final_state.get("adjusted_score"), "adjusted_score": final_state.get("adjusted_score"),
"position_role": final_state.get("position_role"), "position_role": final_state.get("position_role"),
"final_decision": decision, "final_decision": decision,
"stats": st, "stats": st,
} }
_append_event(state, evt) _append_event(state, evt)
await q.put(evt) await q.put(evt)
state["done"] = True state["done"] = True
await q.put(None) await q.put(None)
async def _update_in_progress(chunk, emitted, statuses, state, q, start_time): async def _update_in_progress(chunk, emitted, statuses, state, q, start_time):
"""Heuristic: mark agents as in_progress based on stage progression.""" """Heuristic: mark agents as in_progress based on stage progression."""
# If validation is done, mark tier 1 as in_progress # If validation is done, mark tier 1 as in_progress
if "validation" in emitted: if "validation" in emitted:
for field in ("macro", "liquidity"): for field in ("macro", "liquidity"):
if field not in emitted and statuses.get(field) == "pending": if field not in emitted and statuses.get(field) == "pending":
statuses[field] = "in_progress" statuses[field] = "in_progress"
agent_name, stage = FIELD_AGENT_MAP[field] agent_name, stage = FIELD_AGENT_MAP[field]
evt = { evt = {
"type": "agent_update", "type": "agent_update",
"agent": agent_name, "agent": agent_name,
"stage": stage, "stage": stage,
"status": "in_progress", "status": "in_progress",
"stats": _stats(start_time, emitted), "stats": _stats(start_time, emitted),
} }
_append_event(state, evt) _append_event(state, evt)
await q.put(evt) await q.put(evt)
# If tier 1 done, mark tier 2 in_progress # If tier 1 done, mark tier 2 in_progress
if "macro" in emitted and "liquidity" in emitted: if "macro" in emitted and "liquidity" in emitted:
tier2_fields = [ tier2_fields = [
"business_quality", "institutional_flow", "valuation", "business_quality", "institutional_flow", "valuation",
"entry_timing", "earnings_revisions", "sector_rotation", "entry_timing", "earnings_revisions", "sector_rotation",
"backlog", "crowding", "backlog", "crowding",
] ]
for field in tier2_fields: for field in tier2_fields:
if field not in emitted and statuses.get(field) == "pending": if field not in emitted and statuses.get(field) == "pending":
statuses[field] = "in_progress" statuses[field] = "in_progress"
agent_name, stage = FIELD_AGENT_MAP[field] agent_name, stage = FIELD_AGENT_MAP[field]
evt = { evt = {
"type": "agent_update", "type": "agent_update",
"agent": agent_name, "agent": agent_name,
"stage": stage, "stage": stage,
"status": "in_progress", "status": "in_progress",
"stats": _stats(start_time, emitted), "stats": _stats(start_time, emitted),
} }
_append_event(state, evt) _append_event(state, evt)
await q.put(evt) await q.put(evt)
# If scoring done, mark portfolio analysis in_progress # If scoring done, mark portfolio analysis in_progress
if "master_score" in emitted: if "master_score" in emitted:
for field in ("theme_substitution", "position_replacement"): for field in ("theme_substitution", "position_replacement"):
if field not in emitted and statuses.get(field) == "pending": if field not in emitted and statuses.get(field) == "pending":
statuses[field] = "in_progress" statuses[field] = "in_progress"
agent_name, stage = FIELD_AGENT_MAP[field] agent_name, stage = FIELD_AGENT_MAP[field]
evt = { evt = {
"type": "agent_update", "type": "agent_update",
"agent": agent_name, "agent": agent_name,
"stage": stage, "stage": stage,
"status": "in_progress", "status": "in_progress",
"stats": _stats(start_time, emitted), "stats": _stats(start_time, emitted),
} }
_append_event(state, evt) _append_event(state, evt)
await q.put(evt) await q.put(evt)
def _stats(start_time: float, emitted_fields: set) -> dict: def _stats(start_time: float, emitted_fields: set) -> dict:
return { return {
"agents_done": len(emitted_fields), "agents_done": len(emitted_fields),
"agents_total": len(FIELD_AGENT_MAP), "agents_total": len(FIELD_AGENT_MAP),
"elapsed": round(time.time() - start_time, 1), "elapsed": round(time.time() - start_time, 1),
} }
def _format_report(field: str, value) -> str: def _format_report(field: str, value) -> str:
"""Format a state field value as a readable report string.""" """Format a state field value as a readable report string."""
if isinstance(value, dict): if isinstance(value, dict):
if "summary_1_sentence" in value: if "summary_1_sentence" in value:
return value["summary_1_sentence"] return value["summary_1_sentence"]
if "company_name" in value: if "company_name" in value:
return f"{value.get('company_name', '')} ({value.get('ticker', '')}) — {value.get('sector', '')} / {value.get('industry', '')}" return f"{value.get('company_name', '')} ({value.get('ticker', '')}) — {value.get('sector', '')} / {value.get('industry', '')}"
return json.dumps(value, indent=2, default=str)[:500] return json.dumps(value, indent=2, default=str)[:500]
return str(value)[:500] return str(value)[:500]
async def run_analysis(analysis_id: str, ticker: str, trade_date: str): async def run_analysis(analysis_id: str, ticker: str, trade_date: str):
"""Background task with semaphore and timeout.""" """Background task with semaphore and timeout."""
state = analyses[analysis_id] state = analyses[analysis_id]
q = state["queue"] q = state["queue"]
async with _semaphore: async with _semaphore:
try: try:
await asyncio.wait_for( await asyncio.wait_for(
_run_analysis_inner(analysis_id, ticker, trade_date), _run_analysis_inner(analysis_id, ticker, trade_date),
timeout=3600, timeout=3600,
) )
except asyncio.TimeoutError: except asyncio.TimeoutError:
logger.warning("analysis_timeout analysis_id=%s", analysis_id) logger.warning("analysis_timeout analysis_id=%s", analysis_id)
evt = {"type": "error", "message": "Analysis timed out after 60 minutes"} evt = {"type": "error", "message": "Analysis timed out after 60 minutes"}
_append_event(state, evt) _append_event(state, evt)
await q.put(evt) await q.put(evt)
state["done"] = True state["done"] = True
await q.put(None) await q.put(None)
# --- Cleanup --- # --- Cleanup ---
async def _cleanup_loop(): async def _cleanup_loop():
while True: while True:
await asyncio.sleep(300) await asyncio.sleep(300)
now = time.time() now = time.time()
expired = [aid for aid, s in analyses.items() if now - s["created_at"] > 1800] expired = [aid for aid, s in analyses.items() if now - s["created_at"] > 1800]
for aid in expired: for aid in expired:
analyses.pop(aid, None) analyses.pop(aid, None)
if expired: if expired:
logger.info("cleanup_expired count=%d", len(expired)) logger.info("cleanup_expired count=%d", len(expired))
@app.on_event("startup") @app.on_event("startup")
async def _start_cleanup(): async def _start_cleanup():
asyncio.create_task(_cleanup_loop()) asyncio.create_task(_cleanup_loop())
# --- Routes --- # --- Routes ---
@app.post("/analyze", dependencies=[Depends(verify_api_key)]) @app.post("/analyze", dependencies=[Depends(verify_api_key)])
async def start_analysis(req: AnalyzeRequest): async def start_analysis(req: AnalyzeRequest):
ticker = req.ticker.upper().strip() ticker = req.ticker.upper().strip()
if not ticker: if not ticker:
raise HTTPException(400, "Ticker must not be empty") raise HTTPException(400, "Ticker must not be empty")
if len(ticker) > 10: if len(ticker) > 10:
raise HTTPException(400, f"Ticker too long ({len(ticker)} chars, max 10)") raise HTTPException(400, f"Ticker too long ({len(ticker)} chars, max 10)")
if not re.match(r'^[A-Z0-9.\-]{1,10}$', ticker): if not re.match(r'^[A-Z0-9.\-]{1,10}$', ticker):
raise HTTPException(400, "Invalid ticker — only letters, digits, dots, and hyphens allowed") raise HTTPException(400, "Invalid ticker — only letters, digits, dots, and hyphens allowed")
trade_date = req.date or str(date.today()) trade_date = req.date or str(date.today())
analysis_id = str(uuid.uuid4()) analysis_id = str(uuid.uuid4())
analyses[analysis_id] = { analyses[analysis_id] = {
"queue": asyncio.Queue(), "queue": asyncio.Queue(),
"events": [], "events": [],
"done": False, "done": False,
"created_at": time.time(), "created_at": time.time(),
} }
asyncio.create_task(run_analysis(analysis_id, ticker, trade_date)) asyncio.create_task(run_analysis(analysis_id, ticker, trade_date))
return {"id": analysis_id, "ticker": ticker, "date": trade_date} return {"id": analysis_id, "ticker": ticker, "date": trade_date}
@app.get("/analyze/{analysis_id}/stream", dependencies=[Depends(verify_api_key)]) @app.get("/analyze/{analysis_id}/stream", dependencies=[Depends(verify_api_key)])
async def stream_analysis(analysis_id: str, last_event: int = 0): async def stream_analysis(analysis_id: str, last_event: int = 0):
"""Stream SSE events. Supports reconnection via ?last_event=N.""" """Stream SSE events. Supports reconnection via ?last_event=N."""
if analysis_id not in analyses: if analysis_id not in analyses:
raise HTTPException(404, "Analysis not found") raise HTTPException(404, "Analysis not found")
state = analyses[analysis_id] state = analyses[analysis_id]
async def event_generator(): async def event_generator():
idx = last_event idx = last_event
while idx < len(state["events"]): while idx < len(state["events"]):
evt = state["events"][idx] evt = state["events"][idx]
idx += 1 idx += 1
yield {"id": str(idx), "data": json.dumps(evt)} yield {"id": str(idx), "data": json.dumps(evt)}
if state["done"]: if state["done"]:
return return
q = state["queue"] q = state["queue"]
while True: while True:
try: try:
event = await asyncio.wait_for(q.get(), timeout=15) event = await asyncio.wait_for(q.get(), timeout=15)
except asyncio.TimeoutError: except asyncio.TimeoutError:
yield {"event": "heartbeat", "data": json.dumps({"type": "heartbeat"})} yield {"event": "heartbeat", "data": json.dumps({"type": "heartbeat"})}
continue continue
if event is None: if event is None:
break break
idx += 1 idx += 1
yield {"id": str(idx), "data": json.dumps(event)} yield {"id": str(idx), "data": json.dumps(event)}
return EventSourceResponse(event_generator()) return EventSourceResponse(event_generator())
@app.get("/health") @app.get("/health")
async def health(): async def health():
return {"status": "ok", "engine": "structured_pipeline"} return {"status": "ok", "engine": "structured_pipeline"}
@app.get("/api/status") @app.get("/api/status")
async def get_status(): async def get_status():
"""Structured pipeline status — no auth required.""" """Structured pipeline status — no auth required."""
from datetime import datetime from datetime import datetime
active_count = len(analyses) active_count = len(analyses)
return { return {
"service": "structured-pipeline", "service": "structured-pipeline",
"engine": "TradingAgents", "engine": "TradingAgents",
"active_analyses": active_count, "active_analyses": active_count,
"analyses": {k: {"created": v["created"], "done": v["done"]} for k, v in analyses.items()}, "analyses": {k: {"created": v["created"], "done": v["done"]} for k, v in analyses.items()},
"pid": __import__("os").getpid(), "pid": __import__("os").getpid(),
"uptime": time.time() - __import__("os").getpid(), "uptime": time.time() - __import__("os").getpid(),
} }
@app.get("/api/health") @app.get("/api/health")
async def api_health(): async def api_health():
return {"status": "ok", "service": "structured-pipeline"} return {"status": "ok", "service": "structured-pipeline"}

View File

@ -1,51 +1,51 @@
import getpass import getpass
import requests import requests
from rich.console import Console from rich.console import Console
from rich.panel import Panel from rich.panel import Panel
from cli.config import CLI_CONFIG from cli.config import CLI_CONFIG
def fetch_announcements(url: str = None, timeout: float = None) -> dict: def fetch_announcements(url: str = None, timeout: float = None) -> dict:
"""Fetch announcements from endpoint. Returns dict with announcements and settings.""" """Fetch announcements from endpoint. Returns dict with announcements and settings."""
endpoint = url or CLI_CONFIG["announcements_url"] endpoint = url or CLI_CONFIG["announcements_url"]
timeout = timeout or CLI_CONFIG["announcements_timeout"] timeout = timeout or CLI_CONFIG["announcements_timeout"]
fallback = CLI_CONFIG["announcements_fallback"] fallback = CLI_CONFIG["announcements_fallback"]
try: try:
response = requests.get(endpoint, timeout=timeout) response = requests.get(endpoint, timeout=timeout)
response.raise_for_status() response.raise_for_status()
data = response.json() data = response.json()
return { return {
"announcements": data.get("announcements", [fallback]), "announcements": data.get("announcements", [fallback]),
"require_attention": data.get("require_attention", False), "require_attention": data.get("require_attention", False),
} }
except Exception: except Exception:
return { return {
"announcements": [fallback], "announcements": [fallback],
"require_attention": False, "require_attention": False,
} }
def display_announcements(console: Console, data: dict) -> None: def display_announcements(console: Console, data: dict) -> None:
"""Display announcements panel. Prompts for Enter if require_attention is True.""" """Display announcements panel. Prompts for Enter if require_attention is True."""
announcements = data.get("announcements", []) announcements = data.get("announcements", [])
require_attention = data.get("require_attention", False) require_attention = data.get("require_attention", False)
if not announcements: if not announcements:
return return
content = "\n".join(announcements) content = "\n".join(announcements)
panel = Panel( panel = Panel(
content, content,
border_style="cyan", border_style="cyan",
padding=(1, 2), padding=(1, 2),
title="Announcements", title="Announcements",
) )
console.print(panel) console.print(panel)
if require_attention: if require_attention:
getpass.getpass("Press Enter to continue...") getpass.getpass("Press Enter to continue...")
else: else:
console.print() console.print()

View File

@ -1,6 +1,6 @@
CLI_CONFIG = { CLI_CONFIG = {
# Announcements # Announcements
"announcements_url": "https://api.tauric.ai/v1/announcements", "announcements_url": "https://api.tauric.ai/v1/announcements",
"announcements_timeout": 1.0, "announcements_timeout": 1.0,
"announcements_fallback": "[cyan]For more information, please visit[/cyan] [link=https://github.com/TauricResearch]https://github.com/TauricResearch[/link]", "announcements_fallback": "[cyan]For more information, please visit[/cyan] [link=https://github.com/TauricResearch]https://github.com/TauricResearch[/link]",
} }

File diff suppressed because it is too large Load Diff

View File

@ -1,10 +1,10 @@
from enum import Enum from enum import Enum
from typing import List, Optional, Dict from typing import List, Optional, Dict
from pydantic import BaseModel from pydantic import BaseModel
class AnalystType(str, Enum): class AnalystType(str, Enum):
MARKET = "market" MARKET = "market"
SOCIAL = "social" SOCIAL = "social"
NEWS = "news" NEWS = "news"
FUNDAMENTALS = "fundamentals" FUNDAMENTALS = "fundamentals"

View File

@ -1,7 +1,7 @@
______ ___ ___ __ ______ ___ ___ __
/_ __/________ _____/ (_)___ ____ _/ | ____ ____ ____ / /______ /_ __/________ _____/ (_)___ ____ _/ | ____ ____ ____ / /______
/ / / ___/ __ `/ __ / / __ \/ __ `/ /| |/ __ `/ _ \/ __ \/ __/ ___/ / / / ___/ __ `/ __ / / __ \/ __ `/ /| |/ __ `/ _ \/ __ \/ __/ ___/
/ / / / / /_/ / /_/ / / / / / /_/ / ___ / /_/ / __/ / / / /_(__ ) / / / / / /_/ / /_/ / / / / / /_/ / ___ / /_/ / __/ / / / /_(__ )
/_/ /_/ \__,_/\__,_/_/_/ /_/\__, /_/ |_\__, /\___/_/ /_/\__/____/ /_/ /_/ \__,_/\__,_/_/_/ /_/\__, /_/ |_\__, /\___/_/ /_/\__/____/
/____/ /____/ /____/ /____/

View File

@ -1,76 +1,76 @@
import threading import threading
from typing import Any, Dict, List, Union from typing import Any, Dict, List, Union
from langchain_core.callbacks import BaseCallbackHandler from langchain_core.callbacks import BaseCallbackHandler
from langchain_core.outputs import LLMResult from langchain_core.outputs import LLMResult
from langchain_core.messages import AIMessage from langchain_core.messages import AIMessage
class StatsCallbackHandler(BaseCallbackHandler): class StatsCallbackHandler(BaseCallbackHandler):
"""Callback handler that tracks LLM calls, tool calls, and token usage.""" """Callback handler that tracks LLM calls, tool calls, and token usage."""
def __init__(self) -> None: def __init__(self) -> None:
super().__init__() super().__init__()
self._lock = threading.Lock() self._lock = threading.Lock()
self.llm_calls = 0 self.llm_calls = 0
self.tool_calls = 0 self.tool_calls = 0
self.tokens_in = 0 self.tokens_in = 0
self.tokens_out = 0 self.tokens_out = 0
def on_llm_start( def on_llm_start(
self, self,
serialized: Dict[str, Any], serialized: Dict[str, Any],
prompts: List[str], prompts: List[str],
**kwargs: Any, **kwargs: Any,
) -> None: ) -> None:
"""Increment LLM call counter when an LLM starts.""" """Increment LLM call counter when an LLM starts."""
with self._lock: with self._lock:
self.llm_calls += 1 self.llm_calls += 1
def on_chat_model_start( def on_chat_model_start(
self, self,
serialized: Dict[str, Any], serialized: Dict[str, Any],
messages: List[List[Any]], messages: List[List[Any]],
**kwargs: Any, **kwargs: Any,
) -> None: ) -> None:
"""Increment LLM call counter when a chat model starts.""" """Increment LLM call counter when a chat model starts."""
with self._lock: with self._lock:
self.llm_calls += 1 self.llm_calls += 1
def on_llm_end(self, response: LLMResult, **kwargs: Any) -> None: def on_llm_end(self, response: LLMResult, **kwargs: Any) -> None:
"""Extract token usage from LLM response.""" """Extract token usage from LLM response."""
try: try:
generation = response.generations[0][0] generation = response.generations[0][0]
except (IndexError, TypeError): except (IndexError, TypeError):
return return
usage_metadata = None usage_metadata = None
if hasattr(generation, "message"): if hasattr(generation, "message"):
message = generation.message message = generation.message
if isinstance(message, AIMessage) and hasattr(message, "usage_metadata"): if isinstance(message, AIMessage) and hasattr(message, "usage_metadata"):
usage_metadata = message.usage_metadata usage_metadata = message.usage_metadata
if usage_metadata: if usage_metadata:
with self._lock: with self._lock:
self.tokens_in += usage_metadata.get("input_tokens", 0) self.tokens_in += usage_metadata.get("input_tokens", 0)
self.tokens_out += usage_metadata.get("output_tokens", 0) self.tokens_out += usage_metadata.get("output_tokens", 0)
def on_tool_start( def on_tool_start(
self, self,
serialized: Dict[str, Any], serialized: Dict[str, Any],
input_str: str, input_str: str,
**kwargs: Any, **kwargs: Any,
) -> None: ) -> None:
"""Increment tool call counter when a tool starts.""" """Increment tool call counter when a tool starts."""
with self._lock: with self._lock:
self.tool_calls += 1 self.tool_calls += 1
def get_stats(self) -> Dict[str, Any]: def get_stats(self) -> Dict[str, Any]:
"""Return current statistics.""" """Return current statistics."""
with self._lock: with self._lock:
return { return {
"llm_calls": self.llm_calls, "llm_calls": self.llm_calls,
"tool_calls": self.tool_calls, "tool_calls": self.tool_calls,
"tokens_in": self.tokens_in, "tokens_in": self.tokens_in,
"tokens_out": self.tokens_out, "tokens_out": self.tokens_out,
} }

View File

@ -1,328 +1,328 @@
import questionary import questionary
from typing import List, Optional, Tuple, Dict from typing import List, Optional, Tuple, Dict
from cli.models import AnalystType from cli.models import AnalystType
ANALYST_ORDER = [ ANALYST_ORDER = [
("Market Analyst", AnalystType.MARKET), ("Market Analyst", AnalystType.MARKET),
("Social Media Analyst", AnalystType.SOCIAL), ("Social Media Analyst", AnalystType.SOCIAL),
("News Analyst", AnalystType.NEWS), ("News Analyst", AnalystType.NEWS),
("Fundamentals Analyst", AnalystType.FUNDAMENTALS), ("Fundamentals Analyst", AnalystType.FUNDAMENTALS),
] ]
def get_ticker() -> str: def get_ticker() -> str:
"""Prompt the user to enter a ticker symbol.""" """Prompt the user to enter a ticker symbol."""
ticker = questionary.text( ticker = questionary.text(
"Enter the ticker symbol to analyze:", "Enter the ticker symbol to analyze:",
validate=lambda x: len(x.strip()) > 0 or "Please enter a valid ticker symbol.", validate=lambda x: len(x.strip()) > 0 or "Please enter a valid ticker symbol.",
style=questionary.Style( style=questionary.Style(
[ [
("text", "fg:green"), ("text", "fg:green"),
("highlighted", "noinherit"), ("highlighted", "noinherit"),
] ]
), ),
).ask() ).ask()
if not ticker: if not ticker:
console.print("\n[red]No ticker symbol provided. Exiting...[/red]") console.print("\n[red]No ticker symbol provided. Exiting...[/red]")
exit(1) exit(1)
return ticker.strip().upper() return ticker.strip().upper()
def get_analysis_date() -> str: def get_analysis_date() -> str:
"""Prompt the user to enter a date in YYYY-MM-DD format.""" """Prompt the user to enter a date in YYYY-MM-DD format."""
import re import re
from datetime import datetime from datetime import datetime
def validate_date(date_str: str) -> bool: def validate_date(date_str: str) -> bool:
if not re.match(r"^\d{4}-\d{2}-\d{2}$", date_str): if not re.match(r"^\d{4}-\d{2}-\d{2}$", date_str):
return False return False
try: try:
datetime.strptime(date_str, "%Y-%m-%d") datetime.strptime(date_str, "%Y-%m-%d")
return True return True
except ValueError: except ValueError:
return False return False
date = questionary.text( date = questionary.text(
"Enter the analysis date (YYYY-MM-DD):", "Enter the analysis date (YYYY-MM-DD):",
validate=lambda x: validate_date(x.strip()) validate=lambda x: validate_date(x.strip())
or "Please enter a valid date in YYYY-MM-DD format.", or "Please enter a valid date in YYYY-MM-DD format.",
style=questionary.Style( style=questionary.Style(
[ [
("text", "fg:green"), ("text", "fg:green"),
("highlighted", "noinherit"), ("highlighted", "noinherit"),
] ]
), ),
).ask() ).ask()
if not date: if not date:
console.print("\n[red]No date provided. Exiting...[/red]") console.print("\n[red]No date provided. Exiting...[/red]")
exit(1) exit(1)
return date.strip() return date.strip()
def select_analysts() -> List[AnalystType]: def select_analysts() -> List[AnalystType]:
"""Select analysts using an interactive checkbox.""" """Select analysts using an interactive checkbox."""
choices = questionary.checkbox( choices = questionary.checkbox(
"Select Your [Analysts Team]:", "Select Your [Analysts Team]:",
choices=[ choices=[
questionary.Choice(display, value=value) for display, value in ANALYST_ORDER questionary.Choice(display, value=value) for display, value in ANALYST_ORDER
], ],
instruction="\n- Press Space to select/unselect analysts\n- Press 'a' to select/unselect all\n- Press Enter when done", instruction="\n- Press Space to select/unselect analysts\n- Press 'a' to select/unselect all\n- Press Enter when done",
validate=lambda x: len(x) > 0 or "You must select at least one analyst.", validate=lambda x: len(x) > 0 or "You must select at least one analyst.",
style=questionary.Style( style=questionary.Style(
[ [
("checkbox-selected", "fg:green"), ("checkbox-selected", "fg:green"),
("selected", "fg:green noinherit"), ("selected", "fg:green noinherit"),
("highlighted", "noinherit"), ("highlighted", "noinherit"),
("pointer", "noinherit"), ("pointer", "noinherit"),
] ]
), ),
).ask() ).ask()
if not choices: if not choices:
console.print("\n[red]No analysts selected. Exiting...[/red]") console.print("\n[red]No analysts selected. Exiting...[/red]")
exit(1) exit(1)
return choices return choices
def select_research_depth() -> int: def select_research_depth() -> int:
"""Select research depth using an interactive selection.""" """Select research depth using an interactive selection."""
# Define research depth options with their corresponding values # Define research depth options with their corresponding values
DEPTH_OPTIONS = [ DEPTH_OPTIONS = [
("Shallow - Quick research, few debate and strategy discussion rounds", 1), ("Shallow - Quick research, few debate and strategy discussion rounds", 1),
("Medium - Middle ground, moderate debate rounds and strategy discussion", 3), ("Medium - Middle ground, moderate debate rounds and strategy discussion", 3),
("Deep - Comprehensive research, in depth debate and strategy discussion", 5), ("Deep - Comprehensive research, in depth debate and strategy discussion", 5),
] ]
choice = questionary.select( choice = questionary.select(
"Select Your [Research Depth]:", "Select Your [Research Depth]:",
choices=[ choices=[
questionary.Choice(display, value=value) for display, value in DEPTH_OPTIONS questionary.Choice(display, value=value) for display, value in DEPTH_OPTIONS
], ],
instruction="\n- Use arrow keys to navigate\n- Press Enter to select", instruction="\n- Use arrow keys to navigate\n- Press Enter to select",
style=questionary.Style( style=questionary.Style(
[ [
("selected", "fg:yellow noinherit"), ("selected", "fg:yellow noinherit"),
("highlighted", "fg:yellow noinherit"), ("highlighted", "fg:yellow noinherit"),
("pointer", "fg:yellow noinherit"), ("pointer", "fg:yellow noinherit"),
] ]
), ),
).ask() ).ask()
if choice is None: if choice is None:
console.print("\n[red]No research depth selected. Exiting...[/red]") console.print("\n[red]No research depth selected. Exiting...[/red]")
exit(1) exit(1)
return choice return choice
def select_shallow_thinking_agent(provider) -> str: def select_shallow_thinking_agent(provider) -> str:
"""Select shallow thinking llm engine using an interactive selection.""" """Select shallow thinking llm engine using an interactive selection."""
# Define shallow thinking llm engine options with their corresponding model names # Define shallow thinking llm engine options with their corresponding model names
SHALLOW_AGENT_OPTIONS = { SHALLOW_AGENT_OPTIONS = {
"openai": [ "openai": [
("GPT-5 Mini - Cost-optimized reasoning", "gpt-5-mini"), ("GPT-5 Mini - Cost-optimized reasoning", "gpt-5-mini"),
("GPT-5 Nano - Ultra-fast, high-throughput", "gpt-5-nano"), ("GPT-5 Nano - Ultra-fast, high-throughput", "gpt-5-nano"),
("GPT-5.2 - Latest flagship", "gpt-5.2"), ("GPT-5.2 - Latest flagship", "gpt-5.2"),
("GPT-5.1 - Flexible reasoning", "gpt-5.1"), ("GPT-5.1 - Flexible reasoning", "gpt-5.1"),
("GPT-4.1 - Smartest non-reasoning, 1M context", "gpt-4.1"), ("GPT-4.1 - Smartest non-reasoning, 1M context", "gpt-4.1"),
], ],
"anthropic": [ "anthropic": [
("Claude Haiku 4.5 - Fast + extended thinking", "claude-haiku-4-5"), ("Claude Haiku 4.5 - Fast + extended thinking", "claude-haiku-4-5"),
("Claude Sonnet 4.5 - Best for agents/coding", "claude-sonnet-4-5"), ("Claude Sonnet 4.5 - Best for agents/coding", "claude-sonnet-4-5"),
("Claude Sonnet 4 - High-performance", "claude-sonnet-4-20250514"), ("Claude Sonnet 4 - High-performance", "claude-sonnet-4-20250514"),
], ],
"google": [ "google": [
("Gemini 3 Flash - Next-gen fast", "gemini-3-flash-preview"), ("Gemini 3 Flash - Next-gen fast", "gemini-3-flash-preview"),
("Gemini 2.5 Flash - Balanced, recommended", "gemini-2.5-flash"), ("Gemini 2.5 Flash - Balanced, recommended", "gemini-2.5-flash"),
("Gemini 3 Pro - Reasoning-first", "gemini-3-pro-preview"), ("Gemini 3 Pro - Reasoning-first", "gemini-3-pro-preview"),
("Gemini 2.5 Flash Lite - Fast, low-cost", "gemini-2.5-flash-lite"), ("Gemini 2.5 Flash Lite - Fast, low-cost", "gemini-2.5-flash-lite"),
], ],
"xai": [ "xai": [
("Grok 4.1 Fast (Non-Reasoning) - Speed optimized, 2M ctx", "grok-4-1-fast-non-reasoning"), ("Grok 4.1 Fast (Non-Reasoning) - Speed optimized, 2M ctx", "grok-4-1-fast-non-reasoning"),
("Grok 4 Fast (Non-Reasoning) - Speed optimized", "grok-4-fast-non-reasoning"), ("Grok 4 Fast (Non-Reasoning) - Speed optimized", "grok-4-fast-non-reasoning"),
("Grok 4.1 Fast (Reasoning) - High-performance, 2M ctx", "grok-4-1-fast-reasoning"), ("Grok 4.1 Fast (Reasoning) - High-performance, 2M ctx", "grok-4-1-fast-reasoning"),
("Grok 4 Fast (Reasoning) - High-performance", "grok-4-fast-reasoning"), ("Grok 4 Fast (Reasoning) - High-performance", "grok-4-fast-reasoning"),
], ],
"openrouter": [ "openrouter": [
("NVIDIA Nemotron 3 Nano 30B (free)", "nvidia/nemotron-3-nano-30b-a3b:free"), ("NVIDIA Nemotron 3 Nano 30B (free)", "nvidia/nemotron-3-nano-30b-a3b:free"),
("Z.AI GLM 4.5 Air (free)", "z-ai/glm-4.5-air:free"), ("Z.AI GLM 4.5 Air (free)", "z-ai/glm-4.5-air:free"),
], ],
"ollama": [ "ollama": [
("Qwen3:latest (8B, local)", "qwen3:latest"), ("Qwen3:latest (8B, local)", "qwen3:latest"),
("GPT-OSS:latest (20B, local)", "gpt-oss:latest"), ("GPT-OSS:latest (20B, local)", "gpt-oss:latest"),
("GLM-4.7-Flash:latest (30B, local)", "glm-4.7-flash:latest"), ("GLM-4.7-Flash:latest (30B, local)", "glm-4.7-flash:latest"),
], ],
} }
choice = questionary.select( choice = questionary.select(
"Select Your [Quick-Thinking LLM Engine]:", "Select Your [Quick-Thinking LLM Engine]:",
choices=[ choices=[
questionary.Choice(display, value=value) questionary.Choice(display, value=value)
for display, value in SHALLOW_AGENT_OPTIONS[provider.lower()] for display, value in SHALLOW_AGENT_OPTIONS[provider.lower()]
], ],
instruction="\n- Use arrow keys to navigate\n- Press Enter to select", instruction="\n- Use arrow keys to navigate\n- Press Enter to select",
style=questionary.Style( style=questionary.Style(
[ [
("selected", "fg:magenta noinherit"), ("selected", "fg:magenta noinherit"),
("highlighted", "fg:magenta noinherit"), ("highlighted", "fg:magenta noinherit"),
("pointer", "fg:magenta noinherit"), ("pointer", "fg:magenta noinherit"),
] ]
), ),
).ask() ).ask()
if choice is None: if choice is None:
console.print( console.print(
"\n[red]No shallow thinking llm engine selected. Exiting...[/red]" "\n[red]No shallow thinking llm engine selected. Exiting...[/red]"
) )
exit(1) exit(1)
return choice return choice
def select_deep_thinking_agent(provider) -> str: def select_deep_thinking_agent(provider) -> str:
"""Select deep thinking llm engine using an interactive selection.""" """Select deep thinking llm engine using an interactive selection."""
# Define deep thinking llm engine options with their corresponding model names # Define deep thinking llm engine options with their corresponding model names
DEEP_AGENT_OPTIONS = { DEEP_AGENT_OPTIONS = {
"openai": [ "openai": [
("GPT-5.2 - Latest flagship", "gpt-5.2"), ("GPT-5.2 - Latest flagship", "gpt-5.2"),
("GPT-5.1 - Flexible reasoning", "gpt-5.1"), ("GPT-5.1 - Flexible reasoning", "gpt-5.1"),
("GPT-5 - Advanced reasoning", "gpt-5"), ("GPT-5 - Advanced reasoning", "gpt-5"),
("GPT-4.1 - Smartest non-reasoning, 1M context", "gpt-4.1"), ("GPT-4.1 - Smartest non-reasoning, 1M context", "gpt-4.1"),
("GPT-5 Mini - Cost-optimized reasoning", "gpt-5-mini"), ("GPT-5 Mini - Cost-optimized reasoning", "gpt-5-mini"),
("GPT-5 Nano - Ultra-fast, high-throughput", "gpt-5-nano"), ("GPT-5 Nano - Ultra-fast, high-throughput", "gpt-5-nano"),
], ],
"anthropic": [ "anthropic": [
("Claude Sonnet 4.5 - Best for agents/coding", "claude-sonnet-4-5"), ("Claude Sonnet 4.5 - Best for agents/coding", "claude-sonnet-4-5"),
("Claude Opus 4.5 - Premium, max intelligence", "claude-opus-4-5"), ("Claude Opus 4.5 - Premium, max intelligence", "claude-opus-4-5"),
("Claude Opus 4.1 - Most capable model", "claude-opus-4-1-20250805"), ("Claude Opus 4.1 - Most capable model", "claude-opus-4-1-20250805"),
("Claude Haiku 4.5 - Fast + extended thinking", "claude-haiku-4-5"), ("Claude Haiku 4.5 - Fast + extended thinking", "claude-haiku-4-5"),
("Claude Sonnet 4 - High-performance", "claude-sonnet-4-20250514"), ("Claude Sonnet 4 - High-performance", "claude-sonnet-4-20250514"),
], ],
"google": [ "google": [
("Gemini 3 Pro - Reasoning-first", "gemini-3-pro-preview"), ("Gemini 3 Pro - Reasoning-first", "gemini-3-pro-preview"),
("Gemini 3 Flash - Next-gen fast", "gemini-3-flash-preview"), ("Gemini 3 Flash - Next-gen fast", "gemini-3-flash-preview"),
("Gemini 2.5 Flash - Balanced, recommended", "gemini-2.5-flash"), ("Gemini 2.5 Flash - Balanced, recommended", "gemini-2.5-flash"),
], ],
"xai": [ "xai": [
("Grok 4.1 Fast (Reasoning) - High-performance, 2M ctx", "grok-4-1-fast-reasoning"), ("Grok 4.1 Fast (Reasoning) - High-performance, 2M ctx", "grok-4-1-fast-reasoning"),
("Grok 4 Fast (Reasoning) - High-performance", "grok-4-fast-reasoning"), ("Grok 4 Fast (Reasoning) - High-performance", "grok-4-fast-reasoning"),
("Grok 4 - Flagship model", "grok-4-0709"), ("Grok 4 - Flagship model", "grok-4-0709"),
("Grok 4.1 Fast (Non-Reasoning) - Speed optimized, 2M ctx", "grok-4-1-fast-non-reasoning"), ("Grok 4.1 Fast (Non-Reasoning) - Speed optimized, 2M ctx", "grok-4-1-fast-non-reasoning"),
("Grok 4 Fast (Non-Reasoning) - Speed optimized", "grok-4-fast-non-reasoning"), ("Grok 4 Fast (Non-Reasoning) - Speed optimized", "grok-4-fast-non-reasoning"),
], ],
"openrouter": [ "openrouter": [
("Z.AI GLM 4.5 Air (free)", "z-ai/glm-4.5-air:free"), ("Z.AI GLM 4.5 Air (free)", "z-ai/glm-4.5-air:free"),
("NVIDIA Nemotron 3 Nano 30B (free)", "nvidia/nemotron-3-nano-30b-a3b:free"), ("NVIDIA Nemotron 3 Nano 30B (free)", "nvidia/nemotron-3-nano-30b-a3b:free"),
], ],
"ollama": [ "ollama": [
("GLM-4.7-Flash:latest (30B, local)", "glm-4.7-flash:latest"), ("GLM-4.7-Flash:latest (30B, local)", "glm-4.7-flash:latest"),
("GPT-OSS:latest (20B, local)", "gpt-oss:latest"), ("GPT-OSS:latest (20B, local)", "gpt-oss:latest"),
("Qwen3:latest (8B, local)", "qwen3:latest"), ("Qwen3:latest (8B, local)", "qwen3:latest"),
], ],
} }
choice = questionary.select( choice = questionary.select(
"Select Your [Deep-Thinking LLM Engine]:", "Select Your [Deep-Thinking LLM Engine]:",
choices=[ choices=[
questionary.Choice(display, value=value) questionary.Choice(display, value=value)
for display, value in DEEP_AGENT_OPTIONS[provider.lower()] for display, value in DEEP_AGENT_OPTIONS[provider.lower()]
], ],
instruction="\n- Use arrow keys to navigate\n- Press Enter to select", instruction="\n- Use arrow keys to navigate\n- Press Enter to select",
style=questionary.Style( style=questionary.Style(
[ [
("selected", "fg:magenta noinherit"), ("selected", "fg:magenta noinherit"),
("highlighted", "fg:magenta noinherit"), ("highlighted", "fg:magenta noinherit"),
("pointer", "fg:magenta noinherit"), ("pointer", "fg:magenta noinherit"),
] ]
), ),
).ask() ).ask()
if choice is None: if choice is None:
console.print("\n[red]No deep thinking llm engine selected. Exiting...[/red]") console.print("\n[red]No deep thinking llm engine selected. Exiting...[/red]")
exit(1) exit(1)
return choice return choice
def select_llm_provider() -> tuple[str, str]: def select_llm_provider() -> tuple[str, str]:
"""Select the OpenAI api url using interactive selection.""" """Select the OpenAI api url using interactive selection."""
# Define OpenAI api options with their corresponding endpoints # Define OpenAI api options with their corresponding endpoints
BASE_URLS = [ BASE_URLS = [
("OpenAI", "https://api.openai.com/v1"), ("OpenAI", "https://api.openai.com/v1"),
("Google", "https://generativelanguage.googleapis.com/v1"), ("Google", "https://generativelanguage.googleapis.com/v1"),
("Anthropic", "https://api.anthropic.com/"), ("Anthropic", "https://api.anthropic.com/"),
("xAI", "https://api.x.ai/v1"), ("xAI", "https://api.x.ai/v1"),
("Openrouter", "https://openrouter.ai/api/v1"), ("Openrouter", "https://openrouter.ai/api/v1"),
("Ollama", "http://localhost:11434/v1"), ("Ollama", "http://localhost:11434/v1"),
] ]
choice = questionary.select( choice = questionary.select(
"Select your LLM Provider:", "Select your LLM Provider:",
choices=[ choices=[
questionary.Choice(display, value=(display, value)) questionary.Choice(display, value=(display, value))
for display, value in BASE_URLS for display, value in BASE_URLS
], ],
instruction="\n- Use arrow keys to navigate\n- Press Enter to select", instruction="\n- Use arrow keys to navigate\n- Press Enter to select",
style=questionary.Style( style=questionary.Style(
[ [
("selected", "fg:magenta noinherit"), ("selected", "fg:magenta noinherit"),
("highlighted", "fg:magenta noinherit"), ("highlighted", "fg:magenta noinherit"),
("pointer", "fg:magenta noinherit"), ("pointer", "fg:magenta noinherit"),
] ]
), ),
).ask() ).ask()
if choice is None: if choice is None:
console.print("\n[red]no OpenAI backend selected. Exiting...[/red]") console.print("\n[red]no OpenAI backend selected. Exiting...[/red]")
exit(1) exit(1)
display_name, url = choice display_name, url = choice
print(f"You selected: {display_name}\tURL: {url}") print(f"You selected: {display_name}\tURL: {url}")
return display_name, url return display_name, url
def ask_openai_reasoning_effort() -> str: def ask_openai_reasoning_effort() -> str:
"""Ask for OpenAI reasoning effort level.""" """Ask for OpenAI reasoning effort level."""
choices = [ choices = [
questionary.Choice("Medium (Default)", "medium"), questionary.Choice("Medium (Default)", "medium"),
questionary.Choice("High (More thorough)", "high"), questionary.Choice("High (More thorough)", "high"),
questionary.Choice("Low (Faster)", "low"), questionary.Choice("Low (Faster)", "low"),
] ]
return questionary.select( return questionary.select(
"Select Reasoning Effort:", "Select Reasoning Effort:",
choices=choices, choices=choices,
style=questionary.Style([ style=questionary.Style([
("selected", "fg:cyan noinherit"), ("selected", "fg:cyan noinherit"),
("highlighted", "fg:cyan noinherit"), ("highlighted", "fg:cyan noinherit"),
("pointer", "fg:cyan noinherit"), ("pointer", "fg:cyan noinherit"),
]), ]),
).ask() ).ask()
def ask_gemini_thinking_config() -> str | None: def ask_gemini_thinking_config() -> str | None:
"""Ask for Gemini thinking configuration. """Ask for Gemini thinking configuration.
Returns thinking_level: "high" or "minimal". Returns thinking_level: "high" or "minimal".
Client maps to appropriate API param based on model series. Client maps to appropriate API param based on model series.
""" """
return questionary.select( return questionary.select(
"Select Thinking Mode:", "Select Thinking Mode:",
choices=[ choices=[
questionary.Choice("Enable Thinking (recommended)", "high"), questionary.Choice("Enable Thinking (recommended)", "high"),
questionary.Choice("Minimal/Disable Thinking", "minimal"), questionary.Choice("Minimal/Disable Thinking", "minimal"),
], ],
style=questionary.Style([ style=questionary.Style([
("selected", "fg:green noinherit"), ("selected", "fg:green noinherit"),
("highlighted", "fg:green noinherit"), ("highlighted", "fg:green noinherit"),
("pointer", "fg:green noinherit"), ("pointer", "fg:green noinherit"),
]), ]),
).ask() ).ask()

View File

@ -1,67 +1,67 @@
# TradingAgents Chainlit Web UI — Design # TradingAgents Chainlit Web UI — Design
## Summary ## Summary
Add a Chainlit web UI to TradingAgents so it can be deployed on Railway as a web service. Users interact via chat messages (e.g., "Analyze NVDA") and see live agent progress streamed into the browser. Add a Chainlit web UI to TradingAgents so it can be deployed on Railway as a web service. Users interact via chat messages (e.g., "Analyze NVDA") and see live agent progress streamed into the browser.
## Architecture ## Architecture
Thin Chainlit wrapper around the existing `TradingAgentsGraph` programmatic API. ~150 lines of new code in a single `app.py`. Thin Chainlit wrapper around the existing `TradingAgentsGraph` programmatic API. ~150 lines of new code in a single `app.py`.
## Components ## Components
### `app.py` (Chainlit entry point) ### `app.py` (Chainlit entry point)
- `@cl.on_chat_start` — Welcome message explaining usage (e.g., "Type a ticker like `NVDA` or `Analyze AAPL 2024-12-01`") - `@cl.on_chat_start` — Welcome message explaining usage (e.g., "Type a ticker like `NVDA` or `Analyze AAPL 2024-12-01`")
- `@cl.on_message` — Parse ticker + optional date from user message, create `TradingAgentsGraph` with Anthropic config, run `propagate()` in debug mode, stream Chainlit `Step` messages for each agent phase, send final decision as formatted message - `@cl.on_message` — Parse ticker + optional date from user message, create `TradingAgentsGraph` with Anthropic config, run `propagate()` in debug mode, stream Chainlit `Step` messages for each agent phase, send final decision as formatted message
### `Dockerfile` ### `Dockerfile`
- Python 3.13-slim base - Python 3.13-slim base
- Install requirements.txt - Install requirements.txt
- Expose `$PORT` - Expose `$PORT`
- `CMD: chainlit run app.py --host 0.0.0.0 --port $PORT` - `CMD: chainlit run app.py --host 0.0.0.0 --port $PORT`
### `railway.toml` ### `railway.toml`
- Build from Dockerfile - Build from Dockerfile
- Health check on `/` - Health check on `/`
### Railway Environment Variables ### Railway Environment Variables
- `ANTHROPIC_API_KEY` — required, for Claude models - `ANTHROPIC_API_KEY` — required, for Claude models
- `PORT` — auto-set by Railway - `PORT` — auto-set by Railway
## LLM Configuration ## LLM Configuration
- Provider: `anthropic` - Provider: `anthropic`
- Quick-think model: `claude-haiku-4-5-20251001` - Quick-think model: `claude-haiku-4-5-20251001`
- Deep-think model: `claude-sonnet-4-5-20241022` - Deep-think model: `claude-sonnet-4-5-20241022`
- Data vendor: `yfinance` (no extra API keys needed) - Data vendor: `yfinance` (no extra API keys needed)
## Data Flow ## Data Flow
``` ```
User message: "Analyze NVDA" User message: "Analyze NVDA"
-> Parse: ticker=NVDA, date=today -> Parse: ticker=NVDA, date=today
-> TradingAgentsGraph(config={anthropic, haiku/sonnet}) -> TradingAgentsGraph(config={anthropic, haiku/sonnet})
-> graph.propagate("NVDA", "2026-02-20") -> graph.propagate("NVDA", "2026-02-20")
-> Debug stream chunks -> Debug stream chunks
-> Each chunk -> Chainlit Step (Analyst, Research, Trading, Risk, Portfolio) -> Each chunk -> Chainlit Step (Analyst, Research, Trading, Risk, Portfolio)
-> Final decision -> formatted Chainlit message with markdown -> Final decision -> formatted Chainlit message with markdown
``` ```
## Message Parsing ## Message Parsing
Simple regex/string parsing: Simple regex/string parsing:
- `"NVDA"` -> ticker=NVDA, date=today - `"NVDA"` -> ticker=NVDA, date=today
- `"Analyze AAPL 2024-12-01"` -> ticker=AAPL, date=2024-12-01 - `"Analyze AAPL 2024-12-01"` -> ticker=AAPL, date=2024-12-01
- `"What's the outlook for TSLA?"` -> ticker=TSLA, date=today - `"What's the outlook for TSLA?"` -> ticker=TSLA, date=today
- Extract uppercase 1-5 letter words as potential tickers - Extract uppercase 1-5 letter words as potential tickers
## Deployment ## Deployment
1. Push changes to `github.com/dtarkent2-sys/TradingAgents` main branch 1. Push changes to `github.com/dtarkent2-sys/TradingAgents` main branch
2. Create Railway service from GitHub repo 2. Create Railway service from GitHub repo
3. Set `ANTHROPIC_API_KEY` env var 3. Set `ANTHROPIC_API_KEY` env var
4. Railway auto-deploys, Chainlit serves on assigned PORT 4. Railway auto-deploys, Chainlit serves on assigned PORT

66
main.py
View File

@ -1,33 +1,33 @@
from tradingagents.graph.trading_graph import TradingAgentsGraph from tradingagents.graph.trading_graph import TradingAgentsGraph
from tradingagents.default_config import DEFAULT_CONFIG from tradingagents.default_config import DEFAULT_CONFIG
import os import os
from dotenv import load_dotenv from dotenv import load_dotenv
# Load environment variables from .env file # Load environment variables from .env file
load_dotenv() load_dotenv()
# Create a custom config # Create a custom config
config = DEFAULT_CONFIG.copy() config = DEFAULT_CONFIG.copy()
config["deep_think_llm"] = os.environ.get("DEEP_THINK_LLM", "gpt-5-mini") config["deep_think_llm"] = os.environ.get("DEEP_THINK_LLM", "gpt-5-mini")
config["quick_think_llm"] = os.environ.get("QUICK_THINK_LLM", "gpt-5-mini") config["quick_think_llm"] = os.environ.get("QUICK_THINK_LLM", "gpt-5-mini")
config["max_debate_rounds"] = 1 # Increase debate rounds config["max_debate_rounds"] = 1 # Increase debate rounds
# Configure data vendors (default uses yfinance, no extra API keys needed) # Configure data vendors (default uses yfinance, no extra API keys needed)
config["data_vendors"] = { config["data_vendors"] = {
"core_stock_apis": "yfinance", # Options: alpha_vantage, yfinance "core_stock_apis": "yfinance", # Options: alpha_vantage, yfinance
"technical_indicators": "yfinance", # Options: alpha_vantage, yfinance "technical_indicators": "yfinance", # Options: alpha_vantage, yfinance
"fundamental_data": "yfinance", # Options: alpha_vantage, yfinance "fundamental_data": "yfinance", # Options: alpha_vantage, yfinance
"news_data": "yfinance", # Options: alpha_vantage, yfinance "news_data": "yfinance", # Options: alpha_vantage, yfinance
} }
# Initialize with custom config # Initialize with custom config
ta = TradingAgentsGraph(debug=True, config=config) ta = TradingAgentsGraph(debug=True, config=config)
# forward propagate # forward propagate
_, decision = ta.propagate("NVDA", "2024-05-10") _, decision = ta.propagate("NVDA", "2024-05-10")
print(decision) print(decision)
# Memorize mistakes and reflect # Memorize mistakes and reflect
# ta.reflect_and_remember(1000) # parameter is the position returns # ta.reflect_and_remember(1000) # parameter is the position returns

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -1,36 +1,36 @@
[build-system] [build-system]
requires = ["setuptools>=61.0"] requires = ["setuptools>=61.0"]
build-backend = "setuptools.build_meta" build-backend = "setuptools.build_meta"
[project] [project]
name = "tradingagents" name = "tradingagents"
version = "0.2.0" version = "0.2.0"
description = "TradingAgents: Multi-Agents LLM Financial Trading Framework" description = "TradingAgents: Multi-Agents LLM Financial Trading Framework"
readme = "README.md" readme = "README.md"
requires-python = ">=3.10" requires-python = ">=3.10"
dependencies = [ dependencies = [
"langchain-core>=0.3.81", "langchain-core>=0.3.81",
"langchain-anthropic>=0.3.15", "langchain-anthropic>=0.3.15",
"langchain-experimental>=0.3.4", "langchain-experimental>=0.3.4",
"langchain-google-genai>=2.1.5", "langchain-google-genai>=2.1.5",
"langchain-openai>=0.3.23", "langchain-openai>=0.3.23",
"langgraph>=0.4.8", "langgraph>=0.4.8",
"pandas>=2.3.0", "pandas>=2.3.0",
"pytz>=2025.2", "pytz>=2025.2",
"rank-bm25>=0.2.2", "rank-bm25>=0.2.2",
"requests>=2.32.4", "requests>=2.32.4",
"setuptools>=80.9.0", "setuptools>=80.9.0",
"stockstats>=0.6.5", "stockstats>=0.6.5",
"tqdm>=4.67.1", "tqdm>=4.67.1",
"typing-extensions>=4.14.0", "typing-extensions>=4.14.0",
"yfinance>=0.2.63", "yfinance>=0.2.63",
"fastapi>=0.115.0", "fastapi>=0.115.0",
"uvicorn[standard]>=0.30.0", "uvicorn[standard]>=0.30.0",
"sse-starlette>=2.0.0", "sse-starlette>=2.0.0",
] ]
[project.scripts] [project.scripts]
tradingagents = "cli.main:app" tradingagents = "cli.main:app"
[tool.setuptools.packages.find] [tool.setuptools.packages.find]
include = ["tradingagents*", "cli*"] include = ["tradingagents*", "cli*"]

22
test.py
View File

@ -1,11 +1,11 @@
import time import time
from tradingagents.dataflows.y_finance import get_YFin_data_online, get_stock_stats_indicators_window, get_balance_sheet as get_yfinance_balance_sheet, get_cashflow as get_yfinance_cashflow, get_income_statement as get_yfinance_income_statement, get_insider_transactions as get_yfinance_insider_transactions from tradingagents.dataflows.y_finance import get_YFin_data_online, get_stock_stats_indicators_window, get_balance_sheet as get_yfinance_balance_sheet, get_cashflow as get_yfinance_cashflow, get_income_statement as get_yfinance_income_statement, get_insider_transactions as get_yfinance_insider_transactions
print("Testing optimized implementation with 30-day lookback:") print("Testing optimized implementation with 30-day lookback:")
start_time = time.time() start_time = time.time()
result = get_stock_stats_indicators_window("AAPL", "macd", "2024-11-01", 30) result = get_stock_stats_indicators_window("AAPL", "macd", "2024-11-01", 30)
end_time = time.time() end_time = time.time()
print(f"Execution time: {end_time - start_time:.2f} seconds") print(f"Execution time: {end_time - start_time:.2f} seconds")
print(f"Result length: {len(result)} characters") print(f"Result length: {len(result)} characters")
print(result) print(result)

View File

@ -1,40 +1,40 @@
from .utils.agent_utils import create_msg_delete from .utils.agent_utils import create_msg_delete
from .utils.agent_states import AgentState, InvestDebateState, RiskDebateState from .utils.agent_states import AgentState, InvestDebateState, RiskDebateState
from .utils.memory import FinancialSituationMemory from .utils.memory import FinancialSituationMemory
from .analysts.fundamentals_analyst import create_fundamentals_analyst from .analysts.fundamentals_analyst import create_fundamentals_analyst
from .analysts.market_analyst import create_market_analyst from .analysts.market_analyst import create_market_analyst
from .analysts.news_analyst import create_news_analyst from .analysts.news_analyst import create_news_analyst
from .analysts.social_media_analyst import create_social_media_analyst from .analysts.social_media_analyst import create_social_media_analyst
from .researchers.bear_researcher import create_bear_researcher from .researchers.bear_researcher import create_bear_researcher
from .researchers.bull_researcher import create_bull_researcher from .researchers.bull_researcher import create_bull_researcher
from .risk_mgmt.aggressive_debator import create_aggressive_debator from .risk_mgmt.aggressive_debator import create_aggressive_debator
from .risk_mgmt.conservative_debator import create_conservative_debator from .risk_mgmt.conservative_debator import create_conservative_debator
from .risk_mgmt.neutral_debator import create_neutral_debator from .risk_mgmt.neutral_debator import create_neutral_debator
from .managers.research_manager import create_research_manager from .managers.research_manager import create_research_manager
from .managers.risk_manager import create_risk_manager from .managers.risk_manager import create_risk_manager
from .trader.trader import create_trader from .trader.trader import create_trader
__all__ = [ __all__ = [
"FinancialSituationMemory", "FinancialSituationMemory",
"AgentState", "AgentState",
"create_msg_delete", "create_msg_delete",
"InvestDebateState", "InvestDebateState",
"RiskDebateState", "RiskDebateState",
"create_bear_researcher", "create_bear_researcher",
"create_bull_researcher", "create_bull_researcher",
"create_research_manager", "create_research_manager",
"create_fundamentals_analyst", "create_fundamentals_analyst",
"create_market_analyst", "create_market_analyst",
"create_neutral_debator", "create_neutral_debator",
"create_news_analyst", "create_news_analyst",
"create_aggressive_debator", "create_aggressive_debator",
"create_risk_manager", "create_risk_manager",
"create_conservative_debator", "create_conservative_debator",
"create_social_media_analyst", "create_social_media_analyst",
"create_trader", "create_trader",
] ]

View File

@ -1,63 +1,63 @@
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
import time import time
import json import json
from tradingagents.agents.utils.agent_utils import get_fundamentals, get_balance_sheet, get_cashflow, get_income_statement, get_insider_transactions from tradingagents.agents.utils.agent_utils import get_fundamentals, get_balance_sheet, get_cashflow, get_income_statement, get_insider_transactions
from tradingagents.dataflows.config import get_config from tradingagents.dataflows.config import get_config
def create_fundamentals_analyst(llm): def create_fundamentals_analyst(llm):
def fundamentals_analyst_node(state): def fundamentals_analyst_node(state):
current_date = state["trade_date"] current_date = state["trade_date"]
ticker = state["company_of_interest"] ticker = state["company_of_interest"]
company_name = state["company_of_interest"] company_name = state["company_of_interest"]
tools = [ tools = [
get_fundamentals, get_fundamentals,
get_balance_sheet, get_balance_sheet,
get_cashflow, get_cashflow,
get_income_statement, get_income_statement,
] ]
system_message = ( system_message = (
"You are a researcher tasked with analyzing fundamental information over the past week about a company. Please write a comprehensive report of the company's fundamental information such as financial documents, company profile, basic company financials, and company financial history to gain a full view of the company's fundamental information to inform traders. Make sure to include as much detail as possible. Do not simply state the trends are mixed, provide detailed and finegrained analysis and insights that may help traders make decisions." "You are a researcher tasked with analyzing fundamental information over the past week about a company. Please write a comprehensive report of the company's fundamental information such as financial documents, company profile, basic company financials, and company financial history to gain a full view of the company's fundamental information to inform traders. Make sure to include as much detail as possible. Do not simply state the trends are mixed, provide detailed and finegrained analysis and insights that may help traders make decisions."
+ " Make sure to append a Markdown table at the end of the report to organize key points in the report, organized and easy to read." + " Make sure to append a Markdown table at the end of the report to organize key points in the report, organized and easy to read."
+ " Use the available tools: `get_fundamentals` for comprehensive company analysis, `get_balance_sheet`, `get_cashflow`, and `get_income_statement` for specific financial statements.", + " Use the available tools: `get_fundamentals` for comprehensive company analysis, `get_balance_sheet`, `get_cashflow`, and `get_income_statement` for specific financial statements.",
) )
prompt = ChatPromptTemplate.from_messages( prompt = ChatPromptTemplate.from_messages(
[ [
( (
"system", "system",
"You are a helpful AI assistant, collaborating with other assistants." "You are a helpful AI assistant, collaborating with other assistants."
" Use the provided tools to progress towards answering the question." " Use the provided tools to progress towards answering the question."
" If you are unable to fully answer, that's OK; another assistant with different tools" " If you are unable to fully answer, that's OK; another assistant with different tools"
" will help where you left off. Execute what you can to make progress." " will help where you left off. Execute what you can to make progress."
" If you or any other assistant has the FINAL TRANSACTION PROPOSAL: **BUY/HOLD/SELL** or deliverable," " If you or any other assistant has the FINAL TRANSACTION PROPOSAL: **BUY/HOLD/SELL** or deliverable,"
" prefix your response with FINAL TRANSACTION PROPOSAL: **BUY/HOLD/SELL** so the team knows to stop." " prefix your response with FINAL TRANSACTION PROPOSAL: **BUY/HOLD/SELL** so the team knows to stop."
" You have access to the following tools: {tool_names}.\n{system_message}" " You have access to the following tools: {tool_names}.\n{system_message}"
"For your reference, the current date is {current_date}. The company we want to look at is {ticker}", "For your reference, the current date is {current_date}. The company we want to look at is {ticker}",
), ),
MessagesPlaceholder(variable_name="messages"), MessagesPlaceholder(variable_name="messages"),
] ]
) )
prompt = prompt.partial(system_message=system_message) prompt = prompt.partial(system_message=system_message)
prompt = prompt.partial(tool_names=", ".join([tool.name for tool in tools])) prompt = prompt.partial(tool_names=", ".join([tool.name for tool in tools]))
prompt = prompt.partial(current_date=current_date) prompt = prompt.partial(current_date=current_date)
prompt = prompt.partial(ticker=ticker) prompt = prompt.partial(ticker=ticker)
chain = prompt | llm.bind_tools(tools) chain = prompt | llm.bind_tools(tools)
result = chain.invoke(state["messages"]) result = chain.invoke(state["messages"])
report = "" report = ""
if len(result.tool_calls) == 0: if len(result.tool_calls) == 0:
report = result.content report = result.content
return { return {
"messages": [result], "messages": [result],
"fundamentals_report": report, "fundamentals_report": report,
} }
return fundamentals_analyst_node return fundamentals_analyst_node

View File

@ -1,85 +1,85 @@
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
import time import time
import json import json
from tradingagents.agents.utils.agent_utils import get_stock_data, get_indicators from tradingagents.agents.utils.agent_utils import get_stock_data, get_indicators
from tradingagents.dataflows.config import get_config from tradingagents.dataflows.config import get_config
def create_market_analyst(llm): def create_market_analyst(llm):
def market_analyst_node(state): def market_analyst_node(state):
current_date = state["trade_date"] current_date = state["trade_date"]
ticker = state["company_of_interest"] ticker = state["company_of_interest"]
company_name = state["company_of_interest"] company_name = state["company_of_interest"]
tools = [ tools = [
get_stock_data, get_stock_data,
get_indicators, get_indicators,
] ]
system_message = ( system_message = (
"""You are a trading assistant tasked with analyzing financial markets. Your role is to select the **most relevant indicators** for a given market condition or trading strategy from the following list. The goal is to choose up to **8 indicators** that provide complementary insights without redundancy. Categories and each category's indicators are: """You are a trading assistant tasked with analyzing financial markets. Your role is to select the **most relevant indicators** for a given market condition or trading strategy from the following list. The goal is to choose up to **8 indicators** that provide complementary insights without redundancy. Categories and each category's indicators are:
Moving Averages: Moving Averages:
- close_50_sma: 50 SMA: A medium-term trend indicator. Usage: Identify trend direction and serve as dynamic support/resistance. Tips: It lags price; combine with faster indicators for timely signals. - close_50_sma: 50 SMA: A medium-term trend indicator. Usage: Identify trend direction and serve as dynamic support/resistance. Tips: It lags price; combine with faster indicators for timely signals.
- close_200_sma: 200 SMA: A long-term trend benchmark. Usage: Confirm overall market trend and identify golden/death cross setups. Tips: It reacts slowly; best for strategic trend confirmation rather than frequent trading entries. - close_200_sma: 200 SMA: A long-term trend benchmark. Usage: Confirm overall market trend and identify golden/death cross setups. Tips: It reacts slowly; best for strategic trend confirmation rather than frequent trading entries.
- close_10_ema: 10 EMA: A responsive short-term average. Usage: Capture quick shifts in momentum and potential entry points. Tips: Prone to noise in choppy markets; use alongside longer averages for filtering false signals. - close_10_ema: 10 EMA: A responsive short-term average. Usage: Capture quick shifts in momentum and potential entry points. Tips: Prone to noise in choppy markets; use alongside longer averages for filtering false signals.
MACD Related: MACD Related:
- macd: MACD: Computes momentum via differences of EMAs. Usage: Look for crossovers and divergence as signals of trend changes. Tips: Confirm with other indicators in low-volatility or sideways markets. - macd: MACD: Computes momentum via differences of EMAs. Usage: Look for crossovers and divergence as signals of trend changes. Tips: Confirm with other indicators in low-volatility or sideways markets.
- macds: MACD Signal: An EMA smoothing of the MACD line. Usage: Use crossovers with the MACD line to trigger trades. Tips: Should be part of a broader strategy to avoid false positives. - macds: MACD Signal: An EMA smoothing of the MACD line. Usage: Use crossovers with the MACD line to trigger trades. Tips: Should be part of a broader strategy to avoid false positives.
- macdh: MACD Histogram: Shows the gap between the MACD line and its signal. Usage: Visualize momentum strength and spot divergence early. Tips: Can be volatile; complement with additional filters in fast-moving markets. - macdh: MACD Histogram: Shows the gap between the MACD line and its signal. Usage: Visualize momentum strength and spot divergence early. Tips: Can be volatile; complement with additional filters in fast-moving markets.
Momentum Indicators: Momentum Indicators:
- rsi: RSI: Measures momentum to flag overbought/oversold conditions. Usage: Apply 70/30 thresholds and watch for divergence to signal reversals. Tips: In strong trends, RSI may remain extreme; always cross-check with trend analysis. - rsi: RSI: Measures momentum to flag overbought/oversold conditions. Usage: Apply 70/30 thresholds and watch for divergence to signal reversals. Tips: In strong trends, RSI may remain extreme; always cross-check with trend analysis.
Volatility Indicators: Volatility Indicators:
- boll: Bollinger Middle: A 20 SMA serving as the basis for Bollinger Bands. Usage: Acts as a dynamic benchmark for price movement. Tips: Combine with the upper and lower bands to effectively spot breakouts or reversals. - boll: Bollinger Middle: A 20 SMA serving as the basis for Bollinger Bands. Usage: Acts as a dynamic benchmark for price movement. Tips: Combine with the upper and lower bands to effectively spot breakouts or reversals.
- boll_ub: Bollinger Upper Band: Typically 2 standard deviations above the middle line. Usage: Signals potential overbought conditions and breakout zones. Tips: Confirm signals with other tools; prices may ride the band in strong trends. - boll_ub: Bollinger Upper Band: Typically 2 standard deviations above the middle line. Usage: Signals potential overbought conditions and breakout zones. Tips: Confirm signals with other tools; prices may ride the band in strong trends.
- boll_lb: Bollinger Lower Band: Typically 2 standard deviations below the middle line. Usage: Indicates potential oversold conditions. Tips: Use additional analysis to avoid false reversal signals. - boll_lb: Bollinger Lower Band: Typically 2 standard deviations below the middle line. Usage: Indicates potential oversold conditions. Tips: Use additional analysis to avoid false reversal signals.
- atr: ATR: Averages true range to measure volatility. Usage: Set stop-loss levels and adjust position sizes based on current market volatility. Tips: It's a reactive measure, so use it as part of a broader risk management strategy. - atr: ATR: Averages true range to measure volatility. Usage: Set stop-loss levels and adjust position sizes based on current market volatility. Tips: It's a reactive measure, so use it as part of a broader risk management strategy.
Volume-Based Indicators: Volume-Based Indicators:
- vwma: VWMA: A moving average weighted by volume. Usage: Confirm trends by integrating price action with volume data. Tips: Watch for skewed results from volume spikes; use in combination with other volume analyses. - vwma: VWMA: A moving average weighted by volume. Usage: Confirm trends by integrating price action with volume data. Tips: Watch for skewed results from volume spikes; use in combination with other volume analyses.
- Select indicators that provide diverse and complementary information. Avoid redundancy (e.g., do not select both rsi and stochrsi). Also briefly explain why they are suitable for the given market context. When you tool call, please use the exact name of the indicators provided above as they are defined parameters, otherwise your call will fail. Please make sure to call get_stock_data first to retrieve the CSV that is needed to generate indicators. Then use get_indicators with the specific indicator names. Write a very detailed and nuanced report of the trends you observe. Do not simply state the trends are mixed, provide detailed and finegrained analysis and insights that may help traders make decisions.""" - Select indicators that provide diverse and complementary information. Avoid redundancy (e.g., do not select both rsi and stochrsi). Also briefly explain why they are suitable for the given market context. When you tool call, please use the exact name of the indicators provided above as they are defined parameters, otherwise your call will fail. Please make sure to call get_stock_data first to retrieve the CSV that is needed to generate indicators. Then use get_indicators with the specific indicator names. Write a very detailed and nuanced report of the trends you observe. Do not simply state the trends are mixed, provide detailed and finegrained analysis and insights that may help traders make decisions."""
+ """ Make sure to append a Markdown table at the end of the report to organize key points in the report, organized and easy to read.""" + """ Make sure to append a Markdown table at the end of the report to organize key points in the report, organized and easy to read."""
) )
prompt = ChatPromptTemplate.from_messages( prompt = ChatPromptTemplate.from_messages(
[ [
( (
"system", "system",
"You are a helpful AI assistant, collaborating with other assistants." "You are a helpful AI assistant, collaborating with other assistants."
" Use the provided tools to progress towards answering the question." " Use the provided tools to progress towards answering the question."
" If you are unable to fully answer, that's OK; another assistant with different tools" " If you are unable to fully answer, that's OK; another assistant with different tools"
" will help where you left off. Execute what you can to make progress." " will help where you left off. Execute what you can to make progress."
" If you or any other assistant has the FINAL TRANSACTION PROPOSAL: **BUY/HOLD/SELL** or deliverable," " If you or any other assistant has the FINAL TRANSACTION PROPOSAL: **BUY/HOLD/SELL** or deliverable,"
" prefix your response with FINAL TRANSACTION PROPOSAL: **BUY/HOLD/SELL** so the team knows to stop." " prefix your response with FINAL TRANSACTION PROPOSAL: **BUY/HOLD/SELL** so the team knows to stop."
" You have access to the following tools: {tool_names}.\n{system_message}" " You have access to the following tools: {tool_names}.\n{system_message}"
"For your reference, the current date is {current_date}. The company we want to look at is {ticker}", "For your reference, the current date is {current_date}. The company we want to look at is {ticker}",
), ),
MessagesPlaceholder(variable_name="messages"), MessagesPlaceholder(variable_name="messages"),
] ]
) )
prompt = prompt.partial(system_message=system_message) prompt = prompt.partial(system_message=system_message)
prompt = prompt.partial(tool_names=", ".join([tool.name for tool in tools])) prompt = prompt.partial(tool_names=", ".join([tool.name for tool in tools]))
prompt = prompt.partial(current_date=current_date) prompt = prompt.partial(current_date=current_date)
prompt = prompt.partial(ticker=ticker) prompt = prompt.partial(ticker=ticker)
chain = prompt | llm.bind_tools(tools) chain = prompt | llm.bind_tools(tools)
result = chain.invoke(state["messages"]) result = chain.invoke(state["messages"])
report = "" report = ""
if len(result.tool_calls) == 0: if len(result.tool_calls) == 0:
report = result.content report = result.content
return { return {
"messages": [result], "messages": [result],
"market_report": report, "market_report": report,
} }
return market_analyst_node return market_analyst_node

View File

@ -1,58 +1,58 @@
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
import time import time
import json import json
from tradingagents.agents.utils.agent_utils import get_news, get_global_news from tradingagents.agents.utils.agent_utils import get_news, get_global_news
from tradingagents.dataflows.config import get_config from tradingagents.dataflows.config import get_config
def create_news_analyst(llm): def create_news_analyst(llm):
def news_analyst_node(state): def news_analyst_node(state):
current_date = state["trade_date"] current_date = state["trade_date"]
ticker = state["company_of_interest"] ticker = state["company_of_interest"]
tools = [ tools = [
get_news, get_news,
get_global_news, get_global_news,
] ]
system_message = ( system_message = (
"You are a news researcher tasked with analyzing recent news and trends over the past week. Please write a comprehensive report of the current state of the world that is relevant for trading and macroeconomics. Use the available tools: get_news(query, start_date, end_date) for company-specific or targeted news searches, and get_global_news(curr_date, look_back_days, limit) for broader macroeconomic news. Do not simply state the trends are mixed, provide detailed and finegrained analysis and insights that may help traders make decisions." "You are a news researcher tasked with analyzing recent news and trends over the past week. Please write a comprehensive report of the current state of the world that is relevant for trading and macroeconomics. Use the available tools: get_news(query, start_date, end_date) for company-specific or targeted news searches, and get_global_news(curr_date, look_back_days, limit) for broader macroeconomic news. Do not simply state the trends are mixed, provide detailed and finegrained analysis and insights that may help traders make decisions."
+ """ Make sure to append a Markdown table at the end of the report to organize key points in the report, organized and easy to read.""" + """ Make sure to append a Markdown table at the end of the report to organize key points in the report, organized and easy to read."""
) )
prompt = ChatPromptTemplate.from_messages( prompt = ChatPromptTemplate.from_messages(
[ [
( (
"system", "system",
"You are a helpful AI assistant, collaborating with other assistants." "You are a helpful AI assistant, collaborating with other assistants."
" Use the provided tools to progress towards answering the question." " Use the provided tools to progress towards answering the question."
" If you are unable to fully answer, that's OK; another assistant with different tools" " If you are unable to fully answer, that's OK; another assistant with different tools"
" will help where you left off. Execute what you can to make progress." " will help where you left off. Execute what you can to make progress."
" If you or any other assistant has the FINAL TRANSACTION PROPOSAL: **BUY/HOLD/SELL** or deliverable," " If you or any other assistant has the FINAL TRANSACTION PROPOSAL: **BUY/HOLD/SELL** or deliverable,"
" prefix your response with FINAL TRANSACTION PROPOSAL: **BUY/HOLD/SELL** so the team knows to stop." " prefix your response with FINAL TRANSACTION PROPOSAL: **BUY/HOLD/SELL** so the team knows to stop."
" You have access to the following tools: {tool_names}.\n{system_message}" " You have access to the following tools: {tool_names}.\n{system_message}"
"For your reference, the current date is {current_date}. We are looking at the company {ticker}", "For your reference, the current date is {current_date}. We are looking at the company {ticker}",
), ),
MessagesPlaceholder(variable_name="messages"), MessagesPlaceholder(variable_name="messages"),
] ]
) )
prompt = prompt.partial(system_message=system_message) prompt = prompt.partial(system_message=system_message)
prompt = prompt.partial(tool_names=", ".join([tool.name for tool in tools])) prompt = prompt.partial(tool_names=", ".join([tool.name for tool in tools]))
prompt = prompt.partial(current_date=current_date) prompt = prompt.partial(current_date=current_date)
prompt = prompt.partial(ticker=ticker) prompt = prompt.partial(ticker=ticker)
chain = prompt | llm.bind_tools(tools) chain = prompt | llm.bind_tools(tools)
result = chain.invoke(state["messages"]) result = chain.invoke(state["messages"])
report = "" report = ""
if len(result.tool_calls) == 0: if len(result.tool_calls) == 0:
report = result.content report = result.content
return { return {
"messages": [result], "messages": [result],
"news_report": report, "news_report": report,
} }
return news_analyst_node return news_analyst_node

View File

@ -1,59 +1,59 @@
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
import time import time
import json import json
from tradingagents.agents.utils.agent_utils import get_news from tradingagents.agents.utils.agent_utils import get_news
from tradingagents.dataflows.config import get_config from tradingagents.dataflows.config import get_config
def create_social_media_analyst(llm): def create_social_media_analyst(llm):
def social_media_analyst_node(state): def social_media_analyst_node(state):
current_date = state["trade_date"] current_date = state["trade_date"]
ticker = state["company_of_interest"] ticker = state["company_of_interest"]
company_name = state["company_of_interest"] company_name = state["company_of_interest"]
tools = [ tools = [
get_news, get_news,
] ]
system_message = ( system_message = (
"You are a social media and company specific news researcher/analyst tasked with analyzing social media posts, recent company news, and public sentiment for a specific company over the past week. You will be given a company's name your objective is to write a comprehensive long report detailing your analysis, insights, and implications for traders and investors on this company's current state after looking at social media and what people are saying about that company, analyzing sentiment data of what people feel each day about the company, and looking at recent company news. Use the get_news(query, start_date, end_date) tool to search for company-specific news and social media discussions. Try to look at all sources possible from social media to sentiment to news. Do not simply state the trends are mixed, provide detailed and finegrained analysis and insights that may help traders make decisions." "You are a social media and company specific news researcher/analyst tasked with analyzing social media posts, recent company news, and public sentiment for a specific company over the past week. You will be given a company's name your objective is to write a comprehensive long report detailing your analysis, insights, and implications for traders and investors on this company's current state after looking at social media and what people are saying about that company, analyzing sentiment data of what people feel each day about the company, and looking at recent company news. Use the get_news(query, start_date, end_date) tool to search for company-specific news and social media discussions. Try to look at all sources possible from social media to sentiment to news. Do not simply state the trends are mixed, provide detailed and finegrained analysis and insights that may help traders make decisions."
+ """ Make sure to append a Markdown table at the end of the report to organize key points in the report, organized and easy to read.""", + """ Make sure to append a Markdown table at the end of the report to organize key points in the report, organized and easy to read.""",
) )
prompt = ChatPromptTemplate.from_messages( prompt = ChatPromptTemplate.from_messages(
[ [
( (
"system", "system",
"You are a helpful AI assistant, collaborating with other assistants." "You are a helpful AI assistant, collaborating with other assistants."
" Use the provided tools to progress towards answering the question." " Use the provided tools to progress towards answering the question."
" If you are unable to fully answer, that's OK; another assistant with different tools" " If you are unable to fully answer, that's OK; another assistant with different tools"
" will help where you left off. Execute what you can to make progress." " will help where you left off. Execute what you can to make progress."
" If you or any other assistant has the FINAL TRANSACTION PROPOSAL: **BUY/HOLD/SELL** or deliverable," " If you or any other assistant has the FINAL TRANSACTION PROPOSAL: **BUY/HOLD/SELL** or deliverable,"
" prefix your response with FINAL TRANSACTION PROPOSAL: **BUY/HOLD/SELL** so the team knows to stop." " prefix your response with FINAL TRANSACTION PROPOSAL: **BUY/HOLD/SELL** so the team knows to stop."
" You have access to the following tools: {tool_names}.\n{system_message}" " You have access to the following tools: {tool_names}.\n{system_message}"
"For your reference, the current date is {current_date}. The current company we want to analyze is {ticker}", "For your reference, the current date is {current_date}. The current company we want to analyze is {ticker}",
), ),
MessagesPlaceholder(variable_name="messages"), MessagesPlaceholder(variable_name="messages"),
] ]
) )
prompt = prompt.partial(system_message=system_message) prompt = prompt.partial(system_message=system_message)
prompt = prompt.partial(tool_names=", ".join([tool.name for tool in tools])) prompt = prompt.partial(tool_names=", ".join([tool.name for tool in tools]))
prompt = prompt.partial(current_date=current_date) prompt = prompt.partial(current_date=current_date)
prompt = prompt.partial(ticker=ticker) prompt = prompt.partial(ticker=ticker)
chain = prompt | llm.bind_tools(tools) chain = prompt | llm.bind_tools(tools)
result = chain.invoke(state["messages"]) result = chain.invoke(state["messages"])
report = "" report = ""
if len(result.tool_calls) == 0: if len(result.tool_calls) == 0:
report = result.content report = result.content
return { return {
"messages": [result], "messages": [result],
"sentiment_report": report, "sentiment_report": report,
} }
return social_media_analyst_node return social_media_analyst_node

View File

@ -1,55 +1,55 @@
import time import time
import json import json
def create_research_manager(llm, memory): def create_research_manager(llm, memory):
def research_manager_node(state) -> dict: def research_manager_node(state) -> dict:
history = state["investment_debate_state"].get("history", "") history = state["investment_debate_state"].get("history", "")
market_research_report = state["market_report"] market_research_report = state["market_report"]
sentiment_report = state["sentiment_report"] sentiment_report = state["sentiment_report"]
news_report = state["news_report"] news_report = state["news_report"]
fundamentals_report = state["fundamentals_report"] fundamentals_report = state["fundamentals_report"]
investment_debate_state = state["investment_debate_state"] investment_debate_state = state["investment_debate_state"]
curr_situation = f"{market_research_report}\n\n{sentiment_report}\n\n{news_report}\n\n{fundamentals_report}" curr_situation = f"{market_research_report}\n\n{sentiment_report}\n\n{news_report}\n\n{fundamentals_report}"
past_memories = memory.get_memories(curr_situation, n_matches=2) past_memories = memory.get_memories(curr_situation, n_matches=2)
past_memory_str = "" past_memory_str = ""
for i, rec in enumerate(past_memories, 1): for i, rec in enumerate(past_memories, 1):
past_memory_str += rec["recommendation"] + "\n\n" past_memory_str += rec["recommendation"] + "\n\n"
prompt = f"""As the portfolio manager and debate facilitator, your role is to critically evaluate this round of debate and make a definitive decision: align with the bear analyst, the bull analyst, or choose Hold only if it is strongly justified based on the arguments presented. prompt = f"""As the portfolio manager and debate facilitator, your role is to critically evaluate this round of debate and make a definitive decision: align with the bear analyst, the bull analyst, or choose Hold only if it is strongly justified based on the arguments presented.
Summarize the key points from both sides concisely, focusing on the most compelling evidence or reasoning. Your recommendationBuy, Sell, or Holdmust be clear and actionable. Avoid defaulting to Hold simply because both sides have valid points; commit to a stance grounded in the debate's strongest arguments. Summarize the key points from both sides concisely, focusing on the most compelling evidence or reasoning. Your recommendationBuy, Sell, or Holdmust be clear and actionable. Avoid defaulting to Hold simply because both sides have valid points; commit to a stance grounded in the debate's strongest arguments.
Additionally, develop a detailed investment plan for the trader. This should include: Additionally, develop a detailed investment plan for the trader. This should include:
Your Recommendation: A decisive stance supported by the most convincing arguments. Your Recommendation: A decisive stance supported by the most convincing arguments.
Rationale: An explanation of why these arguments lead to your conclusion. Rationale: An explanation of why these arguments lead to your conclusion.
Strategic Actions: Concrete steps for implementing the recommendation. Strategic Actions: Concrete steps for implementing the recommendation.
Take into account your past mistakes on similar situations. Use these insights to refine your decision-making and ensure you are learning and improving. Present your analysis conversationally, as if speaking naturally, without special formatting. Take into account your past mistakes on similar situations. Use these insights to refine your decision-making and ensure you are learning and improving. Present your analysis conversationally, as if speaking naturally, without special formatting.
Here are your past reflections on mistakes: Here are your past reflections on mistakes:
\"{past_memory_str}\" \"{past_memory_str}\"
Here is the debate: Here is the debate:
Debate History: Debate History:
{history}""" {history}"""
response = llm.invoke(prompt) response = llm.invoke(prompt)
new_investment_debate_state = { new_investment_debate_state = {
"judge_decision": response.content, "judge_decision": response.content,
"history": investment_debate_state.get("history", ""), "history": investment_debate_state.get("history", ""),
"bear_history": investment_debate_state.get("bear_history", ""), "bear_history": investment_debate_state.get("bear_history", ""),
"bull_history": investment_debate_state.get("bull_history", ""), "bull_history": investment_debate_state.get("bull_history", ""),
"current_response": response.content, "current_response": response.content,
"count": investment_debate_state["count"], "count": investment_debate_state["count"],
} }
return { return {
"investment_debate_state": new_investment_debate_state, "investment_debate_state": new_investment_debate_state,
"investment_plan": response.content, "investment_plan": response.content,
} }
return research_manager_node return research_manager_node

View File

@ -1,66 +1,66 @@
import time import time
import json import json
def create_risk_manager(llm, memory): def create_risk_manager(llm, memory):
def risk_manager_node(state) -> dict: def risk_manager_node(state) -> dict:
company_name = state["company_of_interest"] company_name = state["company_of_interest"]
history = state["risk_debate_state"]["history"] history = state["risk_debate_state"]["history"]
risk_debate_state = state["risk_debate_state"] risk_debate_state = state["risk_debate_state"]
market_research_report = state["market_report"] market_research_report = state["market_report"]
news_report = state["news_report"] news_report = state["news_report"]
fundamentals_report = state["news_report"] fundamentals_report = state["news_report"]
sentiment_report = state["sentiment_report"] sentiment_report = state["sentiment_report"]
trader_plan = state["investment_plan"] trader_plan = state["investment_plan"]
curr_situation = f"{market_research_report}\n\n{sentiment_report}\n\n{news_report}\n\n{fundamentals_report}" curr_situation = f"{market_research_report}\n\n{sentiment_report}\n\n{news_report}\n\n{fundamentals_report}"
past_memories = memory.get_memories(curr_situation, n_matches=2) past_memories = memory.get_memories(curr_situation, n_matches=2)
past_memory_str = "" past_memory_str = ""
for i, rec in enumerate(past_memories, 1): for i, rec in enumerate(past_memories, 1):
past_memory_str += rec["recommendation"] + "\n\n" past_memory_str += rec["recommendation"] + "\n\n"
prompt = f"""As the Risk Management Judge and Debate Facilitator, your goal is to evaluate the debate between three risk analysts—Aggressive, Neutral, and Conservative—and determine the best course of action for the trader. Your decision must result in a clear recommendation: Buy, Sell, or Hold. Choose Hold only if strongly justified by specific arguments, not as a fallback when all sides seem valid. Strive for clarity and decisiveness. prompt = f"""As the Risk Management Judge and Debate Facilitator, your goal is to evaluate the debate between three risk analysts—Aggressive, Neutral, and Conservative—and determine the best course of action for the trader. Your decision must result in a clear recommendation: Buy, Sell, or Hold. Choose Hold only if strongly justified by specific arguments, not as a fallback when all sides seem valid. Strive for clarity and decisiveness.
Guidelines for Decision-Making: Guidelines for Decision-Making:
1. **Summarize Key Arguments**: Extract the strongest points from each analyst, focusing on relevance to the context. 1. **Summarize Key Arguments**: Extract the strongest points from each analyst, focusing on relevance to the context.
2. **Provide Rationale**: Support your recommendation with direct quotes and counterarguments from the debate. 2. **Provide Rationale**: Support your recommendation with direct quotes and counterarguments from the debate.
3. **Refine the Trader's Plan**: Start with the trader's original plan, **{trader_plan}**, and adjust it based on the analysts' insights. 3. **Refine the Trader's Plan**: Start with the trader's original plan, **{trader_plan}**, and adjust it based on the analysts' insights.
4. **Learn from Past Mistakes**: Use lessons from **{past_memory_str}** to address prior misjudgments and improve the decision you are making now to make sure you don't make a wrong BUY/SELL/HOLD call that loses money. 4. **Learn from Past Mistakes**: Use lessons from **{past_memory_str}** to address prior misjudgments and improve the decision you are making now to make sure you don't make a wrong BUY/SELL/HOLD call that loses money.
Deliverables: Deliverables:
- A clear and actionable recommendation: Buy, Sell, or Hold. - A clear and actionable recommendation: Buy, Sell, or Hold.
- Detailed reasoning anchored in the debate and past reflections. - Detailed reasoning anchored in the debate and past reflections.
--- ---
**Analysts Debate History:** **Analysts Debate History:**
{history} {history}
--- ---
Focus on actionable insights and continuous improvement. Build on past lessons, critically evaluate all perspectives, and ensure each decision advances better outcomes.""" Focus on actionable insights and continuous improvement. Build on past lessons, critically evaluate all perspectives, and ensure each decision advances better outcomes."""
response = llm.invoke(prompt) response = llm.invoke(prompt)
new_risk_debate_state = { new_risk_debate_state = {
"judge_decision": response.content, "judge_decision": response.content,
"history": risk_debate_state["history"], "history": risk_debate_state["history"],
"aggressive_history": risk_debate_state["aggressive_history"], "aggressive_history": risk_debate_state["aggressive_history"],
"conservative_history": risk_debate_state["conservative_history"], "conservative_history": risk_debate_state["conservative_history"],
"neutral_history": risk_debate_state["neutral_history"], "neutral_history": risk_debate_state["neutral_history"],
"latest_speaker": "Judge", "latest_speaker": "Judge",
"current_aggressive_response": risk_debate_state["current_aggressive_response"], "current_aggressive_response": risk_debate_state["current_aggressive_response"],
"current_conservative_response": risk_debate_state["current_conservative_response"], "current_conservative_response": risk_debate_state["current_conservative_response"],
"current_neutral_response": risk_debate_state["current_neutral_response"], "current_neutral_response": risk_debate_state["current_neutral_response"],
"count": risk_debate_state["count"], "count": risk_debate_state["count"],
} }
return { return {
"risk_debate_state": new_risk_debate_state, "risk_debate_state": new_risk_debate_state,
"final_trade_decision": response.content, "final_trade_decision": response.content,
} }
return risk_manager_node return risk_manager_node

View File

@ -1,61 +1,61 @@
from langchain_core.messages import AIMessage from langchain_core.messages import AIMessage
import time import time
import json import json
def create_bear_researcher(llm, memory): def create_bear_researcher(llm, memory):
def bear_node(state) -> dict: def bear_node(state) -> dict:
investment_debate_state = state["investment_debate_state"] investment_debate_state = state["investment_debate_state"]
history = investment_debate_state.get("history", "") history = investment_debate_state.get("history", "")
bear_history = investment_debate_state.get("bear_history", "") bear_history = investment_debate_state.get("bear_history", "")
current_response = investment_debate_state.get("current_response", "") current_response = investment_debate_state.get("current_response", "")
market_research_report = state["market_report"] market_research_report = state["market_report"]
sentiment_report = state["sentiment_report"] sentiment_report = state["sentiment_report"]
news_report = state["news_report"] news_report = state["news_report"]
fundamentals_report = state["fundamentals_report"] fundamentals_report = state["fundamentals_report"]
curr_situation = f"{market_research_report}\n\n{sentiment_report}\n\n{news_report}\n\n{fundamentals_report}" curr_situation = f"{market_research_report}\n\n{sentiment_report}\n\n{news_report}\n\n{fundamentals_report}"
past_memories = memory.get_memories(curr_situation, n_matches=2) past_memories = memory.get_memories(curr_situation, n_matches=2)
past_memory_str = "" past_memory_str = ""
for i, rec in enumerate(past_memories, 1): for i, rec in enumerate(past_memories, 1):
past_memory_str += rec["recommendation"] + "\n\n" past_memory_str += rec["recommendation"] + "\n\n"
prompt = f"""You are a Bear Analyst making the case against investing in the stock. Your goal is to present a well-reasoned argument emphasizing risks, challenges, and negative indicators. Leverage the provided research and data to highlight potential downsides and counter bullish arguments effectively. prompt = f"""You are a Bear Analyst making the case against investing in the stock. Your goal is to present a well-reasoned argument emphasizing risks, challenges, and negative indicators. Leverage the provided research and data to highlight potential downsides and counter bullish arguments effectively.
Key points to focus on: Key points to focus on:
- Risks and Challenges: Highlight factors like market saturation, financial instability, or macroeconomic threats that could hinder the stock's performance. - Risks and Challenges: Highlight factors like market saturation, financial instability, or macroeconomic threats that could hinder the stock's performance.
- Competitive Weaknesses: Emphasize vulnerabilities such as weaker market positioning, declining innovation, or threats from competitors. - Competitive Weaknesses: Emphasize vulnerabilities such as weaker market positioning, declining innovation, or threats from competitors.
- Negative Indicators: Use evidence from financial data, market trends, or recent adverse news to support your position. - Negative Indicators: Use evidence from financial data, market trends, or recent adverse news to support your position.
- Bull Counterpoints: Critically analyze the bull argument with specific data and sound reasoning, exposing weaknesses or over-optimistic assumptions. - Bull Counterpoints: Critically analyze the bull argument with specific data and sound reasoning, exposing weaknesses or over-optimistic assumptions.
- Engagement: Present your argument in a conversational style, directly engaging with the bull analyst's points and debating effectively rather than simply listing facts. - Engagement: Present your argument in a conversational style, directly engaging with the bull analyst's points and debating effectively rather than simply listing facts.
Resources available: Resources available:
Market research report: {market_research_report} Market research report: {market_research_report}
Social media sentiment report: {sentiment_report} Social media sentiment report: {sentiment_report}
Latest world affairs news: {news_report} Latest world affairs news: {news_report}
Company fundamentals report: {fundamentals_report} Company fundamentals report: {fundamentals_report}
Conversation history of the debate: {history} Conversation history of the debate: {history}
Last bull argument: {current_response} Last bull argument: {current_response}
Reflections from similar situations and lessons learned: {past_memory_str} Reflections from similar situations and lessons learned: {past_memory_str}
Use this information to deliver a compelling bear argument, refute the bull's claims, and engage in a dynamic debate that demonstrates the risks and weaknesses of investing in the stock. You must also address reflections and learn from lessons and mistakes you made in the past. Use this information to deliver a compelling bear argument, refute the bull's claims, and engage in a dynamic debate that demonstrates the risks and weaknesses of investing in the stock. You must also address reflections and learn from lessons and mistakes you made in the past.
""" """
response = llm.invoke(prompt) response = llm.invoke(prompt)
argument = f"Bear Analyst: {response.content}" argument = f"Bear Analyst: {response.content}"
new_investment_debate_state = { new_investment_debate_state = {
"history": history + "\n" + argument, "history": history + "\n" + argument,
"bear_history": bear_history + "\n" + argument, "bear_history": bear_history + "\n" + argument,
"bull_history": investment_debate_state.get("bull_history", ""), "bull_history": investment_debate_state.get("bull_history", ""),
"current_response": argument, "current_response": argument,
"count": investment_debate_state["count"] + 1, "count": investment_debate_state["count"] + 1,
} }
return {"investment_debate_state": new_investment_debate_state} return {"investment_debate_state": new_investment_debate_state}
return bear_node return bear_node

View File

@ -1,59 +1,59 @@
from langchain_core.messages import AIMessage from langchain_core.messages import AIMessage
import time import time
import json import json
def create_bull_researcher(llm, memory): def create_bull_researcher(llm, memory):
def bull_node(state) -> dict: def bull_node(state) -> dict:
investment_debate_state = state["investment_debate_state"] investment_debate_state = state["investment_debate_state"]
history = investment_debate_state.get("history", "") history = investment_debate_state.get("history", "")
bull_history = investment_debate_state.get("bull_history", "") bull_history = investment_debate_state.get("bull_history", "")
current_response = investment_debate_state.get("current_response", "") current_response = investment_debate_state.get("current_response", "")
market_research_report = state["market_report"] market_research_report = state["market_report"]
sentiment_report = state["sentiment_report"] sentiment_report = state["sentiment_report"]
news_report = state["news_report"] news_report = state["news_report"]
fundamentals_report = state["fundamentals_report"] fundamentals_report = state["fundamentals_report"]
curr_situation = f"{market_research_report}\n\n{sentiment_report}\n\n{news_report}\n\n{fundamentals_report}" curr_situation = f"{market_research_report}\n\n{sentiment_report}\n\n{news_report}\n\n{fundamentals_report}"
past_memories = memory.get_memories(curr_situation, n_matches=2) past_memories = memory.get_memories(curr_situation, n_matches=2)
past_memory_str = "" past_memory_str = ""
for i, rec in enumerate(past_memories, 1): for i, rec in enumerate(past_memories, 1):
past_memory_str += rec["recommendation"] + "\n\n" past_memory_str += rec["recommendation"] + "\n\n"
prompt = f"""You are a Bull Analyst advocating for investing in the stock. Your task is to build a strong, evidence-based case emphasizing growth potential, competitive advantages, and positive market indicators. Leverage the provided research and data to address concerns and counter bearish arguments effectively. prompt = f"""You are a Bull Analyst advocating for investing in the stock. Your task is to build a strong, evidence-based case emphasizing growth potential, competitive advantages, and positive market indicators. Leverage the provided research and data to address concerns and counter bearish arguments effectively.
Key points to focus on: Key points to focus on:
- Growth Potential: Highlight the company's market opportunities, revenue projections, and scalability. - Growth Potential: Highlight the company's market opportunities, revenue projections, and scalability.
- Competitive Advantages: Emphasize factors like unique products, strong branding, or dominant market positioning. - Competitive Advantages: Emphasize factors like unique products, strong branding, or dominant market positioning.
- Positive Indicators: Use financial health, industry trends, and recent positive news as evidence. - Positive Indicators: Use financial health, industry trends, and recent positive news as evidence.
- Bear Counterpoints: Critically analyze the bear argument with specific data and sound reasoning, addressing concerns thoroughly and showing why the bull perspective holds stronger merit. - Bear Counterpoints: Critically analyze the bear argument with specific data and sound reasoning, addressing concerns thoroughly and showing why the bull perspective holds stronger merit.
- Engagement: Present your argument in a conversational style, engaging directly with the bear analyst's points and debating effectively rather than just listing data. - Engagement: Present your argument in a conversational style, engaging directly with the bear analyst's points and debating effectively rather than just listing data.
Resources available: Resources available:
Market research report: {market_research_report} Market research report: {market_research_report}
Social media sentiment report: {sentiment_report} Social media sentiment report: {sentiment_report}
Latest world affairs news: {news_report} Latest world affairs news: {news_report}
Company fundamentals report: {fundamentals_report} Company fundamentals report: {fundamentals_report}
Conversation history of the debate: {history} Conversation history of the debate: {history}
Last bear argument: {current_response} Last bear argument: {current_response}
Reflections from similar situations and lessons learned: {past_memory_str} Reflections from similar situations and lessons learned: {past_memory_str}
Use this information to deliver a compelling bull argument, refute the bear's concerns, and engage in a dynamic debate that demonstrates the strengths of the bull position. You must also address reflections and learn from lessons and mistakes you made in the past. Use this information to deliver a compelling bull argument, refute the bear's concerns, and engage in a dynamic debate that demonstrates the strengths of the bull position. You must also address reflections and learn from lessons and mistakes you made in the past.
""" """
response = llm.invoke(prompt) response = llm.invoke(prompt)
argument = f"Bull Analyst: {response.content}" argument = f"Bull Analyst: {response.content}"
new_investment_debate_state = { new_investment_debate_state = {
"history": history + "\n" + argument, "history": history + "\n" + argument,
"bull_history": bull_history + "\n" + argument, "bull_history": bull_history + "\n" + argument,
"bear_history": investment_debate_state.get("bear_history", ""), "bear_history": investment_debate_state.get("bear_history", ""),
"current_response": argument, "current_response": argument,
"count": investment_debate_state["count"] + 1, "count": investment_debate_state["count"] + 1,
} }
return {"investment_debate_state": new_investment_debate_state} return {"investment_debate_state": new_investment_debate_state}
return bull_node return bull_node

View File

@ -1,55 +1,55 @@
import time import time
import json import json
def create_aggressive_debator(llm): def create_aggressive_debator(llm):
def aggressive_node(state) -> dict: def aggressive_node(state) -> dict:
risk_debate_state = state["risk_debate_state"] risk_debate_state = state["risk_debate_state"]
history = risk_debate_state.get("history", "") history = risk_debate_state.get("history", "")
aggressive_history = risk_debate_state.get("aggressive_history", "") aggressive_history = risk_debate_state.get("aggressive_history", "")
current_conservative_response = risk_debate_state.get("current_conservative_response", "") current_conservative_response = risk_debate_state.get("current_conservative_response", "")
current_neutral_response = risk_debate_state.get("current_neutral_response", "") current_neutral_response = risk_debate_state.get("current_neutral_response", "")
market_research_report = state["market_report"] market_research_report = state["market_report"]
sentiment_report = state["sentiment_report"] sentiment_report = state["sentiment_report"]
news_report = state["news_report"] news_report = state["news_report"]
fundamentals_report = state["fundamentals_report"] fundamentals_report = state["fundamentals_report"]
trader_decision = state["trader_investment_plan"] trader_decision = state["trader_investment_plan"]
prompt = f"""As the Aggressive Risk Analyst, your role is to actively champion high-reward, high-risk opportunities, emphasizing bold strategies and competitive advantages. When evaluating the trader's decision or plan, focus intently on the potential upside, growth potential, and innovative benefits—even when these come with elevated risk. Use the provided market data and sentiment analysis to strengthen your arguments and challenge the opposing views. Specifically, respond directly to each point made by the conservative and neutral analysts, countering with data-driven rebuttals and persuasive reasoning. Highlight where their caution might miss critical opportunities or where their assumptions may be overly conservative. Here is the trader's decision: prompt = f"""As the Aggressive Risk Analyst, your role is to actively champion high-reward, high-risk opportunities, emphasizing bold strategies and competitive advantages. When evaluating the trader's decision or plan, focus intently on the potential upside, growth potential, and innovative benefits—even when these come with elevated risk. Use the provided market data and sentiment analysis to strengthen your arguments and challenge the opposing views. Specifically, respond directly to each point made by the conservative and neutral analysts, countering with data-driven rebuttals and persuasive reasoning. Highlight where their caution might miss critical opportunities or where their assumptions may be overly conservative. Here is the trader's decision:
{trader_decision} {trader_decision}
Your task is to create a compelling case for the trader's decision by questioning and critiquing the conservative and neutral stances to demonstrate why your high-reward perspective offers the best path forward. Incorporate insights from the following sources into your arguments: Your task is to create a compelling case for the trader's decision by questioning and critiquing the conservative and neutral stances to demonstrate why your high-reward perspective offers the best path forward. Incorporate insights from the following sources into your arguments:
Market Research Report: {market_research_report} Market Research Report: {market_research_report}
Social Media Sentiment Report: {sentiment_report} Social Media Sentiment Report: {sentiment_report}
Latest World Affairs Report: {news_report} Latest World Affairs Report: {news_report}
Company Fundamentals Report: {fundamentals_report} Company Fundamentals Report: {fundamentals_report}
Here is the current conversation history: {history} Here are the last arguments from the conservative analyst: {current_conservative_response} Here are the last arguments from the neutral analyst: {current_neutral_response}. If there are no responses from the other viewpoints, do not hallucinate and just present your point. Here is the current conversation history: {history} Here are the last arguments from the conservative analyst: {current_conservative_response} Here are the last arguments from the neutral analyst: {current_neutral_response}. If there are no responses from the other viewpoints, do not hallucinate and just present your point.
Engage actively by addressing any specific concerns raised, refuting the weaknesses in their logic, and asserting the benefits of risk-taking to outpace market norms. Maintain a focus on debating and persuading, not just presenting data. Challenge each counterpoint to underscore why a high-risk approach is optimal. Output conversationally as if you are speaking without any special formatting.""" Engage actively by addressing any specific concerns raised, refuting the weaknesses in their logic, and asserting the benefits of risk-taking to outpace market norms. Maintain a focus on debating and persuading, not just presenting data. Challenge each counterpoint to underscore why a high-risk approach is optimal. Output conversationally as if you are speaking without any special formatting."""
response = llm.invoke(prompt) response = llm.invoke(prompt)
argument = f"Aggressive Analyst: {response.content}" argument = f"Aggressive Analyst: {response.content}"
new_risk_debate_state = { new_risk_debate_state = {
"history": history + "\n" + argument, "history": history + "\n" + argument,
"aggressive_history": aggressive_history + "\n" + argument, "aggressive_history": aggressive_history + "\n" + argument,
"conservative_history": risk_debate_state.get("conservative_history", ""), "conservative_history": risk_debate_state.get("conservative_history", ""),
"neutral_history": risk_debate_state.get("neutral_history", ""), "neutral_history": risk_debate_state.get("neutral_history", ""),
"latest_speaker": "Aggressive", "latest_speaker": "Aggressive",
"current_aggressive_response": argument, "current_aggressive_response": argument,
"current_conservative_response": risk_debate_state.get("current_conservative_response", ""), "current_conservative_response": risk_debate_state.get("current_conservative_response", ""),
"current_neutral_response": risk_debate_state.get( "current_neutral_response": risk_debate_state.get(
"current_neutral_response", "" "current_neutral_response", ""
), ),
"count": risk_debate_state["count"] + 1, "count": risk_debate_state["count"] + 1,
} }
return {"risk_debate_state": new_risk_debate_state} return {"risk_debate_state": new_risk_debate_state}
return aggressive_node return aggressive_node

View File

@ -1,58 +1,58 @@
from langchain_core.messages import AIMessage from langchain_core.messages import AIMessage
import time import time
import json import json
def create_conservative_debator(llm): def create_conservative_debator(llm):
def conservative_node(state) -> dict: def conservative_node(state) -> dict:
risk_debate_state = state["risk_debate_state"] risk_debate_state = state["risk_debate_state"]
history = risk_debate_state.get("history", "") history = risk_debate_state.get("history", "")
conservative_history = risk_debate_state.get("conservative_history", "") conservative_history = risk_debate_state.get("conservative_history", "")
current_aggressive_response = risk_debate_state.get("current_aggressive_response", "") current_aggressive_response = risk_debate_state.get("current_aggressive_response", "")
current_neutral_response = risk_debate_state.get("current_neutral_response", "") current_neutral_response = risk_debate_state.get("current_neutral_response", "")
market_research_report = state["market_report"] market_research_report = state["market_report"]
sentiment_report = state["sentiment_report"] sentiment_report = state["sentiment_report"]
news_report = state["news_report"] news_report = state["news_report"]
fundamentals_report = state["fundamentals_report"] fundamentals_report = state["fundamentals_report"]
trader_decision = state["trader_investment_plan"] trader_decision = state["trader_investment_plan"]
prompt = f"""As the Conservative Risk Analyst, your primary objective is to protect assets, minimize volatility, and ensure steady, reliable growth. You prioritize stability, security, and risk mitigation, carefully assessing potential losses, economic downturns, and market volatility. When evaluating the trader's decision or plan, critically examine high-risk elements, pointing out where the decision may expose the firm to undue risk and where more cautious alternatives could secure long-term gains. Here is the trader's decision: prompt = f"""As the Conservative Risk Analyst, your primary objective is to protect assets, minimize volatility, and ensure steady, reliable growth. You prioritize stability, security, and risk mitigation, carefully assessing potential losses, economic downturns, and market volatility. When evaluating the trader's decision or plan, critically examine high-risk elements, pointing out where the decision may expose the firm to undue risk and where more cautious alternatives could secure long-term gains. Here is the trader's decision:
{trader_decision} {trader_decision}
Your task is to actively counter the arguments of the Aggressive and Neutral Analysts, highlighting where their views may overlook potential threats or fail to prioritize sustainability. Respond directly to their points, drawing from the following data sources to build a convincing case for a low-risk approach adjustment to the trader's decision: Your task is to actively counter the arguments of the Aggressive and Neutral Analysts, highlighting where their views may overlook potential threats or fail to prioritize sustainability. Respond directly to their points, drawing from the following data sources to build a convincing case for a low-risk approach adjustment to the trader's decision:
Market Research Report: {market_research_report} Market Research Report: {market_research_report}
Social Media Sentiment Report: {sentiment_report} Social Media Sentiment Report: {sentiment_report}
Latest World Affairs Report: {news_report} Latest World Affairs Report: {news_report}
Company Fundamentals Report: {fundamentals_report} Company Fundamentals Report: {fundamentals_report}
Here is the current conversation history: {history} Here is the last response from the aggressive analyst: {current_aggressive_response} Here is the last response from the neutral analyst: {current_neutral_response}. If there are no responses from the other viewpoints, do not hallucinate and just present your point. Here is the current conversation history: {history} Here is the last response from the aggressive analyst: {current_aggressive_response} Here is the last response from the neutral analyst: {current_neutral_response}. If there are no responses from the other viewpoints, do not hallucinate and just present your point.
Engage by questioning their optimism and emphasizing the potential downsides they may have overlooked. Address each of their counterpoints to showcase why a conservative stance is ultimately the safest path for the firm's assets. Focus on debating and critiquing their arguments to demonstrate the strength of a low-risk strategy over their approaches. Output conversationally as if you are speaking without any special formatting.""" Engage by questioning their optimism and emphasizing the potential downsides they may have overlooked. Address each of their counterpoints to showcase why a conservative stance is ultimately the safest path for the firm's assets. Focus on debating and critiquing their arguments to demonstrate the strength of a low-risk strategy over their approaches. Output conversationally as if you are speaking without any special formatting."""
response = llm.invoke(prompt) response = llm.invoke(prompt)
argument = f"Conservative Analyst: {response.content}" argument = f"Conservative Analyst: {response.content}"
new_risk_debate_state = { new_risk_debate_state = {
"history": history + "\n" + argument, "history": history + "\n" + argument,
"aggressive_history": risk_debate_state.get("aggressive_history", ""), "aggressive_history": risk_debate_state.get("aggressive_history", ""),
"conservative_history": conservative_history + "\n" + argument, "conservative_history": conservative_history + "\n" + argument,
"neutral_history": risk_debate_state.get("neutral_history", ""), "neutral_history": risk_debate_state.get("neutral_history", ""),
"latest_speaker": "Conservative", "latest_speaker": "Conservative",
"current_aggressive_response": risk_debate_state.get( "current_aggressive_response": risk_debate_state.get(
"current_aggressive_response", "" "current_aggressive_response", ""
), ),
"current_conservative_response": argument, "current_conservative_response": argument,
"current_neutral_response": risk_debate_state.get( "current_neutral_response": risk_debate_state.get(
"current_neutral_response", "" "current_neutral_response", ""
), ),
"count": risk_debate_state["count"] + 1, "count": risk_debate_state["count"] + 1,
} }
return {"risk_debate_state": new_risk_debate_state} return {"risk_debate_state": new_risk_debate_state}
return conservative_node return conservative_node

View File

@ -1,55 +1,55 @@
import time import time
import json import json
def create_neutral_debator(llm): def create_neutral_debator(llm):
def neutral_node(state) -> dict: def neutral_node(state) -> dict:
risk_debate_state = state["risk_debate_state"] risk_debate_state = state["risk_debate_state"]
history = risk_debate_state.get("history", "") history = risk_debate_state.get("history", "")
neutral_history = risk_debate_state.get("neutral_history", "") neutral_history = risk_debate_state.get("neutral_history", "")
current_aggressive_response = risk_debate_state.get("current_aggressive_response", "") current_aggressive_response = risk_debate_state.get("current_aggressive_response", "")
current_conservative_response = risk_debate_state.get("current_conservative_response", "") current_conservative_response = risk_debate_state.get("current_conservative_response", "")
market_research_report = state["market_report"] market_research_report = state["market_report"]
sentiment_report = state["sentiment_report"] sentiment_report = state["sentiment_report"]
news_report = state["news_report"] news_report = state["news_report"]
fundamentals_report = state["fundamentals_report"] fundamentals_report = state["fundamentals_report"]
trader_decision = state["trader_investment_plan"] trader_decision = state["trader_investment_plan"]
prompt = f"""As the Neutral Risk Analyst, your role is to provide a balanced perspective, weighing both the potential benefits and risks of the trader's decision or plan. You prioritize a well-rounded approach, evaluating the upsides and downsides while factoring in broader market trends, potential economic shifts, and diversification strategies.Here is the trader's decision: prompt = f"""As the Neutral Risk Analyst, your role is to provide a balanced perspective, weighing both the potential benefits and risks of the trader's decision or plan. You prioritize a well-rounded approach, evaluating the upsides and downsides while factoring in broader market trends, potential economic shifts, and diversification strategies.Here is the trader's decision:
{trader_decision} {trader_decision}
Your task is to challenge both the Aggressive and Conservative Analysts, pointing out where each perspective may be overly optimistic or overly cautious. Use insights from the following data sources to support a moderate, sustainable strategy to adjust the trader's decision: Your task is to challenge both the Aggressive and Conservative Analysts, pointing out where each perspective may be overly optimistic or overly cautious. Use insights from the following data sources to support a moderate, sustainable strategy to adjust the trader's decision:
Market Research Report: {market_research_report} Market Research Report: {market_research_report}
Social Media Sentiment Report: {sentiment_report} Social Media Sentiment Report: {sentiment_report}
Latest World Affairs Report: {news_report} Latest World Affairs Report: {news_report}
Company Fundamentals Report: {fundamentals_report} Company Fundamentals Report: {fundamentals_report}
Here is the current conversation history: {history} Here is the last response from the aggressive analyst: {current_aggressive_response} Here is the last response from the conservative analyst: {current_conservative_response}. If there are no responses from the other viewpoints, do not hallucinate and just present your point. Here is the current conversation history: {history} Here is the last response from the aggressive analyst: {current_aggressive_response} Here is the last response from the conservative analyst: {current_conservative_response}. If there are no responses from the other viewpoints, do not hallucinate and just present your point.
Engage actively by analyzing both sides critically, addressing weaknesses in the aggressive and conservative arguments to advocate for a more balanced approach. Challenge each of their points to illustrate why a moderate risk strategy might offer the best of both worlds, providing growth potential while safeguarding against extreme volatility. Focus on debating rather than simply presenting data, aiming to show that a balanced view can lead to the most reliable outcomes. Output conversationally as if you are speaking without any special formatting.""" Engage actively by analyzing both sides critically, addressing weaknesses in the aggressive and conservative arguments to advocate for a more balanced approach. Challenge each of their points to illustrate why a moderate risk strategy might offer the best of both worlds, providing growth potential while safeguarding against extreme volatility. Focus on debating rather than simply presenting data, aiming to show that a balanced view can lead to the most reliable outcomes. Output conversationally as if you are speaking without any special formatting."""
response = llm.invoke(prompt) response = llm.invoke(prompt)
argument = f"Neutral Analyst: {response.content}" argument = f"Neutral Analyst: {response.content}"
new_risk_debate_state = { new_risk_debate_state = {
"history": history + "\n" + argument, "history": history + "\n" + argument,
"aggressive_history": risk_debate_state.get("aggressive_history", ""), "aggressive_history": risk_debate_state.get("aggressive_history", ""),
"conservative_history": risk_debate_state.get("conservative_history", ""), "conservative_history": risk_debate_state.get("conservative_history", ""),
"neutral_history": neutral_history + "\n" + argument, "neutral_history": neutral_history + "\n" + argument,
"latest_speaker": "Neutral", "latest_speaker": "Neutral",
"current_aggressive_response": risk_debate_state.get( "current_aggressive_response": risk_debate_state.get(
"current_aggressive_response", "" "current_aggressive_response", ""
), ),
"current_conservative_response": risk_debate_state.get("current_conservative_response", ""), "current_conservative_response": risk_debate_state.get("current_conservative_response", ""),
"current_neutral_response": argument, "current_neutral_response": argument,
"count": risk_debate_state["count"] + 1, "count": risk_debate_state["count"] + 1,
} }
return {"risk_debate_state": new_risk_debate_state} return {"risk_debate_state": new_risk_debate_state}
return neutral_node return neutral_node

View File

@ -1,53 +1,53 @@
"""Structured output agents for the equity ranking engine.""" """Structured output agents for the equity ranking engine."""
from .tier1 import ( from .tier1 import (
create_validation_node, create_validation_node,
create_macro_node, create_macro_node,
create_liquidity_node, create_liquidity_node,
) )
from .tier2 import ( from .tier2 import (
create_business_quality_node, create_business_quality_node,
create_institutional_flow_node, create_institutional_flow_node,
create_valuation_node, create_valuation_node,
create_entry_timing_node, create_entry_timing_node,
create_earnings_revisions_node, create_earnings_revisions_node,
create_sector_rotation_node, create_sector_rotation_node,
create_backlog_node, create_backlog_node,
create_crowding_node, create_crowding_node,
create_archetype_node, create_archetype_node,
) )
from .tier3 import ( from .tier3 import (
create_bull_case_node, create_bull_case_node,
create_bear_case_node, create_bear_case_node,
create_debate_node, create_debate_node,
create_risk_node, create_risk_node,
create_final_decision_node, create_final_decision_node,
) )
from .scoring import create_scoring_node from .scoring import create_scoring_node
from .portfolio import ( from .portfolio import (
create_theme_substitution_node, create_theme_substitution_node,
create_position_replacement_node, create_position_replacement_node,
) )
__all__ = [ __all__ = [
"create_validation_node", "create_validation_node",
"create_macro_node", "create_macro_node",
"create_liquidity_node", "create_liquidity_node",
"create_business_quality_node", "create_business_quality_node",
"create_institutional_flow_node", "create_institutional_flow_node",
"create_valuation_node", "create_valuation_node",
"create_entry_timing_node", "create_entry_timing_node",
"create_earnings_revisions_node", "create_earnings_revisions_node",
"create_sector_rotation_node", "create_sector_rotation_node",
"create_backlog_node", "create_backlog_node",
"create_crowding_node", "create_crowding_node",
"create_archetype_node", "create_archetype_node",
"create_bull_case_node", "create_bull_case_node",
"create_bear_case_node", "create_bear_case_node",
"create_debate_node", "create_debate_node",
"create_risk_node", "create_risk_node",
"create_final_decision_node", "create_final_decision_node",
"create_scoring_node", "create_scoring_node",
"create_theme_substitution_node", "create_theme_substitution_node",
"create_position_replacement_node", "create_position_replacement_node",
] ]

View File

@ -1,267 +1,267 @@
"""Portfolio-level agents: Theme Substitution Engine, Position Replacement Agent. """Portfolio-level agents: Theme Substitution Engine, Position Replacement Agent.
These run after scoring, before the debate phase. They use the deep-thinking LLM These run after scoring, before the debate phase. They use the deep-thinking LLM
to evaluate the stock in context is it the best expression of its theme? Should to evaluate the stock in context is it the best expression of its theme? Should
it replace an existing holding? it replace an existing holding?
""" """
from __future__ import annotations from __future__ import annotations
import json import json
import logging import logging
from typing import Any, Dict, List from typing import Any, Dict, List
import yfinance as yf import yfinance as yf
from tradingagents.models import ( from tradingagents.models import (
PositionReplacementOutput, PositionReplacementOutput,
ThemeStock, ThemeStock,
ThemeSubstitutionOutput, ThemeSubstitutionOutput,
invoke_structured, invoke_structured,
) )
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
def _fetch_peer_basics(tickers: List[str]) -> List[dict]: def _fetch_peer_basics(tickers: List[str]) -> List[dict]:
"""Fetch basic yfinance data for a list of peer tickers.""" """Fetch basic yfinance data for a list of peer tickers."""
peers = [] peers = []
for sym in tickers[:8]: # cap at 8 to keep prompt manageable for sym in tickers[:8]: # cap at 8 to keep prompt manageable
try: try:
info = yf.Ticker(sym.upper()).info or {} info = yf.Ticker(sym.upper()).info or {}
peers.append({ peers.append({
"ticker": sym.upper(), "ticker": sym.upper(),
"company_name": info.get("longName") or info.get("shortName") or sym, "company_name": info.get("longName") or info.get("shortName") or sym,
"market_cap": info.get("marketCap"), "market_cap": info.get("marketCap"),
"current_price": info.get("currentPrice") or info.get("regularMarketPrice"), "current_price": info.get("currentPrice") or info.get("regularMarketPrice"),
"trailing_pe": info.get("trailingPE"), "trailing_pe": info.get("trailingPE"),
"forward_pe": info.get("forwardPE"), "forward_pe": info.get("forwardPE"),
"revenue_growth": info.get("revenueGrowth"), "revenue_growth": info.get("revenueGrowth"),
"profit_margins": info.get("profitMargins"), "profit_margins": info.get("profitMargins"),
"return_on_equity": info.get("returnOnEquity"), "return_on_equity": info.get("returnOnEquity"),
"52w_range_pct": _range_pct(info), "52w_range_pct": _range_pct(info),
}) })
except Exception: except Exception:
peers.append({"ticker": sym.upper(), "error": "fetch failed"}) peers.append({"ticker": sym.upper(), "error": "fetch failed"})
return peers return peers
def _range_pct(info: dict) -> float | None: def _range_pct(info: dict) -> float | None:
hi = info.get("fiftyTwoWeekHigh") hi = info.get("fiftyTwoWeekHigh")
lo = info.get("fiftyTwoWeekLow") lo = info.get("fiftyTwoWeekLow")
price = info.get("currentPrice") or info.get("regularMarketPrice") price = info.get("currentPrice") or info.get("regularMarketPrice")
if hi and lo and price and (hi - lo) > 0: if hi and lo and price and (hi - lo) > 0:
return round((price - lo) / (hi - lo) * 100, 1) return round((price - lo) / (hi - lo) * 100, 1)
return None return None
def _summarize_for_theme(state: Dict[str, Any]) -> str: def _summarize_for_theme(state: Dict[str, Any]) -> str:
"""Compact summary of the candidate stock for theme comparison.""" """Compact summary of the candidate stock for theme comparison."""
card = state.get("company_card") or {} card = state.get("company_card") or {}
macro = state.get("macro") or {} macro = state.get("macro") or {}
bq = state.get("business_quality") or {} bq = state.get("business_quality") or {}
inst = state.get("institutional_flow") or {} inst = state.get("institutional_flow") or {}
val = state.get("valuation") or {} val = state.get("valuation") or {}
er = state.get("earnings_revisions") or {} er = state.get("earnings_revisions") or {}
arch = state.get("archetype") or {} arch = state.get("archetype") or {}
return "\n".join([ return "\n".join([
f"Ticker: {card.get('ticker', '?')} | {card.get('company_name', '?')}", f"Ticker: {card.get('ticker', '?')} | {card.get('company_name', '?')}",
f"Sector: {card.get('sector', '?')} | Industry: {card.get('industry', '?')}", f"Sector: {card.get('sector', '?')} | Industry: {card.get('industry', '?')}",
f"Market Cap: {card.get('market_cap_formatted', 'N/A')}", f"Market Cap: {card.get('market_cap_formatted', 'N/A')}",
f"Archetype: {arch.get('archetype', 'N/A')}", f"Archetype: {arch.get('archetype', 'N/A')}",
f"Master Score: {state.get('master_score', 'N/A')}", f"Master Score: {state.get('master_score', 'N/A')}",
f"Adjusted Score: {state.get('adjusted_score', 'N/A')}", f"Adjusted Score: {state.get('adjusted_score', 'N/A')}",
f"Position Role: {state.get('position_role', 'N/A')}", f"Position Role: {state.get('position_role', 'N/A')}",
f"Macro Regime: {macro.get('regime_label', '?')} | Risk: {macro.get('risk_appetite', '?')} | Liq: {macro.get('liquidity_regime', '?')}", f"Macro Regime: {macro.get('regime_label', '?')} | Risk: {macro.get('risk_appetite', '?')} | Liq: {macro.get('liquidity_regime', '?')}",
f"Business Quality: {bq.get('score_0_to_10', 'N/A')} | Moat: {bq.get('competitive_moat', '?')}", f"Business Quality: {bq.get('score_0_to_10', 'N/A')} | Moat: {bq.get('competitive_moat', '?')}",
f"Inst Flow: {inst.get('score_0_to_10', 'N/A')} | Smart Money: {inst.get('smart_money_signal', '?')}", f"Inst Flow: {inst.get('score_0_to_10', 'N/A')} | Smart Money: {inst.get('smart_money_signal', '?')}",
f"Valuation: {val.get('score_0_to_10', 'N/A')} | Verdict: {val.get('valuation_verdict', '?')}", f"Valuation: {val.get('score_0_to_10', 'N/A')} | Verdict: {val.get('valuation_verdict', '?')}",
f"Earnings Rev: {er.get('score_0_to_10', 'N/A')} | Direction: {er.get('eps_revision_direction', '?')}", f"Earnings Rev: {er.get('score_0_to_10', 'N/A')} | Direction: {er.get('eps_revision_direction', '?')}",
]) ])
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# Theme Substitution Engine # Theme Substitution Engine
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
def create_theme_substitution_node(llm): def create_theme_substitution_node(llm):
"""Identifies whether the stock is the best expression of its theme.""" """Identifies whether the stock is the best expression of its theme."""
def node(state: Dict[str, Any]) -> Dict[str, Any]: def node(state: Dict[str, Any]) -> Dict[str, Any]:
ticker = state["ticker"] ticker = state["ticker"]
card = state.get("company_card") or {} card = state.get("company_card") or {}
summary = _summarize_for_theme(state) summary = _summarize_for_theme(state)
master_score = state.get("master_score", 0) master_score = state.get("master_score", 0)
# Use yfinance to find peers in the same industry # Use yfinance to find peers in the same industry
try: try:
t = yf.Ticker(ticker.upper()) t = yf.Ticker(ticker.upper())
info = t.info or {} info = t.info or {}
industry = info.get("industry", "") industry = info.get("industry", "")
sector = info.get("sector", "") sector = info.get("sector", "")
except Exception: except Exception:
industry = card.get("industry", "") industry = card.get("industry", "")
sector = card.get("sector", "") sector = card.get("sector", "")
# Fetch competitor/peer data to ground the LLM's comparison # Fetch competitor/peer data to ground the LLM's comparison
competitors = card.get("competitors") or [] competitors = card.get("competitors") or []
peer_data = _fetch_peer_basics(competitors) if competitors else [] peer_data = _fetch_peer_basics(competitors) if competitors else []
peer_summary = "" peer_summary = ""
if peer_data: if peer_data:
lines = [] lines = []
for p in peer_data: for p in peer_data:
if p.get("error"): if p.get("error"):
continue continue
rg = p.get("revenue_growth") rg = p.get("revenue_growth")
rg_str = f"{rg*100:.1f}%" if rg else "N/A" rg_str = f"{rg*100:.1f}%" if rg else "N/A"
pm = p.get("profit_margins") pm = p.get("profit_margins")
pm_str = f"{pm*100:.1f}%" if pm else "N/A" pm_str = f"{pm*100:.1f}%" if pm else "N/A"
lines.append( lines.append(
f" {p['ticker']}: P/E={p.get('trailing_pe', 'N/A')}, " f" {p['ticker']}: P/E={p.get('trailing_pe', 'N/A')}, "
f"Fwd P/E={p.get('forward_pe', 'N/A')}, " f"Fwd P/E={p.get('forward_pe', 'N/A')}, "
f"RevGrowth={rg_str}, " f"RevGrowth={rg_str}, "
f"Margins={pm_str}, " f"Margins={pm_str}, "
f"52W={p.get('52w_range_pct', 'N/A')}%" f"52W={p.get('52w_range_pct', 'N/A')}%"
) )
peer_summary = "\n".join(lines) peer_summary = "\n".join(lines)
theme_prompt = f"""You are a Theme Substitution Analyst. Your job: determine if {ticker} is the BEST theme_prompt = f"""You are a Theme Substitution Analyst. Your job: determine if {ticker} is the BEST
expression of its investment theme, or if better alternatives exist. expression of its investment theme, or if better alternatives exist.
CANDIDATE STOCK: CANDIDATE STOCK:
{summary} {summary}
{f'PEER FUNDAMENTALS (live data):{chr(10)}{peer_summary}' if peer_summary else 'No live peer data available — use your knowledge of these companies.'} {f'PEER FUNDAMENTALS (live data):{chr(10)}{peer_summary}' if peer_summary else 'No live peer data available — use your knowledge of these companies.'}
INSTRUCTIONS do this in order: INSTRUCTIONS do this in order:
1. IDENTIFY THE THEME: What macro/sector theme does {ticker} express? 1. IDENTIFY THE THEME: What macro/sector theme does {ticker} express?
Examples: "AI infrastructure buildout", "GLP-1 obesity drugs", "defense spending ramp", Examples: "AI infrastructure buildout", "GLP-1 obesity drugs", "defense spending ramp",
"EV supply chain", "cloud migration", "reshoring/nearshoring". "EV supply chain", "cloud migration", "reshoring/nearshoring".
Name it clearly in theme_name. Name it clearly in theme_name.
2. LIST THEME PEERS: Name 3-6 other publicly traded stocks that express the SAME theme. 2. LIST THEME PEERS: Name 3-6 other publicly traded stocks that express the SAME theme.
Use the peer data above if available. These should be the strongest competitors Use the peer data above if available. These should be the strongest competitors
for capital allocation in this theme. for capital allocation in this theme.
For each peer, score master_score_estimate (0-10) based on fundamentals, momentum, For each peer, score master_score_estimate (0-10) based on fundamentals, momentum,
and positioning vs {ticker}. and positioning vs {ticker}.
3. RANK WITHIN THEME: Rank all stocks (including {ticker}) by investment quality. 3. RANK WITHIN THEME: Rank all stocks (including {ticker}) by investment quality.
The stock with the best combination of: business quality, valuation, momentum, The stock with the best combination of: business quality, valuation, momentum,
and institutional positioning should rank #1. and institutional positioning should rank #1.
4. DETERMINE BEST EXPRESSION: 4. DETERMINE BEST EXPRESSION:
- Set best_expression_of_theme=true if {ticker} is rank #1 or close (#1-2). - Set best_expression_of_theme=true if {ticker} is rank #1 or close (#1-2).
- Set best_expression_of_theme=false if clearly better alternatives exist. - Set best_expression_of_theme=false if clearly better alternatives exist.
- List stronger_alternatives (tickers that rank above {ticker}). - List stronger_alternatives (tickers that rank above {ticker}).
- Set relative_score_gap: how many score points {ticker} trails the best alternative - Set relative_score_gap: how many score points {ticker} trails the best alternative
(0 if {ticker} is best, positive number if it trails). (0 if {ticker} is best, positive number if it trails).
5. PORTFOLIO OVERLAP: Flag if {ticker} has high correlation with common holdings. 5. PORTFOLIO OVERLAP: Flag if {ticker} has high correlation with common holdings.
Set portfolio_overlap_warning if this stock would add redundant exposure. Set portfolio_overlap_warning if this stock would add redundant exposure.
Be honest and rigorous. A stock can score well absolutely but still not be the best Be honest and rigorous. A stock can score well absolutely but still not be the best
way to express its theme.""" way to express its theme."""
try: try:
result = invoke_structured(llm, ThemeSubstitutionOutput, theme_prompt) result = invoke_structured(llm, ThemeSubstitutionOutput, theme_prompt)
except Exception as e: except Exception as e:
logger.warning("ThemeSubstitution LLM failed: %s", e) logger.warning("ThemeSubstitution LLM failed: %s", e)
result = ThemeSubstitutionOutput( result = ThemeSubstitutionOutput(
theme_name="Unknown", theme_name="Unknown",
best_expression_of_theme=True, best_expression_of_theme=True,
reasoning="Theme analysis unavailable", reasoning="Theme analysis unavailable",
) )
return {"theme_substitution": result.model_dump()} return {"theme_substitution": result.model_dump()}
return node return node
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# Position Replacement Agent # Position Replacement Agent
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
def create_position_replacement_node(llm): def create_position_replacement_node(llm):
"""Identifies when a new stock is a better use of capital than alternatives.""" """Identifies when a new stock is a better use of capital than alternatives."""
def node(state: Dict[str, Any]) -> Dict[str, Any]: def node(state: Dict[str, Any]) -> Dict[str, Any]:
ticker = state["ticker"] ticker = state["ticker"]
summary = _summarize_for_theme(state) summary = _summarize_for_theme(state)
master_score = state.get("master_score", 0) master_score = state.get("master_score", 0)
theme = state.get("theme_substitution") or {} theme = state.get("theme_substitution") or {}
# Get the strongest alternative from theme analysis # Get the strongest alternative from theme analysis
stronger = theme.get("stronger_alternatives", []) stronger = theme.get("stronger_alternatives", [])
theme_stocks = theme.get("theme_stocks_ranked", []) theme_stocks = theme.get("theme_stocks_ranked", [])
theme_name = theme.get("theme_name", "Unknown") theme_name = theme.get("theme_name", "Unknown")
# If no stronger alternatives, this IS the best — skip deep comparison # If no stronger alternatives, this IS the best — skip deep comparison
if not stronger and theme.get("best_expression_of_theme", True): if not stronger and theme.get("best_expression_of_theme", True):
result = PositionReplacementOutput( result = PositionReplacementOutput(
replace_candidate=ticker, replace_candidate=ticker,
replace_with="", replace_with="",
score_difference=0.0, score_difference=0.0,
theme_overlap=theme_name, theme_overlap=theme_name,
replacement_reason=f"{ticker} is the best expression of the '{theme_name}' theme.", replacement_reason=f"{ticker} is the best expression of the '{theme_name}' theme.",
conviction_level="high", conviction_level="high",
should_replace=False, should_replace=False,
) )
return {"position_replacement": result.model_dump()} return {"position_replacement": result.model_dump()}
# Format theme peers for comparison # Format theme peers for comparison
peer_lines = [] peer_lines = []
for ts in theme_stocks[:6]: for ts in theme_stocks[:6]:
if isinstance(ts, dict): if isinstance(ts, dict):
peer_lines.append( peer_lines.append(
f" {ts.get('ticker', '?')}: est. score {ts.get('master_score_estimate', '?')}/10 " f" {ts.get('ticker', '?')}: est. score {ts.get('master_score_estimate', '?')}/10 "
f"— advantage: {ts.get('key_advantage', 'N/A')}, weakness: {ts.get('key_weakness', 'N/A')}" f"— advantage: {ts.get('key_advantage', 'N/A')}, weakness: {ts.get('key_weakness', 'N/A')}"
) )
prompt = f"""You are a Position Replacement Analyst. Determine if {ticker} should be replaced prompt = f"""You are a Position Replacement Analyst. Determine if {ticker} should be replaced
by a stronger alternative in the same theme. by a stronger alternative in the same theme.
CANDIDATE STOCK: CANDIDATE STOCK:
{summary} {summary}
THEME: {theme_name} THEME: {theme_name}
Best expression: {'Yes' if theme.get('best_expression_of_theme') else 'No'} Best expression: {'Yes' if theme.get('best_expression_of_theme') else 'No'}
Score gap vs best: {theme.get('relative_score_gap', 0):.1f} Score gap vs best: {theme.get('relative_score_gap', 0):.1f}
THEME PEERS: THEME PEERS:
{chr(10).join(peer_lines) or 'No peers available'} {chr(10).join(peer_lines) or 'No peers available'}
STRONGER ALTERNATIVES: {', '.join(stronger) if stronger else 'None'} STRONGER ALTERNATIVES: {', '.join(stronger) if stronger else 'None'}
INSTRUCTIONS: INSTRUCTIONS:
1. Compare {ticker} to the strongest alternative in the theme. 1. Compare {ticker} to the strongest alternative in the theme.
2. Assess on these dimensions: master score, earnings revisions, institutional flow, 2. Assess on these dimensions: master score, earnings revisions, institutional flow,
risk profile, valuation, entry timing. risk profile, valuation, entry timing.
3. Set replace_with to the best alternative ticker (empty if none). 3. Set replace_with to the best alternative ticker (empty if none).
4. Set score_difference: how much better the replacement is (positive = replacement is stronger). 4. Set score_difference: how much better the replacement is (positive = replacement is stronger).
5. Set conviction_level: high / medium / low. 5. Set conviction_level: high / medium / low.
- high: replacement is clearly better on 3+ dimensions. - high: replacement is clearly better on 3+ dimensions.
- medium: replacement is better on 1-2 dimensions, mixed on others. - medium: replacement is better on 1-2 dimensions, mixed on others.
- low: marginal difference, keep current. - low: marginal difference, keep current.
6. Set should_replace=true only if conviction_level is high. 6. Set should_replace=true only if conviction_level is high.
7. List what the replacement is stronger_on and weaker_on vs {ticker}. 7. List what the replacement is stronger_on and weaker_on vs {ticker}.
Be conservative. Don't recommend replacement for marginal differences.""" Be conservative. Don't recommend replacement for marginal differences."""
try: try:
result = invoke_structured(llm, PositionReplacementOutput, prompt) result = invoke_structured(llm, PositionReplacementOutput, prompt)
except Exception as e: except Exception as e:
logger.warning("PositionReplacement LLM failed: %s", e) logger.warning("PositionReplacement LLM failed: %s", e)
result = PositionReplacementOutput( result = PositionReplacementOutput(
replace_candidate=ticker, replace_candidate=ticker,
should_replace=False, should_replace=False,
replacement_reason="Position replacement analysis unavailable", replacement_reason="Position replacement analysis unavailable",
) )
result.replace_candidate = ticker result.replace_candidate = ticker
result.theme_overlap = theme_name result.theme_overlap = theme_name
return {"position_replacement": result.model_dump()} return {"position_replacement": result.model_dump()}
return node return node

View File

@ -1,59 +1,59 @@
"""Deterministic scoring node — no LLM, pure Python. """Deterministic scoring node — no LLM, pure Python.
Computes master_score, applies confidence penalties, checks hard vetoes, Computes master_score, applies confidence penalties, checks hard vetoes,
and assigns position roles. This is the heart of the deterministic pipeline. and assigns position roles. This is the heart of the deterministic pipeline.
""" """
from __future__ import annotations from __future__ import annotations
from typing import Any, Dict from typing import Any, Dict
from tradingagents.models import ( from tradingagents.models import (
DataFlag, DataFlag,
apply_confidence_penalty, apply_confidence_penalty,
assign_position_role, assign_position_role,
compute_master_score, compute_master_score,
) )
def create_scoring_node(): def create_scoring_node():
"""Create the deterministic scoring node (no LLM needed).""" """Create the deterministic scoring node (no LLM needed)."""
def node(state: Dict[str, Any]) -> Dict[str, Any]: def node(state: Dict[str, Any]) -> Dict[str, Any]:
# Extract scores from each agent output # Extract scores from each agent output
bq = (state.get("business_quality") or {}).get("score_0_to_10", 5.0) bq = (state.get("business_quality") or {}).get("score_0_to_10", 5.0)
macro = (state.get("macro") or {}).get("macro_alignment_0_to_10", 5.0) macro = (state.get("macro") or {}).get("macro_alignment_0_to_10", 5.0)
inst = (state.get("institutional_flow") or {}).get("score_0_to_10", 5.0) inst = (state.get("institutional_flow") or {}).get("score_0_to_10", 5.0)
val = (state.get("valuation") or {}).get("score_0_to_10", 5.0) val = (state.get("valuation") or {}).get("score_0_to_10", 5.0)
et = (state.get("entry_timing") or {}).get("score_0_to_10", 5.0) et = (state.get("entry_timing") or {}).get("score_0_to_10", 5.0)
er = (state.get("earnings_revisions") or {}).get("score_0_to_10", 5.0) er = (state.get("earnings_revisions") or {}).get("score_0_to_10", 5.0)
bl = (state.get("backlog") or {}).get("score_0_to_10", 5.0) bl = (state.get("backlog") or {}).get("score_0_to_10", 5.0)
cr = (state.get("crowding") or {}).get("score_0_to_10", 5.0) cr = (state.get("crowding") or {}).get("score_0_to_10", 5.0)
# Regime adjustment from macro agent # Regime adjustment from macro agent
regime_adj = (state.get("macro") or {}).get("regime_score_adjustment", 0.0) regime_adj = (state.get("macro") or {}).get("regime_score_adjustment", 0.0)
master = compute_master_score( master = compute_master_score(
bq, macro, inst, val, et, er, bl, cr, bq, macro, inst, val, et, er, bl, cr,
regime_adjustment=regime_adj, regime_adjustment=regime_adj,
) )
# Collect all data quality flags # Collect all data quality flags
all_flags = [] all_flags = []
for f in (state.get("global_flags") or []): for f in (state.get("global_flags") or []):
if isinstance(f, dict): if isinstance(f, dict):
all_flags.append(DataFlag(**f)) all_flags.append(DataFlag(**f))
elif isinstance(f, DataFlag): elif isinstance(f, DataFlag):
all_flags.append(f) all_flags.append(f)
hard_veto = state.get("hard_veto", False) hard_veto = state.get("hard_veto", False)
adjusted = apply_confidence_penalty(master, all_flags, hard_veto) adjusted = apply_confidence_penalty(master, all_flags, hard_veto)
role = assign_position_role(adjusted) role = assign_position_role(adjusted)
return { return {
"master_score": master, "master_score": master,
"adjusted_score": adjusted, "adjusted_score": adjusted,
"position_role": role, "position_role": role,
} }
return node return node

View File

@ -1,277 +1,277 @@
"""Tier 1 agents: Validation, Macro Regime, Liquidity. """Tier 1 agents: Validation, Macro Regime, Liquidity.
Tier 1 is cheap and fast runs on every stock. Validation is deterministic Tier 1 is cheap and fast runs on every stock. Validation is deterministic
(no LLM). Macro and Liquidity use the quick-thinking LLM. (no LLM). Macro and Liquidity use the quick-thinking LLM.
""" """
from __future__ import annotations from __future__ import annotations
import json import json
import logging import logging
from typing import Any, Dict from typing import Any, Dict
import yfinance as yf import yfinance as yf
from tradingagents.models import ( from tradingagents.models import (
CompanyCard, CompanyCard,
DataFlag, DataFlag,
LiquidityOutput, LiquidityOutput,
MacroRegimeOutput, MacroRegimeOutput,
ValidationOutput, ValidationOutput,
invoke_structured, invoke_structured,
) )
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# Helpers # Helpers
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
def _fmt_num(val): def _fmt_num(val):
if val is None: if val is None:
return None return None
if abs(val) >= 1e12: if abs(val) >= 1e12:
return f"${val / 1e12:.2f}T" return f"${val / 1e12:.2f}T"
if abs(val) >= 1e9: if abs(val) >= 1e9:
return f"${val / 1e9:.2f}B" return f"${val / 1e9:.2f}B"
if abs(val) >= 1e6: if abs(val) >= 1e6:
return f"${val / 1e6:.2f}M" return f"${val / 1e6:.2f}M"
return f"${val:,.0f}" return f"${val:,.0f}"
def _fetch_yf_info(ticker: str) -> dict: def _fetch_yf_info(ticker: str) -> dict:
"""Fetch yfinance info dict for a ticker.""" """Fetch yfinance info dict for a ticker."""
try: try:
t = yf.Ticker(ticker.upper()) t = yf.Ticker(ticker.upper())
return t.info or {} return t.info or {}
except Exception as e: except Exception as e:
logger.warning("yfinance fetch failed for %s: %s", ticker, e) logger.warning("yfinance fetch failed for %s: %s", ticker, e)
return {} return {}
def _fetch_macro_data() -> dict: def _fetch_macro_data() -> dict:
"""Fetch macro indicators via yfinance.""" """Fetch macro indicators via yfinance."""
from tradingagents.dataflows.y_finance import get_macro_indicators from tradingagents.dataflows.y_finance import get_macro_indicators
try: try:
raw = get_macro_indicators() raw = get_macro_indicators()
return json.loads(raw) if isinstance(raw, str) else raw return json.loads(raw) if isinstance(raw, str) else raw
except Exception as e: except Exception as e:
logger.warning("Macro data fetch failed: %s", e) logger.warning("Macro data fetch failed: %s", e)
return {} return {}
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# Validation (deterministic — no LLM) # Validation (deterministic — no LLM)
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
def create_validation_node(llm=None): def create_validation_node(llm=None):
"""Validation + CompanyCard node. Does NOT use LLM — purely data-driven.""" """Validation + CompanyCard node. Does NOT use LLM — purely data-driven."""
def node(state: Dict[str, Any]) -> Dict[str, Any]: def node(state: Dict[str, Any]) -> Dict[str, Any]:
ticker = state["ticker"] ticker = state["ticker"]
info = _fetch_yf_info(ticker) info = _fetch_yf_info(ticker)
# No data at all → hard veto # No data at all → hard veto
company_name = info.get("longName") or info.get("shortName") or "" company_name = info.get("longName") or info.get("shortName") or ""
if not company_name: if not company_name:
v = ValidationOutput( v = ValidationOutput(
ticker_valid=False, ticker_valid=False,
ticker_resolved=ticker.upper(), ticker_resolved=ticker.upper(),
company_name="", company_name="",
veto=True, veto=True,
veto_reason=f"No company data found for {ticker}", veto_reason=f"No company data found for {ticker}",
data_quality_flags=[ data_quality_flags=[
DataFlag(field="ticker", severity="severe", DataFlag(field="ticker", severity="severe",
message=f"No data for {ticker}") message=f"No data for {ticker}")
], ],
) )
return { return {
"validation": v.model_dump(), "validation": v.model_dump(),
"hard_veto": True, "hard_veto": True,
"hard_veto_reason": v.veto_reason, "hard_veto_reason": v.veto_reason,
"global_flags": [ "global_flags": [
DataFlag(field="ticker", severity="severe", DataFlag(field="ticker", severity="severe",
message=f"No data for {ticker}").model_dump() message=f"No data for {ticker}").model_dump()
], ],
} }
validation = ValidationOutput( validation = ValidationOutput(
ticker_valid=True, ticker_valid=True,
ticker_resolved=ticker.upper(), ticker_resolved=ticker.upper(),
company_name=company_name, company_name=company_name,
company_name_match=True, company_name_match=True,
exchange=info.get("exchange"), exchange=info.get("exchange"),
sector=info.get("sector"), sector=info.get("sector"),
industry=info.get("industry"), industry=info.get("industry"),
is_active=True, is_active=True,
) )
# Build company card # Build company card
mc = info.get("marketCap") mc = info.get("marketCap")
if mc and mc >= 10e9: if mc and mc >= 10e9:
mc_cat = "large_cap" mc_cat = "large_cap"
elif mc and mc >= 2e9: elif mc and mc >= 2e9:
mc_cat = "mid_cap" mc_cat = "mid_cap"
elif mc and mc >= 300e6: elif mc and mc >= 300e6:
mc_cat = "small_cap" mc_cat = "small_cap"
else: else:
mc_cat = "micro_cap" if mc else "unknown" mc_cat = "micro_cap" if mc else "unknown"
card = CompanyCard( card = CompanyCard(
company_name=company_name, company_name=company_name,
ticker=ticker.upper(), ticker=ticker.upper(),
sector=info.get("sector", "Unknown"), sector=info.get("sector", "Unknown"),
industry=info.get("industry", "Unknown"), industry=info.get("industry", "Unknown"),
description=(info.get("longBusinessSummary") or "")[:500], description=(info.get("longBusinessSummary") or "")[:500],
market_cap=mc, market_cap=mc,
market_cap_formatted=_fmt_num(mc), market_cap_formatted=_fmt_num(mc),
market_cap_category=mc_cat, market_cap_category=mc_cat,
current_price=info.get("currentPrice") or info.get("regularMarketPrice"), current_price=info.get("currentPrice") or info.get("regularMarketPrice"),
revenue=info.get("totalRevenue"), revenue=info.get("totalRevenue"),
profit_margins=info.get("profitMargins"), profit_margins=info.get("profitMargins"),
employees=info.get("fullTimeEmployees"), employees=info.get("fullTimeEmployees"),
) )
return { return {
"validation": validation.model_dump(), "validation": validation.model_dump(),
"company_card": card.model_dump(), "company_card": card.model_dump(),
} }
return node return node
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# Macro Regime # Macro Regime
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
def create_macro_node(llm): def create_macro_node(llm):
"""Macro regime analysis node — uses quick LLM.""" """Macro regime analysis node — uses quick LLM."""
def node(state: Dict[str, Any]) -> Dict[str, Any]: def node(state: Dict[str, Any]) -> Dict[str, Any]:
ticker = state["ticker"] ticker = state["ticker"]
macro_data = _fetch_macro_data() macro_data = _fetch_macro_data()
card = state.get("company_card") or {} card = state.get("company_card") or {}
sector = card.get("sector", "Unknown") sector = card.get("sector", "Unknown")
spy_perf = (macro_data.get("sector_performance") or {}).get("SPY", {}) spy_perf = (macro_data.get("sector_performance") or {}).get("SPY", {})
sector_perfs = macro_data.get("sector_performance") or {} sector_perfs = macro_data.get("sector_performance") or {}
# Build compact sector table # Build compact sector table
sector_lines = [] sector_lines = []
for etf, data in sorted(sector_perfs.items()): for etf, data in sorted(sector_perfs.items()):
r1 = data.get("return_1m") r1 = data.get("return_1m")
name = data.get("name", etf) name = data.get("name", etf)
if r1 is not None: if r1 is not None:
sector_lines.append(f" {etf} ({name}): {r1:+.1f}% 1M") sector_lines.append(f" {etf} ({name}): {r1:+.1f}% 1M")
prompt = f"""You are a Macro Regime Analyst in a structured equity ranking pipeline. prompt = f"""You are a Macro Regime Analyst in a structured equity ranking pipeline.
Ticker: {ticker} | Sector: {sector} Ticker: {ticker} | Sector: {sector}
MACRO DATA (source: yfinance): MACRO DATA (source: yfinance):
- VIX: {macro_data.get('vix_level', 'N/A')} (source: yfinance) - VIX: {macro_data.get('vix_level', 'N/A')} (source: yfinance)
- 10Y Yield: {macro_data.get('ten_year_yield', 'N/A')}% (source: yfinance) - 10Y Yield: {macro_data.get('ten_year_yield', 'N/A')}% (source: yfinance)
- Dollar 1M: {macro_data.get('dollar_1m_return', 'N/A')}% (source: yfinance) - Dollar 1M: {macro_data.get('dollar_1m_return', 'N/A')}% (source: yfinance)
- Credit Spreads: {macro_data.get('credit_spread_direction', 'N/A')} (source: yfinance) - Credit Spreads: {macro_data.get('credit_spread_direction', 'N/A')} (source: yfinance)
- SPY 1M: {spy_perf.get('return_1m', 'N/A')}% (source: yfinance) - SPY 1M: {spy_perf.get('return_1m', 'N/A')}% (source: yfinance)
SECTOR PERFORMANCE (1M, source: yfinance): SECTOR PERFORMANCE (1M, source: yfinance):
{chr(10).join(sector_lines[:12]) or 'N/A'} {chr(10).join(sector_lines[:12]) or 'N/A'}
NOTE: If a metric shows 'N/A' or 'unknown', say 'data unavailable' rather than guessing. NOTE: If a metric shows 'N/A' or 'unknown', say 'data unavailable' rather than guessing.
INSTRUCTIONS: INSTRUCTIONS:
1. Classify risk_appetite: "risk-on" / "risk-off" / "transitional". 1. Classify risk_appetite: "risk-on" / "risk-off" / "transitional".
- risk-on: VIX low, spreads tight, SPY up, breadth strong. - risk-on: VIX low, spreads tight, SPY up, breadth strong.
- risk-off: VIX elevated, spreads widening, SPY down, flight to safety. - risk-off: VIX elevated, spreads widening, SPY down, flight to safety.
- transitional: mixed signals. - transitional: mixed signals.
2. Classify liquidity_regime: "expansion" / "contraction" / "neutral". 2. Classify liquidity_regime: "expansion" / "contraction" / "neutral".
- expansion: falling yields, dovish Fed, credit flowing, dollar weakening. - expansion: falling yields, dovish Fed, credit flowing, dollar weakening.
- contraction: rising yields, hawkish Fed, tight credit, dollar strengthening. - contraction: rising yields, hawkish Fed, tight credit, dollar strengthening.
3. Set regime_score_adjustment (-10 to +10): 3. Set regime_score_adjustment (-10 to +10):
- +5 to +10 = strong macro tailwind for this specific stock/sector. - +5 to +10 = strong macro tailwind for this specific stock/sector.
- +1 to +4 = mild tailwind. - +1 to +4 = mild tailwind.
- 0 = neutral. - 0 = neutral.
- -1 to -4 = mild headwind. - -1 to -4 = mild headwind.
- -5 to -10 = severe macro headwind (risk-off + contraction + hostile sector). - -5 to -10 = severe macro headwind (risk-off + contraction + hostile sector).
This adjustment directly modifies the 0-100 master score for ALL stocks. This adjustment directly modifies the 0-100 master score for ALL stocks.
4. Score macro_alignment_0_to_10: how well macro supports {ticker} specifically. 4. Score macro_alignment_0_to_10: how well macro supports {ticker} specifically.
5. Also provide score_0_to_10 (overall macro health). 5. Also provide score_0_to_10 (overall macro health).
6. Set regime_label: descriptive label (e.g., "Late Cycle Risk-Off"). 6. Set regime_label: descriptive label (e.g., "Late Cycle Risk-Off").
7. List key positives, negatives, risks. Be concise.""" 7. List key positives, negatives, risks. Be concise."""
try: try:
result = invoke_structured(llm, MacroRegimeOutput, prompt) result = invoke_structured(llm, MacroRegimeOutput, prompt)
except Exception as e: except Exception as e:
logger.warning("Macro LLM call failed: %s", e) logger.warning("Macro LLM call failed: %s", e)
result = MacroRegimeOutput( result = MacroRegimeOutput(
score_0_to_10=5.0, confidence_0_to_1=0.1, score_0_to_10=5.0, confidence_0_to_1=0.1,
summary_1_sentence="Macro analysis unavailable", summary_1_sentence="Macro analysis unavailable",
data_quality_flags=[ data_quality_flags=[
DataFlag(field="macro", severity="moderate", message=str(e)) DataFlag(field="macro", severity="moderate", message=str(e))
], ],
) )
# Override with actual fetched data # Override with actual fetched data
result.vix_level = macro_data.get("vix_level") result.vix_level = macro_data.get("vix_level")
result.vix_regime = macro_data.get("vix_regime", "unknown") result.vix_regime = macro_data.get("vix_regime", "unknown")
result.ten_year_yield = macro_data.get("ten_year_yield") result.ten_year_yield = macro_data.get("ten_year_yield")
result.dollar_strength = macro_data.get("dollar_strength", "unknown") result.dollar_strength = macro_data.get("dollar_strength", "unknown")
result.credit_spread_direction = macro_data.get( result.credit_spread_direction = macro_data.get(
"credit_spread_direction", "unknown" "credit_spread_direction", "unknown"
) )
result.spy_1m_return = spy_perf.get("return_1m") result.spy_1m_return = spy_perf.get("return_1m")
flags = [f.model_dump() for f in result.data_quality_flags] flags = [f.model_dump() for f in result.data_quality_flags]
return {"macro": result.model_dump(), "global_flags": flags} return {"macro": result.model_dump(), "global_flags": flags}
return node return node
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# Liquidity # Liquidity
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
def create_liquidity_node(llm): def create_liquidity_node(llm):
"""Liquidity analysis node — uses quick LLM.""" """Liquidity analysis node — uses quick LLM."""
def node(state: Dict[str, Any]) -> Dict[str, Any]: def node(state: Dict[str, Any]) -> Dict[str, Any]:
ticker = state["ticker"] ticker = state["ticker"]
macro_data = _fetch_macro_data() macro_data = _fetch_macro_data()
card = state.get("company_card") or {} card = state.get("company_card") or {}
prompt = f"""You are a Liquidity Analyst in a structured equity ranking pipeline. prompt = f"""You are a Liquidity Analyst in a structured equity ranking pipeline.
Ticker: {ticker} | Sector: {card.get('sector', 'Unknown')} Ticker: {ticker} | Sector: {card.get('sector', 'Unknown')}
AVAILABLE DATA (source: yfinance macro API): AVAILABLE DATA (source: yfinance macro API):
- VIX: {macro_data.get('vix_level', 'N/A')} (source: yfinance) - VIX: {macro_data.get('vix_level', 'N/A')} (source: yfinance)
- 10Y Yield: {macro_data.get('ten_year_yield', 'N/A')}% (source: yfinance) - 10Y Yield: {macro_data.get('ten_year_yield', 'N/A')}% (source: yfinance)
- Credit Spreads: {macro_data.get('credit_spread_direction', 'N/A')} (source: yfinance) - Credit Spreads: {macro_data.get('credit_spread_direction', 'N/A')} (source: yfinance)
- Dollar Strength: {macro_data.get('dollar_strength', 'N/A')} (source: yfinance) - Dollar Strength: {macro_data.get('dollar_strength', 'N/A')} (source: yfinance)
NOTE: If a metric shows 'N/A' or 'unknown', say 'data unavailable' rather than guessing. NOTE: If a metric shows 'N/A' or 'unknown', say 'data unavailable' rather than guessing.
INSTRUCTIONS: INSTRUCTIONS:
1. Assess Fed stance (dovish / neutral / hawkish) based on yield environment. 1. Assess Fed stance (dovish / neutral / hawkish) based on yield environment.
2. Assess market breadth (strong / moderate / weak). 2. Assess market breadth (strong / moderate / weak).
3. Assess volume profile (above_average / average / below_average). 3. Assess volume profile (above_average / average / below_average).
4. Assess SPY trend (uptrend / downtrend / sideways). 4. Assess SPY trend (uptrend / downtrend / sideways).
5. Score overall liquidity favorability 0-10 for this stock. 5. Score overall liquidity favorability 0-10 for this stock.
6. Be concise.""" 6. Be concise."""
try: try:
result = invoke_structured(llm, LiquidityOutput, prompt) result = invoke_structured(llm, LiquidityOutput, prompt)
except Exception as e: except Exception as e:
logger.warning("Liquidity LLM call failed: %s", e) logger.warning("Liquidity LLM call failed: %s", e)
result = LiquidityOutput( result = LiquidityOutput(
score_0_to_10=5.0, confidence_0_to_1=0.1, score_0_to_10=5.0, confidence_0_to_1=0.1,
summary_1_sentence="Liquidity analysis unavailable", summary_1_sentence="Liquidity analysis unavailable",
) )
flags = [f.model_dump() for f in result.data_quality_flags] flags = [f.model_dump() for f in result.data_quality_flags]
return {"liquidity": result.model_dump(), "global_flags": flags} return {"liquidity": result.model_dump(), "global_flags": flags}
return node return node

File diff suppressed because it is too large Load Diff

View File

@ -1,418 +1,418 @@
"""Tier 3 agents: Bull/Bear debate, Risk assessment, Final decision. """Tier 3 agents: Bull/Bear debate, Risk assessment, Final decision.
Only runs on stocks that pass Tier 1 + Tier 2. Uses the deep-thinking LLM Only runs on stocks that pass Tier 1 + Tier 2. Uses the deep-thinking LLM
for reasoning-heavy tasks (debate, risk, final synthesis). for reasoning-heavy tasks (debate, risk, final synthesis).
""" """
from __future__ import annotations from __future__ import annotations
import json import json
import logging import logging
from typing import Any, Dict from typing import Any, Dict
from tradingagents.models import ( from tradingagents.models import (
BearCaseOutput, BearCaseOutput,
BullCaseOutput, BullCaseOutput,
DataFlag, DataFlag,
DebateRefereeOutput, DebateRefereeOutput,
FinalDecisionOutput, FinalDecisionOutput,
RiskInvalidationOutput, RiskInvalidationOutput,
invoke_structured, invoke_structured,
) )
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
def _low_confidence_warnings(state: Dict[str, Any]) -> str: def _low_confidence_warnings(state: Dict[str, Any]) -> str:
"""Check if any Tier 2 agents have confidence < 0.2 and return warnings.""" """Check if any Tier 2 agents have confidence < 0.2 and return warnings."""
_TIER2_FIELDS = { _TIER2_FIELDS = {
"business_quality": "Business Quality", "business_quality": "Business Quality",
"institutional_flow": "Institutional Flow", "institutional_flow": "Institutional Flow",
"valuation": "Valuation", "valuation": "Valuation",
"entry_timing": "Entry Timing", "entry_timing": "Entry Timing",
"earnings_revisions": "Earnings Revisions", "earnings_revisions": "Earnings Revisions",
"sector_rotation": "Sector Rotation", "sector_rotation": "Sector Rotation",
"backlog": "Backlog / Order Momentum", "backlog": "Backlog / Order Momentum",
"crowding": "Narrative Crowding", "crowding": "Narrative Crowding",
} }
warnings = [] warnings = []
for field, display_name in _TIER2_FIELDS.items(): for field, display_name in _TIER2_FIELDS.items():
agent_data = state.get(field) or {} agent_data = state.get(field) or {}
conf = agent_data.get("confidence_0_to_1") conf = agent_data.get("confidence_0_to_1")
if conf is not None and conf < 0.2: if conf is not None and conf < 0.2:
warnings.append( warnings.append(
f" WARNING: {display_name} has low confidence ({conf:.2f}) — " f" WARNING: {display_name} has low confidence ({conf:.2f}) — "
f"its score may be unreliable (fallback defaults or poor data)" f"its score may be unreliable (fallback defaults or poor data)"
) )
if warnings: if warnings:
return "\nDATA QUALITY WARNINGS:\n" + "\n".join(warnings) + "\n" return "\nDATA QUALITY WARNINGS:\n" + "\n".join(warnings) + "\n"
return "" return ""
def _summarize_tier2(state: Dict[str, Any]) -> str: def _summarize_tier2(state: Dict[str, Any]) -> str:
"""Build a compact summary of all Tier 1+2 findings for Tier 3 prompts.""" """Build a compact summary of all Tier 1+2 findings for Tier 3 prompts."""
card = state.get("company_card") or {} card = state.get("company_card") or {}
macro = state.get("macro") or {} macro = state.get("macro") or {}
liq = state.get("liquidity") or {} liq = state.get("liquidity") or {}
bq = state.get("business_quality") or {} bq = state.get("business_quality") or {}
inst = state.get("institutional_flow") or {} inst = state.get("institutional_flow") or {}
val = state.get("valuation") or {} val = state.get("valuation") or {}
et = state.get("entry_timing") or {} et = state.get("entry_timing") or {}
er = state.get("earnings_revisions") or {} er = state.get("earnings_revisions") or {}
sr = state.get("sector_rotation") or {} sr = state.get("sector_rotation") or {}
bl = state.get("backlog") or {} bl = state.get("backlog") or {}
cr = state.get("crowding") or {} cr = state.get("crowding") or {}
arch = state.get("archetype") or {} arch = state.get("archetype") or {}
# Check for low-confidence Tier 2 agents # Check for low-confidence Tier 2 agents
confidence_warnings = _low_confidence_warnings(state) confidence_warnings = _low_confidence_warnings(state)
lines = [ lines = [
f"Company: {card.get('company_name', '?')} ({card.get('ticker', '?')})", f"Company: {card.get('company_name', '?')} ({card.get('ticker', '?')})",
f"Sector: {card.get('sector', '?')} | Industry: {card.get('industry', '?')}", f"Sector: {card.get('sector', '?')} | Industry: {card.get('industry', '?')}",
f"Market Cap: {card.get('market_cap_formatted', 'N/A')}", f"Market Cap: {card.get('market_cap_formatted', 'N/A')}",
f"Price: ${card.get('current_price', 'N/A')}", f"Price: ${card.get('current_price', 'N/A')}",
f"Archetype: {arch.get('archetype', 'N/A')}", f"Archetype: {arch.get('archetype', 'N/A')}",
"", "",
f"Master Score: {state.get('master_score', 'N/A')} | Role: {state.get('position_role', 'N/A')}", f"Master Score: {state.get('master_score', 'N/A')} | Role: {state.get('position_role', 'N/A')}",
"", "",
"AGENT SCORES (0-10):", "AGENT SCORES (0-10):",
f" Business Quality: {bq.get('score_0_to_10', 'N/A')}{bq.get('summary_1_sentence', '')}", f" Business Quality: {bq.get('score_0_to_10', 'N/A')}{bq.get('summary_1_sentence', '')}",
f" Macro Alignment: {macro.get('macro_alignment_0_to_10', 'N/A')}{macro.get('summary_1_sentence', '')}", f" Macro Alignment: {macro.get('macro_alignment_0_to_10', 'N/A')}{macro.get('summary_1_sentence', '')}",
f" Institutional Flow: {inst.get('score_0_to_10', 'N/A')}{inst.get('summary_1_sentence', '')}", f" Institutional Flow: {inst.get('score_0_to_10', 'N/A')}{inst.get('summary_1_sentence', '')}",
f" Valuation: {val.get('score_0_to_10', 'N/A')}{val.get('summary_1_sentence', '')}", f" Valuation: {val.get('score_0_to_10', 'N/A')}{val.get('summary_1_sentence', '')}",
f" Entry Timing: {et.get('score_0_to_10', 'N/A')}{et.get('summary_1_sentence', '')}", f" Entry Timing: {et.get('score_0_to_10', 'N/A')}{et.get('summary_1_sentence', '')}",
f" Earnings Revisions: {er.get('score_0_to_10', 'N/A')}{er.get('summary_1_sentence', '')}", f" Earnings Revisions: {er.get('score_0_to_10', 'N/A')}{er.get('summary_1_sentence', '')}",
f" Sector Rotation: {sr.get('score_0_to_10', 'N/A')}{sr.get('summary_1_sentence', '')}", f" Sector Rotation: {sr.get('score_0_to_10', 'N/A')}{sr.get('summary_1_sentence', '')}",
f" Backlog: {bl.get('score_0_to_10', 'N/A')}{bl.get('summary_1_sentence', '')}", f" Backlog: {bl.get('score_0_to_10', 'N/A')}{bl.get('summary_1_sentence', '')}",
f" Crowding: {cr.get('score_0_to_10', 'N/A')}{cr.get('summary_1_sentence', '')}", f" Crowding: {cr.get('score_0_to_10', 'N/A')}{cr.get('summary_1_sentence', '')}",
f" Liquidity: {liq.get('score_0_to_10', 'N/A')}{liq.get('summary_1_sentence', '')}", f" Liquidity: {liq.get('score_0_to_10', 'N/A')}{liq.get('summary_1_sentence', '')}",
"", "",
f" Macro Regime: {macro.get('regime_label', '?')} | VIX: {macro.get('vix_level', '?')}", f" Macro Regime: {macro.get('regime_label', '?')} | VIX: {macro.get('vix_level', '?')}",
f" Risk Appetite: {macro.get('risk_appetite', '?')} | Liquidity Regime: {macro.get('liquidity_regime', '?')}", f" Risk Appetite: {macro.get('risk_appetite', '?')} | Liquidity Regime: {macro.get('liquidity_regime', '?')}",
f" Regime Score Adjustment: {macro.get('regime_score_adjustment', 0):+.1f}", f" Regime Score Adjustment: {macro.get('regime_score_adjustment', 0):+.1f}",
f" Moat: {bq.get('competitive_moat', '?')} | Valuation: {val.get('valuation_verdict', '?')}", f" Moat: {bq.get('competitive_moat', '?')} | Valuation: {val.get('valuation_verdict', '?')}",
f" Smart Money: {inst.get('smart_money_signal', '?')} | Accumulation: {inst.get('accumulation_signal', '?')}", f" Smart Money: {inst.get('smart_money_signal', '?')} | Accumulation: {inst.get('accumulation_signal', '?')}",
f" Short Trend: {inst.get('short_interest_trend', '?')} | Insider Signal: {inst.get('insider_transaction_signal', '?')}", f" Short Trend: {inst.get('short_interest_trend', '?')} | Insider Signal: {inst.get('insider_transaction_signal', '?')}",
f" Timing: {et.get('timing_verdict', '?')}", f" Timing: {et.get('timing_verdict', '?')}",
] ]
if confidence_warnings: if confidence_warnings:
lines.append("") lines.append("")
lines.append(confidence_warnings) lines.append(confidence_warnings)
lines.append("Factor these warnings into your analysis — low-confidence scores may not reflect reality.") lines.append("Factor these warnings into your analysis — low-confidence scores may not reflect reality.")
return "\n".join(lines) return "\n".join(lines)
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# Bull Case # Bull Case
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
def create_bull_case_node(llm): def create_bull_case_node(llm):
def node(state: Dict[str, Any]) -> Dict[str, Any]: def node(state: Dict[str, Any]) -> Dict[str, Any]:
ticker = state["ticker"] ticker = state["ticker"]
summary = _summarize_tier2(state) summary = _summarize_tier2(state)
prompt = f"""You are a Bull Case Researcher. Build the strongest possible bullish thesis for {ticker}. prompt = f"""You are a Bull Case Researcher. Build the strongest possible bullish thesis for {ticker}.
{summary} {summary}
INSTRUCTIONS: INSTRUCTIONS:
1. Write a concise thesis (2-3 sentences) for why this stock should be bought. 1. Write a concise thesis (2-3 sentences) for why this stock should be bought.
2. List 3-5 specific catalysts that could drive the stock higher. 2. List 3-5 specific catalysts that could drive the stock higher.
3. Estimate upside_target (price) and upside_pct from current price. 3. Estimate upside_target (price) and upside_pct from current price.
4. List key assumptions your thesis depends on. 4. List key assumptions your thesis depends on.
5. List thesis_invalidation_triggers what would kill the bull case. 5. List thesis_invalidation_triggers what would kill the bull case.
6. Set confidence 0-1 for how strong the bull case is. 6. Set confidence 0-1 for how strong the bull case is.
Attack the investment aggressively. Find every reason to be bullish. Attack the investment aggressively. Find every reason to be bullish.
But be honest don't fabricate catalysts. Use the data above.""" But be honest don't fabricate catalysts. Use the data above."""
try: try:
result = invoke_structured(llm, BullCaseOutput, prompt) result = invoke_structured(llm, BullCaseOutput, prompt)
except Exception as e: except Exception as e:
logger.warning("BullCase LLM failed: %s", e) logger.warning("BullCase LLM failed: %s", e)
result = BullCaseOutput( result = BullCaseOutput(
thesis="Bull case analysis unavailable", thesis="Bull case analysis unavailable",
confidence_0_to_1=0.1, confidence_0_to_1=0.1,
) )
return {"bull_case": result.model_dump()} return {"bull_case": result.model_dump()}
return node return node
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# Bear Case # Bear Case
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
def create_bear_case_node(llm): def create_bear_case_node(llm):
def node(state: Dict[str, Any]) -> Dict[str, Any]: def node(state: Dict[str, Any]) -> Dict[str, Any]:
ticker = state["ticker"] ticker = state["ticker"]
summary = _summarize_tier2(state) summary = _summarize_tier2(state)
prompt = f"""You are a Bear Case Researcher. Build the strongest possible bearish thesis for {ticker}. prompt = f"""You are a Bear Case Researcher. Build the strongest possible bearish thesis for {ticker}.
{summary} {summary}
INSTRUCTIONS: INSTRUCTIONS:
1. Write a concise thesis (2-3 sentences) for why this stock should be avoided or sold. 1. Write a concise thesis (2-3 sentences) for why this stock should be avoided or sold.
2. List 3-5 specific risks that could drive the stock lower. 2. List 3-5 specific risks that could drive the stock lower.
3. Estimate downside_target (price) and downside_pct from current price. 3. Estimate downside_target (price) and downside_pct from current price.
4. List key assumptions your bear thesis depends on. 4. List key assumptions your bear thesis depends on.
5. List thesis_invalidation_triggers what would kill the bear case. 5. List thesis_invalidation_triggers what would kill the bear case.
6. Set confidence 0-1 for how strong the bear case is. 6. Set confidence 0-1 for how strong the bear case is.
Be ruthless. Find every vulnerability, every overvaluation, every risk. Be ruthless. Find every vulnerability, every overvaluation, every risk.
But be honest don't fabricate risks. Use the data above.""" But be honest don't fabricate risks. Use the data above."""
try: try:
result = invoke_structured(llm, BearCaseOutput, prompt) result = invoke_structured(llm, BearCaseOutput, prompt)
except Exception as e: except Exception as e:
logger.warning("BearCase LLM failed: %s", e) logger.warning("BearCase LLM failed: %s", e)
result = BearCaseOutput( result = BearCaseOutput(
thesis="Bear case analysis unavailable", thesis="Bear case analysis unavailable",
confidence_0_to_1=0.1, confidence_0_to_1=0.1,
) )
return {"bear_case": result.model_dump()} return {"bear_case": result.model_dump()}
return node return node
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# Debate Referee # Debate Referee
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
def create_debate_node(llm): def create_debate_node(llm):
"""Referee that evaluates bull vs bear case.""" """Referee that evaluates bull vs bear case."""
def node(state: Dict[str, Any]) -> Dict[str, Any]: def node(state: Dict[str, Any]) -> Dict[str, Any]:
ticker = state["ticker"] ticker = state["ticker"]
bull = state.get("bull_case") or {} bull = state.get("bull_case") or {}
bear = state.get("bear_case") or {} bear = state.get("bear_case") or {}
prompt = f"""You are the Debate Referee. Evaluate the bull vs bear case for {ticker}. prompt = f"""You are the Debate Referee. Evaluate the bull vs bear case for {ticker}.
BULL CASE (confidence: {bull.get('confidence_0_to_1', 'N/A')}): BULL CASE (confidence: {bull.get('confidence_0_to_1', 'N/A')}):
Thesis: {bull.get('thesis', 'N/A')} Thesis: {bull.get('thesis', 'N/A')}
Catalysts: {', '.join(bull.get('catalysts', []))} Catalysts: {', '.join(bull.get('catalysts', []))}
Upside: {bull.get('upside_pct', 'N/A')}% Upside: {bull.get('upside_pct', 'N/A')}%
Invalidation: {', '.join(bull.get('thesis_invalidation_triggers', []))} Invalidation: {', '.join(bull.get('thesis_invalidation_triggers', []))}
BEAR CASE (confidence: {bear.get('confidence_0_to_1', 'N/A')}): BEAR CASE (confidence: {bear.get('confidence_0_to_1', 'N/A')}):
Thesis: {bear.get('thesis', 'N/A')} Thesis: {bear.get('thesis', 'N/A')}
Risks: {', '.join(bear.get('risks', []))} Risks: {', '.join(bear.get('risks', []))}
Downside: {bear.get('downside_pct', 'N/A')}% Downside: {bear.get('downside_pct', 'N/A')}%
Invalidation: {', '.join(bear.get('thesis_invalidation_triggers', []))} Invalidation: {', '.join(bear.get('thesis_invalidation_triggers', []))}
MASTER SCORE: {state.get('master_score', 'N/A')} | ROLE: {state.get('position_role', 'N/A')} MASTER SCORE: {state.get('master_score', 'N/A')} | ROLE: {state.get('position_role', 'N/A')}
INSTRUCTIONS: INSTRUCTIONS:
1. Declare winner: "bull" or "bear". 1. Declare winner: "bull" or "bear".
2. Score each side 0-10 on argument strength. 2. Score each side 0-10 on argument strength.
3. List key unresolved questions. 3. List key unresolved questions.
4. Set net_conviction_adjustment (-2 to +2) to modify the master score. 4. Set net_conviction_adjustment (-2 to +2) to modify the master score.
Positive = debate strengthened the bull case. Negative = weakened it. Positive = debate strengthened the bull case. Negative = weakened it.
5. Provide reasoning for your decision.""" 5. Provide reasoning for your decision."""
try: try:
result = invoke_structured(llm, DebateRefereeOutput, prompt) result = invoke_structured(llm, DebateRefereeOutput, prompt)
except Exception as e: except Exception as e:
logger.warning("Debate LLM failed: %s", e) logger.warning("Debate LLM failed: %s", e)
result = DebateRefereeOutput() result = DebateRefereeOutput()
return {"debate": result.model_dump()} return {"debate": result.model_dump()}
return node return node
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# Risk / Invalidation # Risk / Invalidation
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
def create_risk_node(llm): def create_risk_node(llm):
def node(state: Dict[str, Any]) -> Dict[str, Any]: def node(state: Dict[str, Any]) -> Dict[str, Any]:
ticker = state["ticker"] ticker = state["ticker"]
summary = _summarize_tier2(state) summary = _summarize_tier2(state)
bull = state.get("bull_case") or {} bull = state.get("bull_case") or {}
bear = state.get("bear_case") or {} bear = state.get("bear_case") or {}
debate = state.get("debate") or {} debate = state.get("debate") or {}
prompt = f"""You are the Risk / Invalidation Analyst. Final risk gate for {ticker}. prompt = f"""You are the Risk / Invalidation Analyst. Final risk gate for {ticker}.
{summary} {summary}
DEBATE OUTCOME: {debate.get('winner', '?')} won DEBATE OUTCOME: {debate.get('winner', '?')} won
Bull strength: {debate.get('bull_strength_0_to_10', '?')}/10 Bull strength: {debate.get('bull_strength_0_to_10', '?')}/10
Bear strength: {debate.get('bear_strength_0_to_10', '?')}/10 Bear strength: {debate.get('bear_strength_0_to_10', '?')}/10
Conviction adjustment: {debate.get('net_conviction_adjustment', 0)} Conviction adjustment: {debate.get('net_conviction_adjustment', 0)}
Bear risks: {', '.join(bear.get('risks', []))} Bear risks: {', '.join(bear.get('risks', []))}
Bull invalidation triggers: {', '.join(bull.get('thesis_invalidation_triggers', []))} Bull invalidation triggers: {', '.join(bull.get('thesis_invalidation_triggers', []))}
INSTRUCTIONS: INSTRUCTIONS:
1. Classify overall_risk_level: low / medium / high. 1. Classify overall_risk_level: low / medium / high.
2. Set max_position_size_pct (0-100). Low risk = up to 10%. High risk = max 2%. 2. Set max_position_size_pct (0-100). Low risk = up to 10%. High risk = max 2%.
3. Suggest stop_loss_pct (distance from entry to stop). 3. Suggest stop_loss_pct (distance from entry to stop).
4. List invalidation_triggers concrete events that should trigger exit. 4. List invalidation_triggers concrete events that should trigger exit.
5. Score overall risk-reward 0-10 (10 = great risk/reward). 5. Score overall risk-reward 0-10 (10 = great risk/reward).
6. Set veto=true ONLY if you find impossible/fraudulent data, or risk is so extreme 6. Set veto=true ONLY if you find impossible/fraudulent data, or risk is so extreme
that no position should be taken. This is a hard kill switch. that no position should be taken. This is a hard kill switch.
7. Be concise.""" 7. Be concise."""
try: try:
result = invoke_structured(llm, RiskInvalidationOutput, prompt) result = invoke_structured(llm, RiskInvalidationOutput, prompt)
except Exception as e: except Exception as e:
logger.warning("Risk LLM failed: %s", e) logger.warning("Risk LLM failed: %s", e)
result = RiskInvalidationOutput( result = RiskInvalidationOutput(
score_0_to_10=5.0, confidence_0_to_1=0.3, score_0_to_10=5.0, confidence_0_to_1=0.3,
summary_1_sentence="Risk analysis unavailable", summary_1_sentence="Risk analysis unavailable",
) )
flags = [f.model_dump() for f in result.data_quality_flags] flags = [f.model_dump() for f in result.data_quality_flags]
update: Dict[str, Any] = {"risk": result.model_dump(), "global_flags": flags} update: Dict[str, Any] = {"risk": result.model_dump(), "global_flags": flags}
if result.veto: if result.veto:
update["hard_veto"] = True update["hard_veto"] = True
update["hard_veto_reason"] = result.veto_reason update["hard_veto_reason"] = result.veto_reason
return update return update
return node return node
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# Final Decision (prose generated AFTER all scoring) # Final Decision (prose generated AFTER all scoring)
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
def create_final_decision_node(llm): def create_final_decision_node(llm):
def node(state: Dict[str, Any]) -> Dict[str, Any]: def node(state: Dict[str, Any]) -> Dict[str, Any]:
ticker = state["ticker"] ticker = state["ticker"]
card = state.get("company_card") or {} card = state.get("company_card") or {}
summary = _summarize_tier2(state) summary = _summarize_tier2(state)
bull = state.get("bull_case") or {} bull = state.get("bull_case") or {}
bear = state.get("bear_case") or {} bear = state.get("bear_case") or {}
debate = state.get("debate") or {} debate = state.get("debate") or {}
risk = state.get("risk") or {} risk = state.get("risk") or {}
theme = state.get("theme_substitution") or {} theme = state.get("theme_substitution") or {}
replacement = state.get("position_replacement") or {} replacement = state.get("position_replacement") or {}
master_score = state.get("master_score", 0) master_score = state.get("master_score", 0)
adjusted_score = state.get("adjusted_score", 0) adjusted_score = state.get("adjusted_score", 0)
position_role = state.get("position_role", "Avoid") position_role = state.get("position_role", "Avoid")
conviction_adj = debate.get("net_conviction_adjustment", 0) conviction_adj = debate.get("net_conviction_adjustment", 0)
# Apply debate conviction adjustment # Apply debate conviction adjustment
final_score = round(adjusted_score + conviction_adj, 2) final_score = round(adjusted_score + conviction_adj, 2)
final_role = _role_from_score(final_score) final_role = _role_from_score(final_score)
# Determine action # Determine action
if state.get("hard_veto"): if state.get("hard_veto"):
action = "AVOID" action = "AVOID"
final_role = "Avoid" final_role = "Avoid"
final_score = 0.0 final_score = 0.0
elif final_score >= 70: elif final_score >= 70:
action = "BUY" action = "BUY"
elif final_score >= 50: elif final_score >= 50:
action = "HOLD" action = "HOLD"
else: else:
action = "AVOID" action = "AVOID"
# Theme/replacement context # Theme/replacement context
theme_lines = "" theme_lines = ""
if theme.get("theme_name"): if theme.get("theme_name"):
theme_lines = ( theme_lines = (
f"\nTHEME CONTEXT:" f"\nTHEME CONTEXT:"
f"\n Theme: {theme.get('theme_name', '?')}" f"\n Theme: {theme.get('theme_name', '?')}"
f"\n Best expression: {'Yes' if theme.get('best_expression_of_theme') else 'No'}" f"\n Best expression: {'Yes' if theme.get('best_expression_of_theme') else 'No'}"
f"\n Stronger alternatives: {', '.join(theme.get('stronger_alternatives', [])) or 'None'}" f"\n Stronger alternatives: {', '.join(theme.get('stronger_alternatives', [])) or 'None'}"
f"\n Score gap vs best: {theme.get('relative_score_gap', 0):.1f}" f"\n Score gap vs best: {theme.get('relative_score_gap', 0):.1f}"
) )
if replacement.get("should_replace"): if replacement.get("should_replace"):
theme_lines += ( theme_lines += (
f"\n REPLACEMENT FLAG: Consider {replacement.get('replace_with', '?')} instead" f"\n REPLACEMENT FLAG: Consider {replacement.get('replace_with', '?')} instead"
f"\n Reason: {replacement.get('replacement_reason', '')}" f"\n Reason: {replacement.get('replacement_reason', '')}"
) )
prompt = f"""You are the Final Decision Synthesizer for {ticker}. prompt = f"""You are the Final Decision Synthesizer for {ticker}.
{summary} {summary}
DEBATE: {debate.get('winner', '?')} won | Conviction adjustment: {conviction_adj:+.1f} DEBATE: {debate.get('winner', '?')} won | Conviction adjustment: {conviction_adj:+.1f}
RISK: {risk.get('overall_risk_level', '?')} | Max position: {risk.get('max_position_size_pct', '?')}% RISK: {risk.get('overall_risk_level', '?')} | Max position: {risk.get('max_position_size_pct', '?')}%
{theme_lines} {theme_lines}
FINAL SCORES: FINAL SCORES:
Master Score: {master_score} Master Score: {master_score}
Adjusted Score: {adjusted_score} (after data quality penalties) Adjusted Score: {adjusted_score} (after data quality penalties)
Post-Debate Score: {final_score} (after conviction adjustment) Post-Debate Score: {final_score} (after conviction adjustment)
Position Role: {final_role} Position Role: {final_role}
Action: {action} Action: {action}
INSTRUCTIONS: INSTRUCTIONS:
Write a concise narrative (3-5 sentences) that: Write a concise narrative (3-5 sentences) that:
1. Summarizes the investment thesis. 1. Summarizes the investment thesis.
2. Highlights the top 2-3 catalysts and top 2-3 risks. 2. Highlights the top 2-3 catalysts and top 2-3 risks.
3. States the action ({action}) and position role ({final_role}). 3. States the action ({action}) and position role ({final_role}).
4. Notes what would change the thesis (invalidation triggers). 4. Notes what would change the thesis (invalidation triggers).
5. If theme analysis found stronger alternatives, mention them and whether 5. If theme analysis found stronger alternatives, mention them and whether
this stock is still the best expression of the theme. this stock is still the best expression of the theme.
Also provide: Also provide:
- thesis_summary (one sentence) - thesis_summary (one sentence)
- key_catalysts (top 3 from bull case) - key_catalysts (top 3 from bull case)
- key_risks (top 3 from bear case) - key_risks (top 3 from bear case)
- invalidation_triggers (from risk agent) - invalidation_triggers (from risk agent)
- position_sizing_pct (from risk agent) - position_sizing_pct (from risk agent)
- confidence (average of all agent confidences)""" - confidence (average of all agent confidences)"""
try: try:
result = invoke_structured(llm, FinalDecisionOutput, prompt) result = invoke_structured(llm, FinalDecisionOutput, prompt)
except Exception as e: except Exception as e:
logger.warning("FinalDecision LLM failed: %s", e) logger.warning("FinalDecision LLM failed: %s", e)
result = FinalDecisionOutput() result = FinalDecisionOutput()
# Override with computed values (deterministic, not LLM-driven) # Override with computed values (deterministic, not LLM-driven)
result.ticker = ticker result.ticker = ticker
result.company_name = card.get("company_name", "") result.company_name = card.get("company_name", "")
result.master_score = master_score result.master_score = master_score
result.adjusted_score = final_score result.adjusted_score = final_score
result.position_role = final_role result.position_role = final_role
result.action = action result.action = action
result.risk_level = risk.get("overall_risk_level", "medium") result.risk_level = risk.get("overall_risk_level", "medium")
result.position_sizing_pct = risk.get("max_position_size_pct", 0) result.position_sizing_pct = risk.get("max_position_size_pct", 0)
# Compute aggregate confidence # Compute aggregate confidence
agents_with_confidence = [ agents_with_confidence = [
state.get(k, {}).get("confidence_0_to_1") state.get(k, {}).get("confidence_0_to_1")
for k in ( for k in (
"macro", "liquidity", "business_quality", "institutional_flow", "macro", "liquidity", "business_quality", "institutional_flow",
"valuation", "entry_timing", "earnings_revisions", "valuation", "entry_timing", "earnings_revisions",
"sector_rotation", "backlog", "crowding", "sector_rotation", "backlog", "crowding",
) )
] ]
valid_confs = [c for c in agents_with_confidence if c is not None] valid_confs = [c for c in agents_with_confidence if c is not None]
result.confidence = round(sum(valid_confs) / len(valid_confs), 2) if valid_confs else 0.5 result.confidence = round(sum(valid_confs) / len(valid_confs), 2) if valid_confs else 0.5
return {"final_decision": result.model_dump()} return {"final_decision": result.model_dump()}
return node return node
def _role_from_score(score: float) -> str: def _role_from_score(score: float) -> str:
if score > 80: if score > 80:
return "Core Position" return "Core Position"
if score > 70: if score > 70:
return "Strong Position" return "Strong Position"
if score > 60: if score > 60:
return "Tactical / Satellite" return "Tactical / Satellite"
if score > 50: if score > 50:
return "Watchlist" return "Watchlist"
return "Avoid" return "Avoid"

View File

@ -1,46 +1,46 @@
import functools import functools
import time import time
import json import json
def create_trader(llm, memory): def create_trader(llm, memory):
def trader_node(state, name): def trader_node(state, name):
company_name = state["company_of_interest"] company_name = state["company_of_interest"]
investment_plan = state["investment_plan"] investment_plan = state["investment_plan"]
market_research_report = state["market_report"] market_research_report = state["market_report"]
sentiment_report = state["sentiment_report"] sentiment_report = state["sentiment_report"]
news_report = state["news_report"] news_report = state["news_report"]
fundamentals_report = state["fundamentals_report"] fundamentals_report = state["fundamentals_report"]
curr_situation = f"{market_research_report}\n\n{sentiment_report}\n\n{news_report}\n\n{fundamentals_report}" curr_situation = f"{market_research_report}\n\n{sentiment_report}\n\n{news_report}\n\n{fundamentals_report}"
past_memories = memory.get_memories(curr_situation, n_matches=2) past_memories = memory.get_memories(curr_situation, n_matches=2)
past_memory_str = "" past_memory_str = ""
if past_memories: if past_memories:
for i, rec in enumerate(past_memories, 1): for i, rec in enumerate(past_memories, 1):
past_memory_str += rec["recommendation"] + "\n\n" past_memory_str += rec["recommendation"] + "\n\n"
else: else:
past_memory_str = "No past memories found." past_memory_str = "No past memories found."
context = { context = {
"role": "user", "role": "user",
"content": f"Based on a comprehensive analysis by a team of analysts, here is an investment plan tailored for {company_name}. This plan incorporates insights from current technical market trends, macroeconomic indicators, and social media sentiment. Use this plan as a foundation for evaluating your next trading decision.\n\nProposed Investment Plan: {investment_plan}\n\nLeverage these insights to make an informed and strategic decision.", "content": f"Based on a comprehensive analysis by a team of analysts, here is an investment plan tailored for {company_name}. This plan incorporates insights from current technical market trends, macroeconomic indicators, and social media sentiment. Use this plan as a foundation for evaluating your next trading decision.\n\nProposed Investment Plan: {investment_plan}\n\nLeverage these insights to make an informed and strategic decision.",
} }
messages = [ messages = [
{ {
"role": "system", "role": "system",
"content": f"""You are a trading agent analyzing market data to make investment decisions. Based on your analysis, provide a specific recommendation to buy, sell, or hold. End with a firm decision and always conclude your response with 'FINAL TRANSACTION PROPOSAL: **BUY/HOLD/SELL**' to confirm your recommendation. Do not forget to utilize lessons from past decisions to learn from your mistakes. Here is some reflections from similar situatiosn you traded in and the lessons learned: {past_memory_str}""", "content": f"""You are a trading agent analyzing market data to make investment decisions. Based on your analysis, provide a specific recommendation to buy, sell, or hold. End with a firm decision and always conclude your response with 'FINAL TRANSACTION PROPOSAL: **BUY/HOLD/SELL**' to confirm your recommendation. Do not forget to utilize lessons from past decisions to learn from your mistakes. Here is some reflections from similar situatiosn you traded in and the lessons learned: {past_memory_str}""",
}, },
context, context,
] ]
result = llm.invoke(messages) result = llm.invoke(messages)
return { return {
"messages": [result], "messages": [result],
"trader_investment_plan": result.content, "trader_investment_plan": result.content,
"sender": name, "sender": name,
} }
return functools.partial(trader_node, name="Trader") return functools.partial(trader_node, name="Trader")

View File

@ -1,109 +1,109 @@
"""State definitions for the TradingAgents pipeline. """State definitions for the TradingAgents pipeline.
PipelineState is the new structured state used by the equity ranking engine. PipelineState is the new structured state used by the equity ranking engine.
Legacy state types are preserved for backward compatibility. Legacy state types are preserved for backward compatibility.
""" """
from __future__ import annotations from __future__ import annotations
import operator import operator
from typing import Annotated, Optional, Sequence from typing import Annotated, Optional, Sequence
from typing_extensions import TypedDict from typing_extensions import TypedDict
from langchain_openai import ChatOpenAI from langchain_openai import ChatOpenAI
from langgraph.graph import MessagesState from langgraph.graph import MessagesState
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# New structured pipeline state # New structured pipeline state
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
class PipelineState(TypedDict): class PipelineState(TypedDict):
"""Shared state for the structured equity ranking pipeline. """Shared state for the structured equity ranking pipeline.
Each agent writes its output as a dict (Pydantic .model_dump()). Each agent writes its output as a dict (Pydantic .model_dump()).
The scoring node computes master_score/adjusted_score deterministically. The scoring node computes master_score/adjusted_score deterministically.
global_flags uses operator.add to accumulate across all agents. global_flags uses operator.add to accumulate across all agents.
""" """
ticker: str ticker: str
trade_date: str trade_date: str
# Tier 1 # Tier 1
validation: Optional[dict] validation: Optional[dict]
company_card: Optional[dict] company_card: Optional[dict]
macro: Optional[dict] macro: Optional[dict]
liquidity: Optional[dict] liquidity: Optional[dict]
# Tier 2 # Tier 2
sector_rotation: Optional[dict] sector_rotation: Optional[dict]
business_quality: Optional[dict] business_quality: Optional[dict]
institutional_flow: Optional[dict] institutional_flow: Optional[dict]
valuation: Optional[dict] valuation: Optional[dict]
entry_timing: Optional[dict] entry_timing: Optional[dict]
earnings_revisions: Optional[dict] earnings_revisions: Optional[dict]
backlog: Optional[dict] backlog: Optional[dict]
crowding: Optional[dict] crowding: Optional[dict]
archetype: Optional[dict] archetype: Optional[dict]
# Scoring (deterministic) # Scoring (deterministic)
master_score: Optional[float] master_score: Optional[float]
adjusted_score: Optional[float] adjusted_score: Optional[float]
position_role: Optional[str] position_role: Optional[str]
# Portfolio-level # Portfolio-level
theme_substitution: Optional[dict] theme_substitution: Optional[dict]
position_replacement: Optional[dict] position_replacement: Optional[dict]
# Tier 3 # Tier 3
bull_case: Optional[dict] bull_case: Optional[dict]
bear_case: Optional[dict] bear_case: Optional[dict]
debate: Optional[dict] debate: Optional[dict]
risk: Optional[dict] risk: Optional[dict]
final_decision: Optional[dict] final_decision: Optional[dict]
# Control # Control
hard_veto: bool hard_veto: bool
hard_veto_reason: Optional[str] hard_veto_reason: Optional[str]
global_flags: Annotated[list, operator.add] global_flags: Annotated[list, operator.add]
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# Legacy state types (preserved for backward compatibility) # Legacy state types (preserved for backward compatibility)
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
class InvestDebateState(TypedDict): class InvestDebateState(TypedDict):
bull_history: Annotated[str, "Bullish Conversation history"] bull_history: Annotated[str, "Bullish Conversation history"]
bear_history: Annotated[str, "Bearish Conversation history"] bear_history: Annotated[str, "Bearish Conversation history"]
history: Annotated[str, "Conversation history"] history: Annotated[str, "Conversation history"]
current_response: Annotated[str, "Latest response"] current_response: Annotated[str, "Latest response"]
judge_decision: Annotated[str, "Final judge decision"] judge_decision: Annotated[str, "Final judge decision"]
count: Annotated[int, "Length of the current conversation"] count: Annotated[int, "Length of the current conversation"]
class RiskDebateState(TypedDict): class RiskDebateState(TypedDict):
aggressive_history: Annotated[str, "Aggressive Agent's Conversation history"] aggressive_history: Annotated[str, "Aggressive Agent's Conversation history"]
conservative_history: Annotated[str, "Conservative Agent's Conversation history"] conservative_history: Annotated[str, "Conservative Agent's Conversation history"]
neutral_history: Annotated[str, "Neutral Agent's Conversation history"] neutral_history: Annotated[str, "Neutral Agent's Conversation history"]
history: Annotated[str, "Conversation history"] history: Annotated[str, "Conversation history"]
latest_speaker: Annotated[str, "Analyst that spoke last"] latest_speaker: Annotated[str, "Analyst that spoke last"]
current_aggressive_response: Annotated[str, "Latest response by the aggressive analyst"] current_aggressive_response: Annotated[str, "Latest response by the aggressive analyst"]
current_conservative_response: Annotated[str, "Latest response by the conservative analyst"] current_conservative_response: Annotated[str, "Latest response by the conservative analyst"]
current_neutral_response: Annotated[str, "Latest response by the neutral analyst"] current_neutral_response: Annotated[str, "Latest response by the neutral analyst"]
judge_decision: Annotated[str, "Judge's decision"] judge_decision: Annotated[str, "Judge's decision"]
count: Annotated[int, "Length of the current conversation"] count: Annotated[int, "Length of the current conversation"]
class AgentState(MessagesState): class AgentState(MessagesState):
company_of_interest: Annotated[str, "Company that we are interested in trading"] company_of_interest: Annotated[str, "Company that we are interested in trading"]
trade_date: Annotated[str, "What date we are trading at"] trade_date: Annotated[str, "What date we are trading at"]
sender: Annotated[str, "Agent that sent this message"] sender: Annotated[str, "Agent that sent this message"]
market_report: Annotated[str, "Report from the Market Analyst"] market_report: Annotated[str, "Report from the Market Analyst"]
sentiment_report: Annotated[str, "Report from the Social Media Analyst"] sentiment_report: Annotated[str, "Report from the Social Media Analyst"]
news_report: Annotated[str, "Report from the News Researcher"] news_report: Annotated[str, "Report from the News Researcher"]
fundamentals_report: Annotated[str, "Report from the Fundamentals Researcher"] fundamentals_report: Annotated[str, "Report from the Fundamentals Researcher"]
investment_debate_state: Annotated[InvestDebateState, "Current state of the investment debate"] investment_debate_state: Annotated[InvestDebateState, "Current state of the investment debate"]
investment_plan: Annotated[str, "Plan generated by the Analyst"] investment_plan: Annotated[str, "Plan generated by the Analyst"]
trader_investment_plan: Annotated[str, "Plan generated by the Trader"] trader_investment_plan: Annotated[str, "Plan generated by the Trader"]
risk_debate_state: Annotated[RiskDebateState, "Current state of the risk debate"] risk_debate_state: Annotated[RiskDebateState, "Current state of the risk debate"]
final_trade_decision: Annotated[str, "Final decision made by the Risk Analysts"] final_trade_decision: Annotated[str, "Final decision made by the Risk Analysts"]

View File

@ -1,38 +1,38 @@
from langchain_core.messages import HumanMessage, RemoveMessage from langchain_core.messages import HumanMessage, RemoveMessage
# Import tools from separate utility files # Import tools from separate utility files
from tradingagents.agents.utils.core_stock_tools import ( from tradingagents.agents.utils.core_stock_tools import (
get_stock_data get_stock_data
) )
from tradingagents.agents.utils.technical_indicators_tools import ( from tradingagents.agents.utils.technical_indicators_tools import (
get_indicators get_indicators
) )
from tradingagents.agents.utils.fundamental_data_tools import ( from tradingagents.agents.utils.fundamental_data_tools import (
get_fundamentals, get_fundamentals,
get_balance_sheet, get_balance_sheet,
get_cashflow, get_cashflow,
get_income_statement get_income_statement
) )
from tradingagents.agents.utils.news_data_tools import ( from tradingagents.agents.utils.news_data_tools import (
get_news, get_news,
get_insider_transactions, get_insider_transactions,
get_global_news get_global_news
) )
def create_msg_delete(): def create_msg_delete():
def delete_messages(state): def delete_messages(state):
"""Clear messages and add placeholder for Anthropic compatibility""" """Clear messages and add placeholder for Anthropic compatibility"""
messages = state["messages"] messages = state["messages"]
# Remove all messages # Remove all messages
removal_operations = [RemoveMessage(id=m.id) for m in messages] removal_operations = [RemoveMessage(id=m.id) for m in messages]
# Add a minimal placeholder message # Add a minimal placeholder message
placeholder = HumanMessage(content="Continue") placeholder = HumanMessage(content="Continue")
return {"messages": removal_operations + [placeholder]} return {"messages": removal_operations + [placeholder]}
return delete_messages return delete_messages

View File

@ -1,22 +1,22 @@
from langchain_core.tools import tool from langchain_core.tools import tool
from typing import Annotated from typing import Annotated
from tradingagents.dataflows.interface import route_to_vendor from tradingagents.dataflows.interface import route_to_vendor
@tool @tool
def get_stock_data( def get_stock_data(
symbol: Annotated[str, "ticker symbol of the company"], symbol: Annotated[str, "ticker symbol of the company"],
start_date: Annotated[str, "Start date in yyyy-mm-dd format"], start_date: Annotated[str, "Start date in yyyy-mm-dd format"],
end_date: Annotated[str, "End date in yyyy-mm-dd format"], end_date: Annotated[str, "End date in yyyy-mm-dd format"],
) -> str: ) -> str:
""" """
Retrieve stock price data (OHLCV) for a given ticker symbol. Retrieve stock price data (OHLCV) for a given ticker symbol.
Uses the configured core_stock_apis vendor. Uses the configured core_stock_apis vendor.
Args: Args:
symbol (str): Ticker symbol of the company, e.g. AAPL, TSM symbol (str): Ticker symbol of the company, e.g. AAPL, TSM
start_date (str): Start date in yyyy-mm-dd format start_date (str): Start date in yyyy-mm-dd format
end_date (str): End date in yyyy-mm-dd format end_date (str): End date in yyyy-mm-dd format
Returns: Returns:
str: A formatted dataframe containing the stock price data for the specified ticker symbol in the specified date range. str: A formatted dataframe containing the stock price data for the specified ticker symbol in the specified date range.
""" """
return route_to_vendor("get_stock_data", symbol, start_date, end_date) return route_to_vendor("get_stock_data", symbol, start_date, end_date)

View File

@ -1,77 +1,77 @@
from langchain_core.tools import tool from langchain_core.tools import tool
from typing import Annotated from typing import Annotated
from tradingagents.dataflows.interface import route_to_vendor from tradingagents.dataflows.interface import route_to_vendor
@tool @tool
def get_fundamentals( def get_fundamentals(
ticker: Annotated[str, "ticker symbol"], ticker: Annotated[str, "ticker symbol"],
curr_date: Annotated[str, "current date you are trading at, yyyy-mm-dd"], curr_date: Annotated[str, "current date you are trading at, yyyy-mm-dd"],
) -> str: ) -> str:
""" """
Retrieve comprehensive fundamental data for a given ticker symbol. Retrieve comprehensive fundamental data for a given ticker symbol.
Uses the configured fundamental_data vendor. Uses the configured fundamental_data vendor.
Args: Args:
ticker (str): Ticker symbol of the company ticker (str): Ticker symbol of the company
curr_date (str): Current date you are trading at, yyyy-mm-dd curr_date (str): Current date you are trading at, yyyy-mm-dd
Returns: Returns:
str: A formatted report containing comprehensive fundamental data str: A formatted report containing comprehensive fundamental data
""" """
return route_to_vendor("get_fundamentals", ticker, curr_date) return route_to_vendor("get_fundamentals", ticker, curr_date)
@tool @tool
def get_balance_sheet( def get_balance_sheet(
ticker: Annotated[str, "ticker symbol"], ticker: Annotated[str, "ticker symbol"],
freq: Annotated[str, "reporting frequency: annual/quarterly"] = "quarterly", freq: Annotated[str, "reporting frequency: annual/quarterly"] = "quarterly",
curr_date: Annotated[str, "current date you are trading at, yyyy-mm-dd"] = None, curr_date: Annotated[str, "current date you are trading at, yyyy-mm-dd"] = None,
) -> str: ) -> str:
""" """
Retrieve balance sheet data for a given ticker symbol. Retrieve balance sheet data for a given ticker symbol.
Uses the configured fundamental_data vendor. Uses the configured fundamental_data vendor.
Args: Args:
ticker (str): Ticker symbol of the company ticker (str): Ticker symbol of the company
freq (str): Reporting frequency: annual/quarterly (default quarterly) freq (str): Reporting frequency: annual/quarterly (default quarterly)
curr_date (str): Current date you are trading at, yyyy-mm-dd curr_date (str): Current date you are trading at, yyyy-mm-dd
Returns: Returns:
str: A formatted report containing balance sheet data str: A formatted report containing balance sheet data
""" """
return route_to_vendor("get_balance_sheet", ticker, freq, curr_date) return route_to_vendor("get_balance_sheet", ticker, freq, curr_date)
@tool @tool
def get_cashflow( def get_cashflow(
ticker: Annotated[str, "ticker symbol"], ticker: Annotated[str, "ticker symbol"],
freq: Annotated[str, "reporting frequency: annual/quarterly"] = "quarterly", freq: Annotated[str, "reporting frequency: annual/quarterly"] = "quarterly",
curr_date: Annotated[str, "current date you are trading at, yyyy-mm-dd"] = None, curr_date: Annotated[str, "current date you are trading at, yyyy-mm-dd"] = None,
) -> str: ) -> str:
""" """
Retrieve cash flow statement data for a given ticker symbol. Retrieve cash flow statement data for a given ticker symbol.
Uses the configured fundamental_data vendor. Uses the configured fundamental_data vendor.
Args: Args:
ticker (str): Ticker symbol of the company ticker (str): Ticker symbol of the company
freq (str): Reporting frequency: annual/quarterly (default quarterly) freq (str): Reporting frequency: annual/quarterly (default quarterly)
curr_date (str): Current date you are trading at, yyyy-mm-dd curr_date (str): Current date you are trading at, yyyy-mm-dd
Returns: Returns:
str: A formatted report containing cash flow statement data str: A formatted report containing cash flow statement data
""" """
return route_to_vendor("get_cashflow", ticker, freq, curr_date) return route_to_vendor("get_cashflow", ticker, freq, curr_date)
@tool @tool
def get_income_statement( def get_income_statement(
ticker: Annotated[str, "ticker symbol"], ticker: Annotated[str, "ticker symbol"],
freq: Annotated[str, "reporting frequency: annual/quarterly"] = "quarterly", freq: Annotated[str, "reporting frequency: annual/quarterly"] = "quarterly",
curr_date: Annotated[str, "current date you are trading at, yyyy-mm-dd"] = None, curr_date: Annotated[str, "current date you are trading at, yyyy-mm-dd"] = None,
) -> str: ) -> str:
""" """
Retrieve income statement data for a given ticker symbol. Retrieve income statement data for a given ticker symbol.
Uses the configured fundamental_data vendor. Uses the configured fundamental_data vendor.
Args: Args:
ticker (str): Ticker symbol of the company ticker (str): Ticker symbol of the company
freq (str): Reporting frequency: annual/quarterly (default quarterly) freq (str): Reporting frequency: annual/quarterly (default quarterly)
curr_date (str): Current date you are trading at, yyyy-mm-dd curr_date (str): Current date you are trading at, yyyy-mm-dd
Returns: Returns:
str: A formatted report containing income statement data str: A formatted report containing income statement data
""" """
return route_to_vendor("get_income_statement", ticker, freq, curr_date) return route_to_vendor("get_income_statement", ticker, freq, curr_date)

File diff suppressed because it is too large Load Diff

View File

@ -1,144 +1,144 @@
"""Financial situation memory using BM25 for lexical similarity matching. """Financial situation memory using BM25 for lexical similarity matching.
Uses BM25 (Best Matching 25) algorithm for retrieval - no API calls, Uses BM25 (Best Matching 25) algorithm for retrieval - no API calls,
no token limits, works offline with any LLM provider. no token limits, works offline with any LLM provider.
""" """
from rank_bm25 import BM25Okapi from rank_bm25 import BM25Okapi
from typing import List, Tuple from typing import List, Tuple
import re import re
class FinancialSituationMemory: class FinancialSituationMemory:
"""Memory system for storing and retrieving financial situations using BM25.""" """Memory system for storing and retrieving financial situations using BM25."""
def __init__(self, name: str, config: dict = None): def __init__(self, name: str, config: dict = None):
"""Initialize the memory system. """Initialize the memory system.
Args: Args:
name: Name identifier for this memory instance name: Name identifier for this memory instance
config: Configuration dict (kept for API compatibility, not used for BM25) config: Configuration dict (kept for API compatibility, not used for BM25)
""" """
self.name = name self.name = name
self.documents: List[str] = [] self.documents: List[str] = []
self.recommendations: List[str] = [] self.recommendations: List[str] = []
self.bm25 = None self.bm25 = None
def _tokenize(self, text: str) -> List[str]: def _tokenize(self, text: str) -> List[str]:
"""Tokenize text for BM25 indexing. """Tokenize text for BM25 indexing.
Simple whitespace + punctuation tokenization with lowercasing. Simple whitespace + punctuation tokenization with lowercasing.
""" """
# Lowercase and split on non-alphanumeric characters # Lowercase and split on non-alphanumeric characters
tokens = re.findall(r'\b\w+\b', text.lower()) tokens = re.findall(r'\b\w+\b', text.lower())
return tokens return tokens
def _rebuild_index(self): def _rebuild_index(self):
"""Rebuild the BM25 index after adding documents.""" """Rebuild the BM25 index after adding documents."""
if self.documents: if self.documents:
tokenized_docs = [self._tokenize(doc) for doc in self.documents] tokenized_docs = [self._tokenize(doc) for doc in self.documents]
self.bm25 = BM25Okapi(tokenized_docs) self.bm25 = BM25Okapi(tokenized_docs)
else: else:
self.bm25 = None self.bm25 = None
def add_situations(self, situations_and_advice: List[Tuple[str, str]]): def add_situations(self, situations_and_advice: List[Tuple[str, str]]):
"""Add financial situations and their corresponding advice. """Add financial situations and their corresponding advice.
Args: Args:
situations_and_advice: List of tuples (situation, recommendation) situations_and_advice: List of tuples (situation, recommendation)
""" """
for situation, recommendation in situations_and_advice: for situation, recommendation in situations_and_advice:
self.documents.append(situation) self.documents.append(situation)
self.recommendations.append(recommendation) self.recommendations.append(recommendation)
# Rebuild BM25 index with new documents # Rebuild BM25 index with new documents
self._rebuild_index() self._rebuild_index()
def get_memories(self, current_situation: str, n_matches: int = 1) -> List[dict]: def get_memories(self, current_situation: str, n_matches: int = 1) -> List[dict]:
"""Find matching recommendations using BM25 similarity. """Find matching recommendations using BM25 similarity.
Args: Args:
current_situation: The current financial situation to match against current_situation: The current financial situation to match against
n_matches: Number of top matches to return n_matches: Number of top matches to return
Returns: Returns:
List of dicts with matched_situation, recommendation, and similarity_score List of dicts with matched_situation, recommendation, and similarity_score
""" """
if not self.documents or self.bm25 is None: if not self.documents or self.bm25 is None:
return [] return []
# Tokenize query # Tokenize query
query_tokens = self._tokenize(current_situation) query_tokens = self._tokenize(current_situation)
# Get BM25 scores for all documents # Get BM25 scores for all documents
scores = self.bm25.get_scores(query_tokens) scores = self.bm25.get_scores(query_tokens)
# Get top-n indices sorted by score (descending) # Get top-n indices sorted by score (descending)
top_indices = sorted(range(len(scores)), key=lambda i: scores[i], reverse=True)[:n_matches] top_indices = sorted(range(len(scores)), key=lambda i: scores[i], reverse=True)[:n_matches]
# Build results # Build results
results = [] results = []
max_score = max(scores) if max(scores) > 0 else 1 # Normalize scores max_score = max(scores) if max(scores) > 0 else 1 # Normalize scores
for idx in top_indices: for idx in top_indices:
# Normalize score to 0-1 range for consistency # Normalize score to 0-1 range for consistency
normalized_score = scores[idx] / max_score if max_score > 0 else 0 normalized_score = scores[idx] / max_score if max_score > 0 else 0
results.append({ results.append({
"matched_situation": self.documents[idx], "matched_situation": self.documents[idx],
"recommendation": self.recommendations[idx], "recommendation": self.recommendations[idx],
"similarity_score": normalized_score, "similarity_score": normalized_score,
}) })
return results return results
def clear(self): def clear(self):
"""Clear all stored memories.""" """Clear all stored memories."""
self.documents = [] self.documents = []
self.recommendations = [] self.recommendations = []
self.bm25 = None self.bm25 = None
if __name__ == "__main__": if __name__ == "__main__":
# Example usage # Example usage
matcher = FinancialSituationMemory("test_memory") matcher = FinancialSituationMemory("test_memory")
# Example data # Example data
example_data = [ example_data = [
( (
"High inflation rate with rising interest rates and declining consumer spending", "High inflation rate with rising interest rates and declining consumer spending",
"Consider defensive sectors like consumer staples and utilities. Review fixed-income portfolio duration.", "Consider defensive sectors like consumer staples and utilities. Review fixed-income portfolio duration.",
), ),
( (
"Tech sector showing high volatility with increasing institutional selling pressure", "Tech sector showing high volatility with increasing institutional selling pressure",
"Reduce exposure to high-growth tech stocks. Look for value opportunities in established tech companies with strong cash flows.", "Reduce exposure to high-growth tech stocks. Look for value opportunities in established tech companies with strong cash flows.",
), ),
( (
"Strong dollar affecting emerging markets with increasing forex volatility", "Strong dollar affecting emerging markets with increasing forex volatility",
"Hedge currency exposure in international positions. Consider reducing allocation to emerging market debt.", "Hedge currency exposure in international positions. Consider reducing allocation to emerging market debt.",
), ),
( (
"Market showing signs of sector rotation with rising yields", "Market showing signs of sector rotation with rising yields",
"Rebalance portfolio to maintain target allocations. Consider increasing exposure to sectors benefiting from higher rates.", "Rebalance portfolio to maintain target allocations. Consider increasing exposure to sectors benefiting from higher rates.",
), ),
] ]
# Add the example situations and recommendations # Add the example situations and recommendations
matcher.add_situations(example_data) matcher.add_situations(example_data)
# Example query # Example query
current_situation = """ current_situation = """
Market showing increased volatility in tech sector, with institutional investors Market showing increased volatility in tech sector, with institutional investors
reducing positions and rising interest rates affecting growth stock valuations reducing positions and rising interest rates affecting growth stock valuations
""" """
try: try:
recommendations = matcher.get_memories(current_situation, n_matches=2) recommendations = matcher.get_memories(current_situation, n_matches=2)
for i, rec in enumerate(recommendations, 1): for i, rec in enumerate(recommendations, 1):
print(f"\nMatch {i}:") print(f"\nMatch {i}:")
print(f"Similarity Score: {rec['similarity_score']:.2f}") print(f"Similarity Score: {rec['similarity_score']:.2f}")
print(f"Matched Situation: {rec['matched_situation']}") print(f"Matched Situation: {rec['matched_situation']}")
print(f"Recommendation: {rec['recommendation']}") print(f"Recommendation: {rec['recommendation']}")
except Exception as e: except Exception as e:
print(f"Error during recommendation: {str(e)}") print(f"Error during recommendation: {str(e)}")

View File

@ -1,53 +1,53 @@
from langchain_core.tools import tool from langchain_core.tools import tool
from typing import Annotated from typing import Annotated
from tradingagents.dataflows.interface import route_to_vendor from tradingagents.dataflows.interface import route_to_vendor
@tool @tool
def get_news( def get_news(
ticker: Annotated[str, "Ticker symbol"], ticker: Annotated[str, "Ticker symbol"],
start_date: Annotated[str, "Start date in yyyy-mm-dd format"], start_date: Annotated[str, "Start date in yyyy-mm-dd format"],
end_date: Annotated[str, "End date in yyyy-mm-dd format"], end_date: Annotated[str, "End date in yyyy-mm-dd format"],
) -> str: ) -> str:
""" """
Retrieve news data for a given ticker symbol. Retrieve news data for a given ticker symbol.
Uses the configured news_data vendor. Uses the configured news_data vendor.
Args: Args:
ticker (str): Ticker symbol ticker (str): Ticker symbol
start_date (str): Start date in yyyy-mm-dd format start_date (str): Start date in yyyy-mm-dd format
end_date (str): End date in yyyy-mm-dd format end_date (str): End date in yyyy-mm-dd format
Returns: Returns:
str: A formatted string containing news data str: A formatted string containing news data
""" """
return route_to_vendor("get_news", ticker, start_date, end_date) return route_to_vendor("get_news", ticker, start_date, end_date)
@tool @tool
def get_global_news( def get_global_news(
curr_date: Annotated[str, "Current date in yyyy-mm-dd format"], curr_date: Annotated[str, "Current date in yyyy-mm-dd format"],
look_back_days: Annotated[int, "Number of days to look back"] = 7, look_back_days: Annotated[int, "Number of days to look back"] = 7,
limit: Annotated[int, "Maximum number of articles to return"] = 5, limit: Annotated[int, "Maximum number of articles to return"] = 5,
) -> str: ) -> str:
""" """
Retrieve global news data. Retrieve global news data.
Uses the configured news_data vendor. Uses the configured news_data vendor.
Args: Args:
curr_date (str): Current date in yyyy-mm-dd format curr_date (str): Current date in yyyy-mm-dd format
look_back_days (int): Number of days to look back (default 7) look_back_days (int): Number of days to look back (default 7)
limit (int): Maximum number of articles to return (default 5) limit (int): Maximum number of articles to return (default 5)
Returns: Returns:
str: A formatted string containing global news data str: A formatted string containing global news data
""" """
return route_to_vendor("get_global_news", curr_date, look_back_days, limit) return route_to_vendor("get_global_news", curr_date, look_back_days, limit)
@tool @tool
def get_insider_transactions( def get_insider_transactions(
ticker: Annotated[str, "ticker symbol"], ticker: Annotated[str, "ticker symbol"],
) -> str: ) -> str:
""" """
Retrieve insider transaction information about a company. Retrieve insider transaction information about a company.
Uses the configured news_data vendor. Uses the configured news_data vendor.
Args: Args:
ticker (str): Ticker symbol of the company ticker (str): Ticker symbol of the company
Returns: Returns:
str: A report of insider transaction data str: A report of insider transaction data
""" """
return route_to_vendor("get_insider_transactions", ticker) return route_to_vendor("get_insider_transactions", ticker)

View File

@ -1,26 +1,26 @@
from langchain_core.tools import tool from langchain_core.tools import tool
from typing import Annotated from typing import Annotated
from tradingagents.dataflows.interface import route_to_vendor from tradingagents.dataflows.interface import route_to_vendor
@tool @tool
def get_indicators( def get_indicators(
symbol: Annotated[str, "ticker symbol of the company"], symbol: Annotated[str, "ticker symbol of the company"],
indicator: Annotated[str, "technical indicator to get the analysis and report of"], indicator: Annotated[str, "technical indicator to get the analysis and report of"],
curr_date: Annotated[str, "The current trading date you are trading on, YYYY-mm-dd"], curr_date: Annotated[str, "The current trading date you are trading on, YYYY-mm-dd"],
look_back_days: Annotated[int, "how many days to look back"] = 30, look_back_days: Annotated[int, "how many days to look back"] = 30,
) -> str: ) -> str:
""" """
Retrieve technical indicators for a given ticker symbol. Retrieve technical indicators for a given ticker symbol.
Uses the configured technical_indicators vendor. Uses the configured technical_indicators vendor.
Args: Args:
symbol (str): Ticker symbol of the company, e.g. AAPL, TSM symbol (str): Ticker symbol of the company, e.g. AAPL, TSM
indicator (str): Technical indicator. Supported: close_50_sma, close_200_sma, close_10_ema, macd, macds, macdh, rsi, boll, boll_ub, boll_lb, atr, vwma, mfi indicator (str): Technical indicator. Supported: close_50_sma, close_200_sma, close_10_ema, macd, macds, macdh, rsi, boll, boll_ub, boll_lb, atr, vwma, mfi
curr_date (str): The current trading date you are trading on, YYYY-mm-dd curr_date (str): The current trading date you are trading on, YYYY-mm-dd
look_back_days (int): How many days to look back, default is 30 look_back_days (int): How many days to look back, default is 30
Returns: Returns:
str: A formatted dataframe containing the technical indicators for the specified ticker symbol and indicator. str: A formatted dataframe containing the technical indicators for the specified ticker symbol and indicator.
""" """
try: try:
return route_to_vendor("get_indicators", symbol, indicator, curr_date, look_back_days) return route_to_vendor("get_indicators", symbol, indicator, curr_date, look_back_days)
except ValueError as e: except ValueError as e:
return str(e) return str(e)

View File

@ -1,321 +1,321 @@
"""Alpaca Market Data API client for the equity ranking engine. """Alpaca Market Data API client for the equity ranking engine.
Provides price bars, snapshots, and news. Fundamentals still come from yfinance. Provides price bars, snapshots, and news. Fundamentals still come from yfinance.
Free tier: 10,000 requests/min, up to 7 years of historical data. Free tier: 10,000 requests/min, up to 7 years of historical data.
""" """
from __future__ import annotations from __future__ import annotations
import json import json
import logging import logging
import os import os
from datetime import datetime, timedelta from datetime import datetime, timedelta
from typing import Any, Dict, List, Optional from typing import Any, Dict, List, Optional
import pandas as pd import pandas as pd
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# Client setup (lazy init) # Client setup (lazy init)
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
_stock_client = None _stock_client = None
_news_client = None _news_client = None
def _get_stock_client(): def _get_stock_client():
global _stock_client global _stock_client
if _stock_client is None: if _stock_client is None:
from alpaca.data.historical import StockHistoricalDataClient from alpaca.data.historical import StockHistoricalDataClient
key = os.environ.get("ALPACA_API_KEY", "") key = os.environ.get("ALPACA_API_KEY", "")
secret = os.environ.get("ALPACA_API_SECRET", "") secret = os.environ.get("ALPACA_API_SECRET", "")
if not key or not secret: if not key or not secret:
raise RuntimeError( raise RuntimeError(
"ALPACA_API_KEY and ALPACA_API_SECRET must be set" "ALPACA_API_KEY and ALPACA_API_SECRET must be set"
) )
_stock_client = StockHistoricalDataClient(key, secret) _stock_client = StockHistoricalDataClient(key, secret)
return _stock_client return _stock_client
def _get_news_client(): def _get_news_client():
global _news_client global _news_client
if _news_client is None: if _news_client is None:
from alpaca.data.historical.news import NewsClient from alpaca.data.historical.news import NewsClient
key = os.environ.get("ALPACA_API_KEY", "") key = os.environ.get("ALPACA_API_KEY", "")
secret = os.environ.get("ALPACA_API_SECRET", "") secret = os.environ.get("ALPACA_API_SECRET", "")
_news_client = NewsClient(key, secret) _news_client = NewsClient(key, secret)
return _news_client return _news_client
def alpaca_available() -> bool: def alpaca_available() -> bool:
"""Check if Alpaca credentials are configured.""" """Check if Alpaca credentials are configured."""
return bool( return bool(
os.environ.get("ALPACA_API_KEY") os.environ.get("ALPACA_API_KEY")
and os.environ.get("ALPACA_API_SECRET") and os.environ.get("ALPACA_API_SECRET")
) )
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# Price / Bar data # Price / Bar data
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
def get_bars( def get_bars(
symbol: str, symbol: str,
start_date: str, start_date: str,
end_date: str, end_date: str,
timeframe: str = "1Day", timeframe: str = "1Day",
) -> pd.DataFrame: ) -> pd.DataFrame:
"""Fetch historical bars from Alpaca. """Fetch historical bars from Alpaca.
Args: Args:
symbol: Ticker symbol (e.g., "AAPL") symbol: Ticker symbol (e.g., "AAPL")
start_date: Start date in YYYY-MM-DD format start_date: Start date in YYYY-MM-DD format
end_date: End date in YYYY-MM-DD format end_date: End date in YYYY-MM-DD format
timeframe: "1Min", "5Min", "15Min", "1Hour", "1Day", "1Week", "1Month" timeframe: "1Min", "5Min", "15Min", "1Hour", "1Day", "1Week", "1Month"
Returns: Returns:
DataFrame with OHLCV columns. DataFrame with OHLCV columns.
""" """
from alpaca.data.requests import StockBarsRequest from alpaca.data.requests import StockBarsRequest
from alpaca.data.timeframe import TimeFrame, TimeFrameUnit from alpaca.data.timeframe import TimeFrame, TimeFrameUnit
tf_map = { tf_map = {
"1Min": TimeFrame(1, TimeFrameUnit.Minute), "1Min": TimeFrame(1, TimeFrameUnit.Minute),
"5Min": TimeFrame(5, TimeFrameUnit.Minute), "5Min": TimeFrame(5, TimeFrameUnit.Minute),
"15Min": TimeFrame(15, TimeFrameUnit.Minute), "15Min": TimeFrame(15, TimeFrameUnit.Minute),
"1Hour": TimeFrame(1, TimeFrameUnit.Hour), "1Hour": TimeFrame(1, TimeFrameUnit.Hour),
"1Day": TimeFrame(1, TimeFrameUnit.Day), "1Day": TimeFrame(1, TimeFrameUnit.Day),
"1Week": TimeFrame(1, TimeFrameUnit.Week), "1Week": TimeFrame(1, TimeFrameUnit.Week),
"1Month": TimeFrame(1, TimeFrameUnit.Month), "1Month": TimeFrame(1, TimeFrameUnit.Month),
} }
tf = tf_map.get(timeframe, TimeFrame(1, TimeFrameUnit.Day)) tf = tf_map.get(timeframe, TimeFrame(1, TimeFrameUnit.Day))
client = _get_stock_client() client = _get_stock_client()
request = StockBarsRequest( request = StockBarsRequest(
symbol_or_symbols=symbol.upper(), symbol_or_symbols=symbol.upper(),
timeframe=tf, timeframe=tf,
start=datetime.strptime(start_date, "%Y-%m-%d"), start=datetime.strptime(start_date, "%Y-%m-%d"),
end=datetime.strptime(end_date, "%Y-%m-%d"), end=datetime.strptime(end_date, "%Y-%m-%d"),
feed="iex", feed="iex",
) )
bars = client.get_stock_bars(request) bars = client.get_stock_bars(request)
df = bars.df df = bars.df
if isinstance(df.index, pd.MultiIndex): if isinstance(df.index, pd.MultiIndex):
df = df.droplevel("symbol") df = df.droplevel("symbol")
return df return df
def get_bars_csv(symbol: str, start_date: str, end_date: str) -> str: def get_bars_csv(symbol: str, start_date: str, end_date: str) -> str:
"""Fetch historical bars and return as CSV string (drop-in for get_YFin_data_online).""" """Fetch historical bars and return as CSV string (drop-in for get_YFin_data_online)."""
try: try:
df = get_bars(symbol, start_date, end_date) df = get_bars(symbol, start_date, end_date)
if df.empty: if df.empty:
return f"No data found for '{symbol}' between {start_date} and {end_date}" return f"No data found for '{symbol}' between {start_date} and {end_date}"
# Rename columns to match yfinance output format # Rename columns to match yfinance output format
df = df.rename(columns={ df = df.rename(columns={
"open": "Open", "high": "High", "low": "Low", "open": "Open", "high": "High", "low": "Low",
"close": "Close", "volume": "Volume", "close": "Close", "volume": "Volume",
"trade_count": "Trade Count", "vwap": "VWAP", "trade_count": "Trade Count", "vwap": "VWAP",
}) })
for col in ["Open", "High", "Low", "Close"]: for col in ["Open", "High", "Low", "Close"]:
if col in df.columns: if col in df.columns:
df[col] = df[col].round(2) df[col] = df[col].round(2)
if df.index.tz is not None: if df.index.tz is not None:
df.index = df.index.tz_localize(None) df.index = df.index.tz_localize(None)
csv = df.to_csv() csv = df.to_csv()
header = ( header = (
f"# Stock data for {symbol.upper()} from {start_date} to {end_date}\n" f"# Stock data for {symbol.upper()} from {start_date} to {end_date}\n"
f"# Source: Alpaca Markets (IEX feed)\n" f"# Source: Alpaca Markets (IEX feed)\n"
f"# Total records: {len(df)}\n\n" f"# Total records: {len(df)}\n\n"
) )
return header + csv return header + csv
except Exception as e: except Exception as e:
logger.warning("Alpaca bars failed for %s: %s", symbol, e) logger.warning("Alpaca bars failed for %s: %s", symbol, e)
return f"Error fetching Alpaca data for {symbol}: {e}" return f"Error fetching Alpaca data for {symbol}: {e}"
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# Snapshots (latest quote/trade) # Snapshots (latest quote/trade)
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
def get_snapshot(symbol: str) -> Dict[str, Any]: def get_snapshot(symbol: str) -> Dict[str, Any]:
"""Get the latest snapshot (quote + trade + bar) for a symbol.""" """Get the latest snapshot (quote + trade + bar) for a symbol."""
from alpaca.data.requests import StockSnapshotRequest from alpaca.data.requests import StockSnapshotRequest
client = _get_stock_client() client = _get_stock_client()
request = StockSnapshotRequest(symbol_or_symbols=symbol.upper(), feed="iex") request = StockSnapshotRequest(symbol_or_symbols=symbol.upper(), feed="iex")
snapshots = client.get_stock_snapshot(request) snapshots = client.get_stock_snapshot(request)
snap = snapshots.get(symbol.upper()) snap = snapshots.get(symbol.upper())
if not snap: if not snap:
return {} return {}
result = { result = {
"ticker": symbol.upper(), "ticker": symbol.upper(),
"latest_trade_price": snap.latest_trade.price if snap.latest_trade else None, "latest_trade_price": snap.latest_trade.price if snap.latest_trade else None,
"latest_trade_size": snap.latest_trade.size if snap.latest_trade else None, "latest_trade_size": snap.latest_trade.size if snap.latest_trade else None,
"latest_trade_time": str(snap.latest_trade.timestamp) if snap.latest_trade else None, "latest_trade_time": str(snap.latest_trade.timestamp) if snap.latest_trade else None,
} }
if snap.latest_quote: if snap.latest_quote:
result["bid"] = snap.latest_quote.bid_price result["bid"] = snap.latest_quote.bid_price
result["ask"] = snap.latest_quote.ask_price result["ask"] = snap.latest_quote.ask_price
result["bid_size"] = snap.latest_quote.bid_size result["bid_size"] = snap.latest_quote.bid_size
result["ask_size"] = snap.latest_quote.ask_size result["ask_size"] = snap.latest_quote.ask_size
if snap.daily_bar: if snap.daily_bar:
result["daily_open"] = snap.daily_bar.open result["daily_open"] = snap.daily_bar.open
result["daily_high"] = snap.daily_bar.high result["daily_high"] = snap.daily_bar.high
result["daily_low"] = snap.daily_bar.low result["daily_low"] = snap.daily_bar.low
result["daily_close"] = snap.daily_bar.close result["daily_close"] = snap.daily_bar.close
result["daily_volume"] = snap.daily_bar.volume result["daily_volume"] = snap.daily_bar.volume
result["daily_vwap"] = snap.daily_bar.vwap result["daily_vwap"] = snap.daily_bar.vwap
if snap.previous_daily_bar: if snap.previous_daily_bar:
result["prev_close"] = snap.previous_daily_bar.close result["prev_close"] = snap.previous_daily_bar.close
result["prev_volume"] = snap.previous_daily_bar.volume result["prev_volume"] = snap.previous_daily_bar.volume
return result return result
def get_multi_snapshots(symbols: List[str]) -> Dict[str, Dict[str, Any]]: def get_multi_snapshots(symbols: List[str]) -> Dict[str, Dict[str, Any]]:
"""Get snapshots for multiple symbols at once.""" """Get snapshots for multiple symbols at once."""
from alpaca.data.requests import StockSnapshotRequest from alpaca.data.requests import StockSnapshotRequest
client = _get_stock_client() client = _get_stock_client()
request = StockSnapshotRequest( request = StockSnapshotRequest(
symbol_or_symbols=[s.upper() for s in symbols], symbol_or_symbols=[s.upper() for s in symbols],
feed="iex", feed="iex",
) )
snapshots = client.get_stock_snapshot(request) snapshots = client.get_stock_snapshot(request)
result = {} result = {}
for sym, snap in snapshots.items(): for sym, snap in snapshots.items():
entry = {"ticker": sym} entry = {"ticker": sym}
if snap.latest_trade: if snap.latest_trade:
entry["price"] = snap.latest_trade.price entry["price"] = snap.latest_trade.price
if snap.daily_bar: if snap.daily_bar:
entry["daily_open"] = snap.daily_bar.open entry["daily_open"] = snap.daily_bar.open
entry["daily_high"] = snap.daily_bar.high entry["daily_high"] = snap.daily_bar.high
entry["daily_low"] = snap.daily_bar.low entry["daily_low"] = snap.daily_bar.low
entry["daily_close"] = snap.daily_bar.close entry["daily_close"] = snap.daily_bar.close
entry["daily_volume"] = snap.daily_bar.volume entry["daily_volume"] = snap.daily_bar.volume
entry["daily_vwap"] = snap.daily_bar.vwap entry["daily_vwap"] = snap.daily_bar.vwap
if snap.previous_daily_bar: if snap.previous_daily_bar:
entry["prev_close"] = snap.previous_daily_bar.close entry["prev_close"] = snap.previous_daily_bar.close
result[sym] = entry result[sym] = entry
return result return result
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# Computed indicators from bars # Computed indicators from bars
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
def get_moving_averages(symbol: str) -> Dict[str, Any]: def get_moving_averages(symbol: str) -> Dict[str, Any]:
"""Compute 50-day and 200-day moving averages from Alpaca bars.""" """Compute 50-day and 200-day moving averages from Alpaca bars."""
end = datetime.now() end = datetime.now()
start = end - timedelta(days=300) # ~200 trading days + buffer start = end - timedelta(days=300) # ~200 trading days + buffer
try: try:
df = get_bars(symbol, start.strftime("%Y-%m-%d"), end.strftime("%Y-%m-%d")) df = get_bars(symbol, start.strftime("%Y-%m-%d"), end.strftime("%Y-%m-%d"))
if df.empty or len(df) < 50: if df.empty or len(df) < 50:
return {} return {}
close = df["close"] if "close" in df.columns else df["Close"] close = df["close"] if "close" in df.columns else df["Close"]
result = { result = {
"current_price": float(close.iloc[-1]), "current_price": float(close.iloc[-1]),
"fifty_day_avg": float(close.tail(50).mean()), "fifty_day_avg": float(close.tail(50).mean()),
} }
if len(close) >= 200: if len(close) >= 200:
result["two_hundred_day_avg"] = float(close.tail(200).mean()) result["two_hundred_day_avg"] = float(close.tail(200).mean())
# 52-week high/low (approx 252 trading days) # 52-week high/low (approx 252 trading days)
year_data = close.tail(252) if len(close) >= 252 else close year_data = close.tail(252) if len(close) >= 252 else close
result["fifty_two_week_high"] = float(year_data.max()) result["fifty_two_week_high"] = float(year_data.max())
result["fifty_two_week_low"] = float(year_data.min()) result["fifty_two_week_low"] = float(year_data.min())
hi = result["fifty_two_week_high"] hi = result["fifty_two_week_high"]
lo = result["fifty_two_week_low"] lo = result["fifty_two_week_low"]
price = result["current_price"] price = result["current_price"]
if (hi - lo) > 0: if (hi - lo) > 0:
result["vs_52w_range_pct"] = round((price - lo) / (hi - lo) * 100, 1) result["vs_52w_range_pct"] = round((price - lo) / (hi - lo) * 100, 1)
return result return result
except Exception as e: except Exception as e:
logger.warning("Alpaca moving averages failed for %s: %s", symbol, e) logger.warning("Alpaca moving averages failed for %s: %s", symbol, e)
return {} return {}
def get_sector_etf_performance(etf_symbols: List[str]) -> Dict[str, Dict[str, float]]: def get_sector_etf_performance(etf_symbols: List[str]) -> Dict[str, Dict[str, float]]:
"""Compute 1M and 3M returns for a list of sector ETFs.""" """Compute 1M and 3M returns for a list of sector ETFs."""
end = datetime.now() end = datetime.now()
start_3m = end - timedelta(days=100) start_3m = end - timedelta(days=100)
result = {} result = {}
for sym in etf_symbols: for sym in etf_symbols:
try: try:
df = get_bars(sym, start_3m.strftime("%Y-%m-%d"), end.strftime("%Y-%m-%d")) df = get_bars(sym, start_3m.strftime("%Y-%m-%d"), end.strftime("%Y-%m-%d"))
if df.empty or len(df) < 5: if df.empty or len(df) < 5:
continue continue
close = df["close"] if "close" in df.columns else df["Close"] close = df["close"] if "close" in df.columns else df["Close"]
current = float(close.iloc[-1]) current = float(close.iloc[-1])
ret_1m = None ret_1m = None
if len(close) >= 22: if len(close) >= 22:
price_1m = float(close.iloc[-22]) price_1m = float(close.iloc[-22])
ret_1m = round((current - price_1m) / price_1m * 100, 2) ret_1m = round((current - price_1m) / price_1m * 100, 2)
ret_3m = None ret_3m = None
if len(close) >= 63: if len(close) >= 63:
price_3m = float(close.iloc[-63]) price_3m = float(close.iloc[-63])
ret_3m = round((current - price_3m) / price_3m * 100, 2) ret_3m = round((current - price_3m) / price_3m * 100, 2)
result[sym] = { result[sym] = {
"return_1m": ret_1m, "return_1m": ret_1m,
"return_3m": ret_3m, "return_3m": ret_3m,
"price": current, "price": current,
} }
except Exception as e: except Exception as e:
logger.warning("Alpaca ETF perf failed for %s: %s", sym, e) logger.warning("Alpaca ETF perf failed for %s: %s", sym, e)
return result return result
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# News # News
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
def get_news( def get_news(
symbols: Optional[List[str]] = None, symbols: Optional[List[str]] = None,
limit: int = 10, limit: int = 10,
start_date: Optional[str] = None, start_date: Optional[str] = None,
) -> List[Dict[str, Any]]: ) -> List[Dict[str, Any]]:
"""Fetch news articles from Alpaca News API.""" """Fetch news articles from Alpaca News API."""
try: try:
from alpaca.data.requests import NewsRequest from alpaca.data.requests import NewsRequest
client = _get_news_client() client = _get_news_client()
kwargs: Dict[str, Any] = {"limit": limit} kwargs: Dict[str, Any] = {"limit": limit}
if symbols: if symbols:
kwargs["symbols"] = [s.upper() for s in symbols] kwargs["symbols"] = [s.upper() for s in symbols]
if start_date: if start_date:
kwargs["start"] = datetime.strptime(start_date, "%Y-%m-%d") kwargs["start"] = datetime.strptime(start_date, "%Y-%m-%d")
request = NewsRequest(**kwargs) request = NewsRequest(**kwargs)
news = client.get_news(request) news = client.get_news(request)
return [ return [
{ {
"title": n.headline, "title": n.headline,
"summary": n.summary or "", "summary": n.summary or "",
"url": n.url, "url": n.url,
"source": n.source, "source": n.source,
"created_at": str(n.created_at), "created_at": str(n.created_at),
"symbols": n.symbols or [], "symbols": n.symbols or [],
} }
for n in news.news for n in news.news
] ]
except Exception as e: except Exception as e:
logger.warning("Alpaca news failed: %s", e) logger.warning("Alpaca news failed: %s", e)
return [] return []

View File

@ -1,5 +1,5 @@
# Import functions from specialized modules # Import functions from specialized modules
from .alpha_vantage_stock import get_stock from .alpha_vantage_stock import get_stock
from .alpha_vantage_indicator import get_indicator from .alpha_vantage_indicator import get_indicator
from .alpha_vantage_fundamentals import get_fundamentals, get_balance_sheet, get_cashflow, get_income_statement from .alpha_vantage_fundamentals import get_fundamentals, get_balance_sheet, get_cashflow, get_income_statement
from .alpha_vantage_news import get_news, get_global_news, get_insider_transactions from .alpha_vantage_news import get_news, get_global_news, get_insider_transactions

View File

@ -1,122 +1,122 @@
import os import os
import requests import requests
import pandas as pd import pandas as pd
import json import json
from datetime import datetime from datetime import datetime
from io import StringIO from io import StringIO
API_BASE_URL = "https://www.alphavantage.co/query" API_BASE_URL = "https://www.alphavantage.co/query"
def get_api_key() -> str: def get_api_key() -> str:
"""Retrieve the API key for Alpha Vantage from environment variables.""" """Retrieve the API key for Alpha Vantage from environment variables."""
api_key = os.getenv("ALPHA_VANTAGE_API_KEY") api_key = os.getenv("ALPHA_VANTAGE_API_KEY")
if not api_key: if not api_key:
raise ValueError("ALPHA_VANTAGE_API_KEY environment variable is not set.") raise ValueError("ALPHA_VANTAGE_API_KEY environment variable is not set.")
return api_key return api_key
def format_datetime_for_api(date_input) -> str: def format_datetime_for_api(date_input) -> str:
"""Convert various date formats to YYYYMMDDTHHMM format required by Alpha Vantage API.""" """Convert various date formats to YYYYMMDDTHHMM format required by Alpha Vantage API."""
if isinstance(date_input, str): if isinstance(date_input, str):
# If already in correct format, return as-is # If already in correct format, return as-is
if len(date_input) == 13 and 'T' in date_input: if len(date_input) == 13 and 'T' in date_input:
return date_input return date_input
# Try to parse common date formats # Try to parse common date formats
try: try:
dt = datetime.strptime(date_input, "%Y-%m-%d") dt = datetime.strptime(date_input, "%Y-%m-%d")
return dt.strftime("%Y%m%dT0000") return dt.strftime("%Y%m%dT0000")
except ValueError: except ValueError:
try: try:
dt = datetime.strptime(date_input, "%Y-%m-%d %H:%M") dt = datetime.strptime(date_input, "%Y-%m-%d %H:%M")
return dt.strftime("%Y%m%dT%H%M") return dt.strftime("%Y%m%dT%H%M")
except ValueError: except ValueError:
raise ValueError(f"Unsupported date format: {date_input}") raise ValueError(f"Unsupported date format: {date_input}")
elif isinstance(date_input, datetime): elif isinstance(date_input, datetime):
return date_input.strftime("%Y%m%dT%H%M") return date_input.strftime("%Y%m%dT%H%M")
else: else:
raise ValueError(f"Date must be string or datetime object, got {type(date_input)}") raise ValueError(f"Date must be string or datetime object, got {type(date_input)}")
class AlphaVantageRateLimitError(Exception): class AlphaVantageRateLimitError(Exception):
"""Exception raised when Alpha Vantage API rate limit is exceeded.""" """Exception raised when Alpha Vantage API rate limit is exceeded."""
pass pass
def _make_api_request(function_name: str, params: dict) -> dict | str: def _make_api_request(function_name: str, params: dict) -> dict | str:
"""Helper function to make API requests and handle responses. """Helper function to make API requests and handle responses.
Raises: Raises:
AlphaVantageRateLimitError: When API rate limit is exceeded AlphaVantageRateLimitError: When API rate limit is exceeded
""" """
# Create a copy of params to avoid modifying the original # Create a copy of params to avoid modifying the original
api_params = params.copy() api_params = params.copy()
api_params.update({ api_params.update({
"function": function_name, "function": function_name,
"apikey": get_api_key(), "apikey": get_api_key(),
"source": "trading_agents", "source": "trading_agents",
}) })
# Handle entitlement parameter if present in params or global variable # Handle entitlement parameter if present in params or global variable
current_entitlement = globals().get('_current_entitlement') current_entitlement = globals().get('_current_entitlement')
entitlement = api_params.get("entitlement") or current_entitlement entitlement = api_params.get("entitlement") or current_entitlement
if entitlement: if entitlement:
api_params["entitlement"] = entitlement api_params["entitlement"] = entitlement
elif "entitlement" in api_params: elif "entitlement" in api_params:
# Remove entitlement if it's None or empty # Remove entitlement if it's None or empty
api_params.pop("entitlement", None) api_params.pop("entitlement", None)
response = requests.get(API_BASE_URL, params=api_params) response = requests.get(API_BASE_URL, params=api_params)
response.raise_for_status() response.raise_for_status()
response_text = response.text response_text = response.text
# Check if response is JSON (error responses are typically JSON) # Check if response is JSON (error responses are typically JSON)
try: try:
response_json = json.loads(response_text) response_json = json.loads(response_text)
# Check for rate limit error # Check for rate limit error
if "Information" in response_json: if "Information" in response_json:
info_message = response_json["Information"] info_message = response_json["Information"]
if "rate limit" in info_message.lower() or "api key" in info_message.lower(): if "rate limit" in info_message.lower() or "api key" in info_message.lower():
raise AlphaVantageRateLimitError(f"Alpha Vantage rate limit exceeded: {info_message}") raise AlphaVantageRateLimitError(f"Alpha Vantage rate limit exceeded: {info_message}")
except json.JSONDecodeError: except json.JSONDecodeError:
# Response is not JSON (likely CSV data), which is normal # Response is not JSON (likely CSV data), which is normal
pass pass
return response_text return response_text
def _filter_csv_by_date_range(csv_data: str, start_date: str, end_date: str) -> str: def _filter_csv_by_date_range(csv_data: str, start_date: str, end_date: str) -> str:
""" """
Filter CSV data to include only rows within the specified date range. Filter CSV data to include only rows within the specified date range.
Args: Args:
csv_data: CSV string from Alpha Vantage API csv_data: CSV string from Alpha Vantage API
start_date: Start date in yyyy-mm-dd format start_date: Start date in yyyy-mm-dd format
end_date: End date in yyyy-mm-dd format end_date: End date in yyyy-mm-dd format
Returns: Returns:
Filtered CSV string Filtered CSV string
""" """
if not csv_data or csv_data.strip() == "": if not csv_data or csv_data.strip() == "":
return csv_data return csv_data
try: try:
# Parse CSV data # Parse CSV data
df = pd.read_csv(StringIO(csv_data)) df = pd.read_csv(StringIO(csv_data))
# Assume the first column is the date column (timestamp) # Assume the first column is the date column (timestamp)
date_col = df.columns[0] date_col = df.columns[0]
df[date_col] = pd.to_datetime(df[date_col]) df[date_col] = pd.to_datetime(df[date_col])
# Filter by date range # Filter by date range
start_dt = pd.to_datetime(start_date) start_dt = pd.to_datetime(start_date)
end_dt = pd.to_datetime(end_date) end_dt = pd.to_datetime(end_date)
filtered_df = df[(df[date_col] >= start_dt) & (df[date_col] <= end_dt)] filtered_df = df[(df[date_col] >= start_dt) & (df[date_col] <= end_dt)]
# Convert back to CSV string # Convert back to CSV string
return filtered_df.to_csv(index=False) return filtered_df.to_csv(index=False)
except Exception as e: except Exception as e:
# If filtering fails, return original data with a warning # If filtering fails, return original data with a warning
print(f"Warning: Failed to filter CSV data by date range: {e}") print(f"Warning: Failed to filter CSV data by date range: {e}")
return csv_data return csv_data

View File

@ -1,77 +1,77 @@
from .alpha_vantage_common import _make_api_request from .alpha_vantage_common import _make_api_request
def get_fundamentals(ticker: str, curr_date: str = None) -> str: def get_fundamentals(ticker: str, curr_date: str = None) -> str:
""" """
Retrieve comprehensive fundamental data for a given ticker symbol using Alpha Vantage. Retrieve comprehensive fundamental data for a given ticker symbol using Alpha Vantage.
Args: Args:
ticker (str): Ticker symbol of the company ticker (str): Ticker symbol of the company
curr_date (str): Current date you are trading at, yyyy-mm-dd (not used for Alpha Vantage) curr_date (str): Current date you are trading at, yyyy-mm-dd (not used for Alpha Vantage)
Returns: Returns:
str: Company overview data including financial ratios and key metrics str: Company overview data including financial ratios and key metrics
""" """
params = { params = {
"symbol": ticker, "symbol": ticker,
} }
return _make_api_request("OVERVIEW", params) return _make_api_request("OVERVIEW", params)
def get_balance_sheet(ticker: str, freq: str = "quarterly", curr_date: str = None) -> str: def get_balance_sheet(ticker: str, freq: str = "quarterly", curr_date: str = None) -> str:
""" """
Retrieve balance sheet data for a given ticker symbol using Alpha Vantage. Retrieve balance sheet data for a given ticker symbol using Alpha Vantage.
Args: Args:
ticker (str): Ticker symbol of the company ticker (str): Ticker symbol of the company
freq (str): Reporting frequency: annual/quarterly (default quarterly) - not used for Alpha Vantage freq (str): Reporting frequency: annual/quarterly (default quarterly) - not used for Alpha Vantage
curr_date (str): Current date you are trading at, yyyy-mm-dd (not used for Alpha Vantage) curr_date (str): Current date you are trading at, yyyy-mm-dd (not used for Alpha Vantage)
Returns: Returns:
str: Balance sheet data with normalized fields str: Balance sheet data with normalized fields
""" """
params = { params = {
"symbol": ticker, "symbol": ticker,
} }
return _make_api_request("BALANCE_SHEET", params) return _make_api_request("BALANCE_SHEET", params)
def get_cashflow(ticker: str, freq: str = "quarterly", curr_date: str = None) -> str: def get_cashflow(ticker: str, freq: str = "quarterly", curr_date: str = None) -> str:
""" """
Retrieve cash flow statement data for a given ticker symbol using Alpha Vantage. Retrieve cash flow statement data for a given ticker symbol using Alpha Vantage.
Args: Args:
ticker (str): Ticker symbol of the company ticker (str): Ticker symbol of the company
freq (str): Reporting frequency: annual/quarterly (default quarterly) - not used for Alpha Vantage freq (str): Reporting frequency: annual/quarterly (default quarterly) - not used for Alpha Vantage
curr_date (str): Current date you are trading at, yyyy-mm-dd (not used for Alpha Vantage) curr_date (str): Current date you are trading at, yyyy-mm-dd (not used for Alpha Vantage)
Returns: Returns:
str: Cash flow statement data with normalized fields str: Cash flow statement data with normalized fields
""" """
params = { params = {
"symbol": ticker, "symbol": ticker,
} }
return _make_api_request("CASH_FLOW", params) return _make_api_request("CASH_FLOW", params)
def get_income_statement(ticker: str, freq: str = "quarterly", curr_date: str = None) -> str: def get_income_statement(ticker: str, freq: str = "quarterly", curr_date: str = None) -> str:
""" """
Retrieve income statement data for a given ticker symbol using Alpha Vantage. Retrieve income statement data for a given ticker symbol using Alpha Vantage.
Args: Args:
ticker (str): Ticker symbol of the company ticker (str): Ticker symbol of the company
freq (str): Reporting frequency: annual/quarterly (default quarterly) - not used for Alpha Vantage freq (str): Reporting frequency: annual/quarterly (default quarterly) - not used for Alpha Vantage
curr_date (str): Current date you are trading at, yyyy-mm-dd (not used for Alpha Vantage) curr_date (str): Current date you are trading at, yyyy-mm-dd (not used for Alpha Vantage)
Returns: Returns:
str: Income statement data with normalized fields str: Income statement data with normalized fields
""" """
params = { params = {
"symbol": ticker, "symbol": ticker,
} }
return _make_api_request("INCOME_STATEMENT", params) return _make_api_request("INCOME_STATEMENT", params)

View File

@ -1,222 +1,222 @@
from .alpha_vantage_common import _make_api_request from .alpha_vantage_common import _make_api_request
def get_indicator( def get_indicator(
symbol: str, symbol: str,
indicator: str, indicator: str,
curr_date: str, curr_date: str,
look_back_days: int, look_back_days: int,
interval: str = "daily", interval: str = "daily",
time_period: int = 14, time_period: int = 14,
series_type: str = "close" series_type: str = "close"
) -> str: ) -> str:
""" """
Returns Alpha Vantage technical indicator values over a time window. Returns Alpha Vantage technical indicator values over a time window.
Args: Args:
symbol: ticker symbol of the company symbol: ticker symbol of the company
indicator: technical indicator to get the analysis and report of indicator: technical indicator to get the analysis and report of
curr_date: The current trading date you are trading on, YYYY-mm-dd curr_date: The current trading date you are trading on, YYYY-mm-dd
look_back_days: how many days to look back look_back_days: how many days to look back
interval: Time interval (daily, weekly, monthly) interval: Time interval (daily, weekly, monthly)
time_period: Number of data points for calculation time_period: Number of data points for calculation
series_type: The desired price type (close, open, high, low) series_type: The desired price type (close, open, high, low)
Returns: Returns:
String containing indicator values and description String containing indicator values and description
""" """
from datetime import datetime from datetime import datetime
from dateutil.relativedelta import relativedelta from dateutil.relativedelta import relativedelta
supported_indicators = { supported_indicators = {
"close_50_sma": ("50 SMA", "close"), "close_50_sma": ("50 SMA", "close"),
"close_200_sma": ("200 SMA", "close"), "close_200_sma": ("200 SMA", "close"),
"close_10_ema": ("10 EMA", "close"), "close_10_ema": ("10 EMA", "close"),
"macd": ("MACD", "close"), "macd": ("MACD", "close"),
"macds": ("MACD Signal", "close"), "macds": ("MACD Signal", "close"),
"macdh": ("MACD Histogram", "close"), "macdh": ("MACD Histogram", "close"),
"rsi": ("RSI", "close"), "rsi": ("RSI", "close"),
"boll": ("Bollinger Middle", "close"), "boll": ("Bollinger Middle", "close"),
"boll_ub": ("Bollinger Upper Band", "close"), "boll_ub": ("Bollinger Upper Band", "close"),
"boll_lb": ("Bollinger Lower Band", "close"), "boll_lb": ("Bollinger Lower Band", "close"),
"atr": ("ATR", None), "atr": ("ATR", None),
"vwma": ("VWMA", "close") "vwma": ("VWMA", "close")
} }
indicator_descriptions = { indicator_descriptions = {
"close_50_sma": "50 SMA: A medium-term trend indicator. Usage: Identify trend direction and serve as dynamic support/resistance. Tips: It lags price; combine with faster indicators for timely signals.", "close_50_sma": "50 SMA: A medium-term trend indicator. Usage: Identify trend direction and serve as dynamic support/resistance. Tips: It lags price; combine with faster indicators for timely signals.",
"close_200_sma": "200 SMA: A long-term trend benchmark. Usage: Confirm overall market trend and identify golden/death cross setups. Tips: It reacts slowly; best for strategic trend confirmation rather than frequent trading entries.", "close_200_sma": "200 SMA: A long-term trend benchmark. Usage: Confirm overall market trend and identify golden/death cross setups. Tips: It reacts slowly; best for strategic trend confirmation rather than frequent trading entries.",
"close_10_ema": "10 EMA: A responsive short-term average. Usage: Capture quick shifts in momentum and potential entry points. Tips: Prone to noise in choppy markets; use alongside longer averages for filtering false signals.", "close_10_ema": "10 EMA: A responsive short-term average. Usage: Capture quick shifts in momentum and potential entry points. Tips: Prone to noise in choppy markets; use alongside longer averages for filtering false signals.",
"macd": "MACD: Computes momentum via differences of EMAs. Usage: Look for crossovers and divergence as signals of trend changes. Tips: Confirm with other indicators in low-volatility or sideways markets.", "macd": "MACD: Computes momentum via differences of EMAs. Usage: Look for crossovers and divergence as signals of trend changes. Tips: Confirm with other indicators in low-volatility or sideways markets.",
"macds": "MACD Signal: An EMA smoothing of the MACD line. Usage: Use crossovers with the MACD line to trigger trades. Tips: Should be part of a broader strategy to avoid false positives.", "macds": "MACD Signal: An EMA smoothing of the MACD line. Usage: Use crossovers with the MACD line to trigger trades. Tips: Should be part of a broader strategy to avoid false positives.",
"macdh": "MACD Histogram: Shows the gap between the MACD line and its signal. Usage: Visualize momentum strength and spot divergence early. Tips: Can be volatile; complement with additional filters in fast-moving markets.", "macdh": "MACD Histogram: Shows the gap between the MACD line and its signal. Usage: Visualize momentum strength and spot divergence early. Tips: Can be volatile; complement with additional filters in fast-moving markets.",
"rsi": "RSI: Measures momentum to flag overbought/oversold conditions. Usage: Apply 70/30 thresholds and watch for divergence to signal reversals. Tips: In strong trends, RSI may remain extreme; always cross-check with trend analysis.", "rsi": "RSI: Measures momentum to flag overbought/oversold conditions. Usage: Apply 70/30 thresholds and watch for divergence to signal reversals. Tips: In strong trends, RSI may remain extreme; always cross-check with trend analysis.",
"boll": "Bollinger Middle: A 20 SMA serving as the basis for Bollinger Bands. Usage: Acts as a dynamic benchmark for price movement. Tips: Combine with the upper and lower bands to effectively spot breakouts or reversals.", "boll": "Bollinger Middle: A 20 SMA serving as the basis for Bollinger Bands. Usage: Acts as a dynamic benchmark for price movement. Tips: Combine with the upper and lower bands to effectively spot breakouts or reversals.",
"boll_ub": "Bollinger Upper Band: Typically 2 standard deviations above the middle line. Usage: Signals potential overbought conditions and breakout zones. Tips: Confirm signals with other tools; prices may ride the band in strong trends.", "boll_ub": "Bollinger Upper Band: Typically 2 standard deviations above the middle line. Usage: Signals potential overbought conditions and breakout zones. Tips: Confirm signals with other tools; prices may ride the band in strong trends.",
"boll_lb": "Bollinger Lower Band: Typically 2 standard deviations below the middle line. Usage: Indicates potential oversold conditions. Tips: Use additional analysis to avoid false reversal signals.", "boll_lb": "Bollinger Lower Band: Typically 2 standard deviations below the middle line. Usage: Indicates potential oversold conditions. Tips: Use additional analysis to avoid false reversal signals.",
"atr": "ATR: Averages true range to measure volatility. Usage: Set stop-loss levels and adjust position sizes based on current market volatility. Tips: It's a reactive measure, so use it as part of a broader risk management strategy.", "atr": "ATR: Averages true range to measure volatility. Usage: Set stop-loss levels and adjust position sizes based on current market volatility. Tips: It's a reactive measure, so use it as part of a broader risk management strategy.",
"vwma": "VWMA: A moving average weighted by volume. Usage: Confirm trends by integrating price action with volume data. Tips: Watch for skewed results from volume spikes; use in combination with other volume analyses." "vwma": "VWMA: A moving average weighted by volume. Usage: Confirm trends by integrating price action with volume data. Tips: Watch for skewed results from volume spikes; use in combination with other volume analyses."
} }
if indicator not in supported_indicators: if indicator not in supported_indicators:
raise ValueError( raise ValueError(
f"Indicator {indicator} is not supported. Please choose from: {list(supported_indicators.keys())}" f"Indicator {indicator} is not supported. Please choose from: {list(supported_indicators.keys())}"
) )
curr_date_dt = datetime.strptime(curr_date, "%Y-%m-%d") curr_date_dt = datetime.strptime(curr_date, "%Y-%m-%d")
before = curr_date_dt - relativedelta(days=look_back_days) before = curr_date_dt - relativedelta(days=look_back_days)
# Get the full data for the period instead of making individual calls # Get the full data for the period instead of making individual calls
_, required_series_type = supported_indicators[indicator] _, required_series_type = supported_indicators[indicator]
# Use the provided series_type or fall back to the required one # Use the provided series_type or fall back to the required one
if required_series_type: if required_series_type:
series_type = required_series_type series_type = required_series_type
try: try:
# Get indicator data for the period # Get indicator data for the period
if indicator == "close_50_sma": if indicator == "close_50_sma":
data = _make_api_request("SMA", { data = _make_api_request("SMA", {
"symbol": symbol, "symbol": symbol,
"interval": interval, "interval": interval,
"time_period": "50", "time_period": "50",
"series_type": series_type, "series_type": series_type,
"datatype": "csv" "datatype": "csv"
}) })
elif indicator == "close_200_sma": elif indicator == "close_200_sma":
data = _make_api_request("SMA", { data = _make_api_request("SMA", {
"symbol": symbol, "symbol": symbol,
"interval": interval, "interval": interval,
"time_period": "200", "time_period": "200",
"series_type": series_type, "series_type": series_type,
"datatype": "csv" "datatype": "csv"
}) })
elif indicator == "close_10_ema": elif indicator == "close_10_ema":
data = _make_api_request("EMA", { data = _make_api_request("EMA", {
"symbol": symbol, "symbol": symbol,
"interval": interval, "interval": interval,
"time_period": "10", "time_period": "10",
"series_type": series_type, "series_type": series_type,
"datatype": "csv" "datatype": "csv"
}) })
elif indicator == "macd": elif indicator == "macd":
data = _make_api_request("MACD", { data = _make_api_request("MACD", {
"symbol": symbol, "symbol": symbol,
"interval": interval, "interval": interval,
"series_type": series_type, "series_type": series_type,
"datatype": "csv" "datatype": "csv"
}) })
elif indicator == "macds": elif indicator == "macds":
data = _make_api_request("MACD", { data = _make_api_request("MACD", {
"symbol": symbol, "symbol": symbol,
"interval": interval, "interval": interval,
"series_type": series_type, "series_type": series_type,
"datatype": "csv" "datatype": "csv"
}) })
elif indicator == "macdh": elif indicator == "macdh":
data = _make_api_request("MACD", { data = _make_api_request("MACD", {
"symbol": symbol, "symbol": symbol,
"interval": interval, "interval": interval,
"series_type": series_type, "series_type": series_type,
"datatype": "csv" "datatype": "csv"
}) })
elif indicator == "rsi": elif indicator == "rsi":
data = _make_api_request("RSI", { data = _make_api_request("RSI", {
"symbol": symbol, "symbol": symbol,
"interval": interval, "interval": interval,
"time_period": str(time_period), "time_period": str(time_period),
"series_type": series_type, "series_type": series_type,
"datatype": "csv" "datatype": "csv"
}) })
elif indicator in ["boll", "boll_ub", "boll_lb"]: elif indicator in ["boll", "boll_ub", "boll_lb"]:
data = _make_api_request("BBANDS", { data = _make_api_request("BBANDS", {
"symbol": symbol, "symbol": symbol,
"interval": interval, "interval": interval,
"time_period": "20", "time_period": "20",
"series_type": series_type, "series_type": series_type,
"datatype": "csv" "datatype": "csv"
}) })
elif indicator == "atr": elif indicator == "atr":
data = _make_api_request("ATR", { data = _make_api_request("ATR", {
"symbol": symbol, "symbol": symbol,
"interval": interval, "interval": interval,
"time_period": str(time_period), "time_period": str(time_period),
"datatype": "csv" "datatype": "csv"
}) })
elif indicator == "vwma": elif indicator == "vwma":
# Alpha Vantage doesn't have direct VWMA, so we'll return an informative message # Alpha Vantage doesn't have direct VWMA, so we'll return an informative message
# In a real implementation, this would need to be calculated from OHLCV data # In a real implementation, this would need to be calculated from OHLCV data
return f"## VWMA (Volume Weighted Moving Average) for {symbol}:\n\nVWMA calculation requires OHLCV data and is not directly available from Alpha Vantage API.\nThis indicator would need to be calculated from the raw stock data using volume-weighted price averaging.\n\n{indicator_descriptions.get('vwma', 'No description available.')}" return f"## VWMA (Volume Weighted Moving Average) for {symbol}:\n\nVWMA calculation requires OHLCV data and is not directly available from Alpha Vantage API.\nThis indicator would need to be calculated from the raw stock data using volume-weighted price averaging.\n\n{indicator_descriptions.get('vwma', 'No description available.')}"
else: else:
return f"Error: Indicator {indicator} not implemented yet." return f"Error: Indicator {indicator} not implemented yet."
# Parse CSV data and extract values for the date range # Parse CSV data and extract values for the date range
lines = data.strip().split('\n') lines = data.strip().split('\n')
if len(lines) < 2: if len(lines) < 2:
return f"Error: No data returned for {indicator}" return f"Error: No data returned for {indicator}"
# Parse header and data # Parse header and data
header = [col.strip() for col in lines[0].split(',')] header = [col.strip() for col in lines[0].split(',')]
try: try:
date_col_idx = header.index('time') date_col_idx = header.index('time')
except ValueError: except ValueError:
return f"Error: 'time' column not found in data for {indicator}. Available columns: {header}" return f"Error: 'time' column not found in data for {indicator}. Available columns: {header}"
# Map internal indicator names to expected CSV column names from Alpha Vantage # Map internal indicator names to expected CSV column names from Alpha Vantage
col_name_map = { col_name_map = {
"macd": "MACD", "macds": "MACD_Signal", "macdh": "MACD_Hist", "macd": "MACD", "macds": "MACD_Signal", "macdh": "MACD_Hist",
"boll": "Real Middle Band", "boll_ub": "Real Upper Band", "boll_lb": "Real Lower Band", "boll": "Real Middle Band", "boll_ub": "Real Upper Band", "boll_lb": "Real Lower Band",
"rsi": "RSI", "atr": "ATR", "close_10_ema": "EMA", "rsi": "RSI", "atr": "ATR", "close_10_ema": "EMA",
"close_50_sma": "SMA", "close_200_sma": "SMA" "close_50_sma": "SMA", "close_200_sma": "SMA"
} }
target_col_name = col_name_map.get(indicator) target_col_name = col_name_map.get(indicator)
if not target_col_name: if not target_col_name:
# Default to the second column if no specific mapping exists # Default to the second column if no specific mapping exists
value_col_idx = 1 value_col_idx = 1
else: else:
try: try:
value_col_idx = header.index(target_col_name) value_col_idx = header.index(target_col_name)
except ValueError: except ValueError:
return f"Error: Column '{target_col_name}' not found for indicator '{indicator}'. Available columns: {header}" return f"Error: Column '{target_col_name}' not found for indicator '{indicator}'. Available columns: {header}"
result_data = [] result_data = []
for line in lines[1:]: for line in lines[1:]:
if not line.strip(): if not line.strip():
continue continue
values = line.split(',') values = line.split(',')
if len(values) > value_col_idx: if len(values) > value_col_idx:
try: try:
date_str = values[date_col_idx].strip() date_str = values[date_col_idx].strip()
# Parse the date # Parse the date
date_dt = datetime.strptime(date_str, "%Y-%m-%d") date_dt = datetime.strptime(date_str, "%Y-%m-%d")
# Check if date is in our range # Check if date is in our range
if before <= date_dt <= curr_date_dt: if before <= date_dt <= curr_date_dt:
value = values[value_col_idx].strip() value = values[value_col_idx].strip()
result_data.append((date_dt, value)) result_data.append((date_dt, value))
except (ValueError, IndexError): except (ValueError, IndexError):
continue continue
# Sort by date and format output # Sort by date and format output
result_data.sort(key=lambda x: x[0]) result_data.sort(key=lambda x: x[0])
ind_string = "" ind_string = ""
for date_dt, value in result_data: for date_dt, value in result_data:
ind_string += f"{date_dt.strftime('%Y-%m-%d')}: {value}\n" ind_string += f"{date_dt.strftime('%Y-%m-%d')}: {value}\n"
if not ind_string: if not ind_string:
ind_string = "No data available for the specified date range.\n" ind_string = "No data available for the specified date range.\n"
result_str = ( result_str = (
f"## {indicator.upper()} values from {before.strftime('%Y-%m-%d')} to {curr_date}:\n\n" f"## {indicator.upper()} values from {before.strftime('%Y-%m-%d')} to {curr_date}:\n\n"
+ ind_string + ind_string
+ "\n\n" + "\n\n"
+ indicator_descriptions.get(indicator, "No description available.") + indicator_descriptions.get(indicator, "No description available.")
) )
return result_str return result_str
except Exception as e: except Exception as e:
print(f"Error getting Alpha Vantage indicator data for {indicator}: {e}") print(f"Error getting Alpha Vantage indicator data for {indicator}: {e}")
return f"Error retrieving {indicator} data: {str(e)}" return f"Error retrieving {indicator} data: {str(e)}"

View File

@ -1,71 +1,71 @@
from .alpha_vantage_common import _make_api_request, format_datetime_for_api from .alpha_vantage_common import _make_api_request, format_datetime_for_api
def get_news(ticker, start_date, end_date) -> dict[str, str] | str: def get_news(ticker, start_date, end_date) -> dict[str, str] | str:
"""Returns live and historical market news & sentiment data from premier news outlets worldwide. """Returns live and historical market news & sentiment data from premier news outlets worldwide.
Covers stocks, cryptocurrencies, forex, and topics like fiscal policy, mergers & acquisitions, IPOs. Covers stocks, cryptocurrencies, forex, and topics like fiscal policy, mergers & acquisitions, IPOs.
Args: Args:
ticker: Stock symbol for news articles. ticker: Stock symbol for news articles.
start_date: Start date for news search. start_date: Start date for news search.
end_date: End date for news search. end_date: End date for news search.
Returns: Returns:
Dictionary containing news sentiment data or JSON string. Dictionary containing news sentiment data or JSON string.
""" """
params = { params = {
"tickers": ticker, "tickers": ticker,
"time_from": format_datetime_for_api(start_date), "time_from": format_datetime_for_api(start_date),
"time_to": format_datetime_for_api(end_date), "time_to": format_datetime_for_api(end_date),
} }
return _make_api_request("NEWS_SENTIMENT", params) return _make_api_request("NEWS_SENTIMENT", params)
def get_global_news(curr_date, look_back_days: int = 7, limit: int = 50) -> dict[str, str] | str: def get_global_news(curr_date, look_back_days: int = 7, limit: int = 50) -> dict[str, str] | str:
"""Returns global market news & sentiment data without ticker-specific filtering. """Returns global market news & sentiment data without ticker-specific filtering.
Covers broad market topics like financial markets, economy, and more. Covers broad market topics like financial markets, economy, and more.
Args: Args:
curr_date: Current date in yyyy-mm-dd format. curr_date: Current date in yyyy-mm-dd format.
look_back_days: Number of days to look back (default 7). look_back_days: Number of days to look back (default 7).
limit: Maximum number of articles (default 50). limit: Maximum number of articles (default 50).
Returns: Returns:
Dictionary containing global news sentiment data or JSON string. Dictionary containing global news sentiment data or JSON string.
""" """
from datetime import datetime, timedelta from datetime import datetime, timedelta
# Calculate start date # Calculate start date
curr_dt = datetime.strptime(curr_date, "%Y-%m-%d") curr_dt = datetime.strptime(curr_date, "%Y-%m-%d")
start_dt = curr_dt - timedelta(days=look_back_days) start_dt = curr_dt - timedelta(days=look_back_days)
start_date = start_dt.strftime("%Y-%m-%d") start_date = start_dt.strftime("%Y-%m-%d")
params = { params = {
"topics": "financial_markets,economy_macro,economy_monetary", "topics": "financial_markets,economy_macro,economy_monetary",
"time_from": format_datetime_for_api(start_date), "time_from": format_datetime_for_api(start_date),
"time_to": format_datetime_for_api(curr_date), "time_to": format_datetime_for_api(curr_date),
"limit": str(limit), "limit": str(limit),
} }
return _make_api_request("NEWS_SENTIMENT", params) return _make_api_request("NEWS_SENTIMENT", params)
def get_insider_transactions(symbol: str) -> dict[str, str] | str: def get_insider_transactions(symbol: str) -> dict[str, str] | str:
"""Returns latest and historical insider transactions by key stakeholders. """Returns latest and historical insider transactions by key stakeholders.
Covers transactions by founders, executives, board members, etc. Covers transactions by founders, executives, board members, etc.
Args: Args:
symbol: Ticker symbol. Example: "IBM". symbol: Ticker symbol. Example: "IBM".
Returns: Returns:
Dictionary containing insider transaction data or JSON string. Dictionary containing insider transaction data or JSON string.
""" """
params = { params = {
"symbol": symbol, "symbol": symbol,
} }
return _make_api_request("INSIDER_TRANSACTIONS", params) return _make_api_request("INSIDER_TRANSACTIONS", params)

View File

@ -1,38 +1,38 @@
from datetime import datetime from datetime import datetime
from .alpha_vantage_common import _make_api_request, _filter_csv_by_date_range from .alpha_vantage_common import _make_api_request, _filter_csv_by_date_range
def get_stock( def get_stock(
symbol: str, symbol: str,
start_date: str, start_date: str,
end_date: str end_date: str
) -> str: ) -> str:
""" """
Returns raw daily OHLCV values, adjusted close values, and historical split/dividend events Returns raw daily OHLCV values, adjusted close values, and historical split/dividend events
filtered to the specified date range. filtered to the specified date range.
Args: Args:
symbol: The name of the equity. For example: symbol=IBM symbol: The name of the equity. For example: symbol=IBM
start_date: Start date in yyyy-mm-dd format start_date: Start date in yyyy-mm-dd format
end_date: End date in yyyy-mm-dd format end_date: End date in yyyy-mm-dd format
Returns: Returns:
CSV string containing the daily adjusted time series data filtered to the date range. CSV string containing the daily adjusted time series data filtered to the date range.
""" """
# Parse dates to determine the range # Parse dates to determine the range
start_dt = datetime.strptime(start_date, "%Y-%m-%d") start_dt = datetime.strptime(start_date, "%Y-%m-%d")
today = datetime.now() today = datetime.now()
# Choose outputsize based on whether the requested range is within the latest 100 days # Choose outputsize based on whether the requested range is within the latest 100 days
# Compact returns latest 100 data points, so check if start_date is recent enough # Compact returns latest 100 data points, so check if start_date is recent enough
days_from_today_to_start = (today - start_dt).days days_from_today_to_start = (today - start_dt).days
outputsize = "compact" if days_from_today_to_start < 100 else "full" outputsize = "compact" if days_from_today_to_start < 100 else "full"
params = { params = {
"symbol": symbol, "symbol": symbol,
"outputsize": outputsize, "outputsize": outputsize,
"datatype": "csv", "datatype": "csv",
} }
response = _make_api_request("TIME_SERIES_DAILY_ADJUSTED", params) response = _make_api_request("TIME_SERIES_DAILY_ADJUSTED", params)
return _filter_csv_by_date_range(response, start_date, end_date) return _filter_csv_by_date_range(response, start_date, end_date)

View File

@ -1,31 +1,31 @@
import tradingagents.default_config as default_config import tradingagents.default_config as default_config
from typing import Dict, Optional from typing import Dict, Optional
# Use default config but allow it to be overridden # Use default config but allow it to be overridden
_config: Optional[Dict] = None _config: Optional[Dict] = None
def initialize_config(): def initialize_config():
"""Initialize the configuration with default values.""" """Initialize the configuration with default values."""
global _config global _config
if _config is None: if _config is None:
_config = default_config.DEFAULT_CONFIG.copy() _config = default_config.DEFAULT_CONFIG.copy()
def set_config(config: Dict): def set_config(config: Dict):
"""Update the configuration with custom values.""" """Update the configuration with custom values."""
global _config global _config
if _config is None: if _config is None:
_config = default_config.DEFAULT_CONFIG.copy() _config = default_config.DEFAULT_CONFIG.copy()
_config.update(config) _config.update(config)
def get_config() -> Dict: def get_config() -> Dict:
"""Get the current configuration.""" """Get the current configuration."""
if _config is None: if _config is None:
initialize_config() initialize_config()
return _config.copy() return _config.copy()
# Initialize with default config # Initialize with default config
initialize_config() initialize_config()

View File

@ -1,162 +1,162 @@
from typing import Annotated from typing import Annotated
# Import from vendor-specific modules # Import from vendor-specific modules
from .y_finance import ( from .y_finance import (
get_YFin_data_online, get_YFin_data_online,
get_stock_stats_indicators_window, get_stock_stats_indicators_window,
get_fundamentals as get_yfinance_fundamentals, get_fundamentals as get_yfinance_fundamentals,
get_balance_sheet as get_yfinance_balance_sheet, get_balance_sheet as get_yfinance_balance_sheet,
get_cashflow as get_yfinance_cashflow, get_cashflow as get_yfinance_cashflow,
get_income_statement as get_yfinance_income_statement, get_income_statement as get_yfinance_income_statement,
get_insider_transactions as get_yfinance_insider_transactions, get_insider_transactions as get_yfinance_insider_transactions,
) )
from .yfinance_news import get_news_yfinance, get_global_news_yfinance from .yfinance_news import get_news_yfinance, get_global_news_yfinance
from .alpha_vantage import ( from .alpha_vantage import (
get_stock as get_alpha_vantage_stock, get_stock as get_alpha_vantage_stock,
get_indicator as get_alpha_vantage_indicator, get_indicator as get_alpha_vantage_indicator,
get_fundamentals as get_alpha_vantage_fundamentals, get_fundamentals as get_alpha_vantage_fundamentals,
get_balance_sheet as get_alpha_vantage_balance_sheet, get_balance_sheet as get_alpha_vantage_balance_sheet,
get_cashflow as get_alpha_vantage_cashflow, get_cashflow as get_alpha_vantage_cashflow,
get_income_statement as get_alpha_vantage_income_statement, get_income_statement as get_alpha_vantage_income_statement,
get_insider_transactions as get_alpha_vantage_insider_transactions, get_insider_transactions as get_alpha_vantage_insider_transactions,
get_news as get_alpha_vantage_news, get_news as get_alpha_vantage_news,
get_global_news as get_alpha_vantage_global_news, get_global_news as get_alpha_vantage_global_news,
) )
from .alpha_vantage_common import AlphaVantageRateLimitError from .alpha_vantage_common import AlphaVantageRateLimitError
# Configuration and routing logic # Configuration and routing logic
from .config import get_config from .config import get_config
# Tools organized by category # Tools organized by category
TOOLS_CATEGORIES = { TOOLS_CATEGORIES = {
"core_stock_apis": { "core_stock_apis": {
"description": "OHLCV stock price data", "description": "OHLCV stock price data",
"tools": [ "tools": [
"get_stock_data" "get_stock_data"
] ]
}, },
"technical_indicators": { "technical_indicators": {
"description": "Technical analysis indicators", "description": "Technical analysis indicators",
"tools": [ "tools": [
"get_indicators" "get_indicators"
] ]
}, },
"fundamental_data": { "fundamental_data": {
"description": "Company fundamentals", "description": "Company fundamentals",
"tools": [ "tools": [
"get_fundamentals", "get_fundamentals",
"get_balance_sheet", "get_balance_sheet",
"get_cashflow", "get_cashflow",
"get_income_statement" "get_income_statement"
] ]
}, },
"news_data": { "news_data": {
"description": "News and insider data", "description": "News and insider data",
"tools": [ "tools": [
"get_news", "get_news",
"get_global_news", "get_global_news",
"get_insider_transactions", "get_insider_transactions",
] ]
} }
} }
VENDOR_LIST = [ VENDOR_LIST = [
"yfinance", "yfinance",
"alpha_vantage", "alpha_vantage",
] ]
# Mapping of methods to their vendor-specific implementations # Mapping of methods to their vendor-specific implementations
VENDOR_METHODS = { VENDOR_METHODS = {
# core_stock_apis # core_stock_apis
"get_stock_data": { "get_stock_data": {
"alpha_vantage": get_alpha_vantage_stock, "alpha_vantage": get_alpha_vantage_stock,
"yfinance": get_YFin_data_online, "yfinance": get_YFin_data_online,
}, },
# technical_indicators # technical_indicators
"get_indicators": { "get_indicators": {
"alpha_vantage": get_alpha_vantage_indicator, "alpha_vantage": get_alpha_vantage_indicator,
"yfinance": get_stock_stats_indicators_window, "yfinance": get_stock_stats_indicators_window,
}, },
# fundamental_data # fundamental_data
"get_fundamentals": { "get_fundamentals": {
"alpha_vantage": get_alpha_vantage_fundamentals, "alpha_vantage": get_alpha_vantage_fundamentals,
"yfinance": get_yfinance_fundamentals, "yfinance": get_yfinance_fundamentals,
}, },
"get_balance_sheet": { "get_balance_sheet": {
"alpha_vantage": get_alpha_vantage_balance_sheet, "alpha_vantage": get_alpha_vantage_balance_sheet,
"yfinance": get_yfinance_balance_sheet, "yfinance": get_yfinance_balance_sheet,
}, },
"get_cashflow": { "get_cashflow": {
"alpha_vantage": get_alpha_vantage_cashflow, "alpha_vantage": get_alpha_vantage_cashflow,
"yfinance": get_yfinance_cashflow, "yfinance": get_yfinance_cashflow,
}, },
"get_income_statement": { "get_income_statement": {
"alpha_vantage": get_alpha_vantage_income_statement, "alpha_vantage": get_alpha_vantage_income_statement,
"yfinance": get_yfinance_income_statement, "yfinance": get_yfinance_income_statement,
}, },
# news_data # news_data
"get_news": { "get_news": {
"alpha_vantage": get_alpha_vantage_news, "alpha_vantage": get_alpha_vantage_news,
"yfinance": get_news_yfinance, "yfinance": get_news_yfinance,
}, },
"get_global_news": { "get_global_news": {
"yfinance": get_global_news_yfinance, "yfinance": get_global_news_yfinance,
"alpha_vantage": get_alpha_vantage_global_news, "alpha_vantage": get_alpha_vantage_global_news,
}, },
"get_insider_transactions": { "get_insider_transactions": {
"alpha_vantage": get_alpha_vantage_insider_transactions, "alpha_vantage": get_alpha_vantage_insider_transactions,
"yfinance": get_yfinance_insider_transactions, "yfinance": get_yfinance_insider_transactions,
}, },
} }
def get_category_for_method(method: str) -> str: def get_category_for_method(method: str) -> str:
"""Get the category that contains the specified method.""" """Get the category that contains the specified method."""
for category, info in TOOLS_CATEGORIES.items(): for category, info in TOOLS_CATEGORIES.items():
if method in info["tools"]: if method in info["tools"]:
return category return category
raise ValueError(f"Method '{method}' not found in any category") raise ValueError(f"Method '{method}' not found in any category")
def get_vendor(category: str, method: str = None) -> str: def get_vendor(category: str, method: str = None) -> str:
"""Get the configured vendor for a data category or specific tool method. """Get the configured vendor for a data category or specific tool method.
Tool-level configuration takes precedence over category-level. Tool-level configuration takes precedence over category-level.
""" """
config = get_config() config = get_config()
# Check tool-level configuration first (if method provided) # Check tool-level configuration first (if method provided)
if method: if method:
tool_vendors = config.get("tool_vendors", {}) tool_vendors = config.get("tool_vendors", {})
if method in tool_vendors: if method in tool_vendors:
return tool_vendors[method] return tool_vendors[method]
# Fall back to category-level configuration # Fall back to category-level configuration
return config.get("data_vendors", {}).get(category, "default") return config.get("data_vendors", {}).get(category, "default")
def route_to_vendor(method: str, *args, **kwargs): def route_to_vendor(method: str, *args, **kwargs):
"""Route method calls to appropriate vendor implementation with fallback support.""" """Route method calls to appropriate vendor implementation with fallback support."""
category = get_category_for_method(method) category = get_category_for_method(method)
vendor_config = get_vendor(category, method) vendor_config = get_vendor(category, method)
primary_vendors = [v.strip() for v in vendor_config.split(',')] primary_vendors = [v.strip() for v in vendor_config.split(',')]
if method not in VENDOR_METHODS: if method not in VENDOR_METHODS:
raise ValueError(f"Method '{method}' not supported") raise ValueError(f"Method '{method}' not supported")
# Build fallback chain: primary vendors first, then remaining available vendors # Build fallback chain: primary vendors first, then remaining available vendors
all_available_vendors = list(VENDOR_METHODS[method].keys()) all_available_vendors = list(VENDOR_METHODS[method].keys())
fallback_vendors = primary_vendors.copy() fallback_vendors = primary_vendors.copy()
for vendor in all_available_vendors: for vendor in all_available_vendors:
if vendor not in fallback_vendors: if vendor not in fallback_vendors:
fallback_vendors.append(vendor) fallback_vendors.append(vendor)
for vendor in fallback_vendors: for vendor in fallback_vendors:
if vendor not in VENDOR_METHODS[method]: if vendor not in VENDOR_METHODS[method]:
continue continue
vendor_impl = VENDOR_METHODS[method][vendor] vendor_impl = VENDOR_METHODS[method][vendor]
impl_func = vendor_impl[0] if isinstance(vendor_impl, list) else vendor_impl impl_func = vendor_impl[0] if isinstance(vendor_impl, list) else vendor_impl
try: try:
return impl_func(*args, **kwargs) return impl_func(*args, **kwargs)
except AlphaVantageRateLimitError: except AlphaVantageRateLimitError:
continue # Only rate limits trigger fallback continue # Only rate limits trigger fallback
raise RuntimeError(f"No available vendor for '{method}'") raise RuntimeError(f"No available vendor for '{method}'")

View File

@ -1,64 +1,64 @@
import pandas as pd import pandas as pd
import yfinance as yf import yfinance as yf
from stockstats import wrap from stockstats import wrap
from typing import Annotated from typing import Annotated
import os import os
from .config import get_config from .config import get_config
class StockstatsUtils: class StockstatsUtils:
@staticmethod @staticmethod
def get_stock_stats( def get_stock_stats(
symbol: Annotated[str, "ticker symbol for the company"], symbol: Annotated[str, "ticker symbol for the company"],
indicator: Annotated[ indicator: Annotated[
str, "quantitative indicators based off of the stock data for the company" str, "quantitative indicators based off of the stock data for the company"
], ],
curr_date: Annotated[ curr_date: Annotated[
str, "curr date for retrieving stock price data, YYYY-mm-dd" str, "curr date for retrieving stock price data, YYYY-mm-dd"
], ],
): ):
config = get_config() config = get_config()
today_date = pd.Timestamp.today() today_date = pd.Timestamp.today()
curr_date_dt = pd.to_datetime(curr_date) curr_date_dt = pd.to_datetime(curr_date)
end_date = today_date end_date = today_date
start_date = today_date - pd.DateOffset(years=15) start_date = today_date - pd.DateOffset(years=15)
start_date_str = start_date.strftime("%Y-%m-%d") start_date_str = start_date.strftime("%Y-%m-%d")
end_date_str = end_date.strftime("%Y-%m-%d") end_date_str = end_date.strftime("%Y-%m-%d")
# Ensure cache directory exists # Ensure cache directory exists
os.makedirs(config["data_cache_dir"], exist_ok=True) os.makedirs(config["data_cache_dir"], exist_ok=True)
data_file = os.path.join( data_file = os.path.join(
config["data_cache_dir"], config["data_cache_dir"],
f"{symbol}-YFin-data-{start_date_str}-{end_date_str}.csv", f"{symbol}-YFin-data-{start_date_str}-{end_date_str}.csv",
) )
if os.path.exists(data_file): if os.path.exists(data_file):
data = pd.read_csv(data_file) data = pd.read_csv(data_file)
data["Date"] = pd.to_datetime(data["Date"]) data["Date"] = pd.to_datetime(data["Date"])
else: else:
data = yf.download( data = yf.download(
symbol, symbol,
start=start_date_str, start=start_date_str,
end=end_date_str, end=end_date_str,
multi_level_index=False, multi_level_index=False,
progress=False, progress=False,
auto_adjust=True, auto_adjust=True,
) )
data = data.reset_index() data = data.reset_index()
data.to_csv(data_file, index=False) data.to_csv(data_file, index=False)
df = wrap(data) df = wrap(data)
df["Date"] = df["Date"].dt.strftime("%Y-%m-%d") df["Date"] = df["Date"].dt.strftime("%Y-%m-%d")
curr_date_str = curr_date_dt.strftime("%Y-%m-%d") curr_date_str = curr_date_dt.strftime("%Y-%m-%d")
df[indicator] # trigger stockstats to calculate the indicator df[indicator] # trigger stockstats to calculate the indicator
matching_rows = df[df["Date"].str.startswith(curr_date_str)] matching_rows = df[df["Date"].str.startswith(curr_date_str)]
if not matching_rows.empty: if not matching_rows.empty:
indicator_value = matching_rows[indicator].values[0] indicator_value = matching_rows[indicator].values[0]
return indicator_value return indicator_value
else: else:
return "N/A: Not a trading day (weekend or holiday)" return "N/A: Not a trading day (weekend or holiday)"

View File

@ -1,39 +1,39 @@
import os import os
import json import json
import pandas as pd import pandas as pd
from datetime import date, timedelta, datetime from datetime import date, timedelta, datetime
from typing import Annotated from typing import Annotated
SavePathType = Annotated[str, "File path to save data. If None, data is not saved."] SavePathType = Annotated[str, "File path to save data. If None, data is not saved."]
def save_output(data: pd.DataFrame, tag: str, save_path: SavePathType = None) -> None: def save_output(data: pd.DataFrame, tag: str, save_path: SavePathType = None) -> None:
if save_path: if save_path:
data.to_csv(save_path) data.to_csv(save_path)
print(f"{tag} saved to {save_path}") print(f"{tag} saved to {save_path}")
def get_current_date(): def get_current_date():
return date.today().strftime("%Y-%m-%d") return date.today().strftime("%Y-%m-%d")
def decorate_all_methods(decorator): def decorate_all_methods(decorator):
def class_decorator(cls): def class_decorator(cls):
for attr_name, attr_value in cls.__dict__.items(): for attr_name, attr_value in cls.__dict__.items():
if callable(attr_value): if callable(attr_value):
setattr(cls, attr_name, decorator(attr_value)) setattr(cls, attr_name, decorator(attr_value))
return cls return cls
return class_decorator return class_decorator
def get_next_weekday(date): def get_next_weekday(date):
if not isinstance(date, datetime): if not isinstance(date, datetime):
date = datetime.strptime(date, "%Y-%m-%d") date = datetime.strptime(date, "%Y-%m-%d")
if date.weekday() >= 5: if date.weekday() >= 5:
days_to_add = 7 - date.weekday() days_to_add = 7 - date.weekday()
next_weekday = date + timedelta(days=days_to_add) next_weekday = date + timedelta(days=days_to_add)
return next_weekday return next_weekday
else: else:
return date return date

File diff suppressed because it is too large Load Diff

View File

@ -1,190 +1,190 @@
"""yfinance-based news data fetching functions.""" """yfinance-based news data fetching functions."""
import yfinance as yf import yfinance as yf
from datetime import datetime from datetime import datetime
from dateutil.relativedelta import relativedelta from dateutil.relativedelta import relativedelta
def _extract_article_data(article: dict) -> dict: def _extract_article_data(article: dict) -> dict:
"""Extract article data from yfinance news format (handles nested 'content' structure).""" """Extract article data from yfinance news format (handles nested 'content' structure)."""
# Handle nested content structure # Handle nested content structure
if "content" in article: if "content" in article:
content = article["content"] content = article["content"]
title = content.get("title", "No title") title = content.get("title", "No title")
summary = content.get("summary", "") summary = content.get("summary", "")
provider = content.get("provider", {}) provider = content.get("provider", {})
publisher = provider.get("displayName", "Unknown") publisher = provider.get("displayName", "Unknown")
# Get URL from canonicalUrl or clickThroughUrl # Get URL from canonicalUrl or clickThroughUrl
url_obj = content.get("canonicalUrl") or content.get("clickThroughUrl") or {} url_obj = content.get("canonicalUrl") or content.get("clickThroughUrl") or {}
link = url_obj.get("url", "") link = url_obj.get("url", "")
# Get publish date # Get publish date
pub_date_str = content.get("pubDate", "") pub_date_str = content.get("pubDate", "")
pub_date = None pub_date = None
if pub_date_str: if pub_date_str:
try: try:
pub_date = datetime.fromisoformat(pub_date_str.replace("Z", "+00:00")) pub_date = datetime.fromisoformat(pub_date_str.replace("Z", "+00:00"))
except (ValueError, AttributeError): except (ValueError, AttributeError):
pass pass
return { return {
"title": title, "title": title,
"summary": summary, "summary": summary,
"publisher": publisher, "publisher": publisher,
"link": link, "link": link,
"pub_date": pub_date, "pub_date": pub_date,
} }
else: else:
# Fallback for flat structure # Fallback for flat structure
return { return {
"title": article.get("title", "No title"), "title": article.get("title", "No title"),
"summary": article.get("summary", ""), "summary": article.get("summary", ""),
"publisher": article.get("publisher", "Unknown"), "publisher": article.get("publisher", "Unknown"),
"link": article.get("link", ""), "link": article.get("link", ""),
"pub_date": None, "pub_date": None,
} }
def get_news_yfinance( def get_news_yfinance(
ticker: str, ticker: str,
start_date: str, start_date: str,
end_date: str, end_date: str,
) -> str: ) -> str:
""" """
Retrieve news for a specific stock ticker using yfinance. Retrieve news for a specific stock ticker using yfinance.
Args: Args:
ticker: Stock ticker symbol (e.g., "AAPL") ticker: Stock ticker symbol (e.g., "AAPL")
start_date: Start date in yyyy-mm-dd format start_date: Start date in yyyy-mm-dd format
end_date: End date in yyyy-mm-dd format end_date: End date in yyyy-mm-dd format
Returns: Returns:
Formatted string containing news articles Formatted string containing news articles
""" """
try: try:
stock = yf.Ticker(ticker) stock = yf.Ticker(ticker)
news = stock.get_news(count=20) news = stock.get_news(count=20)
if not news: if not news:
return f"No news found for {ticker}" return f"No news found for {ticker}"
# Parse date range for filtering # Parse date range for filtering
start_dt = datetime.strptime(start_date, "%Y-%m-%d") start_dt = datetime.strptime(start_date, "%Y-%m-%d")
end_dt = datetime.strptime(end_date, "%Y-%m-%d") end_dt = datetime.strptime(end_date, "%Y-%m-%d")
news_str = "" news_str = ""
filtered_count = 0 filtered_count = 0
for article in news: for article in news:
data = _extract_article_data(article) data = _extract_article_data(article)
# Filter by date if publish time is available # Filter by date if publish time is available
if data["pub_date"]: if data["pub_date"]:
pub_date_naive = data["pub_date"].replace(tzinfo=None) pub_date_naive = data["pub_date"].replace(tzinfo=None)
if not (start_dt <= pub_date_naive <= end_dt + relativedelta(days=1)): if not (start_dt <= pub_date_naive <= end_dt + relativedelta(days=1)):
continue continue
news_str += f"### {data['title']} (source: {data['publisher']})\n" news_str += f"### {data['title']} (source: {data['publisher']})\n"
if data["summary"]: if data["summary"]:
news_str += f"{data['summary']}\n" news_str += f"{data['summary']}\n"
if data["link"]: if data["link"]:
news_str += f"Link: {data['link']}\n" news_str += f"Link: {data['link']}\n"
news_str += "\n" news_str += "\n"
filtered_count += 1 filtered_count += 1
if filtered_count == 0: if filtered_count == 0:
return f"No news found for {ticker} between {start_date} and {end_date}" return f"No news found for {ticker} between {start_date} and {end_date}"
return f"## {ticker} News, from {start_date} to {end_date}:\n\n{news_str}" return f"## {ticker} News, from {start_date} to {end_date}:\n\n{news_str}"
except Exception as e: except Exception as e:
return f"Error fetching news for {ticker}: {str(e)}" return f"Error fetching news for {ticker}: {str(e)}"
def get_global_news_yfinance( def get_global_news_yfinance(
curr_date: str, curr_date: str,
look_back_days: int = 7, look_back_days: int = 7,
limit: int = 10, limit: int = 10,
) -> str: ) -> str:
""" """
Retrieve global/macro economic news using yfinance Search. Retrieve global/macro economic news using yfinance Search.
Args: Args:
curr_date: Current date in yyyy-mm-dd format curr_date: Current date in yyyy-mm-dd format
look_back_days: Number of days to look back look_back_days: Number of days to look back
limit: Maximum number of articles to return limit: Maximum number of articles to return
Returns: Returns:
Formatted string containing global news articles Formatted string containing global news articles
""" """
# Search queries for macro/global news # Search queries for macro/global news
search_queries = [ search_queries = [
"stock market economy", "stock market economy",
"Federal Reserve interest rates", "Federal Reserve interest rates",
"inflation economic outlook", "inflation economic outlook",
"global markets trading", "global markets trading",
] ]
all_news = [] all_news = []
seen_titles = set() seen_titles = set()
try: try:
for query in search_queries: for query in search_queries:
search = yf.Search( search = yf.Search(
query=query, query=query,
news_count=limit, news_count=limit,
enable_fuzzy_query=True, enable_fuzzy_query=True,
) )
if search.news: if search.news:
for article in search.news: for article in search.news:
# Handle both flat and nested structures # Handle both flat and nested structures
if "content" in article: if "content" in article:
data = _extract_article_data(article) data = _extract_article_data(article)
title = data["title"] title = data["title"]
else: else:
title = article.get("title", "") title = article.get("title", "")
# Deduplicate by title # Deduplicate by title
if title and title not in seen_titles: if title and title not in seen_titles:
seen_titles.add(title) seen_titles.add(title)
all_news.append(article) all_news.append(article)
if len(all_news) >= limit: if len(all_news) >= limit:
break break
if not all_news: if not all_news:
return f"No global news found for {curr_date}" return f"No global news found for {curr_date}"
# Calculate date range # Calculate date range
curr_dt = datetime.strptime(curr_date, "%Y-%m-%d") curr_dt = datetime.strptime(curr_date, "%Y-%m-%d")
start_dt = curr_dt - relativedelta(days=look_back_days) start_dt = curr_dt - relativedelta(days=look_back_days)
start_date = start_dt.strftime("%Y-%m-%d") start_date = start_dt.strftime("%Y-%m-%d")
news_str = "" news_str = ""
for article in all_news[:limit]: for article in all_news[:limit]:
# Handle both flat and nested structures # Handle both flat and nested structures
if "content" in article: if "content" in article:
data = _extract_article_data(article) data = _extract_article_data(article)
title = data["title"] title = data["title"]
publisher = data["publisher"] publisher = data["publisher"]
link = data["link"] link = data["link"]
summary = data["summary"] summary = data["summary"]
else: else:
title = article.get("title", "No title") title = article.get("title", "No title")
publisher = article.get("publisher", "Unknown") publisher = article.get("publisher", "Unknown")
link = article.get("link", "") link = article.get("link", "")
summary = "" summary = ""
news_str += f"### {title} (source: {publisher})\n" news_str += f"### {title} (source: {publisher})\n"
if summary: if summary:
news_str += f"{summary}\n" news_str += f"{summary}\n"
if link: if link:
news_str += f"Link: {link}\n" news_str += f"Link: {link}\n"
news_str += "\n" news_str += "\n"
return f"## Global Market News, from {start_date} to {curr_date}:\n\n{news_str}" return f"## Global Market News, from {start_date} to {curr_date}:\n\n{news_str}"
except Exception as e: except Exception as e:
return f"Error fetching global news: {str(e)}" return f"Error fetching global news: {str(e)}"

View File

@ -1,9 +1,9 @@
# TradingAgents/graph/__init__.py # TradingAgents/graph/__init__.py
from .trading_graph import TradingAgentsGraph from .trading_graph import TradingAgentsGraph
from .setup import StructuredGraphSetup from .setup import StructuredGraphSetup
__all__ = [ __all__ = [
"TradingAgentsGraph", "TradingAgentsGraph",
"StructuredGraphSetup", "StructuredGraphSetup",
] ]

View File

@ -1,67 +1,67 @@
# TradingAgents/graph/conditional_logic.py # TradingAgents/graph/conditional_logic.py
from tradingagents.agents.utils.agent_states import AgentState from tradingagents.agents.utils.agent_states import AgentState
class ConditionalLogic: class ConditionalLogic:
"""Handles conditional logic for determining graph flow.""" """Handles conditional logic for determining graph flow."""
def __init__(self, max_debate_rounds=1, max_risk_discuss_rounds=1): def __init__(self, max_debate_rounds=1, max_risk_discuss_rounds=1):
"""Initialize with configuration parameters.""" """Initialize with configuration parameters."""
self.max_debate_rounds = max_debate_rounds self.max_debate_rounds = max_debate_rounds
self.max_risk_discuss_rounds = max_risk_discuss_rounds self.max_risk_discuss_rounds = max_risk_discuss_rounds
def should_continue_market(self, state: AgentState): def should_continue_market(self, state: AgentState):
"""Determine if market analysis should continue.""" """Determine if market analysis should continue."""
messages = state["messages"] messages = state["messages"]
last_message = messages[-1] last_message = messages[-1]
if last_message.tool_calls: if last_message.tool_calls:
return "tools_market" return "tools_market"
return "Msg Clear Market" return "Msg Clear Market"
def should_continue_social(self, state: AgentState): def should_continue_social(self, state: AgentState):
"""Determine if social media analysis should continue.""" """Determine if social media analysis should continue."""
messages = state["messages"] messages = state["messages"]
last_message = messages[-1] last_message = messages[-1]
if last_message.tool_calls: if last_message.tool_calls:
return "tools_social" return "tools_social"
return "Msg Clear Social" return "Msg Clear Social"
def should_continue_news(self, state: AgentState): def should_continue_news(self, state: AgentState):
"""Determine if news analysis should continue.""" """Determine if news analysis should continue."""
messages = state["messages"] messages = state["messages"]
last_message = messages[-1] last_message = messages[-1]
if last_message.tool_calls: if last_message.tool_calls:
return "tools_news" return "tools_news"
return "Msg Clear News" return "Msg Clear News"
def should_continue_fundamentals(self, state: AgentState): def should_continue_fundamentals(self, state: AgentState):
"""Determine if fundamentals analysis should continue.""" """Determine if fundamentals analysis should continue."""
messages = state["messages"] messages = state["messages"]
last_message = messages[-1] last_message = messages[-1]
if last_message.tool_calls: if last_message.tool_calls:
return "tools_fundamentals" return "tools_fundamentals"
return "Msg Clear Fundamentals" return "Msg Clear Fundamentals"
def should_continue_debate(self, state: AgentState) -> str: def should_continue_debate(self, state: AgentState) -> str:
"""Determine if debate should continue.""" """Determine if debate should continue."""
if ( if (
state["investment_debate_state"]["count"] >= 2 * self.max_debate_rounds state["investment_debate_state"]["count"] >= 2 * self.max_debate_rounds
): # 3 rounds of back-and-forth between 2 agents ): # 3 rounds of back-and-forth between 2 agents
return "Research Manager" return "Research Manager"
if state["investment_debate_state"]["current_response"].startswith("Bull"): if state["investment_debate_state"]["current_response"].startswith("Bull"):
return "Bear Researcher" return "Bear Researcher"
return "Bull Researcher" return "Bull Researcher"
def should_continue_risk_analysis(self, state: AgentState) -> str: def should_continue_risk_analysis(self, state: AgentState) -> str:
"""Determine if risk analysis should continue.""" """Determine if risk analysis should continue."""
if ( if (
state["risk_debate_state"]["count"] >= 3 * self.max_risk_discuss_rounds state["risk_debate_state"]["count"] >= 3 * self.max_risk_discuss_rounds
): # 3 rounds of back-and-forth between 3 agents ): # 3 rounds of back-and-forth between 3 agents
return "Risk Judge" return "Risk Judge"
if state["risk_debate_state"]["latest_speaker"].startswith("Aggressive"): if state["risk_debate_state"]["latest_speaker"].startswith("Aggressive"):
return "Conservative Analyst" return "Conservative Analyst"
if state["risk_debate_state"]["latest_speaker"].startswith("Conservative"): if state["risk_debate_state"]["latest_speaker"].startswith("Conservative"):
return "Neutral Analyst" return "Neutral Analyst"
return "Aggressive Analyst" return "Aggressive Analyst"

View File

@ -1,222 +1,222 @@
"""Parallel execution nodes for TradingAgents. """Parallel execution nodes for TradingAgents.
Provides parallel wrappers for: Provides parallel wrappers for:
- Analyst phase (Market, Social, News, Fundamentals) - Analyst phase (Market, Social, News, Fundamentals)
- Research debate phase (Bull + Bear) - Research debate phase (Bull + Bear)
- Risk debate phase (Aggressive + Conservative + Neutral) - Risk debate phase (Aggressive + Conservative + Neutral)
""" """
import asyncio import asyncio
import logging import logging
from langchain_core.messages import HumanMessage, RemoveMessage from langchain_core.messages import HumanMessage, RemoveMessage
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
def create_parallel_analyst_node(analyst_fns, tool_nodes, selected_analysts): def create_parallel_analyst_node(analyst_fns, tool_nodes, selected_analysts):
"""Create a single LangGraph node that runs all analysts in parallel. """Create a single LangGraph node that runs all analysts in parallel.
Each analyst gets its own isolated message state and runs its complete Each analyst gets its own isolated message state and runs its complete
tool-calling loop independently. Results are merged at the end. tool-calling loop independently. Results are merged at the end.
Args: Args:
analyst_fns: dict mapping analyst type (e.g. "market") to node function analyst_fns: dict mapping analyst type (e.g. "market") to node function
tool_nodes: dict mapping analyst type to ToolNode instance tool_nodes: dict mapping analyst type to ToolNode instance
selected_analysts: list of analyst types to run selected_analysts: list of analyst types to run
""" """
async def parallel_analysts_node(state): async def parallel_analysts_node(state):
"""Run all analysts concurrently and merge their reports.""" """Run all analysts concurrently and merge their reports."""
async def run_single(analyst_type): async def run_single(analyst_type):
"""Run one analyst through its complete tool-calling loop.""" """Run one analyst through its complete tool-calling loop."""
fn = analyst_fns[analyst_type] fn = analyst_fns[analyst_type]
tn = tool_nodes[analyst_type] tn = tool_nodes[analyst_type]
# Each analyst gets its own isolated message state # Each analyst gets its own isolated message state
local_state = { local_state = {
"messages": list(state["messages"]), "messages": list(state["messages"]),
"trade_date": state["trade_date"], "trade_date": state["trade_date"],
"company_of_interest": state["company_of_interest"], "company_of_interest": state["company_of_interest"],
} }
result = {} result = {}
for _ in range(10): # safety limit on tool rounds for _ in range(10): # safety limit on tool rounds
result = await asyncio.to_thread(fn, local_state) result = await asyncio.to_thread(fn, local_state)
ai_msg = result["messages"][0] ai_msg = result["messages"][0]
local_state["messages"] = local_state["messages"] + [ai_msg] local_state["messages"] = local_state["messages"] + [ai_msg]
if not ai_msg.tool_calls: if not ai_msg.tool_calls:
break break
# Process tool calls # Process tool calls
tool_result = await asyncio.to_thread(tn.invoke, local_state) tool_result = await asyncio.to_thread(tn.invoke, local_state)
local_state["messages"] = ( local_state["messages"] = (
local_state["messages"] + tool_result["messages"] local_state["messages"] + tool_result["messages"]
) )
# Return only report fields (not messages) # Return only report fields (not messages)
return {k: v for k, v in result.items() if k != "messages"} return {k: v for k, v in result.items() if k != "messages"}
# Run all analysts concurrently # Run all analysts concurrently
tasks = [run_single(at) for at in selected_analysts if at in analyst_fns] tasks = [run_single(at) for at in selected_analysts if at in analyst_fns]
results = await asyncio.gather(*tasks) results = await asyncio.gather(*tasks)
# Merge all report fields # Merge all report fields
merged = {} merged = {}
for r in results: for r in results:
merged.update(r) merged.update(r)
# Clear messages and add placeholder (same as Msg Clear nodes) # Clear messages and add placeholder (same as Msg Clear nodes)
messages = state.get("messages", []) messages = state.get("messages", [])
removal_ops = [ removal_ops = [
RemoveMessage(id=m.id) RemoveMessage(id=m.id)
for m in messages for m in messages
if hasattr(m, "id") and m.id if hasattr(m, "id") and m.id
] ]
merged["messages"] = removal_ops + [HumanMessage(content="Continue")] merged["messages"] = removal_ops + [HumanMessage(content="Continue")]
return merged return merged
return parallel_analysts_node return parallel_analysts_node
def _snapshot_research_state(state): def _snapshot_research_state(state):
"""Extract research-relevant fields into a plain dict.""" """Extract research-relevant fields into a plain dict."""
return { return {
"investment_debate_state": dict(state.get("investment_debate_state", {})), "investment_debate_state": dict(state.get("investment_debate_state", {})),
"market_report": state.get("market_report", ""), "market_report": state.get("market_report", ""),
"sentiment_report": state.get("sentiment_report", ""), "sentiment_report": state.get("sentiment_report", ""),
"news_report": state.get("news_report", ""), "news_report": state.get("news_report", ""),
"fundamentals_report": state.get("fundamentals_report", ""), "fundamentals_report": state.get("fundamentals_report", ""),
} }
def _snapshot_risk_state(state): def _snapshot_risk_state(state):
"""Extract risk-relevant fields into a plain dict.""" """Extract risk-relevant fields into a plain dict."""
return { return {
"risk_debate_state": dict(state.get("risk_debate_state", {})), "risk_debate_state": dict(state.get("risk_debate_state", {})),
"market_report": state.get("market_report", ""), "market_report": state.get("market_report", ""),
"sentiment_report": state.get("sentiment_report", ""), "sentiment_report": state.get("sentiment_report", ""),
"news_report": state.get("news_report", ""), "news_report": state.get("news_report", ""),
"fundamentals_report": state.get("fundamentals_report", ""), "fundamentals_report": state.get("fundamentals_report", ""),
"trader_investment_plan": state.get("trader_investment_plan", ""), "trader_investment_plan": state.get("trader_investment_plan", ""),
} }
def create_parallel_research_node(bull_fn, bear_fn): def create_parallel_research_node(bull_fn, bear_fn):
"""Create a node that runs Bull and Bear researchers in parallel. """Create a node that runs Bull and Bear researchers in parallel.
Uses async + asyncio.to_thread + asyncio.gather the same pattern Uses async + asyncio.to_thread + asyncio.gather the same pattern
that works for create_parallel_analyst_node. that works for create_parallel_analyst_node.
""" """
async def parallel_research_node(state): async def parallel_research_node(state):
import time import time
import sys import sys
state_snap = _snapshot_research_state(state) state_snap = _snapshot_research_state(state)
t0 = time.time() t0 = time.time()
async def run_bull(): async def run_bull():
print(f"[PARALLEL] Bull starting at +{time.time()-t0:.1f}s", file=sys.stderr, flush=True) print(f"[PARALLEL] Bull starting at +{time.time()-t0:.1f}s", file=sys.stderr, flush=True)
result = await asyncio.to_thread(bull_fn, state_snap) result = await asyncio.to_thread(bull_fn, state_snap)
print(f"[PARALLEL] Bull done at +{time.time()-t0:.1f}s", file=sys.stderr, flush=True) print(f"[PARALLEL] Bull done at +{time.time()-t0:.1f}s", file=sys.stderr, flush=True)
return result return result
async def run_bear(): async def run_bear():
print(f"[PARALLEL] Bear starting at +{time.time()-t0:.1f}s", file=sys.stderr, flush=True) print(f"[PARALLEL] Bear starting at +{time.time()-t0:.1f}s", file=sys.stderr, flush=True)
result = await asyncio.to_thread(bear_fn, state_snap) result = await asyncio.to_thread(bear_fn, state_snap)
print(f"[PARALLEL] Bear done at +{time.time()-t0:.1f}s", file=sys.stderr, flush=True) print(f"[PARALLEL] Bear done at +{time.time()-t0:.1f}s", file=sys.stderr, flush=True)
return result return result
bull_result, bear_result = await asyncio.gather(run_bull(), run_bear()) bull_result, bear_result = await asyncio.gather(run_bull(), run_bear())
print(f"[PARALLEL] Research total: {time.time()-t0:.1f}s", file=sys.stderr, flush=True) print(f"[PARALLEL] Research total: {time.time()-t0:.1f}s", file=sys.stderr, flush=True)
bull_debate = bull_result["investment_debate_state"] bull_debate = bull_result["investment_debate_state"]
bear_debate = bear_result["investment_debate_state"] bear_debate = bear_result["investment_debate_state"]
merged_debate = { merged_debate = {
"bull_history": bull_debate.get("bull_history", ""), "bull_history": bull_debate.get("bull_history", ""),
"bear_history": bear_debate.get("bear_history", ""), "bear_history": bear_debate.get("bear_history", ""),
"history": bull_debate.get("bull_history", "") "history": bull_debate.get("bull_history", "")
+ "\n" + "\n"
+ bear_debate.get("bear_history", ""), + bear_debate.get("bear_history", ""),
"current_response": bear_debate.get("current_response", ""), "current_response": bear_debate.get("current_response", ""),
"judge_decision": "", "judge_decision": "",
"count": 2, "count": 2,
} }
return {"investment_debate_state": merged_debate} return {"investment_debate_state": merged_debate}
return parallel_research_node return parallel_research_node
def create_parallel_risk_node(aggressive_fn, conservative_fn, neutral_fn): def create_parallel_risk_node(aggressive_fn, conservative_fn, neutral_fn):
"""Create a node that runs all 3 risk analysts in parallel. """Create a node that runs all 3 risk analysts in parallel.
Uses async + asyncio.to_thread + asyncio.gather the same pattern Uses async + asyncio.to_thread + asyncio.gather the same pattern
that works for create_parallel_analyst_node. that works for create_parallel_analyst_node.
""" """
async def parallel_risk_node(state): async def parallel_risk_node(state):
import time import time
import sys import sys
state_snap = _snapshot_risk_state(state) state_snap = _snapshot_risk_state(state)
t0 = time.time() t0 = time.time()
async def run_agg(): async def run_agg():
print(f"[PARALLEL] Aggressive starting at +{time.time()-t0:.1f}s", file=sys.stderr, flush=True) print(f"[PARALLEL] Aggressive starting at +{time.time()-t0:.1f}s", file=sys.stderr, flush=True)
result = await asyncio.to_thread(aggressive_fn, state_snap) result = await asyncio.to_thread(aggressive_fn, state_snap)
print(f"[PARALLEL] Aggressive done at +{time.time()-t0:.1f}s", file=sys.stderr, flush=True) print(f"[PARALLEL] Aggressive done at +{time.time()-t0:.1f}s", file=sys.stderr, flush=True)
return result return result
async def run_con(): async def run_con():
print(f"[PARALLEL] Conservative starting at +{time.time()-t0:.1f}s", file=sys.stderr, flush=True) print(f"[PARALLEL] Conservative starting at +{time.time()-t0:.1f}s", file=sys.stderr, flush=True)
result = await asyncio.to_thread(conservative_fn, state_snap) result = await asyncio.to_thread(conservative_fn, state_snap)
print(f"[PARALLEL] Conservative done at +{time.time()-t0:.1f}s", file=sys.stderr, flush=True) print(f"[PARALLEL] Conservative done at +{time.time()-t0:.1f}s", file=sys.stderr, flush=True)
return result return result
async def run_neu(): async def run_neu():
print(f"[PARALLEL] Neutral starting at +{time.time()-t0:.1f}s", file=sys.stderr, flush=True) print(f"[PARALLEL] Neutral starting at +{time.time()-t0:.1f}s", file=sys.stderr, flush=True)
result = await asyncio.to_thread(neutral_fn, state_snap) result = await asyncio.to_thread(neutral_fn, state_snap)
print(f"[PARALLEL] Neutral done at +{time.time()-t0:.1f}s", file=sys.stderr, flush=True) print(f"[PARALLEL] Neutral done at +{time.time()-t0:.1f}s", file=sys.stderr, flush=True)
return result return result
agg_result, con_result, neu_result = await asyncio.gather( agg_result, con_result, neu_result = await asyncio.gather(
run_agg(), run_con(), run_neu() run_agg(), run_con(), run_neu()
) )
print(f"[PARALLEL] Risk total: {time.time()-t0:.1f}s", file=sys.stderr, flush=True) print(f"[PARALLEL] Risk total: {time.time()-t0:.1f}s", file=sys.stderr, flush=True)
agg_debate = agg_result["risk_debate_state"] agg_debate = agg_result["risk_debate_state"]
con_debate = con_result["risk_debate_state"] con_debate = con_result["risk_debate_state"]
neu_debate = neu_result["risk_debate_state"] neu_debate = neu_result["risk_debate_state"]
merged_debate = { merged_debate = {
"aggressive_history": agg_debate.get("aggressive_history", ""), "aggressive_history": agg_debate.get("aggressive_history", ""),
"conservative_history": con_debate.get("conservative_history", ""), "conservative_history": con_debate.get("conservative_history", ""),
"neutral_history": neu_debate.get("neutral_history", ""), "neutral_history": neu_debate.get("neutral_history", ""),
"history": agg_debate.get("aggressive_history", "") "history": agg_debate.get("aggressive_history", "")
+ "\n" + "\n"
+ con_debate.get("conservative_history", "") + con_debate.get("conservative_history", "")
+ "\n" + "\n"
+ neu_debate.get("neutral_history", ""), + neu_debate.get("neutral_history", ""),
"latest_speaker": "Neutral", "latest_speaker": "Neutral",
"current_aggressive_response": agg_debate.get( "current_aggressive_response": agg_debate.get(
"current_aggressive_response", "" "current_aggressive_response", ""
), ),
"current_conservative_response": con_debate.get( "current_conservative_response": con_debate.get(
"current_conservative_response", "" "current_conservative_response", ""
), ),
"current_neutral_response": neu_debate.get( "current_neutral_response": neu_debate.get(
"current_neutral_response", "" "current_neutral_response", ""
), ),
"judge_decision": "", "judge_decision": "",
"count": 3, "count": 3,
} }
return {"risk_debate_state": merged_debate} return {"risk_debate_state": merged_debate}
return parallel_risk_node return parallel_risk_node

View File

@ -1,57 +1,57 @@
# TradingAgents/graph/propagation.py # TradingAgents/graph/propagation.py
from typing import Dict, Any, List, Optional from typing import Dict, Any, List, Optional
from tradingagents.agents.utils.agent_states import ( from tradingagents.agents.utils.agent_states import (
AgentState, AgentState,
InvestDebateState, InvestDebateState,
RiskDebateState, RiskDebateState,
) )
class Propagator: class Propagator:
"""Handles state initialization and propagation through the graph.""" """Handles state initialization and propagation through the graph."""
def __init__(self, max_recur_limit=100): def __init__(self, max_recur_limit=100):
"""Initialize with configuration parameters.""" """Initialize with configuration parameters."""
self.max_recur_limit = max_recur_limit self.max_recur_limit = max_recur_limit
def create_initial_state( def create_initial_state(
self, company_name: str, trade_date: str self, company_name: str, trade_date: str
) -> Dict[str, Any]: ) -> Dict[str, Any]:
"""Create the initial state for the agent graph.""" """Create the initial state for the agent graph."""
return { return {
"messages": [("human", company_name)], "messages": [("human", company_name)],
"company_of_interest": company_name, "company_of_interest": company_name,
"trade_date": str(trade_date), "trade_date": str(trade_date),
"investment_debate_state": InvestDebateState( "investment_debate_state": InvestDebateState(
{"history": "", "current_response": "", "count": 0} {"history": "", "current_response": "", "count": 0}
), ),
"risk_debate_state": RiskDebateState( "risk_debate_state": RiskDebateState(
{ {
"history": "", "history": "",
"current_aggressive_response": "", "current_aggressive_response": "",
"current_conservative_response": "", "current_conservative_response": "",
"current_neutral_response": "", "current_neutral_response": "",
"count": 0, "count": 0,
} }
), ),
"market_report": "", "market_report": "",
"fundamentals_report": "", "fundamentals_report": "",
"sentiment_report": "", "sentiment_report": "",
"news_report": "", "news_report": "",
} }
def get_graph_args(self, callbacks: Optional[List] = None) -> Dict[str, Any]: def get_graph_args(self, callbacks: Optional[List] = None) -> Dict[str, Any]:
"""Get arguments for the graph invocation. """Get arguments for the graph invocation.
Args: Args:
callbacks: Optional list of callback handlers for tool execution tracking. callbacks: Optional list of callback handlers for tool execution tracking.
Note: LLM callbacks are handled separately via LLM constructor. Note: LLM callbacks are handled separately via LLM constructor.
""" """
config = {"recursion_limit": self.max_recur_limit} config = {"recursion_limit": self.max_recur_limit}
if callbacks: if callbacks:
config["callbacks"] = callbacks config["callbacks"] = callbacks
return { return {
"stream_mode": "values", "stream_mode": "values",
"config": config, "config": config,
} }

View File

@ -1,121 +1,121 @@
# TradingAgents/graph/reflection.py # TradingAgents/graph/reflection.py
from typing import Dict, Any from typing import Dict, Any
from langchain_openai import ChatOpenAI from langchain_openai import ChatOpenAI
class Reflector: class Reflector:
"""Handles reflection on decisions and updating memory.""" """Handles reflection on decisions and updating memory."""
def __init__(self, quick_thinking_llm: ChatOpenAI): def __init__(self, quick_thinking_llm: ChatOpenAI):
"""Initialize the reflector with an LLM.""" """Initialize the reflector with an LLM."""
self.quick_thinking_llm = quick_thinking_llm self.quick_thinking_llm = quick_thinking_llm
self.reflection_system_prompt = self._get_reflection_prompt() self.reflection_system_prompt = self._get_reflection_prompt()
def _get_reflection_prompt(self) -> str: def _get_reflection_prompt(self) -> str:
"""Get the system prompt for reflection.""" """Get the system prompt for reflection."""
return """ return """
You are an expert financial analyst tasked with reviewing trading decisions/analysis and providing a comprehensive, step-by-step analysis. You are an expert financial analyst tasked with reviewing trading decisions/analysis and providing a comprehensive, step-by-step analysis.
Your goal is to deliver detailed insights into investment decisions and highlight opportunities for improvement, adhering strictly to the following guidelines: Your goal is to deliver detailed insights into investment decisions and highlight opportunities for improvement, adhering strictly to the following guidelines:
1. Reasoning: 1. Reasoning:
- For each trading decision, determine whether it was correct or incorrect. A correct decision results in an increase in returns, while an incorrect decision does the opposite. - For each trading decision, determine whether it was correct or incorrect. A correct decision results in an increase in returns, while an incorrect decision does the opposite.
- Analyze the contributing factors to each success or mistake. Consider: - Analyze the contributing factors to each success or mistake. Consider:
- Market intelligence. - Market intelligence.
- Technical indicators. - Technical indicators.
- Technical signals. - Technical signals.
- Price movement analysis. - Price movement analysis.
- Overall market data analysis - Overall market data analysis
- News analysis. - News analysis.
- Social media and sentiment analysis. - Social media and sentiment analysis.
- Fundamental data analysis. - Fundamental data analysis.
- Weight the importance of each factor in the decision-making process. - Weight the importance of each factor in the decision-making process.
2. Improvement: 2. Improvement:
- For any incorrect decisions, propose revisions to maximize returns. - For any incorrect decisions, propose revisions to maximize returns.
- Provide a detailed list of corrective actions or improvements, including specific recommendations (e.g., changing a decision from HOLD to BUY on a particular date). - Provide a detailed list of corrective actions or improvements, including specific recommendations (e.g., changing a decision from HOLD to BUY on a particular date).
3. Summary: 3. Summary:
- Summarize the lessons learned from the successes and mistakes. - Summarize the lessons learned from the successes and mistakes.
- Highlight how these lessons can be adapted for future trading scenarios and draw connections between similar situations to apply the knowledge gained. - Highlight how these lessons can be adapted for future trading scenarios and draw connections between similar situations to apply the knowledge gained.
4. Query: 4. Query:
- Extract key insights from the summary into a concise sentence of no more than 1000 tokens. - Extract key insights from the summary into a concise sentence of no more than 1000 tokens.
- Ensure the condensed sentence captures the essence of the lessons and reasoning for easy reference. - Ensure the condensed sentence captures the essence of the lessons and reasoning for easy reference.
Adhere strictly to these instructions, and ensure your output is detailed, accurate, and actionable. You will also be given objective descriptions of the market from a price movements, technical indicator, news, and sentiment perspective to provide more context for your analysis. Adhere strictly to these instructions, and ensure your output is detailed, accurate, and actionable. You will also be given objective descriptions of the market from a price movements, technical indicator, news, and sentiment perspective to provide more context for your analysis.
""" """
def _extract_current_situation(self, current_state: Dict[str, Any]) -> str: def _extract_current_situation(self, current_state: Dict[str, Any]) -> str:
"""Extract the current market situation from the state.""" """Extract the current market situation from the state."""
curr_market_report = current_state["market_report"] curr_market_report = current_state["market_report"]
curr_sentiment_report = current_state["sentiment_report"] curr_sentiment_report = current_state["sentiment_report"]
curr_news_report = current_state["news_report"] curr_news_report = current_state["news_report"]
curr_fundamentals_report = current_state["fundamentals_report"] curr_fundamentals_report = current_state["fundamentals_report"]
return f"{curr_market_report}\n\n{curr_sentiment_report}\n\n{curr_news_report}\n\n{curr_fundamentals_report}" return f"{curr_market_report}\n\n{curr_sentiment_report}\n\n{curr_news_report}\n\n{curr_fundamentals_report}"
def _reflect_on_component( def _reflect_on_component(
self, component_type: str, report: str, situation: str, returns_losses self, component_type: str, report: str, situation: str, returns_losses
) -> str: ) -> str:
"""Generate reflection for a component.""" """Generate reflection for a component."""
messages = [ messages = [
("system", self.reflection_system_prompt), ("system", self.reflection_system_prompt),
( (
"human", "human",
f"Returns: {returns_losses}\n\nAnalysis/Decision: {report}\n\nObjective Market Reports for Reference: {situation}", f"Returns: {returns_losses}\n\nAnalysis/Decision: {report}\n\nObjective Market Reports for Reference: {situation}",
), ),
] ]
result = self.quick_thinking_llm.invoke(messages).content result = self.quick_thinking_llm.invoke(messages).content
return result return result
def reflect_bull_researcher(self, current_state, returns_losses, bull_memory): def reflect_bull_researcher(self, current_state, returns_losses, bull_memory):
"""Reflect on bull researcher's analysis and update memory.""" """Reflect on bull researcher's analysis and update memory."""
situation = self._extract_current_situation(current_state) situation = self._extract_current_situation(current_state)
bull_debate_history = current_state["investment_debate_state"]["bull_history"] bull_debate_history = current_state["investment_debate_state"]["bull_history"]
result = self._reflect_on_component( result = self._reflect_on_component(
"BULL", bull_debate_history, situation, returns_losses "BULL", bull_debate_history, situation, returns_losses
) )
bull_memory.add_situations([(situation, result)]) bull_memory.add_situations([(situation, result)])
def reflect_bear_researcher(self, current_state, returns_losses, bear_memory): def reflect_bear_researcher(self, current_state, returns_losses, bear_memory):
"""Reflect on bear researcher's analysis and update memory.""" """Reflect on bear researcher's analysis and update memory."""
situation = self._extract_current_situation(current_state) situation = self._extract_current_situation(current_state)
bear_debate_history = current_state["investment_debate_state"]["bear_history"] bear_debate_history = current_state["investment_debate_state"]["bear_history"]
result = self._reflect_on_component( result = self._reflect_on_component(
"BEAR", bear_debate_history, situation, returns_losses "BEAR", bear_debate_history, situation, returns_losses
) )
bear_memory.add_situations([(situation, result)]) bear_memory.add_situations([(situation, result)])
def reflect_trader(self, current_state, returns_losses, trader_memory): def reflect_trader(self, current_state, returns_losses, trader_memory):
"""Reflect on trader's decision and update memory.""" """Reflect on trader's decision and update memory."""
situation = self._extract_current_situation(current_state) situation = self._extract_current_situation(current_state)
trader_decision = current_state["trader_investment_plan"] trader_decision = current_state["trader_investment_plan"]
result = self._reflect_on_component( result = self._reflect_on_component(
"TRADER", trader_decision, situation, returns_losses "TRADER", trader_decision, situation, returns_losses
) )
trader_memory.add_situations([(situation, result)]) trader_memory.add_situations([(situation, result)])
def reflect_invest_judge(self, current_state, returns_losses, invest_judge_memory): def reflect_invest_judge(self, current_state, returns_losses, invest_judge_memory):
"""Reflect on investment judge's decision and update memory.""" """Reflect on investment judge's decision and update memory."""
situation = self._extract_current_situation(current_state) situation = self._extract_current_situation(current_state)
judge_decision = current_state["investment_debate_state"]["judge_decision"] judge_decision = current_state["investment_debate_state"]["judge_decision"]
result = self._reflect_on_component( result = self._reflect_on_component(
"INVEST JUDGE", judge_decision, situation, returns_losses "INVEST JUDGE", judge_decision, situation, returns_losses
) )
invest_judge_memory.add_situations([(situation, result)]) invest_judge_memory.add_situations([(situation, result)])
def reflect_risk_manager(self, current_state, returns_losses, risk_manager_memory): def reflect_risk_manager(self, current_state, returns_losses, risk_manager_memory):
"""Reflect on risk manager's decision and update memory.""" """Reflect on risk manager's decision and update memory."""
situation = self._extract_current_situation(current_state) situation = self._extract_current_situation(current_state)
judge_decision = current_state["risk_debate_state"]["judge_decision"] judge_decision = current_state["risk_debate_state"]["judge_decision"]
result = self._reflect_on_component( result = self._reflect_on_component(
"RISK JUDGE", judge_decision, situation, returns_losses "RISK JUDGE", judge_decision, situation, returns_losses
) )
risk_manager_memory.add_situations([(situation, result)]) risk_manager_memory.add_situations([(situation, result)])

View File

@ -1,208 +1,208 @@
"""Graph setup for the structured equity ranking pipeline. """Graph setup for the structured equity ranking pipeline.
Pipeline stages: Pipeline stages:
START Validation [veto gate] Tier 1 (Macro+Liquidity parallel) START Validation [veto gate] Tier 1 (Macro+Liquidity parallel)
Tier 2 (8 agents parallel) Scoring (Archetype+MasterScore) Tier 2 (8 agents parallel) Scoring (Archetype+MasterScore)
Tier 3 (Bull+Bear parallel Debate Risk FinalDecision) Tier 3 (Bull+Bear parallel Debate Risk FinalDecision)
END END
""" """
from __future__ import annotations from __future__ import annotations
import asyncio import asyncio
import logging import logging
import time import time
from typing import Any, Dict, List from typing import Any, Dict, List
from langgraph.graph import END, START, StateGraph from langgraph.graph import END, START, StateGraph
from tradingagents.agents.utils.agent_states import PipelineState from tradingagents.agents.utils.agent_states import PipelineState
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
class StructuredGraphSetup: class StructuredGraphSetup:
"""Builds the structured equity ranking LangGraph.""" """Builds the structured equity ranking LangGraph."""
def __init__(self, quick_llm, deep_llm): def __init__(self, quick_llm, deep_llm):
self.quick_llm = quick_llm self.quick_llm = quick_llm
self.deep_llm = deep_llm self.deep_llm = deep_llm
def setup_graph(self): def setup_graph(self):
"""Build and compile the structured pipeline graph.""" """Build and compile the structured pipeline graph."""
from tradingagents.agents.structured import ( from tradingagents.agents.structured import (
create_archetype_node, create_archetype_node,
create_backlog_node, create_backlog_node,
create_bear_case_node, create_bear_case_node,
create_bull_case_node, create_bull_case_node,
create_business_quality_node, create_business_quality_node,
create_crowding_node, create_crowding_node,
create_debate_node, create_debate_node,
create_earnings_revisions_node, create_earnings_revisions_node,
create_entry_timing_node, create_entry_timing_node,
create_final_decision_node, create_final_decision_node,
create_institutional_flow_node, create_institutional_flow_node,
create_liquidity_node, create_liquidity_node,
create_macro_node, create_macro_node,
create_position_replacement_node, create_position_replacement_node,
create_risk_node, create_risk_node,
create_scoring_node, create_scoring_node,
create_sector_rotation_node, create_sector_rotation_node,
create_theme_substitution_node, create_theme_substitution_node,
create_validation_node, create_validation_node,
create_valuation_node, create_valuation_node,
) )
# Create node functions # Create node functions
# Tier 1: cheap model (or no LLM for validation) # Tier 1: cheap model (or no LLM for validation)
validation_fn = create_validation_node() validation_fn = create_validation_node()
macro_fn = create_macro_node(self.quick_llm) macro_fn = create_macro_node(self.quick_llm)
liquidity_fn = create_liquidity_node(self.quick_llm) liquidity_fn = create_liquidity_node(self.quick_llm)
# Tier 2: cheap model for analysis # Tier 2: cheap model for analysis
bq_fn = create_business_quality_node(self.quick_llm) bq_fn = create_business_quality_node(self.quick_llm)
inst_fn = create_institutional_flow_node(self.quick_llm) inst_fn = create_institutional_flow_node(self.quick_llm)
val_fn = create_valuation_node(self.quick_llm) val_fn = create_valuation_node(self.quick_llm)
et_fn = create_entry_timing_node(self.quick_llm) et_fn = create_entry_timing_node(self.quick_llm)
er_fn = create_earnings_revisions_node(self.quick_llm) er_fn = create_earnings_revisions_node(self.quick_llm)
sr_fn = create_sector_rotation_node(self.quick_llm) sr_fn = create_sector_rotation_node(self.quick_llm)
bl_fn = create_backlog_node(self.quick_llm) bl_fn = create_backlog_node(self.quick_llm)
cr_fn = create_crowding_node(self.quick_llm) cr_fn = create_crowding_node(self.quick_llm)
arch_fn = create_archetype_node(self.quick_llm) arch_fn = create_archetype_node(self.quick_llm)
score_fn = create_scoring_node() score_fn = create_scoring_node()
# Portfolio-level: deep model for theme/replacement analysis # Portfolio-level: deep model for theme/replacement analysis
theme_fn = create_theme_substitution_node(self.deep_llm) theme_fn = create_theme_substitution_node(self.deep_llm)
replace_fn = create_position_replacement_node(self.deep_llm) replace_fn = create_position_replacement_node(self.deep_llm)
# Tier 3: deep model for reasoning/debate # Tier 3: deep model for reasoning/debate
bull_fn = create_bull_case_node(self.deep_llm) bull_fn = create_bull_case_node(self.deep_llm)
bear_fn = create_bear_case_node(self.deep_llm) bear_fn = create_bear_case_node(self.deep_llm)
debate_fn = create_debate_node(self.deep_llm) debate_fn = create_debate_node(self.deep_llm)
risk_fn = create_risk_node(self.deep_llm) risk_fn = create_risk_node(self.deep_llm)
final_fn = create_final_decision_node(self.deep_llm) final_fn = create_final_decision_node(self.deep_llm)
# Build parallel wrapper nodes # Build parallel wrapper nodes
parallel_tier1 = _create_parallel_node( parallel_tier1 = _create_parallel_node(
[("macro", macro_fn), ("liquidity", liquidity_fn)], [("macro", macro_fn), ("liquidity", liquidity_fn)],
"Tier 1", "Tier 1",
) )
parallel_tier2 = _create_parallel_node( parallel_tier2 = _create_parallel_node(
[ [
("business_quality", bq_fn), ("business_quality", bq_fn),
("institutional_flow", inst_fn), ("institutional_flow", inst_fn),
("valuation", val_fn), ("valuation", val_fn),
("entry_timing", et_fn), ("entry_timing", et_fn),
("earnings_revisions", er_fn), ("earnings_revisions", er_fn),
("sector_rotation", sr_fn), ("sector_rotation", sr_fn),
("backlog", bl_fn), ("backlog", bl_fn),
("crowding", cr_fn), ("crowding", cr_fn),
], ],
"Tier 2", "Tier 2",
) )
parallel_bull_bear = _create_parallel_node( parallel_bull_bear = _create_parallel_node(
[("bull_case", bull_fn), ("bear_case", bear_fn)], [("bull_case", bull_fn), ("bear_case", bear_fn)],
"Bull/Bear", "Bull/Bear",
) )
# Archetype + Score combined node # Archetype + Score combined node
def archetype_and_score(state): def archetype_and_score(state):
arch_result = arch_fn(state) arch_result = arch_fn(state)
merged = {**state, **arch_result} merged = {**state, **arch_result}
score_result = score_fn(merged) score_result = score_fn(merged)
return {**arch_result, **score_result} return {**arch_result, **score_result}
# Theme + Replacement combined node (sequential: theme feeds replacement) # Theme + Replacement combined node (sequential: theme feeds replacement)
def theme_and_replacement(state): def theme_and_replacement(state):
theme_result = theme_fn(state) theme_result = theme_fn(state)
merged = {**state, **theme_result} merged = {**state, **theme_result}
replace_result = replace_fn(merged) replace_result = replace_fn(merged)
return {**theme_result, **replace_result} return {**theme_result, **replace_result}
# Risk + Final Decision combined node # Risk + Final Decision combined node
def risk_and_decision(state): def risk_and_decision(state):
risk_result = risk_fn(state) risk_result = risk_fn(state)
merged = {**state, **risk_result} merged = {**state, **risk_result}
final_result = final_fn(merged) final_result = final_fn(merged)
return {**risk_result, **final_result} return {**risk_result, **final_result}
# Build graph # Build graph
workflow = StateGraph(PipelineState) workflow = StateGraph(PipelineState)
workflow.add_node("Validation", validation_fn) workflow.add_node("Validation", validation_fn)
workflow.add_node("Tier 1 Analysis", parallel_tier1) workflow.add_node("Tier 1 Analysis", parallel_tier1)
workflow.add_node("Tier 2 Analysis", parallel_tier2) workflow.add_node("Tier 2 Analysis", parallel_tier2)
workflow.add_node("Scoring", archetype_and_score) workflow.add_node("Scoring", archetype_and_score)
workflow.add_node("Portfolio Analysis", theme_and_replacement) workflow.add_node("Portfolio Analysis", theme_and_replacement)
workflow.add_node("Debate", parallel_bull_bear) workflow.add_node("Debate", parallel_bull_bear)
workflow.add_node("Debate Referee", debate_fn) workflow.add_node("Debate Referee", debate_fn)
workflow.add_node("Decision", risk_and_decision) workflow.add_node("Decision", risk_and_decision)
# Edges # Edges
workflow.add_edge(START, "Validation") workflow.add_edge(START, "Validation")
workflow.add_conditional_edges( workflow.add_conditional_edges(
"Validation", "Validation",
_veto_gate, _veto_gate,
{END: END, "continue": "Tier 1 Analysis"}, {END: END, "continue": "Tier 1 Analysis"},
) )
workflow.add_edge("Tier 1 Analysis", "Tier 2 Analysis") workflow.add_edge("Tier 1 Analysis", "Tier 2 Analysis")
workflow.add_edge("Tier 2 Analysis", "Scoring") workflow.add_edge("Tier 2 Analysis", "Scoring")
workflow.add_edge("Scoring", "Portfolio Analysis") workflow.add_edge("Scoring", "Portfolio Analysis")
workflow.add_edge("Portfolio Analysis", "Debate") workflow.add_edge("Portfolio Analysis", "Debate")
workflow.add_edge("Debate", "Debate Referee") workflow.add_edge("Debate", "Debate Referee")
workflow.add_edge("Debate Referee", "Decision") workflow.add_edge("Debate Referee", "Decision")
workflow.add_edge("Decision", END) workflow.add_edge("Decision", END)
return workflow.compile() return workflow.compile()
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# Helpers # Helpers
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
def _veto_gate(state: Dict[str, Any]) -> str: def _veto_gate(state: Dict[str, Any]) -> str:
"""Check if validation resulted in a hard veto.""" """Check if validation resulted in a hard veto."""
if state.get("hard_veto"): if state.get("hard_veto"):
return END return END
validation = state.get("validation") or {} validation = state.get("validation") or {}
if validation.get("veto"): if validation.get("veto"):
return END return END
return "continue" return "continue"
def _create_parallel_node(agent_fns: List[tuple], label: str): def _create_parallel_node(agent_fns: List[tuple], label: str):
"""Create an async node that runs multiple agent functions in parallel. """Create an async node that runs multiple agent functions in parallel.
Args: Args:
agent_fns: List of (name, fn) tuples. agent_fns: List of (name, fn) tuples.
label: Label for logging. label: Label for logging.
""" """
async def parallel_node(state): async def parallel_node(state):
t0 = time.time() t0 = time.time()
async def run_one(name, fn): async def run_one(name, fn):
logger.debug("[%s] %s starting", label, name) logger.debug("[%s] %s starting", label, name)
result = await asyncio.to_thread(fn, state) result = await asyncio.to_thread(fn, state)
logger.debug("[%s] %s done (%.1fs)", label, name, time.time() - t0) logger.debug("[%s] %s done (%.1fs)", label, name, time.time() - t0)
return result return result
tasks = [run_one(name, fn) for name, fn in agent_fns] tasks = [run_one(name, fn) for name, fn in agent_fns]
results = await asyncio.gather(*tasks, return_exceptions=True) results = await asyncio.gather(*tasks, return_exceptions=True)
merged: Dict[str, Any] = {} merged: Dict[str, Any] = {}
all_flags: list = [] all_flags: list = []
for (name, _), result in zip(agent_fns, results): for (name, _), result in zip(agent_fns, results):
if isinstance(result, Exception): if isinstance(result, Exception):
logger.error("[%s] %s failed: %s", label, name, result) logger.error("[%s] %s failed: %s", label, name, result)
continue continue
flags = result.pop("global_flags", []) flags = result.pop("global_flags", [])
all_flags.extend(flags) all_flags.extend(flags)
merged.update(result) merged.update(result)
if all_flags: if all_flags:
merged["global_flags"] = all_flags merged["global_flags"] = all_flags
logger.info("[%s] completed in %.1fs", label, time.time() - t0) logger.info("[%s] completed in %.1fs", label, time.time() - t0)
return merged return merged
return parallel_node return parallel_node

View File

@ -1,31 +1,31 @@
# TradingAgents/graph/signal_processing.py # TradingAgents/graph/signal_processing.py
from langchain_openai import ChatOpenAI from langchain_openai import ChatOpenAI
class SignalProcessor: class SignalProcessor:
"""Processes trading signals to extract actionable decisions.""" """Processes trading signals to extract actionable decisions."""
def __init__(self, quick_thinking_llm: ChatOpenAI): def __init__(self, quick_thinking_llm: ChatOpenAI):
"""Initialize with an LLM for processing.""" """Initialize with an LLM for processing."""
self.quick_thinking_llm = quick_thinking_llm self.quick_thinking_llm = quick_thinking_llm
def process_signal(self, full_signal: str) -> str: def process_signal(self, full_signal: str) -> str:
""" """
Process a full trading signal to extract the core decision. Process a full trading signal to extract the core decision.
Args: Args:
full_signal: Complete trading signal text full_signal: Complete trading signal text
Returns: Returns:
Extracted decision (BUY, SELL, or HOLD) Extracted decision (BUY, SELL, or HOLD)
""" """
messages = [ messages = [
( (
"system", "system",
"You are an efficient assistant designed to analyze paragraphs or financial reports provided by a group of analysts. Your task is to extract the investment decision: SELL, BUY, or HOLD. Provide only the extracted decision (SELL, BUY, or HOLD) as your output, without adding any additional text or information.", "You are an efficient assistant designed to analyze paragraphs or financial reports provided by a group of analysts. Your task is to extract the investment decision: SELL, BUY, or HOLD. Provide only the extracted decision (SELL, BUY, or HOLD) as your output, without adding any additional text or information.",
), ),
("human", full_signal), ("human", full_signal),
] ]
return self.quick_thinking_llm.invoke(messages).content return self.quick_thinking_llm.invoke(messages).content

View File

@ -1,198 +1,198 @@
"""Main orchestrator for the structured equity ranking engine. """Main orchestrator for the structured equity ranking engine.
Replaces the old TradingAgentsGraph with a tiered Pydantic-based pipeline. Replaces the old TradingAgentsGraph with a tiered Pydantic-based pipeline.
""" """
from __future__ import annotations from __future__ import annotations
import json import json
import os import os
import logging import logging
from datetime import date from datetime import date
from pathlib import Path from pathlib import Path
from typing import Any, Dict, List, Optional from typing import Any, Dict, List, Optional
from tradingagents.default_config import DEFAULT_CONFIG from tradingagents.default_config import DEFAULT_CONFIG
from tradingagents.llm_clients import create_llm_client from tradingagents.llm_clients import create_llm_client
from tradingagents.dataflows.config import set_config from tradingagents.dataflows.config import set_config
from .setup import StructuredGraphSetup from .setup import StructuredGraphSetup
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
class TradingAgentsGraph: class TradingAgentsGraph:
"""Structured equity ranking engine built on LangGraph.""" """Structured equity ranking engine built on LangGraph."""
def __init__( def __init__(
self, self,
selected_analysts=None, # ignored — all agents run in structured pipeline selected_analysts=None, # ignored — all agents run in structured pipeline
debug=False, debug=False,
config: Optional[Dict[str, Any]] = None, config: Optional[Dict[str, Any]] = None,
callbacks: Optional[List] = None, callbacks: Optional[List] = None,
): ):
self.debug = debug self.debug = debug
self.config = config or DEFAULT_CONFIG self.config = config or DEFAULT_CONFIG
self.callbacks = callbacks or [] self.callbacks = callbacks or []
set_config(self.config) set_config(self.config)
os.makedirs( os.makedirs(
os.path.join(self.config["project_dir"], "dataflows/data_cache"), os.path.join(self.config["project_dir"], "dataflows/data_cache"),
exist_ok=True, exist_ok=True,
) )
# Initialize LLMs # Initialize LLMs
llm_kwargs = self._get_provider_kwargs() llm_kwargs = self._get_provider_kwargs()
if self.callbacks: if self.callbacks:
llm_kwargs["callbacks"] = self.callbacks llm_kwargs["callbacks"] = self.callbacks
deep_client = create_llm_client( deep_client = create_llm_client(
provider=self.config["llm_provider"], provider=self.config["llm_provider"],
model=self.config["deep_think_llm"], model=self.config["deep_think_llm"],
base_url=self.config.get("backend_url"), base_url=self.config.get("backend_url"),
**llm_kwargs, **llm_kwargs,
) )
quick_client = create_llm_client( quick_client = create_llm_client(
provider=self.config["llm_provider"], provider=self.config["llm_provider"],
model=self.config["quick_think_llm"], model=self.config["quick_think_llm"],
base_url=self.config.get("backend_url"), base_url=self.config.get("backend_url"),
**llm_kwargs, **llm_kwargs,
) )
self.deep_thinking_llm = deep_client.get_llm() self.deep_thinking_llm = deep_client.get_llm()
self.quick_thinking_llm = quick_client.get_llm() self.quick_thinking_llm = quick_client.get_llm()
# Build the structured pipeline graph # Build the structured pipeline graph
graph_setup = StructuredGraphSetup( graph_setup = StructuredGraphSetup(
self.quick_thinking_llm, self.deep_thinking_llm self.quick_thinking_llm, self.deep_thinking_llm
) )
self.graph = graph_setup.setup_graph() self.graph = graph_setup.setup_graph()
# State tracking # State tracking
self.curr_state = None self.curr_state = None
self.ticker = None self.ticker = None
def _get_provider_kwargs(self) -> Dict[str, Any]: def _get_provider_kwargs(self) -> Dict[str, Any]:
kwargs = {} kwargs = {}
provider = self.config.get("llm_provider", "").lower() provider = self.config.get("llm_provider", "").lower()
if provider == "google": if provider == "google":
thinking_level = self.config.get("google_thinking_level") thinking_level = self.config.get("google_thinking_level")
if thinking_level: if thinking_level:
kwargs["thinking_level"] = thinking_level kwargs["thinking_level"] = thinking_level
elif provider == "openai": elif provider == "openai":
reasoning_effort = self.config.get("openai_reasoning_effort") reasoning_effort = self.config.get("openai_reasoning_effort")
if reasoning_effort: if reasoning_effort:
kwargs["reasoning_effort"] = reasoning_effort kwargs["reasoning_effort"] = reasoning_effort
return kwargs return kwargs
async def propagate(self, company_name: str, trade_date: str): async def propagate(self, company_name: str, trade_date: str):
"""Run the structured pipeline for a company (async — parallel nodes).""" """Run the structured pipeline for a company (async — parallel nodes)."""
import asyncio import asyncio
self.ticker = company_name self.ticker = company_name
init_state = self._create_initial_state(company_name, trade_date) init_state = self._create_initial_state(company_name, trade_date)
args = {"config": {"recursion_limit": 50}} args = {"config": {"recursion_limit": 50}}
if self.debug: if self.debug:
trace = [] trace = []
async for chunk in self.graph.astream(init_state, stream_mode="values", **args): async for chunk in self.graph.astream(init_state, stream_mode="values", **args):
trace.append(chunk) trace.append(chunk)
final_state = trace[-1] if trace else init_state final_state = trace[-1] if trace else init_state
else: else:
final_state = await self.graph.ainvoke(init_state, **args) final_state = await self.graph.ainvoke(init_state, **args)
self.curr_state = final_state self.curr_state = final_state
self._log_state(trade_date, final_state) self._log_state(trade_date, final_state)
decision = final_state.get("final_decision") or {} decision = final_state.get("final_decision") or {}
signal = decision.get("action", "AVOID") signal = decision.get("action", "AVOID")
return final_state, signal return final_state, signal
def _create_initial_state(self, ticker: str, trade_date: str) -> Dict[str, Any]: def _create_initial_state(self, ticker: str, trade_date: str) -> Dict[str, Any]:
return { return {
"ticker": ticker.upper(), "ticker": ticker.upper(),
"trade_date": str(trade_date), "trade_date": str(trade_date),
"validation": None, "validation": None,
"company_card": None, "company_card": None,
"macro": None, "macro": None,
"liquidity": None, "liquidity": None,
"sector_rotation": None, "sector_rotation": None,
"business_quality": None, "business_quality": None,
"institutional_flow": None, "institutional_flow": None,
"valuation": None, "valuation": None,
"entry_timing": None, "entry_timing": None,
"earnings_revisions": None, "earnings_revisions": None,
"backlog": None, "backlog": None,
"crowding": None, "crowding": None,
"archetype": None, "archetype": None,
"master_score": None, "master_score": None,
"adjusted_score": None, "adjusted_score": None,
"position_role": None, "position_role": None,
"theme_substitution": None, "theme_substitution": None,
"position_replacement": None, "position_replacement": None,
"bull_case": None, "bull_case": None,
"bear_case": None, "bear_case": None,
"debate": None, "debate": None,
"risk": None, "risk": None,
"final_decision": None, "final_decision": None,
"hard_veto": False, "hard_veto": False,
"hard_veto_reason": None, "hard_veto_reason": None,
"global_flags": [], "global_flags": [],
} }
def _log_state(self, trade_date: str, state: Dict[str, Any]): def _log_state(self, trade_date: str, state: Dict[str, Any]):
"""Log the final state to JSON.""" """Log the final state to JSON."""
log_data = { log_data = {
"ticker": state.get("ticker"), "ticker": state.get("ticker"),
"trade_date": str(trade_date), "trade_date": str(trade_date),
"master_score": state.get("master_score"), "master_score": state.get("master_score"),
"adjusted_score": state.get("adjusted_score"), "adjusted_score": state.get("adjusted_score"),
"position_role": state.get("position_role"), "position_role": state.get("position_role"),
"hard_veto": state.get("hard_veto"), "hard_veto": state.get("hard_veto"),
"validation": state.get("validation"), "validation": state.get("validation"),
"company_card": state.get("company_card"), "company_card": state.get("company_card"),
"macro": state.get("macro"), "macro": state.get("macro"),
"liquidity": state.get("liquidity"), "liquidity": state.get("liquidity"),
"business_quality": state.get("business_quality"), "business_quality": state.get("business_quality"),
"institutional_flow": state.get("institutional_flow"), "institutional_flow": state.get("institutional_flow"),
"valuation": state.get("valuation"), "valuation": state.get("valuation"),
"entry_timing": state.get("entry_timing"), "entry_timing": state.get("entry_timing"),
"earnings_revisions": state.get("earnings_revisions"), "earnings_revisions": state.get("earnings_revisions"),
"sector_rotation": state.get("sector_rotation"), "sector_rotation": state.get("sector_rotation"),
"backlog": state.get("backlog"), "backlog": state.get("backlog"),
"crowding": state.get("crowding"), "crowding": state.get("crowding"),
"archetype": state.get("archetype"), "archetype": state.get("archetype"),
"theme_substitution": state.get("theme_substitution"), "theme_substitution": state.get("theme_substitution"),
"position_replacement": state.get("position_replacement"), "position_replacement": state.get("position_replacement"),
"bull_case": state.get("bull_case"), "bull_case": state.get("bull_case"),
"bear_case": state.get("bear_case"), "bear_case": state.get("bear_case"),
"debate": state.get("debate"), "debate": state.get("debate"),
"risk": state.get("risk"), "risk": state.get("risk"),
"final_decision": state.get("final_decision"), "final_decision": state.get("final_decision"),
} }
directory = Path(f"eval_results/{self.ticker}/StructuredPipeline_logs/") directory = Path(f"eval_results/{self.ticker}/StructuredPipeline_logs/")
directory.mkdir(parents=True, exist_ok=True) directory.mkdir(parents=True, exist_ok=True)
filepath = directory / f"analysis_{trade_date}.json" filepath = directory / f"analysis_{trade_date}.json"
with open(filepath, "w") as f: with open(filepath, "w") as f:
json.dump(log_data, f, indent=2, default=str) json.dump(log_data, f, indent=2, default=str)
logger.info("State logged to %s", filepath) logger.info("State logged to %s", filepath)
def process_signal(self, decision_text: str) -> str: def process_signal(self, decision_text: str) -> str:
"""Extract signal from decision text (legacy compatibility).""" """Extract signal from decision text (legacy compatibility)."""
if isinstance(decision_text, dict): if isinstance(decision_text, dict):
return decision_text.get("action", "AVOID") return decision_text.get("action", "AVOID")
text = str(decision_text).upper() text = str(decision_text).upper()
if "BUY" in text: if "BUY" in text:
return "BUY" return "BUY"
if "SELL" in text: if "SELL" in text:
return "SELL" return "SELL"
if "HOLD" in text: if "HOLD" in text:
return "HOLD" return "HOLD"
return "AVOID" return "AVOID"
def reflect_and_remember(self, returns_losses): def reflect_and_remember(self, returns_losses):
"""No-op for structured pipeline (no BM25 memory).""" """No-op for structured pipeline (no BM25 memory)."""
pass pass

View File

@ -1,24 +1,24 @@
# LLM Clients - Consistency Improvements # LLM Clients - Consistency Improvements
## Issues to Fix ## Issues to Fix
### 1. `validate_model()` is never called ### 1. `validate_model()` is never called
- Add validation call in `get_llm()` with warning (not error) for unknown models - Add validation call in `get_llm()` with warning (not error) for unknown models
### 2. Inconsistent parameter handling ### 2. Inconsistent parameter handling
| Client | API Key Param | Special Params | | Client | API Key Param | Special Params |
|--------|---------------|----------------| |--------|---------------|----------------|
| OpenAI | `api_key` | `reasoning_effort` | | OpenAI | `api_key` | `reasoning_effort` |
| Anthropic | `api_key` | `thinking_config``thinking` | | Anthropic | `api_key` | `thinking_config``thinking` |
| Google | `google_api_key` | `thinking_budget` | | Google | `google_api_key` | `thinking_budget` |
**Fix:** Standardize with unified `api_key` that maps to provider-specific keys **Fix:** Standardize with unified `api_key` that maps to provider-specific keys
### 3. `base_url` accepted but ignored ### 3. `base_url` accepted but ignored
- `AnthropicClient`: accepts `base_url` but never uses it - `AnthropicClient`: accepts `base_url` but never uses it
- `GoogleClient`: accepts `base_url` but never uses it (correct - Google doesn't support it) - `GoogleClient`: accepts `base_url` but never uses it (correct - Google doesn't support it)
**Fix:** Remove unused `base_url` from clients that don't support it **Fix:** Remove unused `base_url` from clients that don't support it
### 4. Update validators.py with models from CLI ### 4. Update validators.py with models from CLI
- Sync `VALID_MODELS` dict with CLI model options after Feature 2 is complete - Sync `VALID_MODELS` dict with CLI model options after Feature 2 is complete

View File

@ -1,4 +1,4 @@
from .base_client import BaseLLMClient from .base_client import BaseLLMClient
from .factory import create_llm_client from .factory import create_llm_client
__all__ = ["BaseLLMClient", "create_llm_client"] __all__ = ["BaseLLMClient", "create_llm_client"]

View File

@ -1,27 +1,27 @@
from typing import Any, Optional from typing import Any, Optional
from langchain_anthropic import ChatAnthropic from langchain_anthropic import ChatAnthropic
from .base_client import BaseLLMClient from .base_client import BaseLLMClient
from .validators import validate_model from .validators import validate_model
class AnthropicClient(BaseLLMClient): class AnthropicClient(BaseLLMClient):
"""Client for Anthropic Claude models.""" """Client for Anthropic Claude models."""
def __init__(self, model: str, base_url: Optional[str] = None, **kwargs): def __init__(self, model: str, base_url: Optional[str] = None, **kwargs):
super().__init__(model, base_url, **kwargs) super().__init__(model, base_url, **kwargs)
def get_llm(self) -> Any: def get_llm(self) -> Any:
"""Return configured ChatAnthropic instance.""" """Return configured ChatAnthropic instance."""
llm_kwargs = {"model": self.model} llm_kwargs = {"model": self.model}
for key in ("timeout", "max_retries", "api_key", "max_tokens", "callbacks"): for key in ("timeout", "max_retries", "api_key", "max_tokens", "callbacks"):
if key in self.kwargs: if key in self.kwargs:
llm_kwargs[key] = self.kwargs[key] llm_kwargs[key] = self.kwargs[key]
return ChatAnthropic(**llm_kwargs) return ChatAnthropic(**llm_kwargs)
def validate_model(self) -> bool: def validate_model(self) -> bool:
"""Validate model for Anthropic.""" """Validate model for Anthropic."""
return validate_model("anthropic", self.model) return validate_model("anthropic", self.model)

View File

@ -1,21 +1,21 @@
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from typing import Any, Optional from typing import Any, Optional
class BaseLLMClient(ABC): class BaseLLMClient(ABC):
"""Abstract base class for LLM clients.""" """Abstract base class for LLM clients."""
def __init__(self, model: str, base_url: Optional[str] = None, **kwargs): def __init__(self, model: str, base_url: Optional[str] = None, **kwargs):
self.model = model self.model = model
self.base_url = base_url self.base_url = base_url
self.kwargs = kwargs self.kwargs = kwargs
@abstractmethod @abstractmethod
def get_llm(self) -> Any: def get_llm(self) -> Any:
"""Return the configured LLM instance.""" """Return the configured LLM instance."""
pass pass
@abstractmethod @abstractmethod
def validate_model(self) -> bool: def validate_model(self) -> bool:
"""Validate that the model is supported by this client.""" """Validate that the model is supported by this client."""
pass pass

View File

@ -1,43 +1,43 @@
from typing import Optional from typing import Optional
from .base_client import BaseLLMClient from .base_client import BaseLLMClient
from .openai_client import OpenAIClient from .openai_client import OpenAIClient
from .anthropic_client import AnthropicClient from .anthropic_client import AnthropicClient
from .google_client import GoogleClient from .google_client import GoogleClient
def create_llm_client( def create_llm_client(
provider: str, provider: str,
model: str, model: str,
base_url: Optional[str] = None, base_url: Optional[str] = None,
**kwargs, **kwargs,
) -> BaseLLMClient: ) -> BaseLLMClient:
"""Create an LLM client for the specified provider. """Create an LLM client for the specified provider.
Args: Args:
provider: LLM provider (openai, anthropic, google, xai, ollama, openrouter) provider: LLM provider (openai, anthropic, google, xai, ollama, openrouter)
model: Model name/identifier model: Model name/identifier
base_url: Optional base URL for API endpoint base_url: Optional base URL for API endpoint
**kwargs: Additional provider-specific arguments **kwargs: Additional provider-specific arguments
Returns: Returns:
Configured BaseLLMClient instance Configured BaseLLMClient instance
Raises: Raises:
ValueError: If provider is not supported ValueError: If provider is not supported
""" """
provider_lower = provider.lower() provider_lower = provider.lower()
if provider_lower in ("openai", "ollama", "openrouter"): if provider_lower in ("openai", "ollama", "openrouter"):
return OpenAIClient(model, base_url, provider=provider_lower, **kwargs) return OpenAIClient(model, base_url, provider=provider_lower, **kwargs)
if provider_lower == "xai": if provider_lower == "xai":
return OpenAIClient(model, base_url, provider="xai", **kwargs) return OpenAIClient(model, base_url, provider="xai", **kwargs)
if provider_lower == "anthropic": if provider_lower == "anthropic":
return AnthropicClient(model, base_url, **kwargs) return AnthropicClient(model, base_url, **kwargs)
if provider_lower == "google": if provider_lower == "google":
return GoogleClient(model, base_url, **kwargs) return GoogleClient(model, base_url, **kwargs)
raise ValueError(f"Unsupported LLM provider: {provider}") raise ValueError(f"Unsupported LLM provider: {provider}")

View File

@ -1,65 +1,65 @@
from typing import Any, Optional from typing import Any, Optional
from langchain_google_genai import ChatGoogleGenerativeAI from langchain_google_genai import ChatGoogleGenerativeAI
from .base_client import BaseLLMClient from .base_client import BaseLLMClient
from .validators import validate_model from .validators import validate_model
class NormalizedChatGoogleGenerativeAI(ChatGoogleGenerativeAI): class NormalizedChatGoogleGenerativeAI(ChatGoogleGenerativeAI):
"""ChatGoogleGenerativeAI with normalized content output. """ChatGoogleGenerativeAI with normalized content output.
Gemini 3 models return content as list: [{'type': 'text', 'text': '...'}] Gemini 3 models return content as list: [{'type': 'text', 'text': '...'}]
This normalizes to string for consistent downstream handling. This normalizes to string for consistent downstream handling.
""" """
def _normalize_content(self, response): def _normalize_content(self, response):
content = response.content content = response.content
if isinstance(content, list): if isinstance(content, list):
texts = [ texts = [
item.get("text", "") if isinstance(item, dict) and item.get("type") == "text" item.get("text", "") if isinstance(item, dict) and item.get("type") == "text"
else item if isinstance(item, str) else "" else item if isinstance(item, str) else ""
for item in content for item in content
] ]
response.content = "\n".join(t for t in texts if t) response.content = "\n".join(t for t in texts if t)
return response return response
def invoke(self, input, config=None, **kwargs): def invoke(self, input, config=None, **kwargs):
return self._normalize_content(super().invoke(input, config, **kwargs)) return self._normalize_content(super().invoke(input, config, **kwargs))
class GoogleClient(BaseLLMClient): class GoogleClient(BaseLLMClient):
"""Client for Google Gemini models.""" """Client for Google Gemini models."""
def __init__(self, model: str, base_url: Optional[str] = None, **kwargs): def __init__(self, model: str, base_url: Optional[str] = None, **kwargs):
super().__init__(model, base_url, **kwargs) super().__init__(model, base_url, **kwargs)
def get_llm(self) -> Any: def get_llm(self) -> Any:
"""Return configured ChatGoogleGenerativeAI instance.""" """Return configured ChatGoogleGenerativeAI instance."""
llm_kwargs = {"model": self.model} llm_kwargs = {"model": self.model}
for key in ("timeout", "max_retries", "google_api_key", "callbacks"): for key in ("timeout", "max_retries", "google_api_key", "callbacks"):
if key in self.kwargs: if key in self.kwargs:
llm_kwargs[key] = self.kwargs[key] llm_kwargs[key] = self.kwargs[key]
# Map thinking_level to appropriate API param based on model # Map thinking_level to appropriate API param based on model
# Gemini 3 Pro: low, high # Gemini 3 Pro: low, high
# Gemini 3 Flash: minimal, low, medium, high # Gemini 3 Flash: minimal, low, medium, high
# Gemini 2.5: thinking_budget (0=disable, -1=dynamic) # Gemini 2.5: thinking_budget (0=disable, -1=dynamic)
thinking_level = self.kwargs.get("thinking_level") thinking_level = self.kwargs.get("thinking_level")
if thinking_level: if thinking_level:
model_lower = self.model.lower() model_lower = self.model.lower()
if "gemini-3" in model_lower: if "gemini-3" in model_lower:
# Gemini 3 Pro doesn't support "minimal", use "low" instead # Gemini 3 Pro doesn't support "minimal", use "low" instead
if "pro" in model_lower and thinking_level == "minimal": if "pro" in model_lower and thinking_level == "minimal":
thinking_level = "low" thinking_level = "low"
llm_kwargs["thinking_level"] = thinking_level llm_kwargs["thinking_level"] = thinking_level
else: else:
# Gemini 2.5: map to thinking_budget # Gemini 2.5: map to thinking_budget
llm_kwargs["thinking_budget"] = -1 if thinking_level == "high" else 0 llm_kwargs["thinking_budget"] = -1 if thinking_level == "high" else 0
return NormalizedChatGoogleGenerativeAI(**llm_kwargs) return NormalizedChatGoogleGenerativeAI(**llm_kwargs)
def validate_model(self) -> bool: def validate_model(self) -> bool:
"""Validate model for Google.""" """Validate model for Google."""
return validate_model("google", self.model) return validate_model("google", self.model)

View File

@ -1,75 +1,75 @@
import os import os
from typing import Any, Optional from typing import Any, Optional
from langchain_openai import ChatOpenAI from langchain_openai import ChatOpenAI
from .base_client import BaseLLMClient from .base_client import BaseLLMClient
from .validators import validate_model from .validators import validate_model
class UnifiedChatOpenAI(ChatOpenAI): class UnifiedChatOpenAI(ChatOpenAI):
"""ChatOpenAI subclass that strips incompatible params for certain models.""" """ChatOpenAI subclass that strips incompatible params for certain models."""
def __init__(self, **kwargs): def __init__(self, **kwargs):
model = kwargs.get("model", "") model = kwargs.get("model", "")
if self._is_reasoning_model(model): if self._is_reasoning_model(model):
kwargs.pop("temperature", None) kwargs.pop("temperature", None)
kwargs.pop("top_p", None) kwargs.pop("top_p", None)
super().__init__(**kwargs) super().__init__(**kwargs)
@staticmethod @staticmethod
def _is_reasoning_model(model: str) -> bool: def _is_reasoning_model(model: str) -> bool:
"""Check if model is a reasoning model that doesn't support temperature.""" """Check if model is a reasoning model that doesn't support temperature."""
model_lower = model.lower() model_lower = model.lower()
return ( return (
model_lower.startswith("o1") model_lower.startswith("o1")
or model_lower.startswith("o3") or model_lower.startswith("o3")
or "gpt-5" in model_lower or "gpt-5" in model_lower
) )
class OpenAIClient(BaseLLMClient): class OpenAIClient(BaseLLMClient):
"""Client for OpenAI, Ollama, OpenRouter, and xAI providers.""" """Client for OpenAI, Ollama, OpenRouter, and xAI providers."""
def __init__( def __init__(
self, self,
model: str, model: str,
base_url: Optional[str] = None, base_url: Optional[str] = None,
provider: str = "openai", provider: str = "openai",
**kwargs, **kwargs,
): ):
super().__init__(model, base_url, **kwargs) super().__init__(model, base_url, **kwargs)
self.provider = provider.lower() self.provider = provider.lower()
def get_llm(self) -> Any: def get_llm(self) -> Any:
"""Return configured ChatOpenAI instance.""" """Return configured ChatOpenAI instance."""
llm_kwargs = {"model": self.model} llm_kwargs = {"model": self.model}
if self.provider == "xai": if self.provider == "xai":
llm_kwargs["base_url"] = "https://api.x.ai/v1" llm_kwargs["base_url"] = "https://api.x.ai/v1"
api_key = os.environ.get("XAI_API_KEY") api_key = os.environ.get("XAI_API_KEY")
if api_key: if api_key:
llm_kwargs["api_key"] = api_key llm_kwargs["api_key"] = api_key
elif self.provider == "openrouter": elif self.provider == "openrouter":
llm_kwargs["base_url"] = "https://openrouter.ai/api/v1" llm_kwargs["base_url"] = "https://openrouter.ai/api/v1"
api_key = os.environ.get("OPENROUTER_API_KEY") api_key = os.environ.get("OPENROUTER_API_KEY")
if api_key: if api_key:
llm_kwargs["api_key"] = api_key llm_kwargs["api_key"] = api_key
elif self.provider == "ollama": elif self.provider == "ollama":
llm_kwargs["base_url"] = "http://localhost:11434/v1" llm_kwargs["base_url"] = "http://localhost:11434/v1"
llm_kwargs["api_key"] = "ollama" # Ollama doesn't require auth llm_kwargs["api_key"] = "ollama" # Ollama doesn't require auth
elif self.base_url: elif self.base_url:
llm_kwargs["base_url"] = self.base_url llm_kwargs["base_url"] = self.base_url
api_key = os.environ.get("OPENAI_API_KEY") api_key = os.environ.get("OPENAI_API_KEY")
if api_key: if api_key:
llm_kwargs["api_key"] = api_key llm_kwargs["api_key"] = api_key
for key in ("timeout", "max_retries", "reasoning_effort", "api_key", "callbacks"): for key in ("timeout", "max_retries", "reasoning_effort", "api_key", "callbacks"):
if key in self.kwargs: if key in self.kwargs:
llm_kwargs[key] = self.kwargs[key] llm_kwargs[key] = self.kwargs[key]
return UnifiedChatOpenAI(**llm_kwargs) return UnifiedChatOpenAI(**llm_kwargs)
def validate_model(self) -> bool: def validate_model(self) -> bool:
"""Validate model for the provider.""" """Validate model for the provider."""
return validate_model(self.provider, self.model) return validate_model(self.provider, self.model)

View File

@ -1,82 +1,82 @@
"""Model name validators for each provider. """Model name validators for each provider.
Only validates model names - does NOT enforce limits. Only validates model names - does NOT enforce limits.
Let LLM providers use their own defaults for unspecified params. Let LLM providers use their own defaults for unspecified params.
""" """
VALID_MODELS = { VALID_MODELS = {
"openai": [ "openai": [
# GPT-5 series (2025) # GPT-5 series (2025)
"gpt-5.2", "gpt-5.2",
"gpt-5.1", "gpt-5.1",
"gpt-5", "gpt-5",
"gpt-5-mini", "gpt-5-mini",
"gpt-5-nano", "gpt-5-nano",
# GPT-4.1 series (2025) # GPT-4.1 series (2025)
"gpt-4.1", "gpt-4.1",
"gpt-4.1-mini", "gpt-4.1-mini",
"gpt-4.1-nano", "gpt-4.1-nano",
# o-series reasoning models # o-series reasoning models
"o4-mini", "o4-mini",
"o3", "o3",
"o3-mini", "o3-mini",
"o1", "o1",
"o1-preview", "o1-preview",
# GPT-4o series (legacy but still supported) # GPT-4o series (legacy but still supported)
"gpt-4o", "gpt-4o",
"gpt-4o-mini", "gpt-4o-mini",
], ],
"anthropic": [ "anthropic": [
# Claude 4.5 series (2025) # Claude 4.5 series (2025)
"claude-opus-4-5", "claude-opus-4-5",
"claude-sonnet-4-5", "claude-sonnet-4-5",
"claude-haiku-4-5", "claude-haiku-4-5",
# Claude 4.x series # Claude 4.x series
"claude-opus-4-1-20250805", "claude-opus-4-1-20250805",
"claude-sonnet-4-20250514", "claude-sonnet-4-20250514",
# Claude 3.7 series # Claude 3.7 series
"claude-3-7-sonnet-20250219", "claude-3-7-sonnet-20250219",
# Claude 3.5 series (legacy) # Claude 3.5 series (legacy)
"claude-3-5-haiku-20241022", "claude-3-5-haiku-20241022",
"claude-3-5-sonnet-20241022", "claude-3-5-sonnet-20241022",
], ],
"google": [ "google": [
# Gemini 3 series (preview) # Gemini 3 series (preview)
"gemini-3-pro-preview", "gemini-3-pro-preview",
"gemini-3-flash-preview", "gemini-3-flash-preview",
# Gemini 2.5 series # Gemini 2.5 series
"gemini-2.5-pro", "gemini-2.5-pro",
"gemini-2.5-flash", "gemini-2.5-flash",
"gemini-2.5-flash-lite", "gemini-2.5-flash-lite",
# Gemini 2.0 series # Gemini 2.0 series
"gemini-2.0-flash", "gemini-2.0-flash",
"gemini-2.0-flash-lite", "gemini-2.0-flash-lite",
], ],
"xai": [ "xai": [
# Grok 4.1 series # Grok 4.1 series
"grok-4-1-fast", "grok-4-1-fast",
"grok-4-1-fast-reasoning", "grok-4-1-fast-reasoning",
"grok-4-1-fast-non-reasoning", "grok-4-1-fast-non-reasoning",
# Grok 4 series # Grok 4 series
"grok-4", "grok-4",
"grok-4-0709", "grok-4-0709",
"grok-4-fast-reasoning", "grok-4-fast-reasoning",
"grok-4-fast-non-reasoning", "grok-4-fast-non-reasoning",
], ],
} }
def validate_model(provider: str, model: str) -> bool: def validate_model(provider: str, model: str) -> bool:
"""Check if model name is valid for the given provider. """Check if model name is valid for the given provider.
For ollama, openrouter - any model is accepted. For ollama, openrouter - any model is accepted.
""" """
provider_lower = provider.lower() provider_lower = provider.lower()
if provider_lower in ("ollama", "openrouter"): if provider_lower in ("ollama", "openrouter"):
return True return True
if provider_lower not in VALID_MODELS: if provider_lower not in VALID_MODELS:
return True return True
return model in VALID_MODELS[provider_lower] return model in VALID_MODELS[provider_lower]

8270
uv.lock

File diff suppressed because it is too large Load Diff