Seogmin Claude Opus 4.5 commited on
Commit
60f51cc
·
0 Parent(s):

Initial commit: TradingAgents KR Web App

Browse files

Features:
- Korean stock data via KRX (pykrx, Naver Finance)
- AI-powered stock analysis
- Streamlit web interface

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. .env.example +2 -0
  2. .gitattributes +4 -0
  3. .gitignore +18 -0
  4. .python-version +1 -0
  5. .streamlit/config.toml +11 -0
  6. .streamlit/secrets.toml.example +5 -0
  7. LICENSE +201 -0
  8. README.md +34 -0
  9. app.py +187 -0
  10. data/cache/krx_stocks.json +0 -0
  11. data/users/newmind68/portfolio.json +17 -0
  12. pages/1_홈.py +211 -0
  13. pages/2_나의주식.py +399 -0
  14. pages/3_종목탐색.py +399 -0
  15. pages/4_분석리포트.py +223 -0
  16. pages/5_설정.py +261 -0
  17. requirements.txt +38 -0
  18. services/__init__.py +8 -0
  19. services/analysis_service.py +300 -0
  20. services/portfolio_service.py +246 -0
  21. services/stock_service.py +641 -0
  22. tradingagents/agents/__init__.py +42 -0
  23. tradingagents/agents/analysts/fundamentals_analyst.py +64 -0
  24. tradingagents/agents/analysts/market_analyst.py +86 -0
  25. tradingagents/agents/analysts/news_analyst.py +59 -0
  26. tradingagents/agents/analysts/social_media_analyst.py +60 -0
  27. tradingagents/agents/analysts/timeseries_analyst.py +204 -0
  28. tradingagents/agents/managers/research_manager.py +56 -0
  29. tradingagents/agents/managers/risk_manager.py +67 -0
  30. tradingagents/agents/researchers/bear_researcher.py +65 -0
  31. tradingagents/agents/researchers/bull_researcher.py +63 -0
  32. tradingagents/agents/risk_mgmt/aggresive_debator.py +56 -0
  33. tradingagents/agents/risk_mgmt/conservative_debator.py +59 -0
  34. tradingagents/agents/risk_mgmt/neutral_debator.py +56 -0
  35. tradingagents/agents/trader/trader.py +47 -0
  36. tradingagents/agents/utils/agent_states.py +77 -0
  37. tradingagents/agents/utils/agent_utils.py +44 -0
  38. tradingagents/agents/utils/core_stock_tools.py +22 -0
  39. tradingagents/agents/utils/fundamental_data_tools.py +77 -0
  40. tradingagents/agents/utils/memory.py +114 -0
  41. tradingagents/agents/utils/news_data_tools.py +71 -0
  42. tradingagents/agents/utils/technical_indicators_tools.py +23 -0
  43. tradingagents/agents/utils/timeseries_tools.py +981 -0
  44. tradingagents/dataflows/__init__.py +0 -0
  45. tradingagents/dataflows/alpha_vantage.py +5 -0
  46. tradingagents/dataflows/alpha_vantage_common.py +122 -0
  47. tradingagents/dataflows/alpha_vantage_fundamentals.py +77 -0
  48. tradingagents/dataflows/alpha_vantage_indicator.py +222 -0
  49. tradingagents/dataflows/alpha_vantage_news.py +43 -0
  50. tradingagents/dataflows/alpha_vantage_stock.py +38 -0
.env.example ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ ALPHA_VANTAGE_API_KEY=alpha_vantage_api_key_placeholder
2
+ OPENAI_API_KEY=openai_api_key_placeholder
.gitattributes ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ *.png filter=lfs diff=lfs merge=lfs -text
2
+ *.jpg filter=lfs diff=lfs merge=lfs -text
3
+ *.jpeg filter=lfs diff=lfs merge=lfs -text
4
+ *.gif filter=lfs diff=lfs merge=lfs -text
.gitignore ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ .venv
2
+ venv/
3
+ results
4
+ env/
5
+ __pycache__/
6
+ .DS_Store
7
+ *.csv
8
+ src/
9
+ eval_results/
10
+ eval_data/
11
+ *.egg-info/
12
+ .env
13
+ *.backup
14
+ README.md
15
+ plan.md
16
+ .claude/
17
+ temp_forecast/
18
+
.python-version ADDED
@@ -0,0 +1 @@
 
 
1
+ 3.10
.streamlit/config.toml ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [theme]
2
+ primaryColor = "#1E88E5"
3
+ backgroundColor = "#FFFFFF"
4
+ secondaryBackgroundColor = "#F0F2F6"
5
+ textColor = "#262730"
6
+ font = "sans serif"
7
+
8
+ [server]
9
+ headless = true
10
+ port = 8501
11
+ enableCORS = false
.streamlit/secrets.toml.example ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ # Streamlit Cloud Secrets 설정 예제
2
+ # Streamlit Cloud 대시보드에서 Settings > Secrets에 아래 내용을 입력하세요
3
+
4
+ OPENAI_API_KEY = "your-openai-api-key"
5
+ ALPHA_VANTAGE_API_KEY = "your-alpha-vantage-api-key"
LICENSE ADDED
@@ -0,0 +1,201 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
README.md ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: TradingAgents AI 주식 분석
3
+ emoji: 📈
4
+ colorFrom: blue
5
+ colorTo: indigo
6
+ sdk: streamlit
7
+ sdk_version: 1.40.0
8
+ app_file: app.py
9
+ pinned: false
10
+ license: cc-by-sa-4.0
11
+ ---
12
+
13
+ # TradingAgents - AI 주식 분석 시스템
14
+
15
+ AI 에이전트가 협업하여 미국/한국 주식을 분석하는 시스템입니다.
16
+
17
+ ## 주요 기능
18
+
19
+ - 🇺🇸 **미국 주식**: NYSE, NASDAQ (Yahoo Finance)
20
+ - 🇰🇷 **한국 주식**: 코스피, 코스닥 (pykrx, FinanceDataReader)
21
+ - 🤖 **AI 분석**: 펀더멘탈, 센티먼트, 뉴스, 기술적 분석
22
+ - 📊 **차트**: 캔들스틱 + 거래량 시각화
23
+
24
+ ## 로그인 정보
25
+
26
+ 시스템 관리자에게 문의하세요.
27
+
28
+ ## 면책 조항
29
+
30
+ ⚠️ 본 시스템은 연구 및 교육 목적으로만 사용됩니다. 투자 조언이 아닙니다.
31
+
32
+ ## 크레딧
33
+
34
+ Based on [TradingAgents](https://github.com/TauricResearch/TradingAgents) by Tauric Research
app.py ADDED
@@ -0,0 +1,187 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ TradingAgents 웹 애플리케이션
3
+ AI 기반 미국/한국 주식 분석 시스템 - 멀티페이지 진입점
4
+ """
5
+
6
+ import streamlit as st
7
+ import hashlib
8
+ import sys
9
+ import os
10
+
11
+ # .env 파일 로드
12
+ from dotenv import load_dotenv
13
+ load_dotenv()
14
+
15
+ # 프로젝트 경로 추가
16
+ sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
17
+
18
+ # 페이지 설정
19
+ st.set_page_config(
20
+ page_title="TradingAgents - AI 주식 분석",
21
+ page_icon="📈",
22
+ layout="wide",
23
+ initial_sidebar_state="expanded"
24
+ )
25
+
26
+ # 커스텀 CSS
27
+ st.markdown("""
28
+ <style>
29
+ .main-header {
30
+ font-size: 2.5rem;
31
+ font-weight: bold;
32
+ color: #1E88E5;
33
+ text-align: center;
34
+ margin-bottom: 2rem;
35
+ }
36
+ .sub-header {
37
+ font-size: 1.2rem;
38
+ color: #666;
39
+ text-align: center;
40
+ margin-bottom: 2rem;
41
+ }
42
+ .metric-card {
43
+ background-color: #f0f2f6;
44
+ border-radius: 10px;
45
+ padding: 1rem;
46
+ margin: 0.5rem 0;
47
+ }
48
+ .profit-positive {
49
+ color: #e53935;
50
+ font-weight: bold;
51
+ }
52
+ .profit-negative {
53
+ color: #1e88e5;
54
+ font-weight: bold;
55
+ }
56
+ .stock-card {
57
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
58
+ border-radius: 15px;
59
+ padding: 1.5rem;
60
+ color: white;
61
+ margin: 0.5rem 0;
62
+ }
63
+
64
+ /* 사이드바 폰트 크기 증가 */
65
+ [data-testid="stSidebarNav"] {
66
+ padding-top: 1rem;
67
+ }
68
+ [data-testid="stSidebarNav"] span {
69
+ font-size: 1.1rem !important;
70
+ font-weight: 500 !important;
71
+ }
72
+
73
+ /* app 메뉴 숨기기 (첫 번째 항목) */
74
+ [data-testid="stSidebarNav"] > ul > li:first-child {
75
+ display: none;
76
+ }
77
+
78
+ .stButton>button {
79
+ width: 100%;
80
+ }
81
+ </style>
82
+ """, unsafe_allow_html=True)
83
+
84
+ # 인증 정보 (보안을 위해 해시 처리)
85
+ VALID_CREDENTIALS = {
86
+ "newmind68": hashlib.sha256("@#sksk2739".encode()).hexdigest()
87
+ }
88
+
89
+
90
+ def check_password(username: str, password: str) -> bool:
91
+ """사용자명과 비밀번호 검증"""
92
+ if username in VALID_CREDENTIALS:
93
+ hashed = hashlib.sha256(password.encode()).hexdigest()
94
+ return hashed == VALID_CREDENTIALS[username]
95
+ return False
96
+
97
+
98
+ def login_page():
99
+ """로그인 페이지 표시"""
100
+ st.markdown('<p class="main-header">📈 TradingAgents</p>', unsafe_allow_html=True)
101
+ st.markdown('<p class="sub-header">AI 기반 주식 분석 시스템</p>', unsafe_allow_html=True)
102
+
103
+ col1, col2, col3 = st.columns([1, 2, 1])
104
+
105
+ with col2:
106
+ st.markdown("### 로그인")
107
+
108
+ with st.form("login_form"):
109
+ username = st.text_input("아이디", placeholder="아이디를 입력하세요")
110
+ password = st.text_input("비밀번호", type="password", placeholder="비밀번호를 입력하세요")
111
+ submit = st.form_submit_button("로그인", type="primary")
112
+
113
+ if submit:
114
+ if check_password(username, password):
115
+ st.session_state.authenticated = True
116
+ st.session_state.username = username
117
+ st.rerun()
118
+ else:
119
+ st.error("아이디 또는 비밀번호가 올바르지 않습니다")
120
+
121
+ st.markdown("---")
122
+ st.markdown("""
123
+ <div style="text-align: center; color: #888; font-size: 0.9rem;">
124
+ <p>AI 에이전트가 다양한 관점에서 주식을 분석합니다:</p>
125
+ <p>📊 펀더멘탈 | 📰 뉴스 | 💬 센티먼트 | 📈 기술적 분석</p>
126
+ </div>
127
+ """, unsafe_allow_html=True)
128
+
129
+
130
+ def main_redirect():
131
+ """로그인 후 홈으로 리다이렉트 안내"""
132
+ st.markdown('<p class="main-header">📈 TradingAgents</p>', unsafe_allow_html=True)
133
+
134
+ # 사이드바에 사용자 정보
135
+ with st.sidebar:
136
+ st.markdown(f"### 👤 {st.session_state.username}님")
137
+ st.markdown("---")
138
+
139
+ if st.button("🚪 로그아웃", type="secondary"):
140
+ st.session_state.authenticated = False
141
+ st.session_state.username = None
142
+ st.rerun()
143
+
144
+ # 메인 콘텐츠
145
+ st.markdown("### 환영합니다!")
146
+ st.info("👈 왼쪽 사이드바에서 메뉴를 선택하세요")
147
+
148
+ st.markdown("---")
149
+ st.markdown("""
150
+ ### 📌 주요 메뉴 안내
151
+
152
+ | 메뉴 | 설명 |
153
+ |------|------|
154
+ | **🏠 홈** | 포트폴리오 요약 및 대시보드 |
155
+ | **💼 나의 주식** | 보유 종목 관리, 상세 분석, AI 예측 |
156
+ | **🔍 종목 탐색** | 새로운 종목 검색 및 관심종목 관리 |
157
+ | **📊 분석 리포트** | AI 분석 히스토리 조회 |
158
+ | **⚙️ 설정** | 프로필 및 앱 설정 |
159
+ """)
160
+
161
+ st.markdown("---")
162
+ st.markdown("""
163
+ ### 🚀 시작하기
164
+
165
+ 1. **포트폴리오 구성**: '나의 주식' 메뉴에서 보유 종목을 추가하세요
166
+ 2. **AI 분석 실행**: 종목을 선택하고 AI 분석을 실행하세요
167
+ 3. **인사이트 확인**: 대시보드에서 전체 현황을 확인하세요
168
+ """)
169
+
170
+
171
+ def main():
172
+ """메인 진입점"""
173
+ # 세션 상태 초기화
174
+ if 'authenticated' not in st.session_state:
175
+ st.session_state.authenticated = False
176
+ if 'username' not in st.session_state:
177
+ st.session_state.username = None
178
+
179
+ # 적절한 페이지 표시
180
+ if st.session_state.authenticated:
181
+ main_redirect()
182
+ else:
183
+ login_page()
184
+
185
+
186
+ if __name__ == "__main__":
187
+ main()
data/cache/krx_stocks.json ADDED
The diff for this file is too large to render. See raw diff
 
data/users/newmind68/portfolio.json ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "user_id": "newmind68",
3
+ "created_at": "2026-01-03T21:10:26.376414",
4
+ "updated_at": "2026-01-03T21:10:26.376472",
5
+ "holdings": [
6
+ {
7
+ "id": "3d4a7da5-27ba-48cf-b708-0ff6db31c1f8",
8
+ "symbol": "304100",
9
+ "company_name": "솔트룩스",
10
+ "market": "krx",
11
+ "buy_price": 1300.0,
12
+ "quantity": 7,
13
+ "buy_date": "2025-01-09",
14
+ "memo": ""
15
+ }
16
+ ]
17
+ }
pages/1_홈.py ADDED
@@ -0,0 +1,211 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ 홈 대시보드 - 포트폴리오 요약 및 현황
3
+ """
4
+ import streamlit as st
5
+ import sys
6
+ import os
7
+ from datetime import datetime, timedelta
8
+
9
+ # 프로젝트 경로 추가
10
+ sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
11
+
12
+ from services.portfolio_service import PortfolioService
13
+ from services.stock_service import StockService
14
+ from services.analysis_service import AnalysisService
15
+
16
+ st.set_page_config(page_title="홈 - TradingAgents", page_icon="🏠", layout="wide")
17
+
18
+ # 공통 CSS - app 메뉴 숨김 및 사이드바 스타일
19
+ st.markdown("""
20
+ <style>
21
+ [data-testid="stSidebarNav"] span {
22
+ font-size: 1.1rem !important;
23
+ font-weight: 500 !important;
24
+ }
25
+ [data-testid="stSidebarNav"] > ul > li:first-child {
26
+ display: none;
27
+ }
28
+ </style>
29
+ """, unsafe_allow_html=True)
30
+
31
+
32
+ def check_auth():
33
+ """인증 확인"""
34
+ if 'authenticated' not in st.session_state or not st.session_state.authenticated:
35
+ st.warning("로그인이 필요합니다")
36
+ st.stop()
37
+ return st.session_state.username
38
+
39
+
40
+ def render_portfolio_summary(summary: dict):
41
+ """포트폴리오 요약 카드 렌더링"""
42
+ col1, col2, col3, col4 = st.columns(4)
43
+
44
+ with col1:
45
+ st.metric(
46
+ "총 평가금액",
47
+ f"{summary['total_current']:,.0f}원",
48
+ f"{summary['total_profit']:+,.0f}원"
49
+ )
50
+
51
+ with col2:
52
+ st.metric(
53
+ "총 투자금액",
54
+ f"{summary['total_invested']:,.0f}원"
55
+ )
56
+
57
+ with col3:
58
+ profit_rate = summary['total_profit_rate']
59
+ st.metric(
60
+ "총 수익률",
61
+ f"{profit_rate:+.2f}%"
62
+ )
63
+
64
+ with col4:
65
+ st.metric(
66
+ "보유 종목",
67
+ f"{summary['holdings_count']}개"
68
+ )
69
+
70
+
71
+ def render_holding_card(holding: dict, current_price: float, profit_info: dict):
72
+ """개별 종목 카드 렌더링"""
73
+ profit_rate = profit_info['profit_rate']
74
+ profit_color = "red" if profit_rate >= 0 else "blue"
75
+ arrow = "▲" if profit_rate >= 0 else "▼"
76
+
77
+ # 시장 플래그
78
+ flag = "🇰🇷" if holding['market'] == 'krx' else "🇺🇸"
79
+
80
+ st.markdown(f"""
81
+ <div style="background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
82
+ border-radius: 10px; padding: 1rem; margin: 0.5rem 0;">
83
+ <div style="display: flex; justify-content: space-between; align-items: center;">
84
+ <div>
85
+ <span style="font-size: 1.2rem; font-weight: bold;">{flag} {holding['company_name']}</span>
86
+ <span style="color: #666; margin-left: 0.5rem;">({holding['symbol']})</span>
87
+ </div>
88
+ <div style="text-align: right;">
89
+ <div style="font-size: 1.1rem; font-weight: bold;">{current_price:,.0f}원</div>
90
+ <div style="color: {profit_color};">{arrow} {abs(profit_rate):.2f}%</div>
91
+ </div>
92
+ </div>
93
+ <div style="margin-top: 0.5rem; color: #666; font-size: 0.9rem;">
94
+ 평균단가: {holding['buy_price']:,.0f}원 | 보유: {holding['quantity']:,}주 |
95
+ 평가손익: <span style="color: {profit_color};">{profit_info['profit']:+,.0f}원</span>
96
+ </div>
97
+ </div>
98
+ """, unsafe_allow_html=True)
99
+
100
+
101
+ def render_recent_reports(reports: list):
102
+ """최근 AI 분석 리포트 렌더링"""
103
+ if not reports:
104
+ st.info("아직 AI 분석 리포트가 없습니다")
105
+ return
106
+
107
+ for report in reports[:5]:
108
+ decision = report.get('decision', {})
109
+ action = decision.get('action', 'N/A') if isinstance(decision, dict) else 'N/A'
110
+
111
+ action_color = {
112
+ 'BUY': '🟢', 'STRONG_BUY': '🟢',
113
+ 'SELL': '🔴', 'STRONG_SELL': '🔴',
114
+ 'HOLD': '🟡'
115
+ }.get(action.upper(), '⚪')
116
+
117
+ analysis_date = report.get('analysis_date', '')
118
+ company_name = report.get('company_name', report.get('symbol', ''))
119
+
120
+ st.markdown(f"""
121
+ <div style="padding: 0.5rem; border-bottom: 1px solid #eee;">
122
+ {action_color} <strong>{company_name}</strong> ({report.get('symbol', '')})
123
+ <span style="color: #666; margin-left: 1rem;">{analysis_date}</span>
124
+ <span style="float: right;">{action}</span>
125
+ </div>
126
+ """, unsafe_allow_html=True)
127
+
128
+
129
+ def main():
130
+ username = check_auth()
131
+
132
+ # 사이드바
133
+ with st.sidebar:
134
+ st.markdown(f"### 👤 {username}님")
135
+ st.markdown("---")
136
+
137
+ if st.button("🚪 로그아웃", type="secondary"):
138
+ st.session_state.authenticated = False
139
+ st.session_state.username = None
140
+ st.rerun()
141
+
142
+ # 서비스 초기화
143
+ portfolio_service = PortfolioService(username)
144
+ stock_service = StockService()
145
+ analysis_service = AnalysisService(username)
146
+
147
+ # 헤더
148
+ st.markdown("# 🏠 대시보드")
149
+ st.markdown("---")
150
+
151
+ # 포트폴리오 데이터 로드
152
+ holdings = portfolio_service.get_all_holdings()
153
+
154
+ if not holdings:
155
+ st.info("👋 아직 등록된 종목이 없습니다. '나의 주식' 메뉴에서 종목을 추가해보세요!")
156
+
157
+ # 빠른 시작 가이드
158
+ st.markdown("""
159
+ ### 🚀 시작하기
160
+
161
+ 1. **왼쪽 메뉴**에서 **'나의 주식'**을 선택하세요
162
+ 2. **'종목 추가'** 버튼을 클릭하세요
163
+ 3. 종목 코드, 매수가, 수량을 입력하세요
164
+
165
+ ### 💡 팁
166
+ - 한국 주식: 6자리 숫자 (예: 005930 삼성전자)
167
+ - 미국 주식: 티커 심볼 (예: AAPL, NVDA)
168
+ """)
169
+ else:
170
+ # 현재가 조회
171
+ with st.spinner("현재가 조회 중..."):
172
+ symbols = [h['symbol'] for h in holdings]
173
+ current_prices = stock_service.get_current_prices(symbols)
174
+
175
+ # 포트폴리오 요약
176
+ st.markdown("### 📊 포트폴리오 요약")
177
+ summary = portfolio_service.calculate_portfolio_summary(current_prices)
178
+ render_portfolio_summary(summary)
179
+
180
+ st.markdown("---")
181
+
182
+ # 보유 종목 목록
183
+ st.markdown("### 💼 보유 종목")
184
+
185
+ cols = st.columns(2)
186
+ for i, holding in enumerate(holdings):
187
+ current_price = current_prices.get(holding['symbol'], holding['buy_price'])
188
+ profit_info = portfolio_service.calculate_profit(holding, current_price)
189
+
190
+ with cols[i % 2]:
191
+ render_holding_card(holding, current_price, profit_info)
192
+
193
+ st.markdown("---")
194
+
195
+ # 최근 AI 분석 리포트
196
+ st.markdown("### 🤖 최근 AI 분석 리포트")
197
+ reports = analysis_service.get_reports(limit=5)
198
+ render_recent_reports(reports)
199
+
200
+ # 푸터
201
+ st.markdown("---")
202
+ st.markdown("""
203
+ <div style="text-align: center; color: #888; font-size: 0.8rem;">
204
+ TradingAgents - AI 기반 주식 분석 시스템<br>
205
+ ⚠️ 본 시스템은 투자 참고용이며, 투자 결정에 대한 책임은 사용자에게 있습니다.
206
+ </div>
207
+ """, unsafe_allow_html=True)
208
+
209
+
210
+ if __name__ == "__main__":
211
+ main()
pages/2_나의주식.py ADDED
@@ -0,0 +1,399 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ 나의 주식 - 포트폴리오 관리, 종목 상세, AI 분석
3
+ 메인 기능 페이지
4
+ """
5
+ import streamlit as st
6
+ import sys
7
+ import os
8
+ from datetime import datetime, timedelta
9
+ import pandas as pd
10
+
11
+ sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
12
+
13
+ from services.portfolio_service import PortfolioService
14
+ from services.stock_service import StockService
15
+ from services.analysis_service import AnalysisService
16
+
17
+ st.set_page_config(page_title="나의 주식 - TradingAgents", page_icon="💼", layout="wide")
18
+
19
+ # 공통 CSS - app 메뉴 숨김 및 사이드바 스타일
20
+ st.markdown("""
21
+ <style>
22
+ [data-testid="stSidebarNav"] span {
23
+ font-size: 1.1rem !important;
24
+ font-weight: 500 !important;
25
+ }
26
+ [data-testid="stSidebarNav"] > ul > li:first-child {
27
+ display: none;
28
+ }
29
+ </style>
30
+ """, unsafe_allow_html=True)
31
+
32
+
33
+ def check_auth():
34
+ if 'authenticated' not in st.session_state or not st.session_state.authenticated:
35
+ st.warning("로그인이 필요합니다")
36
+ st.stop()
37
+ return st.session_state.username
38
+
39
+
40
+ def render_add_holding_form(portfolio_service: PortfolioService, stock_service: StockService):
41
+ """종목 추가 폼"""
42
+ st.markdown("### ➕ 종목 추가")
43
+
44
+ with st.form("add_holding_form"):
45
+ col1, col2 = st.columns(2)
46
+
47
+ with col1:
48
+ market = st.radio("시장", ["한국 (KRX)", "미국"], horizontal=True)
49
+ if market == "한국 (KRX)":
50
+ symbol = st.text_input("종목 코드", placeholder="예: 005930")
51
+ st.caption("6자리 종목 코드를 입력하세요")
52
+ else:
53
+ symbol = st.text_input("티커 심볼", placeholder="예: AAPL")
54
+ st.caption("티커 심볼을 입력하세요")
55
+
56
+ with col2:
57
+ buy_price = st.number_input("매수 단가", min_value=0.0, step=100.0)
58
+ quantity = st.number_input("매수 수량", min_value=1, step=1)
59
+ buy_date = st.date_input("매수일", datetime.now())
60
+
61
+ memo = st.text_input("메모 (선택)", placeholder="투자 메모를 입력하세요")
62
+
63
+ submitted = st.form_submit_button("종목 추가", type="primary")
64
+
65
+ if submitted and symbol and buy_price > 0 and quantity > 0:
66
+ # 회사 정보 조회
67
+ company_name, market_info = stock_service.get_company_info(symbol)
68
+ market_code = "krx" if stock_service.is_korean_stock(symbol) else "us"
69
+
70
+ # 포트폴리오에 추가
71
+ portfolio_service.add_holding(
72
+ symbol=symbol,
73
+ company_name=company_name,
74
+ market=market_code,
75
+ buy_price=buy_price,
76
+ quantity=quantity,
77
+ buy_date=buy_date.strftime("%Y-%m-%d"),
78
+ memo=memo
79
+ )
80
+
81
+ st.success(f"✅ {company_name} ({symbol})이(가) 추가되었습니다!")
82
+ st.rerun()
83
+
84
+
85
+ def render_holdings_table(holdings: list, current_prices: dict,
86
+ portfolio_service: PortfolioService):
87
+ """보유 종목 테이블"""
88
+ if not holdings:
89
+ st.info("등록된 종목이 없습니다. 위에서 종목을 추가해보세요!")
90
+ return
91
+
92
+ # 데이터 준비
93
+ data = []
94
+ for h in holdings:
95
+ current_price = current_prices.get(h['symbol'], h['buy_price'])
96
+ profit_info = portfolio_service.calculate_profit(h, current_price)
97
+ buy_date_str = h.get('buy_date', '-')
98
+
99
+ data.append({
100
+ "종목": f"{h['company_name']} ({h['symbol']})",
101
+ "시장": "🇰🇷" if h['market'] == 'krx' else "🇺🇸",
102
+ "매수일": buy_date_str,
103
+ "매수단가": f"{h['buy_price']:,.0f}",
104
+ "현재가": f"{current_price:,.0f}",
105
+ "수량": h['quantity'],
106
+ "평가금액": f"{profit_info['current_value']:,.0f}",
107
+ "수익률": f"{profit_info['profit_rate']:+.2f}%",
108
+ "평가손익": f"{profit_info['profit']:+,.0f}",
109
+ "symbol": h['symbol']
110
+ })
111
+
112
+ df = pd.DataFrame(data)
113
+
114
+ # 테이블 표시
115
+ st.dataframe(
116
+ df[["종목", "시장", "매수일", "매수단가", "현재가", "수량", "평가금액", "수익률", "평가손익"]],
117
+ use_container_width=True,
118
+ hide_index=True
119
+ )
120
+
121
+ # 종목 삭제
122
+ st.markdown("---")
123
+ col1, col2 = st.columns([3, 1])
124
+ with col1:
125
+ symbols_to_delete = [f"{h['company_name']} ({h['symbol']})" for h in holdings]
126
+ selected = st.selectbox("삭제할 종목 선택", ["선택하세요"] + symbols_to_delete)
127
+ with col2:
128
+ st.markdown("<br>", unsafe_allow_html=True)
129
+ if st.button("🗑️ 삭제", type="secondary"):
130
+ if selected != "선택하세요":
131
+ # 심볼 추출
132
+ symbol = selected.split("(")[-1].replace(")", "")
133
+ portfolio_service.remove_holding(symbol)
134
+ st.success(f"✅ {selected} 삭제되었습니다")
135
+ st.rerun()
136
+
137
+
138
+ def render_stock_detail(holdings: list, stock_service: StockService,
139
+ portfolio_service: PortfolioService):
140
+ """종목 상세 보기"""
141
+ if not holdings:
142
+ st.info("먼저 포트폴리오에 종목을 추가해주세요")
143
+ return
144
+
145
+ # 종목 선택
146
+ stock_options = {f"{h['company_name']} ({h['symbol']})": h for h in holdings}
147
+ selected_name = st.selectbox("종목 선택", list(stock_options.keys()))
148
+ holding = stock_options[selected_name]
149
+
150
+ # 매수일 정보 확인
151
+ buy_date_str = holding.get('buy_date', '')
152
+ has_buy_date = bool(buy_date_str)
153
+
154
+ # 기간 선택
155
+ col1, col2, col3 = st.columns([1, 1, 2])
156
+ with col1:
157
+ period_options = ["매수일부터", "1개월", "3개월", "6개월", "1년"] if has_buy_date else ["1개월", "3개월", "6개월", "1년"]
158
+ period = st.selectbox("기간", period_options)
159
+ with col2:
160
+ st.markdown("<br>", unsafe_allow_html=True)
161
+ refresh = st.button("🔄 새로고침")
162
+
163
+ # 기간 계산
164
+ end_date = datetime.now()
165
+ if period == "매수일부터" and has_buy_date:
166
+ start_date = datetime.strptime(buy_date_str, "%Y-%m-%d")
167
+ holding_days = (end_date - start_date).days
168
+ st.caption(f"📅 매수일: {buy_date_str} (보유 {holding_days}일)")
169
+ else:
170
+ period_days = {"1개월": 30, "3개월": 90, "6개월": 180, "1년": 365}
171
+ start_date = end_date - timedelta(days=period_days[period])
172
+
173
+ # 데이터 조회
174
+ with st.spinner("데이터 조회 중..."):
175
+ df, error = stock_service.get_stock_data(
176
+ holding['symbol'],
177
+ start_date.strftime("%Y-%m-%d"),
178
+ end_date.strftime("%Y-%m-%d")
179
+ )
180
+
181
+ if error:
182
+ st.error(f"데이터 조회 오류: {error}")
183
+ return
184
+
185
+ if df is None or df.empty:
186
+ st.warning("데이터가 없습니다")
187
+ return
188
+
189
+ # 주가 차트
190
+ st.markdown("### 📈 주가 차트")
191
+ fig = stock_service.plot_candlestick(df, holding['symbol'], holding['company_name'])
192
+ if fig:
193
+ st.plotly_chart(fig, use_container_width=True)
194
+
195
+ # 주요 지표
196
+ metrics = stock_service.get_stock_metrics(df)
197
+ if metrics:
198
+ st.markdown("### 📊 주요 지표")
199
+ col1, col2, col3, col4 = st.columns(4)
200
+ with col1:
201
+ st.metric("현재가", f"{metrics['current_price']:,.0f}원",
202
+ f"{metrics['change_rate']:+.2f}%")
203
+ with col2:
204
+ st.metric("기간 최고가", f"{metrics['period_high']:,.0f}원")
205
+ with col3:
206
+ st.metric("기간 최저가", f"{metrics['period_low']:,.0f}원")
207
+ with col4:
208
+ st.metric("데이터 수", f"{metrics['data_count']}일")
209
+
210
+ # 나의 투자 현황
211
+ st.markdown("### 💰 나의 투자 현황")
212
+ current_price = metrics.get('current_price', holding['buy_price'])
213
+ profit_info = portfolio_service.calculate_profit(holding, current_price)
214
+
215
+ col1, col2, col3, col4 = st.columns(4)
216
+ with col1:
217
+ st.metric("매수일", buy_date_str if has_buy_date else "-")
218
+ with col2:
219
+ st.metric("평균 매수가", f"{holding['buy_price']:,.0f}원")
220
+ with col3:
221
+ st.metric("보유 수량", f"{holding['quantity']:,}주")
222
+ with col4:
223
+ profit_color = "normal" if profit_info['profit'] >= 0 else "inverse"
224
+ st.metric("평가 손익", f"{profit_info['profit']:+,.0f}원",
225
+ f"{profit_info['profit_rate']:+.2f}%", delta_color=profit_color)
226
+
227
+ # 기술적 지표
228
+ with st.expander("📉 기술적 지표 보기"):
229
+ indicator = st.selectbox("지표 선택", ["RSI", "MACD", "볼린저밴드"])
230
+ try:
231
+ indicator_df, err = stock_service.get_technical_indicators(
232
+ holding['symbol'],
233
+ start_date.strftime("%Y-%m-%d"),
234
+ end_date.strftime("%Y-%m-%d")
235
+ )
236
+ if indicator_df is not None:
237
+ indicator_type = {"RSI": "RSI", "MACD": "MACD", "볼린저밴드": "BB"}[indicator]
238
+ indicator_fig = stock_service.plot_indicators(indicator_df, indicator_type)
239
+ if indicator_fig:
240
+ st.plotly_chart(indicator_fig, use_container_width=True)
241
+ except Exception as e:
242
+ st.warning(f"기술적 지표 조회 실패: {e}")
243
+
244
+ # 원본 데이터
245
+ with st.expander("📋 원본 데이터 보기"):
246
+ st.dataframe(df.tail(20), use_container_width=True)
247
+
248
+
249
+ def render_ai_analysis(holdings: list, analysis_service: AnalysisService):
250
+ """AI 분석 탭"""
251
+ st.markdown("""
252
+ ### 🤖 AI 기반 종목 분석
253
+
254
+ TradingAgents는 여러 AI 에이전트가 협업하여 주식을 분석합니다:
255
+ - **펀더멘탈 분석가**: 재무제표, 실적, 밸류에이션
256
+ - **센티먼트 분석가**: 시장 심리 및 소셜 미디어
257
+ - **뉴스 분석가**: 최신 뉴스 및 이벤트
258
+ - **기술적 분석가**: 가격 패턴 및 지표
259
+ - **Bull vs Bear 토론**: 강세론과 약세론 토론 후 합의
260
+ """)
261
+
262
+ st.markdown("---")
263
+
264
+ if not holdings:
265
+ st.info("먼저 포트폴리오에 종목을 추가해주세요")
266
+ return
267
+
268
+ # 종목 선택
269
+ col1, col2 = st.columns([2, 1])
270
+ with col1:
271
+ stock_options = {f"{h['company_name']} ({h['symbol']})": h['symbol'] for h in holdings}
272
+ selected = st.selectbox("분석할 종목", list(stock_options.keys()))
273
+ symbol = stock_options[selected]
274
+ with col2:
275
+ analysis_date = st.date_input("분석 기준일", datetime.now())
276
+
277
+ # 최근 분석 결과 표시
278
+ latest_report = analysis_service.get_latest_report(symbol)
279
+ if latest_report:
280
+ st.markdown("#### 📋 최근 분석 결과")
281
+ st.info(f"마지막 분석일: {latest_report.get('analysis_date', 'N/A')}")
282
+
283
+ decision = latest_report.get('decision', {})
284
+ if isinstance(decision, dict):
285
+ action = decision.get('action', 'N/A')
286
+ confidence = decision.get('confidence', 0)
287
+ st.markdown(f"**투자 의견**: {action} (신뢰도: {confidence}%)")
288
+
289
+ # 분석 실행 버튼
290
+ st.markdown("---")
291
+ if st.button("🚀 AI 분석 시작", type="primary"):
292
+ st.warning("⚠️ AI 분석은 OpenAI API를 사용하며 비용이 발생할 수 있습니다.")
293
+ st.info("⏱️ 분석에 3-5분이 소요됩니다...")
294
+
295
+ progress_bar = st.progress(0)
296
+ status_text = st.empty()
297
+
298
+ def progress_callback(msg):
299
+ status_text.text(msg)
300
+
301
+ with st.spinner("AI 에이전트 분석 중..."):
302
+ progress_bar.progress(30)
303
+ decision, error = analysis_service.run_analysis(
304
+ symbol,
305
+ analysis_date.strftime("%Y-%m-%d"),
306
+ progress_callback
307
+ )
308
+ progress_bar.progress(100)
309
+
310
+ if error:
311
+ st.error(f"분석 오류: {error}")
312
+ elif decision:
313
+ st.success("✅ 분석 완료!")
314
+ st.markdown("### 📊 분석 결과")
315
+
316
+ # 결과 표시
317
+ if isinstance(decision, dict):
318
+ st.json(decision)
319
+ else:
320
+ st.write(decision)
321
+
322
+ # 한글 리포트
323
+ korean_report = analysis_service.format_decision_korean(decision)
324
+ st.markdown(korean_report)
325
+ else:
326
+ st.info("분석이 완료되었지만 결과가 반환되지 않았습니다.")
327
+
328
+ # 분석 리포트 목록
329
+ st.markdown("---")
330
+ st.markdown("#### 📚 이 종목의 분석 기록")
331
+ reports = analysis_service.get_reports(symbol=symbol, limit=5)
332
+
333
+ if reports:
334
+ for report in reports:
335
+ col1, col2, col3 = st.columns([2, 1, 1])
336
+ with col1:
337
+ st.write(f"📅 {report.get('analysis_date', 'N/A')}")
338
+ with col2:
339
+ decision = report.get('decision', {})
340
+ action = decision.get('action', 'N/A') if isinstance(decision, dict) else 'N/A'
341
+ st.write(f"**{action}**")
342
+ with col3:
343
+ if st.button("삭제", key=f"del_{report.get('report_id', '')}"):
344
+ analysis_service.delete_report(symbol, report.get('report_id', ''))
345
+ st.rerun()
346
+ else:
347
+ st.info("아직 분석 기록이 없습니다")
348
+
349
+
350
+ def main():
351
+ username = check_auth()
352
+
353
+ # 사이드바
354
+ with st.sidebar:
355
+ st.markdown(f"### 👤 {username}님")
356
+ st.markdown("---")
357
+ if st.button("🚪 로그아웃", type="secondary"):
358
+ st.session_state.authenticated = False
359
+ st.session_state.username = None
360
+ st.rerun()
361
+
362
+ # 서비스 초기화
363
+ portfolio_service = PortfolioService(username)
364
+ stock_service = StockService()
365
+ analysis_service = AnalysisService(username)
366
+
367
+ # 헤더
368
+ st.markdown("# 💼 나의 주식")
369
+
370
+ # 탭
371
+ tab1, tab2, tab3 = st.tabs(["📋 포트폴리오 관리", "📈 종목 상세", "🤖 AI 분석"])
372
+
373
+ with tab1:
374
+ # 종목 추가 폼
375
+ render_add_holding_form(portfolio_service, stock_service)
376
+
377
+ st.markdown("---")
378
+
379
+ # 보유 종목 테이블
380
+ st.markdown("### 📊 보유 종목 현황")
381
+ holdings = portfolio_service.get_all_holdings()
382
+
383
+ with st.spinner("현재가 조회 중..."):
384
+ symbols = [h['symbol'] for h in holdings]
385
+ current_prices = stock_service.get_current_prices(symbols) if symbols else {}
386
+
387
+ render_holdings_table(holdings, current_prices, portfolio_service)
388
+
389
+ with tab2:
390
+ holdings = portfolio_service.get_all_holdings()
391
+ render_stock_detail(holdings, stock_service, portfolio_service)
392
+
393
+ with tab3:
394
+ holdings = portfolio_service.get_all_holdings()
395
+ render_ai_analysis(holdings, analysis_service)
396
+
397
+
398
+ if __name__ == "__main__":
399
+ main()
pages/3_종목탐색.py ADDED
@@ -0,0 +1,399 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ 종목 탐색 - 주식 검색 및 관심종목 관리
3
+ """
4
+ import streamlit as st
5
+ import sys
6
+ import os
7
+ from datetime import datetime, timedelta
8
+
9
+ sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
10
+
11
+ from services.portfolio_service import PortfolioService
12
+ from services.stock_service import StockService
13
+
14
+ st.set_page_config(page_title="종목 탐색 - TradingAgents", page_icon="🔍", layout="wide")
15
+
16
+ # 공통 CSS - app 메뉴 숨김 및 사이드바 스타일
17
+ st.markdown("""
18
+ <style>
19
+ [data-testid="stSidebarNav"] span {
20
+ font-size: 1.1rem !important;
21
+ font-weight: 500 !important;
22
+ }
23
+ [data-testid="stSidebarNav"] > ul > li:first-child {
24
+ display: none;
25
+ }
26
+ </style>
27
+ """, unsafe_allow_html=True)
28
+
29
+
30
+ def check_auth():
31
+ if 'authenticated' not in st.session_state or not st.session_state.authenticated:
32
+ st.warning("로그인이 필요합니다")
33
+ st.stop()
34
+ return st.session_state.username
35
+
36
+
37
+ # 인기 종목 데이터
38
+ POPULAR_STOCKS = {
39
+ "한국": [
40
+ {"symbol": "005930", "name": "삼성전자"},
41
+ {"symbol": "000660", "name": "SK하이닉스"},
42
+ {"symbol": "035720", "name": "카카오"},
43
+ {"symbol": "051910", "name": "LG화학"},
44
+ {"symbol": "005380", "name": "현대자동차"},
45
+ {"symbol": "006400", "name": "삼성SDI"},
46
+ {"symbol": "035420", "name": "NAVER"},
47
+ {"symbol": "000270", "name": "기아"},
48
+ ],
49
+ "미국": [
50
+ {"symbol": "AAPL", "name": "Apple"},
51
+ {"symbol": "NVDA", "name": "NVIDIA"},
52
+ {"symbol": "MSFT", "name": "Microsoft"},
53
+ {"symbol": "GOOGL", "name": "Alphabet"},
54
+ {"symbol": "AMZN", "name": "Amazon"},
55
+ {"symbol": "TSLA", "name": "Tesla"},
56
+ {"symbol": "META", "name": "Meta"},
57
+ {"symbol": "AMD", "name": "AMD"},
58
+ ]
59
+ }
60
+
61
+
62
+ def render_search_result(symbol: str, stock_service: StockService,
63
+ portfolio_service: PortfolioService):
64
+ """검색 결과 표시"""
65
+ with st.spinner("종목 정보 조회 중..."):
66
+ company_name, market_info = stock_service.get_company_info(symbol)
67
+ current_price = stock_service.get_current_price(symbol)
68
+
69
+ # 최근 데이터 조회
70
+ end_date = datetime.now()
71
+ start_date = end_date - timedelta(days=30)
72
+ df, error = stock_service.get_stock_data(
73
+ symbol,
74
+ start_date.strftime("%Y-%m-%d"),
75
+ end_date.strftime("%Y-%m-%d")
76
+ )
77
+
78
+ if error:
79
+ st.error(f"조회 오류: {error}")
80
+ return
81
+
82
+ # 결과 표시
83
+ st.markdown(f"### 📊 {company_name} ({symbol})")
84
+ st.markdown(f"**시장**: {market_info}")
85
+
86
+ col1, col2 = st.columns([2, 1])
87
+
88
+ with col1:
89
+ # 미니 차트
90
+ if df is not None and not df.empty:
91
+ fig = stock_service.plot_mini_chart(df, symbol)
92
+ if fig:
93
+ fig.update_layout(height=200)
94
+ st.plotly_chart(fig, use_container_width=True)
95
+
96
+ # 지표
97
+ metrics = stock_service.get_stock_metrics(df)
98
+ if metrics:
99
+ mcol1, mcol2, mcol3 = st.columns(3)
100
+ with mcol1:
101
+ st.metric("현재가", f"{metrics['current_price']:,.0f}원",
102
+ f"{metrics['change_rate']:+.2f}%")
103
+ with mcol2:
104
+ st.metric("최고가", f"{metrics['period_high']:,.0f}원")
105
+ with mcol3:
106
+ st.metric("최저가", f"{metrics['period_low']:,.0f}원")
107
+
108
+ with col2:
109
+ st.markdown("#### 빠른 작업")
110
+
111
+ # 관심종목 추가
112
+ watchlist = portfolio_service.get_watchlist()
113
+ is_in_watchlist = any(s['symbol'] == symbol for s in watchlist)
114
+
115
+ if is_in_watchlist:
116
+ if st.button("⭐ 관심종목에서 제거", key="remove_watch"):
117
+ portfolio_service.remove_from_watchlist(symbol)
118
+ st.success("관심종목에서 제거되었습니다")
119
+ st.rerun()
120
+ else:
121
+ target_price = st.number_input("목표가 (선택)", min_value=0.0)
122
+ notes = st.text_input("메모 (선택)")
123
+ if st.button("⭐ 관심종목 추가", key="add_watch"):
124
+ market = "krx" if stock_service.is_korean_stock(symbol) else "us"
125
+ portfolio_service.add_to_watchlist(
126
+ symbol, company_name, market,
127
+ target_price if target_price > 0 else None,
128
+ notes
129
+ )
130
+ st.success("관심종목에 추가되었습니다!")
131
+ st.rerun()
132
+
133
+ st.markdown("---")
134
+
135
+ # 포트폴리오 추가
136
+ holdings = portfolio_service.get_all_holdings()
137
+ is_in_portfolio = any(h['symbol'] == symbol for h in holdings)
138
+
139
+ if is_in_portfolio:
140
+ st.info("이미 보유 중인 종목입니다")
141
+ else:
142
+ with st.form("quick_add_form"):
143
+ buy_price = st.number_input("매수 단가", min_value=0.0,
144
+ value=float(current_price) if current_price else 0.0)
145
+ quantity = st.number_input("수량", min_value=1, value=1)
146
+
147
+ if st.form_submit_button("💼 포트폴리오에 추가"):
148
+ market = "krx" if stock_service.is_korean_stock(symbol) else "us"
149
+ portfolio_service.add_holding(
150
+ symbol=symbol,
151
+ company_name=company_name,
152
+ market=market,
153
+ buy_price=buy_price,
154
+ quantity=quantity,
155
+ buy_date=datetime.now().strftime("%Y-%m-%d")
156
+ )
157
+ st.success("포트폴리오에 추가되었습니다!")
158
+ st.rerun()
159
+
160
+
161
+ def render_watchlist(portfolio_service: PortfolioService, stock_service: StockService):
162
+ """관심종목 목록"""
163
+ watchlist = portfolio_service.get_watchlist()
164
+
165
+ if not watchlist:
166
+ st.info("등록된 관심종목이 없습니다. 종목을 검색하여 추가해보세요!")
167
+ return
168
+
169
+ for stock in watchlist:
170
+ col1, col2, col3, col4 = st.columns([2, 1, 1, 1])
171
+
172
+ with col1:
173
+ flag = "🇰🇷" if stock['market'] == 'krx' else "🇺🇸"
174
+ st.markdown(f"**{flag} {stock['company_name']}** ({stock['symbol']})")
175
+ if stock.get('notes'):
176
+ st.caption(stock['notes'])
177
+
178
+ with col2:
179
+ current_price = stock_service.get_current_price(stock['symbol'])
180
+ if current_price:
181
+ st.metric("현재가", f"{current_price:,.0f}")
182
+
183
+ with col3:
184
+ if stock.get('target_price'):
185
+ st.metric("목표가", f"{stock['target_price']:,.0f}")
186
+ if current_price:
187
+ gap = ((stock['target_price'] - current_price) / current_price) * 100
188
+ st.caption(f"Gap: {gap:+.1f}%")
189
+
190
+ with col4:
191
+ if st.button("삭제", key=f"del_watch_{stock['symbol']}"):
192
+ portfolio_service.remove_from_watchlist(stock['symbol'])
193
+ st.rerun()
194
+
195
+ st.markdown("---")
196
+
197
+
198
+ def render_popular_stocks(stock_service: StockService, portfolio_service: PortfolioService):
199
+ """인기 종목 표시"""
200
+ tab_kr, tab_us = st.tabs(["🇰🇷 한국", "🇺🇸 미국"])
201
+
202
+ with tab_kr:
203
+ cols = st.columns(4)
204
+ for i, stock in enumerate(POPULAR_STOCKS["한국"]):
205
+ with cols[i % 4]:
206
+ if st.button(f"{stock['name']}\n({stock['symbol']})",
207
+ key=f"pop_kr_{stock['symbol']}",
208
+ use_container_width=True):
209
+ st.session_state.search_symbol = stock['symbol']
210
+ st.rerun()
211
+
212
+ with tab_us:
213
+ cols = st.columns(4)
214
+ for i, stock in enumerate(POPULAR_STOCKS["미국"]):
215
+ with cols[i % 4]:
216
+ if st.button(f"{stock['name']}\n({stock['symbol']})",
217
+ key=f"pop_us_{stock['symbol']}",
218
+ use_container_width=True):
219
+ st.session_state.search_symbol = stock['symbol']
220
+ st.rerun()
221
+
222
+
223
+ def render_search_results(results: list, stock_service: StockService, portfolio_service: PortfolioService):
224
+ """검색 결과 목록 표시"""
225
+ for stock in results:
226
+ col1, col2, col3 = st.columns([3, 1, 1])
227
+
228
+ with col1:
229
+ st.markdown(f"**{stock['name']}** ({stock['symbol']})")
230
+ st.caption(stock['market'])
231
+
232
+ with col2:
233
+ if st.button("상세보기", key=f"view_{stock['symbol']}"):
234
+ st.session_state.search_symbol = stock['symbol']
235
+ st.rerun()
236
+
237
+ with col3:
238
+ if st.button("➕ 추가", key=f"add_{stock['symbol']}"):
239
+ st.session_state.quick_add_symbol = stock['symbol']
240
+ st.session_state.quick_add_name = stock['name']
241
+ st.rerun()
242
+
243
+ st.markdown("---")
244
+
245
+
246
+ def main():
247
+ username = check_auth()
248
+
249
+ # 사이드바
250
+ with st.sidebar:
251
+ st.markdown(f"### 👤 {username}님")
252
+ st.markdown("---")
253
+ if st.button("🚪 로그아웃", type="secondary"):
254
+ st.session_state.authenticated = False
255
+ st.session_state.username = None
256
+ st.rerun()
257
+
258
+ # 서비스 초기화
259
+ portfolio_service = PortfolioService(username)
260
+ stock_service = StockService()
261
+
262
+ # 헤더
263
+ st.markdown("# 🔍 종목 탐색")
264
+
265
+ # 두 가지 검색 방식
266
+ tab_name, tab_code = st.tabs(["📝 종목명 검색", "🔢 종목코드 검색"])
267
+
268
+ with tab_name:
269
+ st.caption("종목 이름으로 검색 (KOSPI/KOSDAQ 전체)")
270
+ with st.form("search_form"):
271
+ col1, col2, col3 = st.columns([3, 1, 1])
272
+ with col1:
273
+ query = st.text_input(
274
+ "종목명",
275
+ placeholder="예: 삼성전자, SK하이닉스, 카카오..."
276
+ )
277
+ with col2:
278
+ market_filter = st.selectbox("시장", ["전체", "한국", "미국"])
279
+ with col3:
280
+ st.markdown("<br>", unsafe_allow_html=True)
281
+ search_submitted = st.form_submit_button("🔍 검색", type="primary")
282
+
283
+ if search_submitted and query:
284
+ st.session_state.last_query = query
285
+ st.session_state.last_market = market_filter
286
+ st.session_state.code_result = None
287
+
288
+ if 'last_query' in st.session_state and st.session_state.last_query:
289
+ market_map = {"전체": "all", "한국": "kr", "미국": "us"}
290
+ with st.spinner("검색 중..."):
291
+ results = stock_service.search_stocks(
292
+ st.session_state.last_query,
293
+ market_map[st.session_state.get('last_market', '전체')]
294
+ )
295
+ if results:
296
+ st.markdown(f"**{len(results)}개 종목 발견**")
297
+ render_search_results(results, stock_service, portfolio_service)
298
+ else:
299
+ st.info("검색 결과가 없습니다. 종목코드 검색을 이용해보세요.")
300
+
301
+ with tab_code:
302
+ st.caption("6자리 종목코드 직접 입력 (모든 상장종목 조회 가능)")
303
+ with st.form("code_search_form"):
304
+ col1, col2 = st.columns([2, 1])
305
+ with col1:
306
+ code_input = st.text_input(
307
+ "종목코드 (6자리)",
308
+ placeholder="예: 005930, 000660, 035420..."
309
+ )
310
+ with col2:
311
+ st.markdown("<br>", unsafe_allow_html=True)
312
+ code_search = st.form_submit_button("🔍 조회", type="primary")
313
+
314
+ if code_search and code_input:
315
+ code_input = code_input.strip().zfill(6)
316
+ with st.spinner("조회 중..."):
317
+ result = stock_service.search_by_code(code_input)
318
+ if result:
319
+ st.session_state.code_result = result
320
+ st.session_state.last_query = None
321
+ else:
322
+ st.error(f"종목코드 '{code_input}'을 찾을 수 없습니다.")
323
+ st.session_state.code_result = None
324
+
325
+ if 'code_result' in st.session_state and st.session_state.code_result:
326
+ result = st.session_state.code_result
327
+ st.success(f"✅ **{result['name']}** ({result['symbol']}) - {result['market']}")
328
+ col1, col2 = st.columns(2)
329
+ with col1:
330
+ if st.button("📊 상세보기", key="code_view"):
331
+ st.session_state.search_symbol = result['symbol']
332
+ st.rerun()
333
+ with col2:
334
+ if st.button("➕ 포트폴리오 추가", key="code_add"):
335
+ st.session_state.quick_add_symbol = result['symbol']
336
+ st.session_state.quick_add_name = result['name']
337
+ st.rerun()
338
+
339
+ # 빠른 추가 모달
340
+ if 'quick_add_symbol' in st.session_state and st.session_state.quick_add_symbol:
341
+ symbol = st.session_state.quick_add_symbol
342
+ name = st.session_state.get('quick_add_name', symbol)
343
+
344
+ st.markdown("---")
345
+ st.markdown(f"### 💼 {name} 포트폴리오에 추가")
346
+
347
+ with st.form("quick_add_form_modal"):
348
+ col1, col2, col3 = st.columns(3)
349
+ with col1:
350
+ buy_price = st.number_input("매수 단가", min_value=0.0, step=100.0)
351
+ with col2:
352
+ quantity = st.number_input("수량", min_value=1, value=1)
353
+ with col3:
354
+ buy_date = st.date_input("매수일", datetime.now())
355
+
356
+ col_submit, col_cancel = st.columns(2)
357
+ with col_submit:
358
+ if st.form_submit_button("추가", type="primary"):
359
+ market = "krx" if stock_service.is_korean_stock(symbol) else "us"
360
+ portfolio_service.add_holding(
361
+ symbol=symbol,
362
+ company_name=name,
363
+ market=market,
364
+ buy_price=buy_price,
365
+ quantity=quantity,
366
+ buy_date=buy_date.strftime("%Y-%m-%d")
367
+ )
368
+ st.success(f"✅ {name} 추가 완료!")
369
+ st.session_state.quick_add_symbol = None
370
+ st.rerun()
371
+
372
+ if st.button("취소"):
373
+ st.session_state.quick_add_symbol = None
374
+ st.rerun()
375
+
376
+ # 선택된 종목 상세보기
377
+ if 'search_symbol' in st.session_state and st.session_state.search_symbol:
378
+ st.markdown("---")
379
+ st.markdown("### 📊 종목 상세 정보")
380
+ if st.button("❌ 닫기", key="close_detail"):
381
+ st.session_state.search_symbol = None
382
+ st.rerun()
383
+ render_search_result(st.session_state.search_symbol, stock_service, portfolio_service)
384
+
385
+ st.markdown("---")
386
+
387
+ # 인기 종목
388
+ st.markdown("### 📈 인기 종목")
389
+ render_popular_stocks(stock_service, portfolio_service)
390
+
391
+ st.markdown("---")
392
+
393
+ # 관심종목
394
+ st.markdown("### ⭐ 나의 관심종목")
395
+ render_watchlist(portfolio_service, stock_service)
396
+
397
+
398
+ if __name__ == "__main__":
399
+ main()
pages/4_분석리포트.py ADDED
@@ -0,0 +1,223 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ 분석 리포트 - AI 분석 히스토리 조회
3
+ """
4
+ import streamlit as st
5
+ import sys
6
+ import os
7
+ from datetime import datetime
8
+ import pandas as pd
9
+
10
+ sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
11
+
12
+ from services.analysis_service import AnalysisService
13
+ from services.portfolio_service import PortfolioService
14
+
15
+ st.set_page_config(page_title="분석 리포트 - TradingAgents", page_icon="📊", layout="wide")
16
+
17
+ # 공통 CSS - app 메뉴 숨김 및 사이드바 스타일
18
+ st.markdown("""
19
+ <style>
20
+ [data-testid="stSidebarNav"] span {
21
+ font-size: 1.1rem !important;
22
+ font-weight: 500 !important;
23
+ }
24
+ [data-testid="stSidebarNav"] > ul > li:first-child {
25
+ display: none;
26
+ }
27
+ </style>
28
+ """, unsafe_allow_html=True)
29
+
30
+
31
+ def check_auth():
32
+ if 'authenticated' not in st.session_state or not st.session_state.authenticated:
33
+ st.warning("로그인이 필요합니다")
34
+ st.stop()
35
+ return st.session_state.username
36
+
37
+
38
+ def render_report_list(reports: list, analysis_service: AnalysisService):
39
+ """리포트 목록 렌더링"""
40
+ if not reports:
41
+ st.info("아직 분석 리포트가 없습니다. '나의 주식' 메뉴에서 AI 분석을 실행해보세요!")
42
+ return
43
+
44
+ # 테이블 형태로 표시
45
+ data = []
46
+ for r in reports:
47
+ decision = r.get('decision', {})
48
+ action = decision.get('action', 'N/A') if isinstance(decision, dict) else 'N/A'
49
+ confidence = decision.get('confidence', 0) if isinstance(decision, dict) else 0
50
+
51
+ action_emoji = {
52
+ 'BUY': '🟢', 'STRONG_BUY': '🟢',
53
+ 'SELL': '🔴', 'STRONG_SELL': '🔴',
54
+ 'HOLD': '🟡'
55
+ }.get(action.upper(), '⚪')
56
+
57
+ data.append({
58
+ "분석일": r.get('analysis_date', ''),
59
+ "종목": f"{r.get('company_name', '')} ({r.get('symbol', '')})",
60
+ "시장": "🇰🇷" if r.get('market') == 'krx' else "🇺🇸",
61
+ "의견": f"{action_emoji} {action}",
62
+ "신뢰도": f"{confidence}%",
63
+ "생성일": r.get('created_at', '')[:10],
64
+ "report_id": r.get('report_id', ''),
65
+ "symbol": r.get('symbol', '')
66
+ })
67
+
68
+ df = pd.DataFrame(data)
69
+
70
+ # 테이블 표시
71
+ st.dataframe(
72
+ df[["분석일", "종목", "시장", "의견", "신뢰도", "생성일"]],
73
+ use_container_width=True,
74
+ hide_index=True
75
+ )
76
+
77
+ return data
78
+
79
+
80
+ def render_report_detail(report: dict):
81
+ """리포트 상세 보기"""
82
+ if not report:
83
+ return
84
+
85
+ st.markdown(f"## 📋 {report.get('company_name', '')} ({report.get('symbol', '')})")
86
+ st.markdown(f"**분석일**: {report.get('analysis_date', '')} | **생성일**: {report.get('created_at', '')[:19]}")
87
+
88
+ st.markdown("---")
89
+
90
+ # 결정
91
+ decision = report.get('decision', {})
92
+ if isinstance(decision, dict):
93
+ col1, col2 = st.columns(2)
94
+ with col1:
95
+ action = decision.get('action', 'N/A')
96
+ action_korean = {
97
+ 'BUY': '매수', 'STRONG_BUY': '적극 매수',
98
+ 'SELL': '매도', 'STRONG_SELL': '적극 매도',
99
+ 'HOLD': '보유 유지'
100
+ }.get(action.upper(), action)
101
+ st.markdown(f"### 투자 의견: {action_korean}")
102
+ with col2:
103
+ st.metric("신뢰도", f"{decision.get('confidence', 0)}%")
104
+
105
+ if decision.get('reasoning'):
106
+ st.markdown("### 분석 근거")
107
+ st.write(decision.get('reasoning'))
108
+
109
+ st.markdown("---")
110
+
111
+ # 요약 보고서
112
+ summary = report.get('summary', {})
113
+ if summary:
114
+ tabs = st.tabs(["📈 시장 분석", "📰 뉴스", "💬 센티먼트", "📊 펀더멘탈", "📝 투자 계획"])
115
+
116
+ with tabs[0]:
117
+ st.markdown(summary.get('market_report', '정보 없음'))
118
+
119
+ with tabs[1]:
120
+ st.markdown(summary.get('news_report', '정보 없음'))
121
+
122
+ with tabs[2]:
123
+ st.markdown(summary.get('sentiment_report', '정보 없음'))
124
+
125
+ with tabs[3]:
126
+ st.markdown(summary.get('fundamentals_report', '정보 없음'))
127
+
128
+ with tabs[4]:
129
+ st.markdown(summary.get('investment_plan', '정보 없음'))
130
+ st.markdown("---")
131
+ st.markdown("**최종 거래 결정**")
132
+ st.markdown(summary.get('final_trade_decision', '정보 없음'))
133
+
134
+ # 전체 상태 (접힌 상태)
135
+ full_state = report.get('full_state')
136
+ if full_state:
137
+ with st.expander("🔍 전체 분석 데이터 (JSON)"):
138
+ st.json(full_state)
139
+
140
+
141
+ def main():
142
+ username = check_auth()
143
+
144
+ # 사이드바
145
+ with st.sidebar:
146
+ st.markdown(f"### 👤 {username}님")
147
+ st.markdown("---")
148
+ if st.button("🚪 로그아웃", type="secondary"):
149
+ st.session_state.authenticated = False
150
+ st.session_state.username = None
151
+ st.rerun()
152
+
153
+ # 서비스 초기화
154
+ analysis_service = AnalysisService(username)
155
+ portfolio_service = PortfolioService(username)
156
+
157
+ # 헤더
158
+ st.markdown("# 📊 분석 리포트")
159
+
160
+ # 통계
161
+ stats = analysis_service.get_report_statistics()
162
+ col1, col2, col3 = st.columns(3)
163
+ with col1:
164
+ st.metric("총 분석 수", stats.get('total_reports', 0))
165
+ with col2:
166
+ st.metric("분석 종목 수", stats.get('symbols_analyzed', 0))
167
+ with col3:
168
+ dist = stats.get('decision_distribution', {})
169
+ most_common = max(dist.items(), key=lambda x: x[1])[0] if dist else 'N/A'
170
+ st.metric("주요 의견", most_common)
171
+
172
+ st.markdown("---")
173
+
174
+ # 필터
175
+ col1, col2 = st.columns([2, 1])
176
+ with col1:
177
+ holdings = portfolio_service.get_all_holdings()
178
+ symbol_options = ["전체"] + [f"{h['company_name']} ({h['symbol']})" for h in holdings]
179
+ selected_filter = st.selectbox("종목 필터", symbol_options)
180
+
181
+ # 종목 필터 적용
182
+ if selected_filter == "전체":
183
+ reports = analysis_service.get_reports(limit=50)
184
+ else:
185
+ symbol = selected_filter.split("(")[-1].replace(")", "")
186
+ reports = analysis_service.get_reports(symbol=symbol, limit=50)
187
+
188
+ st.markdown("---")
189
+
190
+ # 리포트 목록
191
+ st.markdown("### 📚 분석 리포트 목록")
192
+ report_data = render_report_list(reports, analysis_service)
193
+
194
+ if report_data:
195
+ st.markdown("---")
196
+
197
+ # 리포트 상세 보기
198
+ st.markdown("### 📋 리포트 상세 보기")
199
+
200
+ report_options = {
201
+ f"{r['분석일']} - {r['종목']} ({r['의견']})": (r['symbol'], r['report_id'])
202
+ for r in report_data
203
+ }
204
+
205
+ selected_report = st.selectbox("리포트 선택", list(report_options.keys()))
206
+
207
+ if selected_report:
208
+ symbol, report_id = report_options[selected_report]
209
+ full_report = analysis_service.get_report(symbol, report_id)
210
+ render_report_detail(full_report)
211
+
212
+ # 삭제 버튼
213
+ st.markdown("---")
214
+ col1, col2, col3 = st.columns([1, 1, 2])
215
+ with col1:
216
+ if st.button("🗑️ 이 리포트 삭제", type="secondary"):
217
+ analysis_service.delete_report(symbol, report_id)
218
+ st.success("리포트가 삭제되었습니다")
219
+ st.rerun()
220
+
221
+
222
+ if __name__ == "__main__":
223
+ main()
pages/5_설정.py ADDED
@@ -0,0 +1,261 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ 설정 - 프로필 및 앱 설정
3
+ """
4
+ import streamlit as st
5
+ import sys
6
+ import os
7
+ import json
8
+ from pathlib import Path
9
+ from datetime import datetime
10
+
11
+ sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
12
+
13
+ from services.portfolio_service import PortfolioService
14
+ from services.analysis_service import AnalysisService
15
+
16
+ st.set_page_config(page_title="설정 - TradingAgents", page_icon="⚙️", layout="wide")
17
+
18
+ # 공통 CSS - app 메뉴 숨김 및 사이드바 스타일
19
+ st.markdown("""
20
+ <style>
21
+ [data-testid="stSidebarNav"] span {
22
+ font-size: 1.1rem !important;
23
+ font-weight: 500 !important;
24
+ }
25
+ [data-testid="stSidebarNav"] > ul > li:first-child {
26
+ display: none;
27
+ }
28
+ </style>
29
+ """, unsafe_allow_html=True)
30
+
31
+
32
+ def check_auth():
33
+ if 'authenticated' not in st.session_state or not st.session_state.authenticated:
34
+ st.warning("로그인이 필요합니다")
35
+ st.stop()
36
+ return st.session_state.username
37
+
38
+
39
+ def get_data_size(path: Path) -> str:
40
+ """디렉토리 크기 계산"""
41
+ total = 0
42
+ if path.exists():
43
+ for f in path.rglob('*'):
44
+ if f.is_file():
45
+ total += f.stat().st_size
46
+ if total < 1024:
47
+ return f"{total} B"
48
+ elif total < 1024 * 1024:
49
+ return f"{total / 1024:.1f} KB"
50
+ else:
51
+ return f"{total / (1024 * 1024):.1f} MB"
52
+
53
+
54
+ def render_profile_section(username: str):
55
+ """프로필 섹션"""
56
+ st.markdown("### 👤 프로필")
57
+
58
+ col1, col2 = st.columns(2)
59
+ with col1:
60
+ st.markdown(f"**사용자 ID**: {username}")
61
+ st.markdown(f"**가입일**: 2024-01-01") # 예시
62
+ with col2:
63
+ st.markdown(f"**마지막 접속**: {datetime.now().strftime('%Y-%m-%d %H:%M')}")
64
+
65
+
66
+ def render_data_section(username: str, portfolio_service: PortfolioService,
67
+ analysis_service: AnalysisService):
68
+ """데이터 관리 섹션"""
69
+ st.markdown("### 💾 데이터 관리")
70
+
71
+ # 데이터 현황
72
+ holdings = portfolio_service.get_all_holdings()
73
+ watchlist = portfolio_service.get_watchlist()
74
+ reports = analysis_service.get_reports(limit=100)
75
+
76
+ col1, col2, col3 = st.columns(3)
77
+ with col1:
78
+ st.metric("보유 종목", f"{len(holdings)}개")
79
+ with col2:
80
+ st.metric("관심 종목", f"{len(watchlist)}개")
81
+ with col3:
82
+ st.metric("분석 리포트", f"{len(reports)}개")
83
+
84
+ # 데이터 크기
85
+ data_path = Path(__file__).parent.parent / "data" / "users" / username
86
+ st.info(f"데이터 저장 위치: `{data_path}`")
87
+ st.info(f"사용 용량: {get_data_size(data_path)}")
88
+
89
+ st.markdown("---")
90
+
91
+ # 데이터 내보내기
92
+ st.markdown("#### 📤 데이터 내보내기")
93
+
94
+ col1, col2 = st.columns(2)
95
+
96
+ with col1:
97
+ if st.button("📋 포트폴리오 내보내기 (JSON)"):
98
+ portfolio = portfolio_service.load_portfolio()
99
+ json_str = json.dumps(portfolio, ensure_ascii=False, indent=2)
100
+ st.download_button(
101
+ "💾 다운로드",
102
+ json_str,
103
+ file_name=f"portfolio_{username}_{datetime.now().strftime('%Y%m%d')}.json",
104
+ mime="application/json"
105
+ )
106
+
107
+ with col2:
108
+ if st.button("⭐ 관심종목 내보내기 (JSON)"):
109
+ watchlist_data = portfolio_service.load_watchlist()
110
+ json_str = json.dumps(watchlist_data, ensure_ascii=False, indent=2)
111
+ st.download_button(
112
+ "💾 다운로드",
113
+ json_str,
114
+ file_name=f"watchlist_{username}_{datetime.now().strftime('%Y%m%d')}.json",
115
+ mime="application/json"
116
+ )
117
+
118
+ st.markdown("---")
119
+
120
+ # 데이터 초기화
121
+ st.markdown("#### ⚠️ 데이터 초기화")
122
+ st.warning("주의: 데이터 초기화는 되돌릴 수 없습니다!")
123
+
124
+ col1, col2, col3 = st.columns(3)
125
+
126
+ with col1:
127
+ if st.button("🗑️ 포트폴리오 초기화", type="secondary"):
128
+ if 'confirm_portfolio_reset' not in st.session_state:
129
+ st.session_state.confirm_portfolio_reset = True
130
+ st.rerun()
131
+
132
+ if st.session_state.get('confirm_portfolio_reset'):
133
+ st.error("정말 삭제하시겠습니까?")
134
+ if st.button("✅ 확인", key="confirm_port"):
135
+ portfolio_path = data_path / "portfolio.json"
136
+ if portfolio_path.exists():
137
+ portfolio_path.unlink()
138
+ st.session_state.confirm_portfolio_reset = False
139
+ st.success("포트폴리오가 초기화되었습니다")
140
+ st.rerun()
141
+ if st.button("❌ 취소", key="cancel_port"):
142
+ st.session_state.confirm_portfolio_reset = False
143
+ st.rerun()
144
+
145
+ with col2:
146
+ if st.button("🗑️ 관심종목 초기화", type="secondary"):
147
+ watchlist_path = data_path / "watchlist.json"
148
+ if watchlist_path.exists():
149
+ watchlist_path.unlink()
150
+ st.success("관심종목이 초기화되었습니다")
151
+ st.rerun()
152
+
153
+ with col3:
154
+ if st.button("🗑️ 분석 리포트 초기화", type="secondary"):
155
+ reports_path = data_path / "reports"
156
+ if reports_path.exists():
157
+ import shutil
158
+ shutil.rmtree(reports_path)
159
+ st.success("분석 리포트가 초기화되었습니다")
160
+ st.rerun()
161
+
162
+
163
+ def render_api_section():
164
+ """API 설정 섹션"""
165
+ st.markdown("### 🔑 API 설정")
166
+
167
+ st.info("AI 분석에 사용되는 API 키는 `.env` 파일에서 관리됩니다.")
168
+
169
+ env_path = Path(__file__).parent.parent / ".env"
170
+ env_example_path = Path(__file__).parent.parent / ".env.example"
171
+
172
+ if env_path.exists():
173
+ st.success("✅ `.env` 파일이 설정되어 있습니다")
174
+ else:
175
+ st.warning("⚠️ `.env` 파일이 없습니다. `.env.example`을 참고하여 생성해주세요")
176
+
177
+ st.markdown("""
178
+ **필요한 API 키:**
179
+ - `OPENAI_API_KEY`: OpenAI API 키 (필수)
180
+ - `ALPHA_VANTAGE_API_KEY`: Alpha Vantage API 키 (선택)
181
+ - `FINNHUB_API_KEY`: Finnhub API 키 (선택)
182
+ """)
183
+
184
+
185
+ def render_about_section():
186
+ """앱 정보 섹션"""
187
+ st.markdown("### ℹ️ 앱 정보")
188
+
189
+ st.markdown("""
190
+ **TradingAgents** - AI 기반 주식 분석 시스템
191
+
192
+ | 항목 | 정보 |
193
+ |------|------|
194
+ | 버전 | 1.0.0 |
195
+ | 프레임워크 | Streamlit |
196
+ | AI 엔진 | LangGraph + OpenAI |
197
+ | 데이터 소스 | Yahoo Finance, pykrx |
198
+
199
+ ---
200
+
201
+ **지원 기능:**
202
+ - 🇰🇷 한국 주식 (KOSPI, KOSDAQ)
203
+ - 🇺🇸 미국 주식 (NYSE, NASDAQ)
204
+ - 🤖 AI 멀티 에이전트 분석
205
+ - 📊 기술적 지표 분석
206
+ - 📰 뉴스 & 센티먼트 분석
207
+
208
+ ---
209
+
210
+ **면책 조항:**
211
+
212
+ ⚠️ 본 시스템은 **교육 및 연구 목적**으로만 사용됩니다.
213
+ - 투자 조언이 아닙니다
214
+ - 실제 투자 결정에 사용하지 마세요
215
+ - 과거 성과가 미래 수익을 보장하지 않습니다
216
+ - AI 분석 결과는 실행할 때마다 다를 수 있습니다
217
+
218
+ ---
219
+
220
+ **크레딧:**
221
+ - [TradingAgents](https://github.com/TauricResearch/TradingAgents) by Tauric Research
222
+ """)
223
+
224
+
225
+ def main():
226
+ username = check_auth()
227
+
228
+ # 사이드바
229
+ with st.sidebar:
230
+ st.markdown(f"### 👤 {username}님")
231
+ st.markdown("---")
232
+ if st.button("🚪 로그아웃", type="secondary"):
233
+ st.session_state.authenticated = False
234
+ st.session_state.username = None
235
+ st.rerun()
236
+
237
+ # 서비스 초기화
238
+ portfolio_service = PortfolioService(username)
239
+ analysis_service = AnalysisService(username)
240
+
241
+ # 헤더
242
+ st.markdown("# ⚙️ 설정")
243
+
244
+ # 탭
245
+ tab1, tab2, tab3, tab4 = st.tabs(["👤 프로필", "💾 데이터", "🔑 API", "ℹ️ 정보"])
246
+
247
+ with tab1:
248
+ render_profile_section(username)
249
+
250
+ with tab2:
251
+ render_data_section(username, portfolio_service, analysis_service)
252
+
253
+ with tab3:
254
+ render_api_section()
255
+
256
+ with tab4:
257
+ render_about_section()
258
+
259
+
260
+ if __name__ == "__main__":
261
+ main()
requirements.txt ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ typing-extensions
2
+ python-dotenv
3
+ langchain-openai
4
+ tenacity
5
+ beautifulsoup4
6
+ langchain-experimental
7
+ pandas
8
+ plotly
9
+ streamlit
10
+ yfinance
11
+ praw
12
+ feedparser
13
+ stockstats
14
+ eodhd
15
+ langgraph
16
+ chromadb
17
+ setuptools
18
+ backtrader
19
+ akshare
20
+ tushare
21
+ finnhub-python
22
+ parsel
23
+ requests
24
+ tqdm
25
+ pytz
26
+ redis
27
+ chainlit
28
+ rich
29
+ questionary
30
+ langchain_anthropic
31
+ langchain-google-genai
32
+ # 한국 주식 데이터 라이브러리
33
+ pykrx
34
+ finance-datareader
35
+
36
+ # 딥러닝 시계열 예측
37
+ neuralforecast
38
+ scipy
services/__init__.py ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ """
2
+ TradingAgents 서비스 모듈
3
+ """
4
+ from .portfolio_service import PortfolioService
5
+ from .stock_service import StockService
6
+ from .analysis_service import AnalysisService
7
+
8
+ __all__ = ['PortfolioService', 'StockService', 'AnalysisService']
services/analysis_service.py ADDED
@@ -0,0 +1,300 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ AI 분석 서비스
3
+ TradingAgents AI 분석 실행 및 리포트 관리
4
+ """
5
+ import json
6
+ import os
7
+ from pathlib import Path
8
+ from datetime import datetime
9
+ from typing import Dict, Optional, List, Tuple
10
+ import uuid
11
+
12
+ # .env 파일 로드
13
+ from dotenv import load_dotenv
14
+ load_dotenv()
15
+
16
+
17
+ class AnalysisService:
18
+ """AI 분석 및 리포트 관리 서비스"""
19
+
20
+ def __init__(self, user_id: str):
21
+ self.user_id = user_id
22
+ self.reports_path = Path(__file__).parent.parent / "data" / "users" / user_id / "reports"
23
+ self._ensure_reports_directory()
24
+
25
+ def _ensure_reports_directory(self):
26
+ """리포트 디렉토리 생성"""
27
+ self.reports_path.mkdir(parents=True, exist_ok=True)
28
+
29
+ def run_analysis(self, symbol: str, analysis_date: str,
30
+ progress_callback=None) -> Tuple[Optional[Dict], Optional[str]]:
31
+ """
32
+ AI 분석 실행
33
+
34
+ Args:
35
+ symbol: 종목 코드/티커
36
+ analysis_date: 분석 기준일 (YYYY-MM-DD)
37
+ progress_callback: 진행 상황 콜백 함수
38
+
39
+ Returns:
40
+ (decision_dict, error_message)
41
+ """
42
+ try:
43
+ from tradingagents.graph.trading_graph import TradingAgentsGraph
44
+ from tradingagents.default_config import DEFAULT_CONFIG
45
+
46
+ config = DEFAULT_CONFIG.copy()
47
+ ta = TradingAgentsGraph(debug=False, config=config)
48
+
49
+ if progress_callback:
50
+ progress_callback("AI 에이전트 초기화 완료")
51
+
52
+ # 분석 실행
53
+ final_state, decision = ta.propagate(symbol, analysis_date)
54
+
55
+ if progress_callback:
56
+ progress_callback("분석 완료")
57
+
58
+ # decision을 상세 딕셔너리로 변환
59
+ decision_dict = self._build_decision_dict(decision, final_state)
60
+
61
+ # 리포트 저장
62
+ report = self._create_report(symbol, analysis_date, final_state, decision_dict)
63
+ self.save_report(report)
64
+
65
+ return decision_dict, None
66
+
67
+ except Exception as e:
68
+ return None, str(e)
69
+
70
+ def _build_decision_dict(self, decision, final_state: Dict) -> Dict:
71
+ """단순 결정을 상세 딕셔너리로 변환"""
72
+ # decision이 이미 딕셔너리면 그대로 반환
73
+ if isinstance(decision, dict):
74
+ return decision
75
+
76
+ # 문자열인 경우 상세 정보 구성
77
+ action = decision.upper() if isinstance(decision, str) else "HOLD"
78
+
79
+ # final_state에서 근거 추출
80
+ reasoning_parts = []
81
+
82
+ # 최종 거래 결정에서 근거 추출
83
+ final_decision = final_state.get("final_trade_decision", "")
84
+ if final_decision:
85
+ reasoning_parts.append(f"**최종 판단:**\n{final_decision[:500]}")
86
+
87
+ # 투자 계획에서 근거 추출
88
+ investment_plan = final_state.get("investment_plan", "")
89
+ if investment_plan:
90
+ reasoning_parts.append(f"**투자 계획:**\n{investment_plan[:500]}")
91
+
92
+ # 각 분석 리포트 요약
93
+ reports = []
94
+ if final_state.get("market_report"):
95
+ reports.append(f"📈 시장분석: {final_state['market_report'][:200]}...")
96
+ if final_state.get("news_report"):
97
+ reports.append(f"📰 뉴스분석: {final_state['news_report'][:200]}...")
98
+ if final_state.get("fundamentals_report"):
99
+ reports.append(f"📊 펀더멘탈: {final_state['fundamentals_report'][:200]}...")
100
+ if final_state.get("sentiment_report"):
101
+ reports.append(f"💬 센티먼트: {final_state['sentiment_report'][:200]}...")
102
+
103
+ if reports:
104
+ reasoning_parts.append("**분석 요약:**\n" + "\n".join(reports))
105
+
106
+ reasoning = "\n\n".join(reasoning_parts) if reasoning_parts else "상세 분석 정보 없음"
107
+
108
+ # 신뢰도 계산 (분석 리포트 수에 따라)
109
+ report_count = sum(1 for k in ["market_report", "news_report", "fundamentals_report", "sentiment_report"]
110
+ if final_state.get(k))
111
+ confidence = min(report_count * 20 + 20, 80) # 20~80%
112
+
113
+ return {
114
+ "action": action,
115
+ "confidence": confidence,
116
+ "reasoning": reasoning
117
+ }
118
+
119
+ def _create_report(self, symbol: str, analysis_date: str,
120
+ final_state: Dict, decision: Dict) -> Dict:
121
+ """분석 결과로 리포트 생성"""
122
+ from services.stock_service import StockService
123
+ stock_service = StockService()
124
+ company_name, market = stock_service.get_company_info(symbol)
125
+
126
+ return {
127
+ "report_id": str(uuid.uuid4()),
128
+ "user_id": self.user_id,
129
+ "symbol": symbol,
130
+ "company_name": company_name,
131
+ "market": "krx" if StockService.is_korean_stock(symbol) else "us",
132
+ "analysis_date": analysis_date,
133
+ "created_at": datetime.now().isoformat(),
134
+ "decision": decision,
135
+ "summary": self._extract_summary(final_state),
136
+ "full_state": final_state
137
+ }
138
+
139
+ def _extract_summary(self, final_state: Dict) -> Dict:
140
+ """전체 상태에서 요약 정보 추출"""
141
+ if not final_state:
142
+ return {}
143
+
144
+ return {
145
+ "market_report": final_state.get("market_report", ""),
146
+ "sentiment_report": final_state.get("sentiment_report", ""),
147
+ "news_report": final_state.get("news_report", ""),
148
+ "fundamentals_report": final_state.get("fundamentals_report", ""),
149
+ "investment_plan": final_state.get("investment_plan", ""),
150
+ "final_trade_decision": final_state.get("final_trade_decision", "")
151
+ }
152
+
153
+ def save_report(self, report: Dict) -> str:
154
+ """리포트 저장"""
155
+ symbol = report["symbol"]
156
+ report_id = report["report_id"]
157
+ analysis_date = report["analysis_date"]
158
+
159
+ # 종목별 디렉토리 생성
160
+ symbol_path = self.reports_path / symbol
161
+ symbol_path.mkdir(exist_ok=True)
162
+
163
+ # 파일명: {날짜}_{report_id}.json
164
+ filename = f"{analysis_date}_{report_id[:8]}.json"
165
+ file_path = symbol_path / filename
166
+
167
+ with open(file_path, 'w', encoding='utf-8') as f:
168
+ json.dump(report, f, ensure_ascii=False, indent=2, default=str)
169
+
170
+ return str(file_path)
171
+
172
+ def get_reports(self, symbol: str = None, limit: int = 20) -> List[Dict]:
173
+ """리포트 목록 조회"""
174
+ reports = []
175
+
176
+ if symbol:
177
+ # 특정 종목 리포트
178
+ symbol_path = self.reports_path / symbol
179
+ if symbol_path.exists():
180
+ for file_path in sorted(symbol_path.glob("*.json"), reverse=True)[:limit]:
181
+ try:
182
+ with open(file_path, 'r', encoding='utf-8') as f:
183
+ report = json.load(f)
184
+ # 전체 상태는 목록에서 제외 (크기 줄이기)
185
+ report.pop("full_state", None)
186
+ reports.append(report)
187
+ except:
188
+ pass
189
+ else:
190
+ # 전체 리포트
191
+ for symbol_dir in self.reports_path.iterdir():
192
+ if symbol_dir.is_dir():
193
+ for file_path in symbol_dir.glob("*.json"):
194
+ try:
195
+ with open(file_path, 'r', encoding='utf-8') as f:
196
+ report = json.load(f)
197
+ report.pop("full_state", None)
198
+ reports.append(report)
199
+ except:
200
+ pass
201
+
202
+ # 날짜순 정렬
203
+ reports.sort(key=lambda x: x.get("created_at", ""), reverse=True)
204
+ reports = reports[:limit]
205
+
206
+ return reports
207
+
208
+ def get_report(self, symbol: str, report_id: str) -> Optional[Dict]:
209
+ """특정 리포트 상세 조회"""
210
+ symbol_path = self.reports_path / symbol
211
+
212
+ if not symbol_path.exists():
213
+ return None
214
+
215
+ for file_path in symbol_path.glob(f"*_{report_id[:8]}.json"):
216
+ try:
217
+ with open(file_path, 'r', encoding='utf-8') as f:
218
+ return json.load(f)
219
+ except:
220
+ pass
221
+
222
+ return None
223
+
224
+ def delete_report(self, symbol: str, report_id: str) -> bool:
225
+ """리포트 삭제"""
226
+ symbol_path = self.reports_path / symbol
227
+
228
+ if not symbol_path.exists():
229
+ return False
230
+
231
+ for file_path in symbol_path.glob(f"*_{report_id[:8]}.json"):
232
+ try:
233
+ file_path.unlink()
234
+ return True
235
+ except:
236
+ pass
237
+
238
+ return False
239
+
240
+ def get_latest_report(self, symbol: str) -> Optional[Dict]:
241
+ """특정 종목의 최신 리포트 조회"""
242
+ reports = self.get_reports(symbol=symbol, limit=1)
243
+ return reports[0] if reports else None
244
+
245
+ def format_decision_korean(self, decision) -> str:
246
+ """결정 정보를 한국어로 포맷팅"""
247
+ if not decision:
248
+ return "분석 결과 없음"
249
+
250
+ # decision이 문자열인 경우 처리
251
+ if isinstance(decision, str):
252
+ action = decision
253
+ confidence = 0
254
+ reasoning = ""
255
+ else:
256
+ action = decision.get("action", "HOLD")
257
+ confidence = decision.get("confidence", 0)
258
+ reasoning = decision.get("reasoning", "")
259
+
260
+ action_korean = {
261
+ "BUY": "매수",
262
+ "SELL": "매도",
263
+ "HOLD": "보유 유지",
264
+ "STRONG_BUY": "적극 매수",
265
+ "STRONG_SELL": "적극 매도"
266
+ }.get(action.upper(), action)
267
+
268
+ return f"""
269
+ ## 투자 의견: {action_korean}
270
+
271
+ **신뢰도**: {confidence}%
272
+
273
+ ### 분석 근거
274
+ {reasoning}
275
+ """
276
+
277
+ def get_report_statistics(self) -> Dict:
278
+ """리포트 통계 조회"""
279
+ reports = self.get_reports(limit=100)
280
+
281
+ if not reports:
282
+ return {
283
+ "total_reports": 0,
284
+ "symbols_analyzed": 0,
285
+ "decision_distribution": {}
286
+ }
287
+
288
+ symbols = set(r.get("symbol") for r in reports)
289
+ decisions = {}
290
+ for r in reports:
291
+ decision = r.get("decision", {})
292
+ action = decision.get("action", "UNKNOWN") if isinstance(decision, dict) else "UNKNOWN"
293
+ decisions[action] = decisions.get(action, 0) + 1
294
+
295
+ return {
296
+ "total_reports": len(reports),
297
+ "symbols_analyzed": len(symbols),
298
+ "decision_distribution": decisions,
299
+ "recent_reports": reports[:5]
300
+ }
services/portfolio_service.py ADDED
@@ -0,0 +1,246 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ 포트폴리오 관리 서비스
3
+ 사용자별 포트폴리오 데이터를 JSON 파일로 관리
4
+ """
5
+ import json
6
+ import os
7
+ from pathlib import Path
8
+ from datetime import datetime
9
+ from typing import List, Dict, Optional
10
+ import uuid
11
+
12
+
13
+ class PortfolioService:
14
+ """포트폴리오 CRUD 서비스"""
15
+
16
+ def __init__(self, user_id: str):
17
+ self.user_id = user_id
18
+ self.base_path = Path(__file__).parent.parent / "data" / "users" / user_id
19
+ self.portfolio_path = self.base_path / "portfolio.json"
20
+ self.watchlist_path = self.base_path / "watchlist.json"
21
+ self._ensure_user_directory()
22
+
23
+ def _ensure_user_directory(self):
24
+ """사용자 디렉토리가 없으면 생성"""
25
+ self.base_path.mkdir(parents=True, exist_ok=True)
26
+
27
+ def _get_empty_portfolio(self) -> Dict:
28
+ """빈 포트폴리오 구조 반환"""
29
+ return {
30
+ "user_id": self.user_id,
31
+ "created_at": datetime.now().isoformat(),
32
+ "updated_at": datetime.now().isoformat(),
33
+ "holdings": []
34
+ }
35
+
36
+ def _get_empty_watchlist(self) -> Dict:
37
+ """빈 관심종목 구조 반환"""
38
+ return {
39
+ "user_id": self.user_id,
40
+ "updated_at": datetime.now().isoformat(),
41
+ "stocks": []
42
+ }
43
+
44
+ # ==================== 포트폴리오 CRUD ====================
45
+
46
+ def load_portfolio(self) -> Dict:
47
+ """포트폴리오 로드"""
48
+ if self.portfolio_path.exists():
49
+ with open(self.portfolio_path, 'r', encoding='utf-8') as f:
50
+ return json.load(f)
51
+ return self._get_empty_portfolio()
52
+
53
+ def save_portfolio(self, portfolio: Dict):
54
+ """포트폴리오 저장"""
55
+ portfolio["updated_at"] = datetime.now().isoformat()
56
+ with open(self.portfolio_path, 'w', encoding='utf-8') as f:
57
+ json.dump(portfolio, f, ensure_ascii=False, indent=2)
58
+
59
+ def add_holding(self, symbol: str, company_name: str, market: str,
60
+ buy_price: float, quantity: int, buy_date: str,
61
+ memo: str = "") -> Dict:
62
+ """종목 추가 또는 추가 매수"""
63
+ portfolio = self.load_portfolio()
64
+
65
+ # 기존 보유 종목인지 확인
66
+ existing = next(
67
+ (h for h in portfolio["holdings"] if h["symbol"] == symbol),
68
+ None
69
+ )
70
+
71
+ if existing:
72
+ # 추가 매수: 평균 단가 재계산
73
+ total_cost = (existing["buy_price"] * existing["quantity"]) + (buy_price * quantity)
74
+ total_qty = existing["quantity"] + quantity
75
+ existing["buy_price"] = round(total_cost / total_qty, 2)
76
+ existing["quantity"] = total_qty
77
+ existing["memo"] = memo if memo else existing.get("memo", "")
78
+ holding = existing
79
+ else:
80
+ # 새 종목 추가
81
+ holding = {
82
+ "id": str(uuid.uuid4()),
83
+ "symbol": symbol,
84
+ "company_name": company_name,
85
+ "market": market, # "krx" or "us"
86
+ "buy_price": buy_price,
87
+ "quantity": quantity,
88
+ "buy_date": buy_date,
89
+ "memo": memo
90
+ }
91
+ portfolio["holdings"].append(holding)
92
+
93
+ self.save_portfolio(portfolio)
94
+ return holding
95
+
96
+ def update_holding(self, symbol: str, buy_price: float = None,
97
+ quantity: int = None, memo: str = None) -> Optional[Dict]:
98
+ """보유 종목 수정"""
99
+ portfolio = self.load_portfolio()
100
+
101
+ for holding in portfolio["holdings"]:
102
+ if holding["symbol"] == symbol:
103
+ if buy_price is not None:
104
+ holding["buy_price"] = buy_price
105
+ if quantity is not None:
106
+ holding["quantity"] = quantity
107
+ if memo is not None:
108
+ holding["memo"] = memo
109
+ self.save_portfolio(portfolio)
110
+ return holding
111
+
112
+ return None
113
+
114
+ def remove_holding(self, symbol: str) -> bool:
115
+ """보유 종목 삭제"""
116
+ portfolio = self.load_portfolio()
117
+ original_len = len(portfolio["holdings"])
118
+ portfolio["holdings"] = [
119
+ h for h in portfolio["holdings"] if h["symbol"] != symbol
120
+ ]
121
+
122
+ if len(portfolio["holdings"]) < original_len:
123
+ self.save_portfolio(portfolio)
124
+ return True
125
+ return False
126
+
127
+ def get_holding(self, symbol: str) -> Optional[Dict]:
128
+ """특정 보유 종목 조회"""
129
+ portfolio = self.load_portfolio()
130
+ return next(
131
+ (h for h in portfolio["holdings"] if h["symbol"] == symbol),
132
+ None
133
+ )
134
+
135
+ def get_all_holdings(self) -> List[Dict]:
136
+ """전체 보유 종목 조회"""
137
+ portfolio = self.load_portfolio()
138
+ return portfolio.get("holdings", [])
139
+
140
+ # ==================== 관심종목 CRUD ====================
141
+
142
+ def load_watchlist(self) -> Dict:
143
+ """관심종목 로드"""
144
+ if self.watchlist_path.exists():
145
+ with open(self.watchlist_path, 'r', encoding='utf-8') as f:
146
+ return json.load(f)
147
+ return self._get_empty_watchlist()
148
+
149
+ def save_watchlist(self, watchlist: Dict):
150
+ """관심종목 저장"""
151
+ watchlist["updated_at"] = datetime.now().isoformat()
152
+ with open(self.watchlist_path, 'w', encoding='utf-8') as f:
153
+ json.dump(watchlist, f, ensure_ascii=False, indent=2)
154
+
155
+ def add_to_watchlist(self, symbol: str, company_name: str, market: str,
156
+ target_price: float = None, notes: str = "") -> Dict:
157
+ """관심종목 추가"""
158
+ watchlist = self.load_watchlist()
159
+
160
+ # 이미 있는지 확인
161
+ if any(s["symbol"] == symbol for s in watchlist["stocks"]):
162
+ return None
163
+
164
+ stock = {
165
+ "symbol": symbol,
166
+ "company_name": company_name,
167
+ "market": market,
168
+ "added_at": datetime.now().isoformat(),
169
+ "target_price": target_price,
170
+ "notes": notes
171
+ }
172
+ watchlist["stocks"].append(stock)
173
+ self.save_watchlist(watchlist)
174
+ return stock
175
+
176
+ def remove_from_watchlist(self, symbol: str) -> bool:
177
+ """관심종목 삭제"""
178
+ watchlist = self.load_watchlist()
179
+ original_len = len(watchlist["stocks"])
180
+ watchlist["stocks"] = [
181
+ s for s in watchlist["stocks"] if s["symbol"] != symbol
182
+ ]
183
+
184
+ if len(watchlist["stocks"]) < original_len:
185
+ self.save_watchlist(watchlist)
186
+ return True
187
+ return False
188
+
189
+ def get_watchlist(self) -> List[Dict]:
190
+ """관심종목 목록 조회"""
191
+ watchlist = self.load_watchlist()
192
+ return watchlist.get("stocks", [])
193
+
194
+ # ==================== 수익률 계산 ====================
195
+
196
+ def calculate_profit(self, holding: Dict, current_price: float) -> Dict:
197
+ """개별 종목 수익률 계산"""
198
+ invested = holding["buy_price"] * holding["quantity"]
199
+ current_value = current_price * holding["quantity"]
200
+ profit = current_value - invested
201
+ profit_rate = (profit / invested) * 100 if invested > 0 else 0
202
+
203
+ return {
204
+ "symbol": holding["symbol"],
205
+ "company_name": holding["company_name"],
206
+ "buy_price": holding["buy_price"],
207
+ "current_price": current_price,
208
+ "quantity": holding["quantity"],
209
+ "invested": invested,
210
+ "current_value": current_value,
211
+ "profit": profit,
212
+ "profit_rate": round(profit_rate, 2)
213
+ }
214
+
215
+ def calculate_portfolio_summary(self, current_prices: Dict[str, float]) -> Dict:
216
+ """포트폴리오 전체 요약 계산"""
217
+ holdings = self.get_all_holdings()
218
+
219
+ if not holdings:
220
+ return {
221
+ "total_invested": 0,
222
+ "total_current": 0,
223
+ "total_profit": 0,
224
+ "total_profit_rate": 0,
225
+ "holdings_count": 0
226
+ }
227
+
228
+ total_invested = 0
229
+ total_current = 0
230
+
231
+ for h in holdings:
232
+ invested = h["buy_price"] * h["quantity"]
233
+ current = current_prices.get(h["symbol"], h["buy_price"]) * h["quantity"]
234
+ total_invested += invested
235
+ total_current += current
236
+
237
+ total_profit = total_current - total_invested
238
+ total_profit_rate = (total_profit / total_invested) * 100 if total_invested > 0 else 0
239
+
240
+ return {
241
+ "total_invested": round(total_invested, 2),
242
+ "total_current": round(total_current, 2),
243
+ "total_profit": round(total_profit, 2),
244
+ "total_profit_rate": round(total_profit_rate, 2),
245
+ "holdings_count": len(holdings)
246
+ }
services/stock_service.py ADDED
@@ -0,0 +1,641 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ 주식 데이터 서비스
3
+ 한국/미국 주식 데이터 조회 및 차트 생성
4
+ """
5
+ from __future__ import annotations
6
+
7
+ import pandas as pd
8
+ from datetime import datetime, timedelta
9
+ from typing import Tuple, Optional, Dict, List, TYPE_CHECKING
10
+ from io import StringIO
11
+
12
+ if TYPE_CHECKING:
13
+ import plotly.graph_objects as go
14
+
15
+
16
+ class StockService:
17
+ """주식 데이터 조회 및 차트 생성 서비스"""
18
+
19
+ _krx_stock_list = None # 메모리 캐시
20
+ _cache_file = None # 파일 캐시 경로
21
+
22
+ # 주요 한국 종목 리스트 (API 실패 시 백업)
23
+ KRX_POPULAR_STOCKS = [
24
+ # KOSPI 대형주
25
+ {"symbol": "005930", "name": "삼성전자", "market": "KOSPI"},
26
+ {"symbol": "000660", "name": "SK하이닉스", "market": "KOSPI"},
27
+ {"symbol": "005380", "name": "현대차", "market": "KOSPI"},
28
+ {"symbol": "000270", "name": "기아", "market": "KOSPI"},
29
+ {"symbol": "005490", "name": "POSCO홀딩스", "market": "KOSPI"},
30
+ {"symbol": "035420", "name": "NAVER", "market": "KOSPI"},
31
+ {"symbol": "035720", "name": "카카오", "market": "KOSPI"},
32
+ {"symbol": "051910", "name": "LG화학", "market": "KOSPI"},
33
+ {"symbol": "006400", "name": "삼성SDI", "market": "KOSPI"},
34
+ {"symbol": "003670", "name": "포스코퓨처엠", "market": "KOSPI"},
35
+ {"symbol": "068270", "name": "셀트리온", "market": "KOSPI"},
36
+ {"symbol": "207940", "name": "삼성바이오로직스", "market": "KOSPI"},
37
+ {"symbol": "005935", "name": "삼성전자우", "market": "KOSPI"},
38
+ {"symbol": "028260", "name": "삼성물산", "market": "KOSPI"},
39
+ {"symbol": "012330", "name": "현대모비스", "market": "KOSPI"},
40
+ {"symbol": "066570", "name": "LG전자", "market": "KOSPI"},
41
+ {"symbol": "003550", "name": "LG", "market": "KOSPI"},
42
+ {"symbol": "096770", "name": "SK이노베이션", "market": "KOSPI"},
43
+ {"symbol": "034730", "name": "SK", "market": "KOSPI"},
44
+ {"symbol": "015760", "name": "한국전력", "market": "KOSPI"},
45
+ {"symbol": "017670", "name": "SK텔레콤", "market": "KOSPI"},
46
+ {"symbol": "030200", "name": "KT", "market": "KOSPI"},
47
+ {"symbol": "032830", "name": "삼성생명", "market": "KOSPI"},
48
+ {"symbol": "055550", "name": "신한지주", "market": "KOSPI"},
49
+ {"symbol": "105560", "name": "KB금융", "market": "KOSPI"},
50
+ {"symbol": "086790", "name": "하나금융지주", "market": "KOSPI"},
51
+ {"symbol": "316140", "name": "우리금융지주", "market": "KOSPI"},
52
+ {"symbol": "009150", "name": "삼성전기", "market": "KOSPI"},
53
+ {"symbol": "000810", "name": "삼성화재", "market": "KOSPI"},
54
+ {"symbol": "018260", "name": "삼성에스디에스", "market": "KOSPI"},
55
+ {"symbol": "033780", "name": "KT&G", "market": "KOSPI"},
56
+ {"symbol": "010130", "name": "고려아연", "market": "KOSPI"},
57
+ {"symbol": "090430", "name": "아모레퍼시픽", "market": "KOSPI"},
58
+ {"symbol": "011170", "name": "롯데케미칼", "market": "KOSPI"},
59
+ {"symbol": "010950", "name": "S-Oil", "market": "KOSPI"},
60
+ {"symbol": "036570", "name": "엔씨소프트", "market": "KOSPI"},
61
+ {"symbol": "251270", "name": "넷마블", "market": "KOSPI"},
62
+ {"symbol": "352820", "name": "하이브", "market": "KOSPI"},
63
+ {"symbol": "011780", "name": "금호석유", "market": "KOSPI"},
64
+ {"symbol": "086280", "name": "현대글로비스", "market": "KOSPI"},
65
+ # KOSDAQ 주요 종목
66
+ {"symbol": "247540", "name": "에코프로비엠", "market": "KOSDAQ"},
67
+ {"symbol": "086520", "name": "에코프로", "market": "KOSDAQ"},
68
+ {"symbol": "028300", "name": "HLB", "market": "KOSDAQ"},
69
+ {"symbol": "293490", "name": "카카오게임즈", "market": "KOSDAQ"},
70
+ {"symbol": "263750", "name": "펄어비스", "market": "KOSDAQ"},
71
+ {"symbol": "112040", "name": "위메이드", "market": "KOSDAQ"},
72
+ {"symbol": "041510", "name": "에스엠", "market": "KOSDAQ"},
73
+ {"symbol": "035900", "name": "JYP Ent.", "market": "KOSDAQ"},
74
+ {"symbol": "122870", "name": "와이지엔터테인먼트", "market": "KOSDAQ"},
75
+ {"symbol": "377300", "name": "카카오페이", "market": "KOSDAQ"},
76
+ {"symbol": "323410", "name": "카카오뱅크", "market": "KOSDAQ"},
77
+ {"symbol": "145020", "name": "휴젤", "market": "KOSDAQ"},
78
+ {"symbol": "091990", "name": "셀트리온헬스케어", "market": "KOSDAQ"},
79
+ {"symbol": "196170", "name": "알테오젠", "market": "KOSDAQ"},
80
+ {"symbol": "141080", "name": "레고켐바이오", "market": "KOSDAQ"},
81
+ {"symbol": "095340", "name": "ISC", "market": "KOSDAQ"},
82
+ {"symbol": "357780", "name": "솔브레인", "market": "KOSDAQ"},
83
+ {"symbol": "058470", "name": "리노공업", "market": "KOSDAQ"},
84
+ {"symbol": "140860", "name": "파크시스템스", "market": "KOSDAQ"},
85
+ {"symbol": "067310", "name": "하나마이크론", "market": "KOSDAQ"},
86
+ ]
87
+
88
+ @classmethod
89
+ def _get_cache_path(cls):
90
+ """캐시 파일 경로"""
91
+ import os
92
+ cache_dir = os.path.join(os.path.dirname(__file__), '..', 'data', 'cache')
93
+ os.makedirs(cache_dir, exist_ok=True)
94
+ return os.path.join(cache_dir, 'krx_stocks.json')
95
+
96
+ @classmethod
97
+ def _load_from_cache(cls) -> Optional[pd.DataFrame]:
98
+ """로컬 캐시에서 로드 (24시간 유효)"""
99
+ import os
100
+ import json
101
+ cache_path = cls._get_cache_path()
102
+ if os.path.exists(cache_path):
103
+ try:
104
+ mtime = os.path.getmtime(cache_path)
105
+ age_hours = (datetime.now().timestamp() - mtime) / 3600
106
+ if age_hours < 24: # 24시간 이내
107
+ with open(cache_path, 'r', encoding='utf-8') as f:
108
+ data = json.load(f)
109
+ if data:
110
+ print(f"캐시 로드: {len(data)}개 종목 ({age_hours:.1f}시간 전)")
111
+ return pd.DataFrame(data)
112
+ except Exception as e:
113
+ print(f"캐시 로드 실패: {e}")
114
+ return None
115
+
116
+ @classmethod
117
+ def _save_to_cache(cls, df: pd.DataFrame):
118
+ """로컬 캐시에 저장"""
119
+ import json
120
+ try:
121
+ cache_path = cls._get_cache_path()
122
+ with open(cache_path, 'w', encoding='utf-8') as f:
123
+ json.dump(df.to_dict('records'), f, ensure_ascii=False)
124
+ except Exception as e:
125
+ print(f"캐시 저장 실패: {e}")
126
+
127
+ @classmethod
128
+ def get_krx_stock_list(cls) -> pd.DataFrame:
129
+ """한국 주식 전체 종목 리스트 (파일 캐시 → 네이버 API → 폴백)"""
130
+ if cls._krx_stock_list is not None:
131
+ return cls._krx_stock_list
132
+
133
+ # 1) 파일 캐시 확인 (가장 빠름)
134
+ stock_list = cls._load_from_cache()
135
+ if stock_list is not None and not stock_list.empty:
136
+ cls._krx_stock_list = stock_list
137
+ return cls._krx_stock_list
138
+
139
+ # 2) 네이버 금융 API
140
+ try:
141
+ import requests
142
+ all_stocks = []
143
+ for mkt in ['KOSPI', 'KOSDAQ']:
144
+ for page in range(1, 50):
145
+ url = f'https://m.stock.naver.com/api/stocks/marketValue/{mkt}?page={page}&pageSize=100'
146
+ res = requests.get(url, timeout=10)
147
+ if res.status_code != 200:
148
+ break
149
+ data = res.json()
150
+ stocks = data.get('stocks', [])
151
+ if not stocks:
152
+ break
153
+ for s in stocks:
154
+ code = s.get('itemCode', '')
155
+ name = s.get('stockName', '')
156
+ if code and name:
157
+ all_stocks.append({"symbol": code, "name": name, "market": mkt})
158
+ if all_stocks:
159
+ stock_list = pd.DataFrame(all_stocks)
160
+ cls._save_to_cache(stock_list) # 캐시 저장
161
+ print(f"KRX 종목 로드 완료: {len(all_stocks)}개 (네이버)")
162
+ except Exception as e:
163
+ print(f"네이버 API 실패: {e}")
164
+ stock_list = None
165
+
166
+ # 3) pykrx 폴백
167
+ if stock_list is None or stock_list.empty:
168
+ try:
169
+ from pykrx import stock as pykrx_stock
170
+ tickers_with_market = []
171
+ for market in ["KOSPI", "KOSDAQ"]:
172
+ try:
173
+ tickers = pykrx_stock.get_market_ticker_list(market=market)
174
+ except:
175
+ tickers = []
176
+ if tickers:
177
+ tickers_with_market.extend((t, market) for t in tickers)
178
+ if tickers_with_market:
179
+ symbols = [t for t, _ in tickers_with_market]
180
+ markets = [m for _, m in tickers_with_market]
181
+ names = [pykrx_stock.get_market_ticker_name(t) for t in symbols]
182
+ stock_list = pd.DataFrame({"symbol": symbols, "name": names, "market": markets})
183
+ cls._save_to_cache(stock_list)
184
+ except:
185
+ stock_list = None
186
+
187
+ # 4) 최종 폴백: 인기 종목 리스트
188
+ if stock_list is None or stock_list.empty:
189
+ stock_list = pd.DataFrame(cls.KRX_POPULAR_STOCKS)
190
+ print(f"백업 리스트 사용: {len(cls.KRX_POPULAR_STOCKS)}개")
191
+
192
+ cls._krx_stock_list = stock_list.reset_index(drop=True)
193
+ return cls._krx_stock_list
194
+
195
+ def search_by_code(self, code: str) -> Optional[Dict]:
196
+ """종목코드로 직접 조회 (6자리)"""
197
+ if not code or len(code) != 6 or not code.isdigit():
198
+ return None
199
+ try:
200
+ import requests
201
+ url = f'https://m.stock.naver.com/api/stock/{code}/basic'
202
+ res = requests.get(url, timeout=5)
203
+ if res.status_code == 200:
204
+ data = res.json()
205
+ name = data.get('stockName', '')
206
+ exchange = data.get('stockExchangeType', {})
207
+ market = exchange.get('nameKor', 'KRX') if isinstance(exchange, dict) else 'KRX'
208
+ if name:
209
+ return {"symbol": code, "name": name, "market": f"🇰🇷 {market}"}
210
+ except:
211
+ pass
212
+ return None
213
+
214
+ def search_stocks(self, query: str, market: str = "all", limit: int = 20) -> List[Dict]:
215
+ """
216
+ 종목명/코드로 검색
217
+
218
+ Args:
219
+ query: 검색어 (종목명 또는 코드)
220
+ market: "kr", "us", "all"
221
+ limit: 최대 결과 수
222
+
223
+ Returns:
224
+ [{"symbol": "005930", "name": "삼성전자", "market": "KOSPI"}, ...]
225
+ """
226
+ results = []
227
+ query = query.strip()
228
+
229
+ if not query:
230
+ return results
231
+
232
+ # 한국 주식 검색
233
+ if market in ["kr", "all"]:
234
+ kr_stocks = self.get_krx_stock_list()
235
+ if not kr_stocks.empty:
236
+ # 종목명 또는 코드에서 검색 (한글은 대소문자 구분 없음)
237
+ mask = (
238
+ kr_stocks['name'].astype(str).str.contains(query, case=False, regex=False, na=False) |
239
+ kr_stocks['symbol'].astype(str).str.contains(query, regex=False, na=False)
240
+ )
241
+ kr_limit = limit if market == "kr" else max(1, limit // 2)
242
+ matches = kr_stocks[mask].head(kr_limit)
243
+ for _, row in matches.iterrows():
244
+ results.append({
245
+ "symbol": row['symbol'],
246
+ "name": row['name'],
247
+ "market": f"🇰🇷 {row['market']}"
248
+ })
249
+
250
+ # 미국 주식 검색
251
+ if market in ["us", "all"]:
252
+ try:
253
+ import yfinance as yf
254
+ # yfinance는 직접 검색 API가 없어서 인기 종목에서 매칭
255
+ us_popular = [
256
+ {"symbol": "AAPL", "name": "Apple Inc."},
257
+ {"symbol": "MSFT", "name": "Microsoft Corporation"},
258
+ {"symbol": "GOOGL", "name": "Alphabet Inc."},
259
+ {"symbol": "AMZN", "name": "Amazon.com Inc."},
260
+ {"symbol": "NVDA", "name": "NVIDIA Corporation"},
261
+ {"symbol": "META", "name": "Meta Platforms Inc."},
262
+ {"symbol": "TSLA", "name": "Tesla Inc."},
263
+ {"symbol": "AMD", "name": "Advanced Micro Devices"},
264
+ {"symbol": "NFLX", "name": "Netflix Inc."},
265
+ {"symbol": "INTC", "name": "Intel Corporation"},
266
+ {"symbol": "CRM", "name": "Salesforce Inc."},
267
+ {"symbol": "ORCL", "name": "Oracle Corporation"},
268
+ {"symbol": "CSCO", "name": "Cisco Systems"},
269
+ {"symbol": "ADBE", "name": "Adobe Inc."},
270
+ {"symbol": "QCOM", "name": "Qualcomm Inc."},
271
+ {"symbol": "IBM", "name": "IBM"},
272
+ {"symbol": "BA", "name": "Boeing Company"},
273
+ {"symbol": "DIS", "name": "Walt Disney Company"},
274
+ {"symbol": "JPM", "name": "JPMorgan Chase"},
275
+ {"symbol": "V", "name": "Visa Inc."},
276
+ {"symbol": "MA", "name": "Mastercard Inc."},
277
+ {"symbol": "KO", "name": "Coca-Cola Company"},
278
+ {"symbol": "PEP", "name": "PepsiCo Inc."},
279
+ {"symbol": "MCD", "name": "McDonald's Corporation"},
280
+ {"symbol": "NKE", "name": "Nike Inc."},
281
+ {"symbol": "WMT", "name": "Walmart Inc."},
282
+ {"symbol": "COST", "name": "Costco Wholesale"},
283
+ {"symbol": "HD", "name": "Home Depot"},
284
+ {"symbol": "UNH", "name": "UnitedHealth Group"},
285
+ {"symbol": "JNJ", "name": "Johnson & Johnson"},
286
+ {"symbol": "PFE", "name": "Pfizer Inc."},
287
+ {"symbol": "MRNA", "name": "Moderna Inc."},
288
+ {"symbol": "BABA", "name": "Alibaba Group"},
289
+ {"symbol": "TSM", "name": "Taiwan Semiconductor"},
290
+ {"symbol": "ASML", "name": "ASML Holding"},
291
+ {"symbol": "AVGO", "name": "Broadcom Inc."},
292
+ {"symbol": "TXN", "name": "Texas Instruments"},
293
+ {"symbol": "MU", "name": "Micron Technology"},
294
+ {"symbol": "PLTR", "name": "Palantir Technologies"},
295
+ {"symbol": "SNOW", "name": "Snowflake Inc."},
296
+ ]
297
+
298
+ query_upper = query.upper()
299
+ for stock in us_popular:
300
+ if query_upper in stock['symbol'] or query_upper in stock['name'].upper():
301
+ results.append({
302
+ "symbol": stock['symbol'],
303
+ "name": stock['name'],
304
+ "market": "🇺🇸 NASDAQ/NYSE"
305
+ })
306
+ if len(results) >= limit:
307
+ break
308
+
309
+ # 직접 티커 조회 시도 (영문만)
310
+ if len(query) <= 5 and query.isalpha():
311
+ try:
312
+ ticker = yf.Ticker(query_upper)
313
+ info = ticker.info
314
+ if info.get('regularMarketPrice'):
315
+ # 중복 체크
316
+ if not any(r['symbol'] == query_upper for r in results):
317
+ results.append({
318
+ "symbol": query_upper,
319
+ "name": info.get('longName', query_upper),
320
+ "market": "🇺🇸 " + info.get('exchange', 'US')
321
+ })
322
+ except:
323
+ pass
324
+ except Exception as e:
325
+ print(f"US 종목 검색 실패: {e}")
326
+
327
+ return results[:limit]
328
+
329
+ @staticmethod
330
+ def is_korean_stock(symbol: str) -> bool:
331
+ """한국 주식 여부 판단 (6자리 숫자)"""
332
+ return symbol.isdigit() and len(symbol) == 6
333
+
334
+ @staticmethod
335
+ def get_market(symbol: str) -> str:
336
+ """시장 구분 반환"""
337
+ return "krx" if StockService.is_korean_stock(symbol) else "us"
338
+
339
+ def get_stock_data(self, symbol: str, start_date: str, end_date: str) -> Tuple[Optional[pd.DataFrame], Optional[str]]:
340
+ """
341
+ 종목 유형에 따라 주식 데이터 가져오기
342
+
343
+ Args:
344
+ symbol: 종목 코드/티커
345
+ start_date: 시작일 (YYYY-MM-DD)
346
+ end_date: 종료일 (YYYY-MM-DD)
347
+
348
+ Returns:
349
+ (DataFrame, error_message)
350
+ """
351
+ from tradingagents.dataflows.krx import is_korean_stock, get_krx_stock_data
352
+ from tradingagents.dataflows.y_finance import get_YFin_data_online
353
+
354
+ try:
355
+ if is_korean_stock(symbol):
356
+ data_str = get_krx_stock_data(symbol, start_date, end_date)
357
+ else:
358
+ data_str = get_YFin_data_online(symbol, start_date, end_date)
359
+
360
+ # CSV 문자열을 DataFrame으로 변환
361
+ lines = data_str.strip().split('\n')
362
+ # 실제 데이터 시작 위치 찾기 (헤더 주석 이후)
363
+ data_start = 0
364
+ for i, line in enumerate(lines):
365
+ if not line.startswith('#') and line.strip():
366
+ data_start = i
367
+ break
368
+
369
+ data_str_clean = '\n'.join(lines[data_start:])
370
+ df = pd.read_csv(StringIO(data_str_clean))
371
+
372
+ # 컬럼명 정리
373
+ df.columns = [col.strip() for col in df.columns]
374
+
375
+ # 날짜 컬럼 찾기 및 인덱스 설정
376
+ date_cols = [col for col in df.columns if 'date' in col.lower() or '날짜' in col]
377
+ if date_cols:
378
+ df['Date'] = pd.to_datetime(df[date_cols[0]])
379
+ df = df.set_index('Date')
380
+
381
+ return df, None
382
+ except Exception as e:
383
+ return None, str(e)
384
+
385
+ def get_company_info(self, symbol: str) -> Tuple[str, str]:
386
+ """
387
+ 회사명 및 시장 정보 가져오기
388
+
389
+ Returns:
390
+ (company_name, market_info)
391
+ """
392
+ from tradingagents.dataflows.krx import is_korean_stock, get_company_name
393
+
394
+ if is_korean_stock(symbol):
395
+ name = get_company_name(symbol)
396
+ market = "한국 (KRX)"
397
+ else:
398
+ try:
399
+ import yfinance as yf
400
+ ticker = yf.Ticker(symbol)
401
+ info = ticker.info
402
+ name = info.get('longName', symbol)
403
+ except:
404
+ name = symbol
405
+ market = "미국"
406
+
407
+ return name, market
408
+
409
+ def get_current_price(self, symbol: str) -> Optional[float]:
410
+ """현재가 조회"""
411
+ end_date = datetime.now().strftime("%Y-%m-%d")
412
+ start_date = (datetime.now() - timedelta(days=7)).strftime("%Y-%m-%d")
413
+
414
+ df, error = self.get_stock_data(symbol, start_date, end_date)
415
+ if df is not None and not df.empty:
416
+ close_col = self._find_close_column(df)
417
+ if close_col:
418
+ return float(df[close_col].iloc[-1])
419
+ return None
420
+
421
+ def get_current_prices(self, symbols: List[str]) -> Dict[str, float]:
422
+ """여러 종목 현재가 일괄 조회"""
423
+ prices = {}
424
+ for symbol in symbols:
425
+ price = self.get_current_price(symbol)
426
+ if price:
427
+ prices[symbol] = price
428
+ return prices
429
+
430
+ def _find_close_column(self, df: pd.DataFrame) -> Optional[str]:
431
+ """종가 컬럼 찾기"""
432
+ return next((c for c in df.columns if 'close' in c.lower() or '종가' in c), None)
433
+
434
+ def _find_ohlcv_columns(self, df: pd.DataFrame) -> Dict[str, Optional[str]]:
435
+ """OHLCV 컬럼 찾기"""
436
+ return {
437
+ 'open': next((c for c in df.columns if 'open' in c.lower() or '시가' in c), None),
438
+ 'high': next((c for c in df.columns if 'high' in c.lower() or '고가' in c), None),
439
+ 'low': next((c for c in df.columns if 'low' in c.lower() or '저가' in c), None),
440
+ 'close': next((c for c in df.columns if 'close' in c.lower() or '종가' in c), None),
441
+ 'volume': next((c for c in df.columns if 'volume' in c.lower() or '거래량' in c), None)
442
+ }
443
+
444
+ def plot_candlestick(self, df: pd.DataFrame, symbol: str, company_name: str,
445
+ show_volume: bool = True) -> Optional["go.Figure"]:
446
+ """캔들스틱 차트 생성"""
447
+ try:
448
+ import plotly.graph_objects as go
449
+ from plotly.subplots import make_subplots
450
+ except ImportError:
451
+ return None
452
+
453
+ cols = self._find_ohlcv_columns(df)
454
+
455
+ if not all([cols['open'], cols['high'], cols['low'], cols['close']]):
456
+ return None
457
+
458
+ # 서브플롯 생성 (거래량 포함 여부에 따라)
459
+ if show_volume and cols['volume']:
460
+ fig = make_subplots(
461
+ rows=2, cols=1, shared_xaxes=True,
462
+ vertical_spacing=0.03,
463
+ row_heights=[0.7, 0.3]
464
+ )
465
+ else:
466
+ fig = make_subplots(rows=1, cols=1)
467
+
468
+ # 캔들스틱
469
+ fig.add_trace(
470
+ go.Candlestick(
471
+ x=df.index,
472
+ open=df[cols['open']],
473
+ high=df[cols['high']],
474
+ low=df[cols['low']],
475
+ close=df[cols['close']],
476
+ name="가격",
477
+ increasing_line_color='red',
478
+ decreasing_line_color='blue'
479
+ ),
480
+ row=1, col=1
481
+ )
482
+
483
+ # 거래량
484
+ if show_volume and cols['volume']:
485
+ colors = [
486
+ 'red' if df[cols['close']].iloc[i] >= df[cols['open']].iloc[i] else 'blue'
487
+ for i in range(len(df))
488
+ ]
489
+ fig.add_trace(
490
+ go.Bar(x=df.index, y=df[cols['volume']], name="거래량", marker_color=colors),
491
+ row=2, col=1
492
+ )
493
+ fig.update_yaxes(title_text="거래량", row=2, col=1)
494
+
495
+ fig.update_layout(
496
+ title=f"{company_name} ({symbol})",
497
+ xaxis_rangeslider_visible=False,
498
+ height=600,
499
+ showlegend=False
500
+ )
501
+
502
+ fig.update_xaxes(title_text="날짜", row=2 if show_volume else 1, col=1)
503
+ fig.update_yaxes(title_text="가격", row=1, col=1)
504
+
505
+ return fig
506
+
507
+ def plot_mini_chart(self, df: pd.DataFrame, symbol: str) -> Optional["go.Figure"]:
508
+ """미니 라인 차트 생성 (카드용)"""
509
+ try:
510
+ import plotly.graph_objects as go
511
+ except ImportError:
512
+ return None
513
+
514
+ close_col = self._find_close_column(df)
515
+ if not close_col:
516
+ return None
517
+
518
+ fig = go.Figure()
519
+ fig.add_trace(
520
+ go.Scatter(
521
+ x=df.index,
522
+ y=df[close_col],
523
+ mode='lines',
524
+ line=dict(color='#1E88E5', width=2),
525
+ fill='tozeroy',
526
+ fillcolor='rgba(30, 136, 229, 0.1)'
527
+ )
528
+ )
529
+
530
+ fig.update_layout(
531
+ height=150,
532
+ margin=dict(l=0, r=0, t=0, b=0),
533
+ showlegend=False,
534
+ xaxis=dict(visible=False),
535
+ yaxis=dict(visible=False),
536
+ plot_bgcolor='rgba(0,0,0,0)',
537
+ paper_bgcolor='rgba(0,0,0,0)'
538
+ )
539
+
540
+ return fig
541
+
542
+ def get_stock_metrics(self, df: pd.DataFrame) -> Dict:
543
+ """주식 주요 지표 계산"""
544
+ close_col = self._find_close_column(df)
545
+ if not close_col or df.empty:
546
+ return {}
547
+
548
+ current_price = df[close_col].iloc[-1]
549
+ prev_price = df[close_col].iloc[0]
550
+ change = ((current_price - prev_price) / prev_price) * 100
551
+ high = df[close_col].max()
552
+ low = df[close_col].min()
553
+
554
+ return {
555
+ "current_price": current_price,
556
+ "prev_price": prev_price,
557
+ "change_rate": round(change, 2),
558
+ "period_high": high,
559
+ "period_low": low,
560
+ "data_count": len(df)
561
+ }
562
+
563
+ def get_technical_indicators(self, symbol: str, start_date: str, end_date: str) -> Tuple[Optional[pd.DataFrame], Optional[str]]:
564
+ """기술적 지표 계산 (주식 데이터에서 직접 계산)"""
565
+ try:
566
+ # 주식 데이터 조회
567
+ df, error = self.get_stock_data(symbol, start_date, end_date)
568
+ if error or df is None or df.empty:
569
+ return None, error or "데이터 없음"
570
+
571
+ close_col = self._find_close_column(df)
572
+ if not close_col:
573
+ return None, "종가 컬럼을 찾을 수 없습니다"
574
+
575
+ # 기술적 ���표 계산
576
+ close = df[close_col]
577
+
578
+ # RSI (14일)
579
+ delta = close.diff()
580
+ gain = (delta.where(delta > 0, 0)).rolling(window=14).mean()
581
+ loss = (-delta.where(delta < 0, 0)).rolling(window=14).mean()
582
+ rs = gain / loss
583
+ df['RSI'] = 100 - (100 / (1 + rs))
584
+
585
+ # MACD
586
+ exp12 = close.ewm(span=12, adjust=False).mean()
587
+ exp26 = close.ewm(span=26, adjust=False).mean()
588
+ df['MACD'] = exp12 - exp26
589
+ df['Signal'] = df['MACD'].ewm(span=9, adjust=False).mean()
590
+ df['MACD_Hist'] = df['MACD'] - df['Signal']
591
+
592
+ # Bollinger Bands
593
+ sma20 = close.rolling(window=20).mean()
594
+ std20 = close.rolling(window=20).std()
595
+ df['BB_upper'] = sma20 + (std20 * 2)
596
+ df['BB_middle'] = sma20
597
+ df['BB_lower'] = sma20 - (std20 * 2)
598
+
599
+ return df, None
600
+ except Exception as e:
601
+ return None, str(e)
602
+
603
+ def plot_indicators(self, df: pd.DataFrame, indicator_type: str = "RSI") -> Optional["go.Figure"]:
604
+ """기술지표 차트 생성"""
605
+ try:
606
+ import plotly.graph_objects as go
607
+ except ImportError:
608
+ return None
609
+
610
+ fig = go.Figure()
611
+
612
+ if indicator_type == "RSI" and 'RSI' in df.columns:
613
+ fig.add_trace(go.Scatter(
614
+ x=df.index, y=df['RSI'],
615
+ mode='lines', name='RSI',
616
+ line=dict(color='purple', width=2)
617
+ ))
618
+ fig.add_hline(y=70, line_dash="dash", line_color="red", annotation_text="과매수 (70)")
619
+ fig.add_hline(y=30, line_dash="dash", line_color="green", annotation_text="과매도 (30)")
620
+ fig.update_layout(title="RSI (Relative Strength Index)", yaxis_range=[0, 100])
621
+
622
+ elif indicator_type == "MACD":
623
+ if 'MACD' in df.columns and 'Signal' in df.columns:
624
+ fig.add_trace(go.Scatter(x=df.index, y=df['MACD'], mode='lines', name='MACD'))
625
+ fig.add_trace(go.Scatter(x=df.index, y=df['Signal'], mode='lines', name='Signal'))
626
+ if 'MACD_Hist' in df.columns:
627
+ colors = ['green' if v >= 0 else 'red' for v in df['MACD_Hist']]
628
+ fig.add_trace(go.Bar(x=df.index, y=df['MACD_Hist'], name='Histogram', marker_color=colors))
629
+ fig.update_layout(title="MACD")
630
+
631
+ elif indicator_type == "BB" and all(c in df.columns for c in ['BB_upper', 'BB_middle', 'BB_lower']):
632
+ close_col = self._find_close_column(df)
633
+ if close_col:
634
+ fig.add_trace(go.Scatter(x=df.index, y=df[close_col], mode='lines', name='Price'))
635
+ fig.add_trace(go.Scatter(x=df.index, y=df['BB_upper'], mode='lines', name='Upper', line=dict(dash='dash')))
636
+ fig.add_trace(go.Scatter(x=df.index, y=df['BB_middle'], mode='lines', name='Middle'))
637
+ fig.add_trace(go.Scatter(x=df.index, y=df['BB_lower'], mode='lines', name='Lower', line=dict(dash='dash')))
638
+ fig.update_layout(title="Bollinger Bands")
639
+
640
+ fig.update_layout(height=300, showlegend=True)
641
+ return fig
tradingagents/agents/__init__.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from .utils.agent_utils import create_msg_delete
2
+ from .utils.agent_states import AgentState, InvestDebateState, RiskDebateState
3
+ from .utils.memory import FinancialSituationMemory
4
+
5
+ from .analysts.fundamentals_analyst import create_fundamentals_analyst
6
+ from .analysts.market_analyst import create_market_analyst
7
+ from .analysts.news_analyst import create_news_analyst
8
+ from .analysts.social_media_analyst import create_social_media_analyst
9
+ from .analysts.timeseries_analyst import create_timeseries_analyst
10
+
11
+ from .researchers.bear_researcher import create_bear_researcher
12
+ from .researchers.bull_researcher import create_bull_researcher
13
+
14
+ from .risk_mgmt.aggresive_debator import create_risky_debator
15
+ from .risk_mgmt.conservative_debator import create_safe_debator
16
+ from .risk_mgmt.neutral_debator import create_neutral_debator
17
+
18
+ from .managers.research_manager import create_research_manager
19
+ from .managers.risk_manager import create_risk_manager
20
+
21
+ from .trader.trader import create_trader
22
+
23
+ __all__ = [
24
+ "FinancialSituationMemory",
25
+ "AgentState",
26
+ "create_msg_delete",
27
+ "InvestDebateState",
28
+ "RiskDebateState",
29
+ "create_bear_researcher",
30
+ "create_bull_researcher",
31
+ "create_research_manager",
32
+ "create_fundamentals_analyst",
33
+ "create_market_analyst",
34
+ "create_neutral_debator",
35
+ "create_news_analyst",
36
+ "create_risky_debator",
37
+ "create_risk_manager",
38
+ "create_safe_debator",
39
+ "create_social_media_analyst",
40
+ "create_timeseries_analyst",
41
+ "create_trader",
42
+ ]
tradingagents/agents/analysts/fundamentals_analyst.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
2
+ import time
3
+ import json
4
+ from tradingagents.agents.utils.agent_utils import get_fundamentals, get_balance_sheet, get_cashflow, get_income_statement, get_insider_sentiment, get_insider_transactions, KOREAN_INSTRUCTION
5
+ from tradingagents.dataflows.config import get_config
6
+
7
+
8
+ def create_fundamentals_analyst(llm):
9
+ def fundamentals_analyst_node(state):
10
+ current_date = state["trade_date"]
11
+ ticker = state["company_of_interest"]
12
+ company_name = state["company_of_interest"]
13
+
14
+ tools = [
15
+ get_fundamentals,
16
+ get_balance_sheet,
17
+ get_cashflow,
18
+ get_income_statement,
19
+ ]
20
+
21
+ system_message = (
22
+ KOREAN_INSTRUCTION +
23
+ "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."
24
+ + " 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."
25
+ + " Use the available tools: `get_fundamentals` for comprehensive company analysis, `get_balance_sheet`, `get_cashflow`, and `get_income_statement` for specific financial statements.",
26
+ )
27
+
28
+ prompt = ChatPromptTemplate.from_messages(
29
+ [
30
+ (
31
+ "system",
32
+ "You are a helpful AI assistant, collaborating with other assistants."
33
+ " Use the provided tools to progress towards answering the question."
34
+ " If you are unable to fully answer, that's OK; another assistant with different tools"
35
+ " will help where you left off. Execute what you can to make progress."
36
+ " If you or any other assistant has the FINAL TRANSACTION PROPOSAL: **BUY/HOLD/SELL** or deliverable,"
37
+ " prefix your response with FINAL TRANSACTION PROPOSAL: **BUY/HOLD/SELL** so the team knows to stop."
38
+ " You have access to the following tools: {tool_names}.\n{system_message}"
39
+ "For your reference, the current date is {current_date}. The company we want to look at is {ticker}",
40
+ ),
41
+ MessagesPlaceholder(variable_name="messages"),
42
+ ]
43
+ )
44
+
45
+ prompt = prompt.partial(system_message=system_message)
46
+ prompt = prompt.partial(tool_names=", ".join([tool.name for tool in tools]))
47
+ prompt = prompt.partial(current_date=current_date)
48
+ prompt = prompt.partial(ticker=ticker)
49
+
50
+ chain = prompt | llm.bind_tools(tools)
51
+
52
+ result = chain.invoke(state["messages"])
53
+
54
+ report = ""
55
+
56
+ if len(result.tool_calls) == 0:
57
+ report = result.content
58
+
59
+ return {
60
+ "messages": [result],
61
+ "fundamentals_report": report,
62
+ }
63
+
64
+ return fundamentals_analyst_node
tradingagents/agents/analysts/market_analyst.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
2
+ import time
3
+ import json
4
+ from tradingagents.agents.utils.agent_utils import get_stock_data, get_indicators, KOREAN_INSTRUCTION
5
+ from tradingagents.dataflows.config import get_config
6
+
7
+
8
+ def create_market_analyst(llm):
9
+
10
+ def market_analyst_node(state):
11
+ current_date = state["trade_date"]
12
+ ticker = state["company_of_interest"]
13
+ company_name = state["company_of_interest"]
14
+
15
+ tools = [
16
+ get_stock_data,
17
+ get_indicators,
18
+ ]
19
+
20
+ system_message = (
21
+ KOREAN_INSTRUCTION +
22
+ """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:
23
+
24
+ Moving Averages:
25
+ - 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.
26
+ - 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.
27
+ - 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.
28
+
29
+ MACD Related:
30
+ - 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.
31
+ - 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.
32
+ - 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.
33
+
34
+ Momentum Indicators:
35
+ - 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.
36
+
37
+ Volatility Indicators:
38
+ - 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.
39
+ - 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.
40
+ - 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.
41
+ - 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.
42
+
43
+ Volume-Based Indicators:
44
+ - 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.
45
+
46
+ - 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."""
47
+ + """ 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."""
48
+ )
49
+
50
+ prompt = ChatPromptTemplate.from_messages(
51
+ [
52
+ (
53
+ "system",
54
+ "You are a helpful AI assistant, collaborating with other assistants."
55
+ " Use the provided tools to progress towards answering the question."
56
+ " If you are unable to fully answer, that's OK; another assistant with different tools"
57
+ " will help where you left off. Execute what you can to make progress."
58
+ " If you or any other assistant has the FINAL TRANSACTION PROPOSAL: **BUY/HOLD/SELL** or deliverable,"
59
+ " prefix your response with FINAL TRANSACTION PROPOSAL: **BUY/HOLD/SELL** so the team knows to stop."
60
+ " You have access to the following tools: {tool_names}.\n{system_message}"
61
+ "For your reference, the current date is {current_date}. The company we want to look at is {ticker}",
62
+ ),
63
+ MessagesPlaceholder(variable_name="messages"),
64
+ ]
65
+ )
66
+
67
+ prompt = prompt.partial(system_message=system_message)
68
+ prompt = prompt.partial(tool_names=", ".join([tool.name for tool in tools]))
69
+ prompt = prompt.partial(current_date=current_date)
70
+ prompt = prompt.partial(ticker=ticker)
71
+
72
+ chain = prompt | llm.bind_tools(tools)
73
+
74
+ result = chain.invoke(state["messages"])
75
+
76
+ report = ""
77
+
78
+ if len(result.tool_calls) == 0:
79
+ report = result.content
80
+
81
+ return {
82
+ "messages": [result],
83
+ "market_report": report,
84
+ }
85
+
86
+ return market_analyst_node
tradingagents/agents/analysts/news_analyst.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
2
+ import time
3
+ import json
4
+ from tradingagents.agents.utils.agent_utils import get_news, get_global_news, KOREAN_INSTRUCTION
5
+ from tradingagents.dataflows.config import get_config
6
+
7
+
8
+ def create_news_analyst(llm):
9
+ def news_analyst_node(state):
10
+ current_date = state["trade_date"]
11
+ ticker = state["company_of_interest"]
12
+
13
+ tools = [
14
+ get_news,
15
+ get_global_news,
16
+ ]
17
+
18
+ system_message = (
19
+ KOREAN_INSTRUCTION +
20
+ "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."
21
+ + """ 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."""
22
+ )
23
+
24
+ prompt = ChatPromptTemplate.from_messages(
25
+ [
26
+ (
27
+ "system",
28
+ "You are a helpful AI assistant, collaborating with other assistants."
29
+ " Use the provided tools to progress towards answering the question."
30
+ " If you are unable to fully answer, that's OK; another assistant with different tools"
31
+ " will help where you left off. Execute what you can to make progress."
32
+ " If you or any other assistant has the FINAL TRANSACTION PROPOSAL: **BUY/HOLD/SELL** or deliverable,"
33
+ " prefix your response with FINAL TRANSACTION PROPOSAL: **BUY/HOLD/SELL** so the team knows to stop."
34
+ " You have access to the following tools: {tool_names}.\n{system_message}"
35
+ "For your reference, the current date is {current_date}. We are looking at the company {ticker}",
36
+ ),
37
+ MessagesPlaceholder(variable_name="messages"),
38
+ ]
39
+ )
40
+
41
+ prompt = prompt.partial(system_message=system_message)
42
+ prompt = prompt.partial(tool_names=", ".join([tool.name for tool in tools]))
43
+ prompt = prompt.partial(current_date=current_date)
44
+ prompt = prompt.partial(ticker=ticker)
45
+
46
+ chain = prompt | llm.bind_tools(tools)
47
+ result = chain.invoke(state["messages"])
48
+
49
+ report = ""
50
+
51
+ if len(result.tool_calls) == 0:
52
+ report = result.content
53
+
54
+ return {
55
+ "messages": [result],
56
+ "news_report": report,
57
+ }
58
+
59
+ return news_analyst_node
tradingagents/agents/analysts/social_media_analyst.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
2
+ import time
3
+ import json
4
+ from tradingagents.agents.utils.agent_utils import get_news, KOREAN_INSTRUCTION
5
+ from tradingagents.dataflows.config import get_config
6
+
7
+
8
+ def create_social_media_analyst(llm):
9
+ def social_media_analyst_node(state):
10
+ current_date = state["trade_date"]
11
+ ticker = state["company_of_interest"]
12
+ company_name = state["company_of_interest"]
13
+
14
+ tools = [
15
+ get_news,
16
+ ]
17
+
18
+ system_message = (
19
+ KOREAN_INSTRUCTION +
20
+ "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."
21
+ + """ 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.""",
22
+ )
23
+
24
+ prompt = ChatPromptTemplate.from_messages(
25
+ [
26
+ (
27
+ "system",
28
+ "You are a helpful AI assistant, collaborating with other assistants."
29
+ " Use the provided tools to progress towards answering the question."
30
+ " If you are unable to fully answer, that's OK; another assistant with different tools"
31
+ " will help where you left off. Execute what you can to make progress."
32
+ " If you or any other assistant has the FINAL TRANSACTION PROPOSAL: **BUY/HOLD/SELL** or deliverable,"
33
+ " prefix your response with FINAL TRANSACTION PROPOSAL: **BUY/HOLD/SELL** so the team knows to stop."
34
+ " You have access to the following tools: {tool_names}.\n{system_message}"
35
+ "For your reference, the current date is {current_date}. The current company we want to analyze is {ticker}",
36
+ ),
37
+ MessagesPlaceholder(variable_name="messages"),
38
+ ]
39
+ )
40
+
41
+ prompt = prompt.partial(system_message=system_message)
42
+ prompt = prompt.partial(tool_names=", ".join([tool.name for tool in tools]))
43
+ prompt = prompt.partial(current_date=current_date)
44
+ prompt = prompt.partial(ticker=ticker)
45
+
46
+ chain = prompt | llm.bind_tools(tools)
47
+
48
+ result = chain.invoke(state["messages"])
49
+
50
+ report = ""
51
+
52
+ if len(result.tool_calls) == 0:
53
+ report = result.content
54
+
55
+ return {
56
+ "messages": [result],
57
+ "sentiment_report": report,
58
+ }
59
+
60
+ return social_media_analyst_node
tradingagents/agents/analysts/timeseries_analyst.py ADDED
@@ -0,0 +1,204 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ 시계열 분석가 에이전트
3
+ AI가 자동으로 관련 변수를 발견하고 시계열 예측을 수행합니다.
4
+ 딥러닝 앙상블 모델 (DLinear, NHITS, NBEATS) 지원
5
+ """
6
+
7
+ from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
8
+ import time
9
+ import json
10
+ from tradingagents.agents.utils.agent_utils import KOREAN_INSTRUCTION
11
+ from tradingagents.agents.utils.timeseries_tools import (
12
+ get_macro_indicators,
13
+ get_sector_performance,
14
+ get_correlated_assets,
15
+ analyze_stock_for_forecasting,
16
+ run_simple_forecast,
17
+ collect_forecast_features,
18
+ run_ensemble_forecast, # 딥러닝 앙상블 예측 추가
19
+ )
20
+
21
+
22
+ def create_timeseries_analyst(llm):
23
+ """
24
+ 시계열 분석가 에이전트를 생성합니다.
25
+
26
+ 이 에이전트는:
27
+ 1. 종목의 특성을 분석하여 관련 변수를 자동으로 식별
28
+ 2. 거시경제 지표, 섹터 데이터, 상관관계 분석
29
+ 3. 시계열 예측 수행
30
+
31
+ Args:
32
+ llm: LangChain LLM 인스턴스
33
+
34
+ Returns:
35
+ 시계열 분석가 노드 함수
36
+ """
37
+
38
+ def timeseries_analyst_node(state):
39
+ current_date = state["trade_date"]
40
+ ticker = state["company_of_interest"]
41
+
42
+ tools = [
43
+ analyze_stock_for_forecasting, # 종목 분석 및 변수 식별
44
+ get_macro_indicators, # 거시경제 지표
45
+ get_sector_performance, # 섹터 성과
46
+ get_correlated_assets, # 상관관계 분석
47
+ collect_forecast_features, # 특성 데이터 수집
48
+ run_simple_forecast, # 기술적 분석 기반 예측
49
+ run_ensemble_forecast, # 딥러닝 앙상블 예측 (DLinear+NHITS+NBEATS)
50
+ ]
51
+
52
+ system_message = KOREAN_INSTRUCTION + """You are an expert Time Series Analyst specializing in stock price forecasting using multiple data sources and deep learning models.
53
+
54
+ Your task is to perform a comprehensive time series analysis that:
55
+
56
+ 1. **Variable Discovery**: First, analyze the stock to understand:
57
+ - What sector/industry does this company belong to?
58
+ - What macro-economic factors typically affect this type of stock?
59
+ - What other assets are correlated with this stock?
60
+
61
+ 2. **Data Collection**: Gather relevant data:
62
+ - Use `analyze_stock_for_forecasting` to identify key features
63
+ - Use `get_macro_indicators` to get current economic environment
64
+ - Use `get_sector_performance` to understand sector trends
65
+ - Use `get_correlated_assets` to find empirical correlations
66
+
67
+ 3. **Forecasting**: Run predictions using BOTH methods:
68
+ - Use `run_simple_forecast` for technical indicator-based forecast
69
+ - Use `run_ensemble_forecast` for deep learning ensemble forecast (DLinear+NHITS+NBEATS)
70
+ - Compare both forecasts and synthesize the final prediction
71
+
72
+ 4. **Report Generation**: Write a detailed report including:
73
+ - Key variables identified and why they matter for this stock
74
+ - Current macro environment assessment
75
+ - Sector positioning
76
+ - Technical analysis forecast results
77
+ - Deep learning ensemble forecast results
78
+ - Final synthesized prediction with confidence level
79
+ - Risk factors to watch
80
+
81
+ **Important Guidelines**:
82
+ - ALWAYS run BOTH forecast methods (run_simple_forecast AND run_ensemble_forecast)
83
+ - The ensemble forecast uses 3 neural network models: DLinear (30%), NHITS (40%), NBEATS (30%)
84
+ - Compare the technical and ML-based forecasts to improve confidence
85
+ - DO NOT just list data - provide INSIGHTS and ANALYSIS
86
+ - Explain WHY certain variables matter for this specific stock
87
+ - Be specific about the forecast direction and reasoning
88
+ - Include actionable information for traders
89
+
90
+ Make sure to append a Markdown table at the end summarizing:
91
+ | Factor | Current Status | Impact on Forecast |
92
+
93
+ And a comparison table:
94
+ | Method | Predicted Price | Change % | Signal |
95
+ """
96
+
97
+ prompt = ChatPromptTemplate.from_messages(
98
+ [
99
+ (
100
+ "system",
101
+ "You are a helpful AI assistant specialized in time series forecasting, collaborating with other assistants."
102
+ " Use the provided tools to analyze the stock and make predictions."
103
+ " If you are unable to fully answer, that's OK; another assistant with different tools"
104
+ " will help where you left off. Execute what you can to make progress."
105
+ " If you or any other assistant has the FINAL TRANSACTION PROPOSAL: **BUY/HOLD/SELL** or deliverable,"
106
+ " prefix your response with FINAL TRANSACTION PROPOSAL: **BUY/HOLD/SELL** so the team knows to stop."
107
+ " You have access to the following tools: {tool_names}.\n{system_message}"
108
+ " For your reference, the current date is {current_date}. The company we want to analyze is {ticker}",
109
+ ),
110
+ MessagesPlaceholder(variable_name="messages"),
111
+ ]
112
+ )
113
+
114
+ prompt = prompt.partial(system_message=system_message)
115
+ prompt = prompt.partial(tool_names=", ".join([tool.name for tool in tools]))
116
+ prompt = prompt.partial(current_date=current_date)
117
+ prompt = prompt.partial(ticker=ticker)
118
+
119
+ chain = prompt | llm.bind_tools(tools)
120
+
121
+ result = chain.invoke(state["messages"])
122
+
123
+ report = ""
124
+
125
+ if len(result.tool_calls) == 0:
126
+ report = result.content
127
+
128
+ return {
129
+ "messages": [result],
130
+ "timeseries_report": report,
131
+ }
132
+
133
+ return timeseries_analyst_node
134
+
135
+
136
+ def create_variable_discovery_agent(llm):
137
+ """
138
+ 변수 발견 전문 에이전트를 생성합니다.
139
+ LLM이 종목의 특성을 분석하여 예측에 필요한 변수를 자동으로 식별합니다.
140
+
141
+ Args:
142
+ llm: LangChain LLM 인스턴스
143
+
144
+ Returns:
145
+ 변수 발견 에이전트 노드 함수
146
+ """
147
+
148
+ def variable_discovery_node(state):
149
+ ticker = state["company_of_interest"]
150
+ current_date = state["trade_date"]
151
+
152
+ # LLM에게 직접 종목 분석 요청
153
+ discovery_prompt = f"""Analyze the stock {ticker} and identify all relevant variables that could affect its price.
154
+
155
+ Consider the following categories:
156
+
157
+ 1. **Sector/Industry Factors**:
158
+ - What sector does this company belong to?
159
+ - What are the key drivers for this sector?
160
+ - Which sector ETFs track this industry?
161
+
162
+ 2. **Macro-Economic Factors**:
163
+ - Interest rates sensitivity
164
+ - Currency exposure (dollar strength)
165
+ - Commodity dependencies (oil, metals, etc.)
166
+ - Consumer spending correlation
167
+
168
+ 3. **Market Factors**:
169
+ - Beta to S&P 500
170
+ - VIX sensitivity
171
+ - Growth vs Value characteristics
172
+
173
+ 4. **Company-Specific Factors**:
174
+ - Key customers/suppliers
175
+ - Competitive dynamics
176
+ - Regulatory environment
177
+
178
+ 5. **Seasonal/Cyclical Patterns**:
179
+ - Earnings seasonality
180
+ - Holiday effects
181
+ - Economic cycle sensitivity
182
+
183
+ Please provide a structured JSON response with:
184
+ {{
185
+ "ticker": "{ticker}",
186
+ "sector": "...",
187
+ "industry": "...",
188
+ "recommended_features": [
189
+ {{"name": "...", "type": "macro/sector/market/specific", "reason": "..."}},
190
+ ...
191
+ ],
192
+ "key_risks": ["...", "..."],
193
+ "seasonality_notes": "..."
194
+ }}
195
+ """
196
+
197
+ response = llm.invoke(discovery_prompt)
198
+
199
+ return {
200
+ "messages": [response],
201
+ "discovered_variables": response.content,
202
+ }
203
+
204
+ return variable_discovery_node
tradingagents/agents/managers/research_manager.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import time
2
+ import json
3
+ from tradingagents.agents.utils.agent_utils import KOREAN_INSTRUCTION
4
+
5
+
6
+ def create_research_manager(llm, memory):
7
+ def research_manager_node(state) -> dict:
8
+ history = state["investment_debate_state"].get("history", "")
9
+ market_research_report = state["market_report"]
10
+ sentiment_report = state["sentiment_report"]
11
+ news_report = state["news_report"]
12
+ fundamentals_report = state["fundamentals_report"]
13
+
14
+ investment_debate_state = state["investment_debate_state"]
15
+
16
+ curr_situation = f"{market_research_report}\n\n{sentiment_report}\n\n{news_report}\n\n{fundamentals_report}"
17
+ past_memories = memory.get_memories(curr_situation, n_matches=2)
18
+
19
+ past_memory_str = ""
20
+ for i, rec in enumerate(past_memories, 1):
21
+ past_memory_str += rec["recommendation"] + "\n\n"
22
+
23
+ prompt = KOREAN_INSTRUCTION + 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.
24
+
25
+ Summarize the key points from both sides concisely, focusing on the most compelling evidence or reasoning. Your recommendation—Buy, Sell, or Hold—must 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.
26
+
27
+ Additionally, develop a detailed investment plan for the trader. This should include:
28
+
29
+ Your Recommendation: A decisive stance supported by the most convincing arguments.
30
+ Rationale: An explanation of why these arguments lead to your conclusion.
31
+ Strategic Actions: Concrete steps for implementing the recommendation.
32
+ 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.
33
+
34
+ Here are your past reflections on mistakes:
35
+ \"{past_memory_str}\"
36
+
37
+ Here is the debate:
38
+ Debate History:
39
+ {history}"""
40
+ response = llm.invoke(prompt)
41
+
42
+ new_investment_debate_state = {
43
+ "judge_decision": response.content,
44
+ "history": investment_debate_state.get("history", ""),
45
+ "bear_history": investment_debate_state.get("bear_history", ""),
46
+ "bull_history": investment_debate_state.get("bull_history", ""),
47
+ "current_response": response.content,
48
+ "count": investment_debate_state["count"],
49
+ }
50
+
51
+ return {
52
+ "investment_debate_state": new_investment_debate_state,
53
+ "investment_plan": response.content,
54
+ }
55
+
56
+ return research_manager_node
tradingagents/agents/managers/risk_manager.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import time
2
+ import json
3
+ from tradingagents.agents.utils.agent_utils import KOREAN_INSTRUCTION
4
+
5
+
6
+ def create_risk_manager(llm, memory):
7
+ def risk_manager_node(state) -> dict:
8
+
9
+ company_name = state["company_of_interest"]
10
+
11
+ history = state["risk_debate_state"]["history"]
12
+ risk_debate_state = state["risk_debate_state"]
13
+ market_research_report = state["market_report"]
14
+ news_report = state["news_report"]
15
+ fundamentals_report = state["news_report"]
16
+ sentiment_report = state["sentiment_report"]
17
+ trader_plan = state["investment_plan"]
18
+
19
+ curr_situation = f"{market_research_report}\n\n{sentiment_report}\n\n{news_report}\n\n{fundamentals_report}"
20
+ past_memories = memory.get_memories(curr_situation, n_matches=2)
21
+
22
+ past_memory_str = ""
23
+ for i, rec in enumerate(past_memories, 1):
24
+ past_memory_str += rec["recommendation"] + "\n\n"
25
+
26
+ prompt = KOREAN_INSTRUCTION + f"""As the Risk Management Judge and Debate Facilitator, your goal is to evaluate the debate between three risk analysts—Risky, Neutral, and Safe/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.
27
+
28
+ Guidelines for Decision-Making:
29
+ 1. **Summarize Key Arguments**: Extract the strongest points from each analyst, focusing on relevance to the context.
30
+ 2. **Provide Rationale**: Support your recommendation with direct quotes and counterarguments from the debate.
31
+ 3. **Refine the Trader's Plan**: Start with the trader's original plan, **{trader_plan}**, and adjust it based on the analysts' insights.
32
+ 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.
33
+
34
+ Deliverables:
35
+ - A clear and actionable recommendation: Buy, Sell, or Hold.
36
+ - Detailed reasoning anchored in the debate and past reflections.
37
+
38
+ ---
39
+
40
+ **Analysts Debate History:**
41
+ {history}
42
+
43
+ ---
44
+
45
+ Focus on actionable insights and continuous improvement. Build on past lessons, critically evaluate all perspectives, and ensure each decision advances better outcomes."""
46
+
47
+ response = llm.invoke(prompt)
48
+
49
+ new_risk_debate_state = {
50
+ "judge_decision": response.content,
51
+ "history": risk_debate_state["history"],
52
+ "risky_history": risk_debate_state["risky_history"],
53
+ "safe_history": risk_debate_state["safe_history"],
54
+ "neutral_history": risk_debate_state["neutral_history"],
55
+ "latest_speaker": "Judge",
56
+ "current_risky_response": risk_debate_state["current_risky_response"],
57
+ "current_safe_response": risk_debate_state["current_safe_response"],
58
+ "current_neutral_response": risk_debate_state["current_neutral_response"],
59
+ "count": risk_debate_state["count"],
60
+ }
61
+
62
+ return {
63
+ "risk_debate_state": new_risk_debate_state,
64
+ "final_trade_decision": response.content,
65
+ }
66
+
67
+ return risk_manager_node
tradingagents/agents/researchers/bear_researcher.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain_core.messages import AIMessage
2
+ import time
3
+ import json
4
+ from tradingagents.agents.utils.agent_utils import KOREAN_INSTRUCTION
5
+
6
+
7
+ def create_bear_researcher(llm, memory):
8
+ def bear_node(state) -> dict:
9
+ investment_debate_state = state["investment_debate_state"]
10
+ history = investment_debate_state.get("history", "")
11
+ bear_history = investment_debate_state.get("bear_history", "")
12
+
13
+ current_response = investment_debate_state.get("current_response", "")
14
+ market_research_report = state["market_report"]
15
+ sentiment_report = state["sentiment_report"]
16
+ news_report = state["news_report"]
17
+ fundamentals_report = state["fundamentals_report"]
18
+ timeseries_report = state.get("timeseries_report", "")
19
+
20
+ curr_situation = f"{market_research_report}\n\n{sentiment_report}\n\n{news_report}\n\n{fundamentals_report}\n\n{timeseries_report}"
21
+ past_memories = memory.get_memories(curr_situation, n_matches=2)
22
+
23
+ past_memory_str = ""
24
+ for i, rec in enumerate(past_memories, 1):
25
+ past_memory_str += rec["recommendation"] + "\n\n"
26
+
27
+ prompt = KOREAN_INSTRUCTION + 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.
28
+
29
+ Key points to focus on:
30
+
31
+ - Risks and Challenges: Highlight factors like market saturation, financial instability, or macroeconomic threats that could hinder the stock's performance.
32
+ - Competitive Weaknesses: Emphasize vulnerabilities such as weaker market positioning, declining innovation, or threats from competitors.
33
+ - Negative Indicators: Use evidence from financial data, market trends, or recent adverse news to support your position.
34
+ - Time Series Forecast: Consider the AI-driven price forecast uncertainties and technical warning signals.
35
+ - Bull Counterpoints: Critically analyze the bull argument with specific data and sound reasoning, exposing weaknesses or over-optimistic assumptions.
36
+ - Engagement: Present your argument in a conversational style, directly engaging with the bull analyst's points and debating effectively rather than simply listing facts.
37
+
38
+ Resources available:
39
+
40
+ Market research report: {market_research_report}
41
+ Social media sentiment report: {sentiment_report}
42
+ Latest world affairs news: {news_report}
43
+ Company fundamentals report: {fundamentals_report}
44
+ Time series forecast report: {timeseries_report}
45
+ Conversation history of the debate: {history}
46
+ Last bull argument: {current_response}
47
+ Reflections from similar situations and lessons learned: {past_memory_str}
48
+ 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.
49
+ """
50
+
51
+ response = llm.invoke(prompt)
52
+
53
+ argument = f"Bear Analyst: {response.content}"
54
+
55
+ new_investment_debate_state = {
56
+ "history": history + "\n" + argument,
57
+ "bear_history": bear_history + "\n" + argument,
58
+ "bull_history": investment_debate_state.get("bull_history", ""),
59
+ "current_response": argument,
60
+ "count": investment_debate_state["count"] + 1,
61
+ }
62
+
63
+ return {"investment_debate_state": new_investment_debate_state}
64
+
65
+ return bear_node
tradingagents/agents/researchers/bull_researcher.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain_core.messages import AIMessage
2
+ import time
3
+ import json
4
+ from tradingagents.agents.utils.agent_utils import KOREAN_INSTRUCTION
5
+
6
+
7
+ def create_bull_researcher(llm, memory):
8
+ def bull_node(state) -> dict:
9
+ investment_debate_state = state["investment_debate_state"]
10
+ history = investment_debate_state.get("history", "")
11
+ bull_history = investment_debate_state.get("bull_history", "")
12
+
13
+ current_response = investment_debate_state.get("current_response", "")
14
+ market_research_report = state["market_report"]
15
+ sentiment_report = state["sentiment_report"]
16
+ news_report = state["news_report"]
17
+ fundamentals_report = state["fundamentals_report"]
18
+ timeseries_report = state.get("timeseries_report", "")
19
+
20
+ curr_situation = f"{market_research_report}\n\n{sentiment_report}\n\n{news_report}\n\n{fundamentals_report}\n\n{timeseries_report}"
21
+ past_memories = memory.get_memories(curr_situation, n_matches=2)
22
+
23
+ past_memory_str = ""
24
+ for i, rec in enumerate(past_memories, 1):
25
+ past_memory_str += rec["recommendation"] + "\n\n"
26
+
27
+ prompt = KOREAN_INSTRUCTION + 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.
28
+
29
+ Key points to focus on:
30
+ - Growth Potential: Highlight the company's market opportunities, revenue projections, and scalability.
31
+ - Competitive Advantages: Emphasize factors like unique products, strong branding, or dominant market positioning.
32
+ - Positive Indicators: Use financial health, industry trends, and recent positive news as evidence.
33
+ - Time Series Forecast: Consider the AI-driven price forecast and technical signals.
34
+ - 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.
35
+ - Engagement: Present your argument in a conversational style, engaging directly with the bear analyst's points and debating effectively rather than just listing data.
36
+
37
+ Resources available:
38
+ Market research report: {market_research_report}
39
+ Social media sentiment report: {sentiment_report}
40
+ Latest world affairs news: {news_report}
41
+ Company fundamentals report: {fundamentals_report}
42
+ Time series forecast report: {timeseries_report}
43
+ Conversation history of the debate: {history}
44
+ Last bear argument: {current_response}
45
+ Reflections from similar situations and lessons learned: {past_memory_str}
46
+ 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.
47
+ """
48
+
49
+ response = llm.invoke(prompt)
50
+
51
+ argument = f"Bull Analyst: {response.content}"
52
+
53
+ new_investment_debate_state = {
54
+ "history": history + "\n" + argument,
55
+ "bull_history": bull_history + "\n" + argument,
56
+ "bear_history": investment_debate_state.get("bear_history", ""),
57
+ "current_response": argument,
58
+ "count": investment_debate_state["count"] + 1,
59
+ }
60
+
61
+ return {"investment_debate_state": new_investment_debate_state}
62
+
63
+ return bull_node
tradingagents/agents/risk_mgmt/aggresive_debator.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import time
2
+ import json
3
+ from tradingagents.agents.utils.agent_utils import KOREAN_INSTRUCTION
4
+
5
+
6
+ def create_risky_debator(llm):
7
+ def risky_node(state) -> dict:
8
+ risk_debate_state = state["risk_debate_state"]
9
+ history = risk_debate_state.get("history", "")
10
+ risky_history = risk_debate_state.get("risky_history", "")
11
+
12
+ current_safe_response = risk_debate_state.get("current_safe_response", "")
13
+ current_neutral_response = risk_debate_state.get("current_neutral_response", "")
14
+
15
+ market_research_report = state["market_report"]
16
+ sentiment_report = state["sentiment_report"]
17
+ news_report = state["news_report"]
18
+ fundamentals_report = state["fundamentals_report"]
19
+
20
+ trader_decision = state["trader_investment_plan"]
21
+
22
+ prompt = KOREAN_INSTRUCTION + f"""As the Risky 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:
23
+
24
+ {trader_decision}
25
+
26
+ 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:
27
+
28
+ Market Research Report: {market_research_report}
29
+ Social Media Sentiment Report: {sentiment_report}
30
+ Latest World Affairs Report: {news_report}
31
+ Company Fundamentals Report: {fundamentals_report}
32
+ Here is the current conversation history: {history} Here are the last arguments from the conservative analyst: {current_safe_response} Here are the last arguments from the neutral analyst: {current_neutral_response}. If there are no responses from the other viewpoints, do not halluncinate and just present your point.
33
+
34
+ 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."""
35
+
36
+ response = llm.invoke(prompt)
37
+
38
+ argument = f"Risky Analyst: {response.content}"
39
+
40
+ new_risk_debate_state = {
41
+ "history": history + "\n" + argument,
42
+ "risky_history": risky_history + "\n" + argument,
43
+ "safe_history": risk_debate_state.get("safe_history", ""),
44
+ "neutral_history": risk_debate_state.get("neutral_history", ""),
45
+ "latest_speaker": "Risky",
46
+ "current_risky_response": argument,
47
+ "current_safe_response": risk_debate_state.get("current_safe_response", ""),
48
+ "current_neutral_response": risk_debate_state.get(
49
+ "current_neutral_response", ""
50
+ ),
51
+ "count": risk_debate_state["count"] + 1,
52
+ }
53
+
54
+ return {"risk_debate_state": new_risk_debate_state}
55
+
56
+ return risky_node
tradingagents/agents/risk_mgmt/conservative_debator.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain_core.messages import AIMessage
2
+ import time
3
+ import json
4
+ from tradingagents.agents.utils.agent_utils import KOREAN_INSTRUCTION
5
+
6
+
7
+ def create_safe_debator(llm):
8
+ def safe_node(state) -> dict:
9
+ risk_debate_state = state["risk_debate_state"]
10
+ history = risk_debate_state.get("history", "")
11
+ safe_history = risk_debate_state.get("safe_history", "")
12
+
13
+ current_risky_response = risk_debate_state.get("current_risky_response", "")
14
+ current_neutral_response = risk_debate_state.get("current_neutral_response", "")
15
+
16
+ market_research_report = state["market_report"]
17
+ sentiment_report = state["sentiment_report"]
18
+ news_report = state["news_report"]
19
+ fundamentals_report = state["fundamentals_report"]
20
+
21
+ trader_decision = state["trader_investment_plan"]
22
+
23
+ prompt = KOREAN_INSTRUCTION + f"""As the Safe/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:
24
+
25
+ {trader_decision}
26
+
27
+ Your task is to actively counter the arguments of the Risky 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:
28
+
29
+ Market Research Report: {market_research_report}
30
+ Social Media Sentiment Report: {sentiment_report}
31
+ Latest World Affairs Report: {news_report}
32
+ Company Fundamentals Report: {fundamentals_report}
33
+ Here is the current conversation history: {history} Here is the last response from the risky analyst: {current_risky_response} Here is the last response from the neutral analyst: {current_neutral_response}. If there are no responses from the other viewpoints, do not halluncinate and just present your point.
34
+
35
+ 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."""
36
+
37
+ response = llm.invoke(prompt)
38
+
39
+ argument = f"Safe Analyst: {response.content}"
40
+
41
+ new_risk_debate_state = {
42
+ "history": history + "\n" + argument,
43
+ "risky_history": risk_debate_state.get("risky_history", ""),
44
+ "safe_history": safe_history + "\n" + argument,
45
+ "neutral_history": risk_debate_state.get("neutral_history", ""),
46
+ "latest_speaker": "Safe",
47
+ "current_risky_response": risk_debate_state.get(
48
+ "current_risky_response", ""
49
+ ),
50
+ "current_safe_response": argument,
51
+ "current_neutral_response": risk_debate_state.get(
52
+ "current_neutral_response", ""
53
+ ),
54
+ "count": risk_debate_state["count"] + 1,
55
+ }
56
+
57
+ return {"risk_debate_state": new_risk_debate_state}
58
+
59
+ return safe_node
tradingagents/agents/risk_mgmt/neutral_debator.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import time
2
+ import json
3
+ from tradingagents.agents.utils.agent_utils import KOREAN_INSTRUCTION
4
+
5
+
6
+ def create_neutral_debator(llm):
7
+ def neutral_node(state) -> dict:
8
+ risk_debate_state = state["risk_debate_state"]
9
+ history = risk_debate_state.get("history", "")
10
+ neutral_history = risk_debate_state.get("neutral_history", "")
11
+
12
+ current_risky_response = risk_debate_state.get("current_risky_response", "")
13
+ current_safe_response = risk_debate_state.get("current_safe_response", "")
14
+
15
+ market_research_report = state["market_report"]
16
+ sentiment_report = state["sentiment_report"]
17
+ news_report = state["news_report"]
18
+ fundamentals_report = state["fundamentals_report"]
19
+
20
+ trader_decision = state["trader_investment_plan"]
21
+
22
+ prompt = KOREAN_INSTRUCTION + 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:
23
+
24
+ {trader_decision}
25
+
26
+ Your task is to challenge both the Risky and Safe 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:
27
+
28
+ Market Research Report: {market_research_report}
29
+ Social Media Sentiment Report: {sentiment_report}
30
+ Latest World Affairs Report: {news_report}
31
+ Company Fundamentals Report: {fundamentals_report}
32
+ Here is the current conversation history: {history} Here is the last response from the risky analyst: {current_risky_response} Here is the last response from the safe analyst: {current_safe_response}. If there are no responses from the other viewpoints, do not halluncinate and just present your point.
33
+
34
+ Engage actively by analyzing both sides critically, addressing weaknesses in the risky 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."""
35
+
36
+ response = llm.invoke(prompt)
37
+
38
+ argument = f"Neutral Analyst: {response.content}"
39
+
40
+ new_risk_debate_state = {
41
+ "history": history + "\n" + argument,
42
+ "risky_history": risk_debate_state.get("risky_history", ""),
43
+ "safe_history": risk_debate_state.get("safe_history", ""),
44
+ "neutral_history": neutral_history + "\n" + argument,
45
+ "latest_speaker": "Neutral",
46
+ "current_risky_response": risk_debate_state.get(
47
+ "current_risky_response", ""
48
+ ),
49
+ "current_safe_response": risk_debate_state.get("current_safe_response", ""),
50
+ "current_neutral_response": argument,
51
+ "count": risk_debate_state["count"] + 1,
52
+ }
53
+
54
+ return {"risk_debate_state": new_risk_debate_state}
55
+
56
+ return neutral_node
tradingagents/agents/trader/trader.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import functools
2
+ import time
3
+ import json
4
+ from tradingagents.agents.utils.agent_utils import KOREAN_INSTRUCTION
5
+
6
+
7
+ def create_trader(llm, memory):
8
+ def trader_node(state, name):
9
+ company_name = state["company_of_interest"]
10
+ investment_plan = state["investment_plan"]
11
+ market_research_report = state["market_report"]
12
+ sentiment_report = state["sentiment_report"]
13
+ news_report = state["news_report"]
14
+ fundamentals_report = state["fundamentals_report"]
15
+
16
+ curr_situation = f"{market_research_report}\n\n{sentiment_report}\n\n{news_report}\n\n{fundamentals_report}"
17
+ past_memories = memory.get_memories(curr_situation, n_matches=2)
18
+
19
+ past_memory_str = ""
20
+ if past_memories:
21
+ for i, rec in enumerate(past_memories, 1):
22
+ past_memory_str += rec["recommendation"] + "\n\n"
23
+ else:
24
+ past_memory_str = "No past memories found."
25
+
26
+ context = {
27
+ "role": "user",
28
+ "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.",
29
+ }
30
+
31
+ messages = [
32
+ {
33
+ "role": "system",
34
+ "content": KOREAN_INSTRUCTION + 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}""",
35
+ },
36
+ context,
37
+ ]
38
+
39
+ result = llm.invoke(messages)
40
+
41
+ return {
42
+ "messages": [result],
43
+ "trader_investment_plan": result.content,
44
+ "sender": name,
45
+ }
46
+
47
+ return functools.partial(trader_node, name="Trader")
tradingagents/agents/utils/agent_states.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Annotated, Sequence
2
+ from datetime import date, timedelta, datetime
3
+ from typing_extensions import TypedDict, Optional
4
+ from langchain_openai import ChatOpenAI
5
+ from tradingagents.agents import *
6
+ from langgraph.prebuilt import ToolNode
7
+ from langgraph.graph import END, StateGraph, START, MessagesState
8
+
9
+
10
+ # Researcher team state
11
+ class InvestDebateState(TypedDict):
12
+ bull_history: Annotated[
13
+ str, "Bullish Conversation history"
14
+ ] # Bullish Conversation history
15
+ bear_history: Annotated[
16
+ str, "Bearish Conversation history"
17
+ ] # Bullish Conversation history
18
+ history: Annotated[str, "Conversation history"] # Conversation history
19
+ current_response: Annotated[str, "Latest response"] # Last response
20
+ judge_decision: Annotated[str, "Final judge decision"] # Last response
21
+ count: Annotated[int, "Length of the current conversation"] # Conversation length
22
+
23
+
24
+ # Risk management team state
25
+ class RiskDebateState(TypedDict):
26
+ risky_history: Annotated[
27
+ str, "Risky Agent's Conversation history"
28
+ ] # Conversation history
29
+ safe_history: Annotated[
30
+ str, "Safe Agent's Conversation history"
31
+ ] # Conversation history
32
+ neutral_history: Annotated[
33
+ str, "Neutral Agent's Conversation history"
34
+ ] # Conversation history
35
+ history: Annotated[str, "Conversation history"] # Conversation history
36
+ latest_speaker: Annotated[str, "Analyst that spoke last"]
37
+ current_risky_response: Annotated[
38
+ str, "Latest response by the risky analyst"
39
+ ] # Last response
40
+ current_safe_response: Annotated[
41
+ str, "Latest response by the safe analyst"
42
+ ] # Last response
43
+ current_neutral_response: Annotated[
44
+ str, "Latest response by the neutral analyst"
45
+ ] # Last response
46
+ judge_decision: Annotated[str, "Judge's decision"]
47
+ count: Annotated[int, "Length of the current conversation"] # Conversation length
48
+
49
+
50
+ class AgentState(MessagesState):
51
+ company_of_interest: Annotated[str, "Company that we are interested in trading"]
52
+ trade_date: Annotated[str, "What date we are trading at"]
53
+
54
+ sender: Annotated[str, "Agent that sent this message"]
55
+
56
+ # research step
57
+ market_report: Annotated[str, "Report from the Market Analyst"]
58
+ sentiment_report: Annotated[str, "Report from the Social Media Analyst"]
59
+ news_report: Annotated[
60
+ str, "Report from the News Researcher of current world affairs"
61
+ ]
62
+ fundamentals_report: Annotated[str, "Report from the Fundamentals Researcher"]
63
+ timeseries_report: Annotated[str, "Report from the Time Series Analyst with forecast"]
64
+
65
+ # researcher team discussion step
66
+ investment_debate_state: Annotated[
67
+ InvestDebateState, "Current state of the debate on if to invest or not"
68
+ ]
69
+ investment_plan: Annotated[str, "Plan generated by the Analyst"]
70
+
71
+ trader_investment_plan: Annotated[str, "Plan generated by the Trader"]
72
+
73
+ # risk management team discussion step
74
+ risk_debate_state: Annotated[
75
+ RiskDebateState, "Current state of the debate on evaluating risk"
76
+ ]
77
+ final_trade_decision: Annotated[str, "Final decision made by the Risk Analysts"]
tradingagents/agents/utils/agent_utils.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain_core.messages import HumanMessage, RemoveMessage
2
+
3
+ # 한글 응답 지시 (모든 에이전트 프롬프트에 추가)
4
+ KOREAN_INSTRUCTION = """
5
+ [중요: 모든 응답을 한국어로 작성하세요. 분석 리포트, 의견, 결론 모두 한국어로 작성합니다.]
6
+ """
7
+
8
+ # Import tools from separate utility files
9
+ from tradingagents.agents.utils.core_stock_tools import (
10
+ get_stock_data
11
+ )
12
+ from tradingagents.agents.utils.technical_indicators_tools import (
13
+ get_indicators
14
+ )
15
+ from tradingagents.agents.utils.fundamental_data_tools import (
16
+ get_fundamentals,
17
+ get_balance_sheet,
18
+ get_cashflow,
19
+ get_income_statement
20
+ )
21
+ from tradingagents.agents.utils.news_data_tools import (
22
+ get_news,
23
+ get_insider_sentiment,
24
+ get_insider_transactions,
25
+ get_global_news
26
+ )
27
+
28
+ def create_msg_delete():
29
+ def delete_messages(state):
30
+ """Clear messages and add placeholder for Anthropic compatibility"""
31
+ messages = state["messages"]
32
+
33
+ # Remove all messages
34
+ removal_operations = [RemoveMessage(id=m.id) for m in messages]
35
+
36
+ # Add a minimal placeholder message
37
+ placeholder = HumanMessage(content="Continue")
38
+
39
+ return {"messages": removal_operations + [placeholder]}
40
+
41
+ return delete_messages
42
+
43
+
44
+
tradingagents/agents/utils/core_stock_tools.py ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain_core.tools import tool
2
+ from typing import Annotated
3
+ from tradingagents.dataflows.interface import route_to_vendor
4
+
5
+
6
+ @tool
7
+ def get_stock_data(
8
+ symbol: Annotated[str, "ticker symbol of the company"],
9
+ start_date: Annotated[str, "Start date in yyyy-mm-dd format"],
10
+ end_date: Annotated[str, "End date in yyyy-mm-dd format"],
11
+ ) -> str:
12
+ """
13
+ Retrieve stock price data (OHLCV) for a given ticker symbol.
14
+ Uses the configured core_stock_apis vendor.
15
+ Args:
16
+ symbol (str): Ticker symbol of the company, e.g. AAPL, TSM
17
+ start_date (str): Start date in yyyy-mm-dd format
18
+ end_date (str): End date in yyyy-mm-dd format
19
+ Returns:
20
+ str: A formatted dataframe containing the stock price data for the specified ticker symbol in the specified date range.
21
+ """
22
+ return route_to_vendor("get_stock_data", symbol, start_date, end_date)
tradingagents/agents/utils/fundamental_data_tools.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain_core.tools import tool
2
+ from typing import Annotated
3
+ from tradingagents.dataflows.interface import route_to_vendor
4
+
5
+
6
+ @tool
7
+ def get_fundamentals(
8
+ ticker: Annotated[str, "ticker symbol"],
9
+ curr_date: Annotated[str, "current date you are trading at, yyyy-mm-dd"],
10
+ ) -> str:
11
+ """
12
+ Retrieve comprehensive fundamental data for a given ticker symbol.
13
+ Uses the configured fundamental_data vendor.
14
+ Args:
15
+ ticker (str): Ticker symbol of the company
16
+ curr_date (str): Current date you are trading at, yyyy-mm-dd
17
+ Returns:
18
+ str: A formatted report containing comprehensive fundamental data
19
+ """
20
+ return route_to_vendor("get_fundamentals", ticker, curr_date)
21
+
22
+
23
+ @tool
24
+ def get_balance_sheet(
25
+ ticker: Annotated[str, "ticker symbol"],
26
+ freq: Annotated[str, "reporting frequency: annual/quarterly"] = "quarterly",
27
+ curr_date: Annotated[str, "current date you are trading at, yyyy-mm-dd"] = None,
28
+ ) -> str:
29
+ """
30
+ Retrieve balance sheet data for a given ticker symbol.
31
+ Uses the configured fundamental_data vendor.
32
+ Args:
33
+ ticker (str): Ticker symbol of the company
34
+ freq (str): Reporting frequency: annual/quarterly (default quarterly)
35
+ curr_date (str): Current date you are trading at, yyyy-mm-dd
36
+ Returns:
37
+ str: A formatted report containing balance sheet data
38
+ """
39
+ return route_to_vendor("get_balance_sheet", ticker, freq, curr_date)
40
+
41
+
42
+ @tool
43
+ def get_cashflow(
44
+ ticker: Annotated[str, "ticker symbol"],
45
+ freq: Annotated[str, "reporting frequency: annual/quarterly"] = "quarterly",
46
+ curr_date: Annotated[str, "current date you are trading at, yyyy-mm-dd"] = None,
47
+ ) -> str:
48
+ """
49
+ Retrieve cash flow statement data for a given ticker symbol.
50
+ Uses the configured fundamental_data vendor.
51
+ Args:
52
+ ticker (str): Ticker symbol of the company
53
+ freq (str): Reporting frequency: annual/quarterly (default quarterly)
54
+ curr_date (str): Current date you are trading at, yyyy-mm-dd
55
+ Returns:
56
+ str: A formatted report containing cash flow statement data
57
+ """
58
+ return route_to_vendor("get_cashflow", ticker, freq, curr_date)
59
+
60
+
61
+ @tool
62
+ def get_income_statement(
63
+ ticker: Annotated[str, "ticker symbol"],
64
+ freq: Annotated[str, "reporting frequency: annual/quarterly"] = "quarterly",
65
+ curr_date: Annotated[str, "current date you are trading at, yyyy-mm-dd"] = None,
66
+ ) -> str:
67
+ """
68
+ Retrieve income statement data for a given ticker symbol.
69
+ Uses the configured fundamental_data vendor.
70
+ Args:
71
+ ticker (str): Ticker symbol of the company
72
+ freq (str): Reporting frequency: annual/quarterly (default quarterly)
73
+ curr_date (str): Current date you are trading at, yyyy-mm-dd
74
+ Returns:
75
+ str: A formatted report containing income statement data
76
+ """
77
+ return route_to_vendor("get_income_statement", ticker, freq, curr_date)
tradingagents/agents/utils/memory.py ADDED
@@ -0,0 +1,114 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import chromadb
2
+ from chromadb.config import Settings
3
+ from openai import OpenAI
4
+
5
+
6
+ class FinancialSituationMemory:
7
+ def __init__(self, name, config):
8
+ if config["backend_url"] == "http://localhost:11434/v1":
9
+ self.embedding = "nomic-embed-text"
10
+ else:
11
+ self.embedding = "text-embedding-3-small"
12
+ self.client = OpenAI(base_url=config["backend_url"])
13
+ self.chroma_client = chromadb.Client(Settings(allow_reset=True))
14
+ # get_or_create_collection을 사용하여 중복 생성 오류 방지
15
+ self.situation_collection = self.chroma_client.get_or_create_collection(name=name)
16
+
17
+ def get_embedding(self, text):
18
+ """Get OpenAI embedding for a text"""
19
+
20
+ response = self.client.embeddings.create(
21
+ model=self.embedding, input=text
22
+ )
23
+ return response.data[0].embedding
24
+
25
+ def add_situations(self, situations_and_advice):
26
+ """Add financial situations and their corresponding advice. Parameter is a list of tuples (situation, rec)"""
27
+
28
+ situations = []
29
+ advice = []
30
+ ids = []
31
+ embeddings = []
32
+
33
+ offset = self.situation_collection.count()
34
+
35
+ for i, (situation, recommendation) in enumerate(situations_and_advice):
36
+ situations.append(situation)
37
+ advice.append(recommendation)
38
+ ids.append(str(offset + i))
39
+ embeddings.append(self.get_embedding(situation))
40
+
41
+ self.situation_collection.add(
42
+ documents=situations,
43
+ metadatas=[{"recommendation": rec} for rec in advice],
44
+ embeddings=embeddings,
45
+ ids=ids,
46
+ )
47
+
48
+ def get_memories(self, current_situation, n_matches=1):
49
+ """Find matching recommendations using OpenAI embeddings"""
50
+ query_embedding = self.get_embedding(current_situation)
51
+
52
+ results = self.situation_collection.query(
53
+ query_embeddings=[query_embedding],
54
+ n_results=n_matches,
55
+ include=["metadatas", "documents", "distances"],
56
+ )
57
+
58
+ matched_results = []
59
+ for i in range(len(results["documents"][0])):
60
+ matched_results.append(
61
+ {
62
+ "matched_situation": results["documents"][0][i],
63
+ "recommendation": results["metadatas"][0][i]["recommendation"],
64
+ "similarity_score": 1 - results["distances"][0][i],
65
+ }
66
+ )
67
+
68
+ return matched_results
69
+
70
+
71
+ if __name__ == "__main__":
72
+ # Example usage
73
+ matcher = FinancialSituationMemory()
74
+
75
+ # Example data
76
+ example_data = [
77
+ (
78
+ "High inflation rate with rising interest rates and declining consumer spending",
79
+ "Consider defensive sectors like consumer staples and utilities. Review fixed-income portfolio duration.",
80
+ ),
81
+ (
82
+ "Tech sector showing high volatility with increasing institutional selling pressure",
83
+ "Reduce exposure to high-growth tech stocks. Look for value opportunities in established tech companies with strong cash flows.",
84
+ ),
85
+ (
86
+ "Strong dollar affecting emerging markets with increasing forex volatility",
87
+ "Hedge currency exposure in international positions. Consider reducing allocation to emerging market debt.",
88
+ ),
89
+ (
90
+ "Market showing signs of sector rotation with rising yields",
91
+ "Rebalance portfolio to maintain target allocations. Consider increasing exposure to sectors benefiting from higher rates.",
92
+ ),
93
+ ]
94
+
95
+ # Add the example situations and recommendations
96
+ matcher.add_situations(example_data)
97
+
98
+ # Example query
99
+ current_situation = """
100
+ Market showing increased volatility in tech sector, with institutional investors
101
+ reducing positions and rising interest rates affecting growth stock valuations
102
+ """
103
+
104
+ try:
105
+ recommendations = matcher.get_memories(current_situation, n_matches=2)
106
+
107
+ for i, rec in enumerate(recommendations, 1):
108
+ print(f"\nMatch {i}:")
109
+ print(f"Similarity Score: {rec['similarity_score']:.2f}")
110
+ print(f"Matched Situation: {rec['matched_situation']}")
111
+ print(f"Recommendation: {rec['recommendation']}")
112
+
113
+ except Exception as e:
114
+ print(f"Error during recommendation: {str(e)}")
tradingagents/agents/utils/news_data_tools.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain_core.tools import tool
2
+ from typing import Annotated
3
+ from tradingagents.dataflows.interface import route_to_vendor
4
+
5
+ @tool
6
+ def get_news(
7
+ ticker: Annotated[str, "Ticker symbol"],
8
+ start_date: Annotated[str, "Start date in yyyy-mm-dd format"],
9
+ end_date: Annotated[str, "End date in yyyy-mm-dd format"],
10
+ ) -> str:
11
+ """
12
+ Retrieve news data for a given ticker symbol.
13
+ Uses the configured news_data vendor.
14
+ Args:
15
+ ticker (str): Ticker symbol
16
+ start_date (str): Start date in yyyy-mm-dd format
17
+ end_date (str): End date in yyyy-mm-dd format
18
+ Returns:
19
+ str: A formatted string containing news data
20
+ """
21
+ return route_to_vendor("get_news", ticker, start_date, end_date)
22
+
23
+ @tool
24
+ def get_global_news(
25
+ curr_date: Annotated[str, "Current date in yyyy-mm-dd format"],
26
+ look_back_days: Annotated[int, "Number of days to look back"] = 7,
27
+ limit: Annotated[int, "Maximum number of articles to return"] = 5,
28
+ ) -> str:
29
+ """
30
+ Retrieve global news data.
31
+ Uses the configured news_data vendor.
32
+ Args:
33
+ curr_date (str): Current date in yyyy-mm-dd format
34
+ look_back_days (int): Number of days to look back (default 7)
35
+ limit (int): Maximum number of articles to return (default 5)
36
+ Returns:
37
+ str: A formatted string containing global news data
38
+ """
39
+ return route_to_vendor("get_global_news", curr_date, look_back_days, limit)
40
+
41
+ @tool
42
+ def get_insider_sentiment(
43
+ ticker: Annotated[str, "ticker symbol for the company"],
44
+ curr_date: Annotated[str, "current date you are trading at, yyyy-mm-dd"],
45
+ ) -> str:
46
+ """
47
+ Retrieve insider sentiment information about a company.
48
+ Uses the configured news_data vendor.
49
+ Args:
50
+ ticker (str): Ticker symbol of the company
51
+ curr_date (str): Current date you are trading at, yyyy-mm-dd
52
+ Returns:
53
+ str: A report of insider sentiment data
54
+ """
55
+ return route_to_vendor("get_insider_sentiment", ticker, curr_date)
56
+
57
+ @tool
58
+ def get_insider_transactions(
59
+ ticker: Annotated[str, "ticker symbol"],
60
+ curr_date: Annotated[str, "current date you are trading at, yyyy-mm-dd"],
61
+ ) -> str:
62
+ """
63
+ Retrieve insider transaction information about a company.
64
+ Uses the configured news_data vendor.
65
+ Args:
66
+ ticker (str): Ticker symbol of the company
67
+ curr_date (str): Current date you are trading at, yyyy-mm-dd
68
+ Returns:
69
+ str: A report of insider transaction data
70
+ """
71
+ return route_to_vendor("get_insider_transactions", ticker, curr_date)
tradingagents/agents/utils/technical_indicators_tools.py ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain_core.tools import tool
2
+ from typing import Annotated
3
+ from tradingagents.dataflows.interface import route_to_vendor
4
+
5
+ @tool
6
+ def get_indicators(
7
+ symbol: Annotated[str, "ticker symbol of the company"],
8
+ indicator: Annotated[str, "technical indicator to get the analysis and report of"],
9
+ curr_date: Annotated[str, "The current trading date you are trading on, YYYY-mm-dd"],
10
+ look_back_days: Annotated[int, "how many days to look back"] = 30,
11
+ ) -> str:
12
+ """
13
+ Retrieve technical indicators for a given ticker symbol.
14
+ Uses the configured technical_indicators vendor.
15
+ Args:
16
+ symbol (str): Ticker symbol of the company, e.g. AAPL, TSM
17
+ indicator (str): Technical indicator to get the analysis and report of
18
+ curr_date (str): The current trading date you are trading on, YYYY-mm-dd
19
+ look_back_days (int): How many days to look back, default is 30
20
+ Returns:
21
+ str: A formatted dataframe containing the technical indicators for the specified ticker symbol and indicator.
22
+ """
23
+ return route_to_vendor("get_indicators", symbol, indicator, curr_date, look_back_days)
tradingagents/agents/utils/timeseries_tools.py ADDED
@@ -0,0 +1,981 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ 시계열 분석 도구 모듈
3
+ LLM 에이전트가 사용할 수 있는 시계열 분석/예측 도구들
4
+ 딥러닝 기반 앙상블 예측 포함 (DLinear, NHITS, NBEATS)
5
+ """
6
+
7
+ from langchain_core.tools import tool
8
+ from typing import Annotated, List, Dict, Any, Optional
9
+ import pandas as pd
10
+ import numpy as np
11
+ from datetime import datetime, timedelta
12
+ import json
13
+ import yfinance as yf
14
+ import gc
15
+ import warnings
16
+ warnings.filterwarnings('ignore')
17
+
18
+ from tradingagents.dataflows.macro_data import (
19
+ get_macro_summary,
20
+ get_all_macro_features,
21
+ get_related_sector_data,
22
+ get_yahoo_macro_data,
23
+ get_sector_etf_data,
24
+ SECTOR_ETFS,
25
+ YAHOO_MACRO_TICKERS,
26
+ TICKER_SECTOR_MAP,
27
+ )
28
+
29
+
30
+ @tool
31
+ def get_macro_indicators(
32
+ end_date: Annotated[str, "The end date for macro data, YYYY-MM-DD format"],
33
+ indicators: Annotated[List[str], "List of macro indicators to fetch. Options: vix, dollar_index, gold, oil_wti, oil_brent, natural_gas, copper, sp500_index, nasdaq, treasury_10y"] = None,
34
+ ) -> str:
35
+ """
36
+ 거시경제 지표 데이터를 가져옵니다.
37
+ 금리, VIX, 달러 인덱스, 원자재 가격 등 시장에 영향을 미치는 거시경제 데이터를 수집합니다.
38
+
39
+ Args:
40
+ end_date: 기준 날짜
41
+ indicators: 가져올 지표 목록 (None이면 주요 지표 모두)
42
+
43
+ Returns:
44
+ 거시경제 지표 요약 문자열
45
+ """
46
+ try:
47
+ summary = get_macro_summary(end_date, lookback_days=90)
48
+
49
+ if not summary:
50
+ return "Failed to fetch macro indicators. Please check the date."
51
+
52
+ result = f"# Macro Economic Indicators as of {end_date}\n\n"
53
+
54
+ for indicator, data in summary.items():
55
+ result += f"## {indicator.upper().replace('_', ' ')}\n"
56
+ for key, value in data.items():
57
+ if isinstance(value, float):
58
+ if 'change' in key:
59
+ result += f"- {key}: {value:+.2f}%\n"
60
+ else:
61
+ result += f"- {key}: {value:.2f}\n"
62
+ else:
63
+ result += f"- {key}: {value}\n"
64
+ result += "\n"
65
+
66
+ return result
67
+ except Exception as e:
68
+ return f"Error fetching macro indicators: {str(e)}"
69
+
70
+
71
+ @tool
72
+ def get_sector_performance(
73
+ end_date: Annotated[str, "The end date for sector data, YYYY-MM-DD format"],
74
+ sectors: Annotated[List[str], "List of sectors. Options: technology, healthcare, financials, energy, semiconductors, consumer_discretionary, industrials, materials, utilities, real_estate, communication"] = None,
75
+ lookback_days: Annotated[int, "Number of days to look back"] = 30,
76
+ ) -> str:
77
+ """
78
+ 섹터별 성과 데이터를 가져옵니다.
79
+ 섹터 ETF의 수익률과 변동성을 분석하여 시장 동향을 파악합니다.
80
+
81
+ Args:
82
+ end_date: 기준 날짜
83
+ sectors: 분석할 섹터 목록
84
+ lookback_days: 과거 데이터 기간
85
+
86
+ Returns:
87
+ 섹터별 성과 분석 문자열
88
+ """
89
+ if sectors is None:
90
+ sectors = ["technology", "healthcare", "financials", "energy", "semiconductors"]
91
+
92
+ end_dt = pd.to_datetime(end_date)
93
+ start_dt = end_dt - timedelta(days=lookback_days + 30) # 여유 기간
94
+ start_date = start_dt.strftime("%Y-%m-%d")
95
+
96
+ result = f"# Sector Performance Analysis ({lookback_days} days ending {end_date})\n\n"
97
+
98
+ sector_data = []
99
+ for sector in sectors:
100
+ try:
101
+ data = get_sector_etf_data(sector, start_date, end_date)
102
+ if not data.empty:
103
+ close_col = f"{sector}_close"
104
+ if close_col in data.columns:
105
+ prices = data[close_col].dropna()
106
+ if len(prices) >= 2:
107
+ returns = float((prices.iloc[-1] / prices.iloc[0] - 1) * 100)
108
+ volatility = float(prices.pct_change().std() * np.sqrt(252) * 100)
109
+ current_price = float(prices.iloc[-1])
110
+
111
+ sector_data.append({
112
+ "sector": sector,
113
+ "return": returns,
114
+ "volatility": volatility,
115
+ "current": current_price
116
+ })
117
+ except Exception as e:
118
+ continue
119
+
120
+ if sector_data:
121
+ # 수익률 기준 정렬
122
+ sector_data.sort(key=lambda x: x["return"], reverse=True)
123
+
124
+ result += "| Sector | Return | Volatility (Ann.) | Current Price |\n"
125
+ result += "|--------|--------|-------------------|---------------|\n"
126
+
127
+ for s in sector_data:
128
+ result += f"| {s['sector'].title()} | {s['return']:+.2f}% | {s['volatility']:.2f}% | ${s['current']:.2f} |\n"
129
+
130
+ # 요약
131
+ result += f"\n## Summary\n"
132
+ best = sector_data[0]
133
+ worst = sector_data[-1]
134
+ result += f"- **Best performing**: {best['sector'].title()} ({best['return']:+.2f}%)\n"
135
+ result += f"- **Worst performing**: {worst['sector'].title()} ({worst['return']:+.2f}%)\n"
136
+ else:
137
+ result += "No sector data available.\n"
138
+
139
+ return result
140
+
141
+
142
+ @tool
143
+ def get_correlated_assets(
144
+ symbol: Annotated[str, "The stock ticker symbol to find correlations for"],
145
+ end_date: Annotated[str, "The end date, YYYY-MM-DD format"],
146
+ lookback_days: Annotated[int, "Number of days to calculate correlation"] = 90,
147
+ ) -> str:
148
+ """
149
+ 주어진 종목과 상관관계가 높은 자산들을 찾습니다.
150
+ 섹터 ETF, 지수, 원자재 등과의 상관관계를 분석합니다.
151
+
152
+ Args:
153
+ symbol: 분석할 종목 티커
154
+ end_date: 기준 날짜
155
+ lookback_days: 상관관계 계산 기간
156
+
157
+ Returns:
158
+ 상관관계 분석 결과 문자열
159
+ """
160
+ end_dt = pd.to_datetime(end_date)
161
+ start_dt = end_dt - timedelta(days=lookback_days + 30)
162
+ start_date = start_dt.strftime("%Y-%m-%d")
163
+
164
+ try:
165
+ # 대상 종목 데이터
166
+ target_data = yf.download(symbol, start=start_date, end=end_date, progress=False)
167
+ if target_data.empty:
168
+ return f"No data found for {symbol}"
169
+
170
+ # MultiIndex columns 처리
171
+ if isinstance(target_data.columns, pd.MultiIndex):
172
+ target_data.columns = target_data.columns.get_level_values(0)
173
+
174
+ target_returns = target_data["Close"].pct_change().dropna()
175
+
176
+ # 비교할 자산들
177
+ comparison_assets = {
178
+ # 지수
179
+ "S&P 500": "^GSPC",
180
+ "NASDAQ": "^IXIC",
181
+ "Russell 2000": "^RUT",
182
+ # 섹터 ETF
183
+ "Technology (XLK)": "XLK",
184
+ "Semiconductors (SOXX)": "SOXX",
185
+ "Energy (XLE)": "XLE",
186
+ "Financials (XLF)": "XLF",
187
+ # 거시
188
+ "VIX": "^VIX",
189
+ "Gold": "GC=F",
190
+ "Oil WTI": "CL=F",
191
+ "Dollar Index": "DX-Y.NYB",
192
+ }
193
+
194
+ correlations = []
195
+
196
+ for name, ticker in comparison_assets.items():
197
+ try:
198
+ comp_data = yf.download(ticker, start=start_date, end=end_date, progress=False)
199
+ if not comp_data.empty:
200
+ # MultiIndex columns 처리
201
+ if isinstance(comp_data.columns, pd.MultiIndex):
202
+ comp_data.columns = comp_data.columns.get_level_values(0)
203
+
204
+ comp_returns = comp_data["Close"].pct_change().dropna()
205
+
206
+ # 날짜 정렬
207
+ aligned = pd.concat([target_returns, comp_returns], axis=1).dropna()
208
+ if len(aligned) > 10:
209
+ corr = aligned.iloc[:, 0].corr(aligned.iloc[:, 1])
210
+ correlations.append({"asset": name, "correlation": corr})
211
+ except:
212
+ continue
213
+
214
+ if not correlations:
215
+ return f"Could not calculate correlations for {symbol}"
216
+
217
+ # 상관관계 절대값 기준 정렬
218
+ correlations.sort(key=lambda x: abs(x["correlation"]), reverse=True)
219
+
220
+ result = f"# Correlation Analysis for {symbol}\n"
221
+ result += f"Period: {lookback_days} days ending {end_date}\n\n"
222
+
223
+ result += "| Asset | Correlation |\n"
224
+ result += "|-------|-------------|\n"
225
+
226
+ for c in correlations:
227
+ corr_val = c["correlation"]
228
+ if corr_val > 0.7:
229
+ strength = "🟢 Strong positive"
230
+ elif corr_val > 0.4:
231
+ strength = "🔵 Moderate positive"
232
+ elif corr_val > -0.4:
233
+ strength = "⚪ Weak"
234
+ elif corr_val > -0.7:
235
+ strength = "🟠 Moderate negative"
236
+ else:
237
+ strength = "🔴 Strong negative"
238
+
239
+ result += f"| {c['asset']} | {corr_val:+.3f} ({strength}) |\n"
240
+
241
+ # 인사이트
242
+ result += "\n## Key Insights\n"
243
+
244
+ high_positive = [c for c in correlations if c["correlation"] > 0.6]
245
+ high_negative = [c for c in correlations if c["correlation"] < -0.4]
246
+
247
+ if high_positive:
248
+ result += f"- **Moves with**: {', '.join([c['asset'] for c in high_positive[:3]])}\n"
249
+ if high_negative:
250
+ result += f"- **Hedges against**: {', '.join([c['asset'] for c in high_negative[:3]])}\n"
251
+
252
+ return result
253
+
254
+ except Exception as e:
255
+ return f"Error calculating correlations: {str(e)}"
256
+
257
+
258
+ @tool
259
+ def analyze_stock_for_forecasting(
260
+ symbol: Annotated[str, "The stock ticker symbol to analyze"],
261
+ end_date: Annotated[str, "The analysis date, YYYY-MM-DD format"],
262
+ ) -> str:
263
+ """
264
+ 주식의 특성을 분석하여 시계열 예측에 필요한 변수들을 식별합니다.
265
+ 종목의 섹터, 관련 지수, 영향을 미치는 거시��제 요인 등을 분석합니다.
266
+
267
+ Args:
268
+ symbol: 분석할 종목 티커
269
+ end_date: 기준 날짜
270
+
271
+ Returns:
272
+ 종목 분석 및 추천 변수 목록
273
+ """
274
+ try:
275
+ # yfinance로 종목 정보 가져오기
276
+ stock = yf.Ticker(symbol)
277
+ info = stock.info
278
+
279
+ result = f"# Stock Analysis for Time Series Forecasting: {symbol}\n\n"
280
+
281
+ # 기본 정보
282
+ result += "## Company Information\n"
283
+ result += f"- **Name**: {info.get('longName', 'N/A')}\n"
284
+ result += f"- **Sector**: {info.get('sector', 'N/A')}\n"
285
+ result += f"- **Industry**: {info.get('industry', 'N/A')}\n"
286
+ result += f"- **Market Cap**: ${info.get('marketCap', 0)/1e9:.2f}B\n"
287
+
288
+ # 미리 정의된 섹터 매핑 확인
289
+ known_sectors = TICKER_SECTOR_MAP.get(symbol.upper(), [])
290
+
291
+ result += "\n## Recommended Features for Forecasting\n"
292
+
293
+ # 섹터 기반 추천
294
+ sector = info.get('sector', '').lower()
295
+ industry = info.get('industry', '').lower()
296
+
297
+ recommended_features = []
298
+
299
+ # 기본 시장 지표
300
+ recommended_features.extend([
301
+ ("S&P 500 Index", "Market benchmark"),
302
+ ("VIX", "Market volatility/fear gauge"),
303
+ ])
304
+
305
+ # 섹터별 추천
306
+ if 'technology' in sector or 'semiconductor' in industry:
307
+ recommended_features.extend([
308
+ ("NASDAQ Index", "Tech-heavy index correlation"),
309
+ ("Semiconductors ETF (SOXX)", "Sector performance"),
310
+ ("Technology ETF (XLK)", "Broad tech sector"),
311
+ ])
312
+
313
+ if 'energy' in sector or 'oil' in industry:
314
+ recommended_features.extend([
315
+ ("Oil WTI", "Commodity price driver"),
316
+ ("Natural Gas", "Energy commodity"),
317
+ ("Energy ETF (XLE)", "Sector performance"),
318
+ ])
319
+
320
+ if 'financial' in sector or 'bank' in industry:
321
+ recommended_features.extend([
322
+ ("10Y Treasury Yield", "Interest rate sensitivity"),
323
+ ("Yield Spread (10Y-2Y)", "Economic outlook indicator"),
324
+ ("Financials ETF (XLF)", "Sector performance"),
325
+ ])
326
+
327
+ # 공통 거시경제 지표
328
+ recommended_features.extend([
329
+ ("Dollar Index", "Currency impact"),
330
+ ("Gold", "Safe haven indicator"),
331
+ ("10Y Treasury Yield", "Risk-free rate benchmark"),
332
+ ])
333
+
334
+ # 중복 제거
335
+ seen = set()
336
+ unique_features = []
337
+ for f in recommended_features:
338
+ if f[0] not in seen:
339
+ seen.add(f[0])
340
+ unique_features.append(f)
341
+
342
+ result += "\n| Feature | Reason |\n"
343
+ result += "|---------|--------|\n"
344
+ for feature, reason in unique_features:
345
+ result += f"| {feature} | {reason} |\n"
346
+
347
+ # 상관관계 기반 추가 분석 제안
348
+ result += "\n## Additional Analysis Suggestions\n"
349
+ result += "- Use `get_correlated_assets` to find empirical correlations\n"
350
+ result += "- Use `get_sector_performance` to compare sector trends\n"
351
+ result += "- Use `get_macro_indicators` to get current macro environment\n"
352
+
353
+ return result
354
+
355
+ except Exception as e:
356
+ return f"Error analyzing stock: {str(e)}"
357
+
358
+
359
+ @tool
360
+ def run_simple_forecast(
361
+ symbol: Annotated[str, "The stock ticker symbol to forecast"],
362
+ end_date: Annotated[str, "The forecast start date, YYYY-MM-DD format"],
363
+ forecast_days: Annotated[int, "Number of days to forecast"] = 30,
364
+ include_features: Annotated[List[str], "Additional features to include: vix, gold, oil_wti, dollar_index, sector"] = None,
365
+ ) -> str:
366
+ """
367
+ 간단한 시계열 예측을 수행합니다.
368
+ 이동평균, 추세, 기술적 지표를 기반으로 향후 가격 방향을 예측합니다.
369
+
370
+ Args:
371
+ symbol: 예측할 종목 티커
372
+ end_date: 예측 시작 날짜
373
+ forecast_days: 예측 기간 (일)
374
+ include_features: 포함할 추가 특성
375
+
376
+ Returns:
377
+ 예측 결과 및 분석 문자열
378
+ """
379
+ try:
380
+ end_dt = pd.to_datetime(end_date)
381
+ start_dt = end_dt - timedelta(days=365)
382
+ start_date = start_dt.strftime("%Y-%m-%d")
383
+
384
+ # 주가 데이터
385
+ stock_data = yf.download(symbol, start=start_date, end=end_date, progress=False)
386
+ if stock_data.empty:
387
+ return f"No data found for {symbol}"
388
+
389
+ # MultiIndex columns 처리
390
+ if isinstance(stock_data.columns, pd.MultiIndex):
391
+ stock_data.columns = stock_data.columns.get_level_values(0)
392
+
393
+ close = stock_data["Close"]
394
+
395
+ # 기술적 지표 계산
396
+ sma_20 = close.rolling(20).mean()
397
+ sma_50 = close.rolling(50).mean()
398
+ sma_200 = close.rolling(200).mean()
399
+
400
+ # RSI
401
+ delta = close.diff()
402
+ gain = delta.where(delta > 0, 0).rolling(14).mean()
403
+ loss = (-delta.where(delta < 0, 0)).rolling(14).mean()
404
+ rs = gain / loss
405
+ rsi = 100 - (100 / (1 + rs))
406
+
407
+ # 변동성
408
+ volatility = close.pct_change().rolling(20).std() * np.sqrt(252) * 100
409
+
410
+ # 추세 분석 - .iloc[0] 사용하여 스칼라 추출
411
+ current_price = float(close.iloc[-1].iloc[0]) if hasattr(close.iloc[-1], 'iloc') else float(close.iloc[-1])
412
+ current_sma20 = float(sma_20.iloc[-1].iloc[0]) if hasattr(sma_20.iloc[-1], 'iloc') else float(sma_20.iloc[-1])
413
+ current_sma50 = float(sma_50.iloc[-1].iloc[0]) if hasattr(sma_50.iloc[-1], 'iloc') else float(sma_50.iloc[-1])
414
+
415
+ if len(sma_200.dropna()) > 0:
416
+ current_sma200 = float(sma_200.iloc[-1].iloc[0]) if hasattr(sma_200.iloc[-1], 'iloc') else float(sma_200.iloc[-1])
417
+ else:
418
+ current_sma200 = current_price
419
+
420
+ current_rsi = float(rsi.iloc[-1].iloc[0]) if hasattr(rsi.iloc[-1], 'iloc') else float(rsi.iloc[-1])
421
+ current_vol = float(volatility.iloc[-1].iloc[0]) if hasattr(volatility.iloc[-1], 'iloc') else float(volatility.iloc[-1])
422
+
423
+ # 추세 점수 계산
424
+ trend_score = 0
425
+ signals = []
426
+
427
+ # SMA 기반 신호
428
+ if current_price > current_sma20:
429
+ trend_score += 1
430
+ signals.append("Price above SMA20 (short-term bullish)")
431
+ else:
432
+ trend_score -= 1
433
+ signals.append("Price below SMA20 (short-term bearish)")
434
+
435
+ if current_price > current_sma50:
436
+ trend_score += 1
437
+ signals.append("Price above SMA50 (medium-term bullish)")
438
+ else:
439
+ trend_score -= 1
440
+ signals.append("Price below SMA50 (medium-term bearish)")
441
+
442
+ if current_price > current_sma200:
443
+ trend_score += 2
444
+ signals.append("Price above SMA200 (long-term bullish)")
445
+ else:
446
+ trend_score -= 2
447
+ signals.append("Price below SMA200 (long-term bearish)")
448
+
449
+ # RSI 기반 신호
450
+ if current_rsi > 70:
451
+ trend_score -= 1
452
+ signals.append(f"RSI={current_rsi:.1f} (Overbought - potential reversal)")
453
+ elif current_rsi < 30:
454
+ trend_score += 1
455
+ signals.append(f"RSI={current_rsi:.1f} (Oversold - potential bounce)")
456
+ else:
457
+ signals.append(f"RSI={current_rsi:.1f} (Neutral)")
458
+
459
+ # Golden/Death Cross 확인
460
+ if len(sma_50) > 5 and len(sma_200.dropna()) > 5:
461
+ recent_cross = sma_50.iloc[-5:] - sma_200.iloc[-5:]
462
+ # Series에서 스칼라 값 추출
463
+ last_cross = float(recent_cross.iloc[-1].iloc[0]) if hasattr(recent_cross.iloc[-1], 'iloc') else float(recent_cross.iloc[-1])
464
+ first_cross = float(recent_cross.iloc[0].iloc[0]) if hasattr(recent_cross.iloc[0], 'iloc') else float(recent_cross.iloc[0])
465
+ if last_cross > 0 and first_cross < 0:
466
+ trend_score += 2
467
+ signals.append("🔥 Golden Cross detected (Very Bullish)")
468
+ elif last_cross < 0 and first_cross > 0:
469
+ trend_score -= 2
470
+ signals.append("💀 Death Cross detected (Very Bearish)")
471
+
472
+ # 모멘텀 계산
473
+ price_22 = float(close.iloc[-22].iloc[0]) if hasattr(close.iloc[-22], 'iloc') else float(close.iloc[-22])
474
+ price_66 = float(close.iloc[-66].iloc[0]) if hasattr(close.iloc[-66], 'iloc') else float(close.iloc[-66])
475
+ momentum_1m = (current_price / price_22 - 1) * 100 if len(close) > 22 else 0
476
+ momentum_3m = (current_price / price_66 - 1) * 100 if len(close) > 66 else 0
477
+
478
+ # 예측 방향 결정
479
+ if trend_score >= 3:
480
+ direction = "BULLISH"
481
+ confidence = "High"
482
+ emoji = "🚀"
483
+ elif trend_score >= 1:
484
+ direction = "SLIGHTLY BULLISH"
485
+ confidence = "Moderate"
486
+ emoji = "📈"
487
+ elif trend_score <= -3:
488
+ direction = "BEARISH"
489
+ confidence = "High"
490
+ emoji = "📉"
491
+ elif trend_score <= -1:
492
+ direction = "SLIGHTLY BEARISH"
493
+ confidence = "Moderate"
494
+ emoji = "🔻"
495
+ else:
496
+ direction = "NEUTRAL"
497
+ confidence = "Low"
498
+ emoji = "➡️"
499
+
500
+ # 가격 목표 계산 (간단한 추정)
501
+ avg_daily_return = float(close.pct_change().tail(60).mean())
502
+ expected_return = avg_daily_return * forecast_days
503
+
504
+ if trend_score > 0:
505
+ target_price = current_price * (1 + abs(expected_return) * (1 + trend_score/5))
506
+ elif trend_score < 0:
507
+ target_price = current_price * (1 - abs(expected_return) * (1 + abs(trend_score)/5))
508
+ else:
509
+ target_price = current_price * (1 + expected_return)
510
+
511
+ # 결과 생성
512
+ result = f"# Time Series Forecast: {symbol}\n"
513
+ result += f"Analysis Date: {end_date}\n"
514
+ result += f"Forecast Period: {forecast_days} days\n\n"
515
+
516
+ result += "## Current Status\n"
517
+ result += f"- **Current Price**: ${current_price:.2f}\n"
518
+ result += f"- **20-day SMA**: ${current_sma20:.2f}\n"
519
+ result += f"- **50-day SMA**: ${current_sma50:.2f}\n"
520
+ result += f"- **200-day SMA**: ${current_sma200:.2f}\n"
521
+ result += f"- **RSI (14)**: {current_rsi:.1f}\n"
522
+ result += f"- **Volatility (Ann.)**: {current_vol:.1f}%\n\n"
523
+
524
+ result += "## Momentum\n"
525
+ result += f"- **1-Month Return**: {momentum_1m:+.2f}%\n"
526
+ result += f"- **3-Month Return**: {momentum_3m:+.2f}%\n\n"
527
+
528
+ result += "## Technical Signals\n"
529
+ for signal in signals:
530
+ result += f"- {signal}\n"
531
+
532
+ result += f"\n## Forecast Result {emoji}\n"
533
+ result += f"- **Direction**: {direction}\n"
534
+ result += f"- **Confidence**: {confidence}\n"
535
+ result += f"- **Trend Score**: {trend_score:+d} (range: -6 to +6)\n"
536
+ result += f"- **Price Target ({forecast_days}d)**: ${target_price:.2f} ({((target_price/current_price)-1)*100:+.2f}%)\n"
537
+
538
+ result += f"\n## Risk Assessment\n"
539
+ result += f"- **Annualized Volatility**: {current_vol:.1f}%\n"
540
+ result += f"- **Expected {forecast_days}d Range**: ${current_price * (1 - current_vol/100 * np.sqrt(forecast_days/252)):.2f} - ${current_price * (1 + current_vol/100 * np.sqrt(forecast_days/252)):.2f}\n"
541
+
542
+ result += f"\n⚠️ *This is a simplified technical analysis forecast. Not financial advice.*\n"
543
+
544
+ return result
545
+
546
+ except Exception as e:
547
+ return f"Error running forecast: {str(e)}"
548
+
549
+
550
+ @tool
551
+ def collect_forecast_features(
552
+ symbol: Annotated[str, "The stock ticker symbol"],
553
+ end_date: Annotated[str, "The end date, YYYY-MM-DD format"],
554
+ lookback_days: Annotated[int, "Days of historical data to collect"] = 365,
555
+ ) -> str:
556
+ """
557
+ 시계열 예측을 위한 모든 특성 데이터를 수집합니다.
558
+ 주가, 거시경제 지표, 섹터 데이터를 통합하여 예측 모델에 사용할 수 있는 데이터셋을 생성합니다.
559
+
560
+ Args:
561
+ symbol: 대상 종목 티커
562
+ end_date: 기준 날짜
563
+ lookback_days: 과거 데이터 기간
564
+
565
+ Returns:
566
+ 수집된 특성 데이터 요약
567
+ """
568
+ try:
569
+ end_dt = pd.to_datetime(end_date)
570
+ start_dt = end_dt - timedelta(days=lookback_days)
571
+ start_date = start_dt.strftime("%Y-%m-%d")
572
+
573
+ # 1. 주가 데이터
574
+ stock_data = yf.download(symbol, start=start_date, end=end_date, progress=False)
575
+ if stock_data.empty:
576
+ return f"No data found for {symbol}"
577
+
578
+ # MultiIndex columns 처리
579
+ if isinstance(stock_data.columns, pd.MultiIndex):
580
+ stock_data.columns = stock_data.columns.get_level_values(0)
581
+
582
+ # 2. 거시경제 데이터
583
+ macro_data = get_all_macro_features(end_date, lookback_days)
584
+
585
+ # 3. 섹터 데이터
586
+ sector_data = get_related_sector_data(symbol, start_date, end_date)
587
+
588
+ # 데이터 병합
589
+ result_df = stock_data[["Close", "Volume"]].copy()
590
+ result_df.columns = [f"{symbol}_close", f"{symbol}_volume"]
591
+
592
+ if not macro_data.empty:
593
+ result_df = result_df.join(macro_data, how="left")
594
+
595
+ if not sector_data.empty:
596
+ result_df = result_df.join(sector_data, how="left")
597
+
598
+ result_df = result_df.ffill().bfill().dropna()
599
+
600
+ # 통계 요약
601
+ result = f"# Feature Collection Summary for {symbol}\n"
602
+ result += f"Period: {start_date} to {end_date}\n\n"
603
+
604
+ result += "## Collected Features\n"
605
+ result += f"- **Total Data Points**: {len(result_df)}\n"
606
+ result += f"- **Number of Features**: {len(result_df.columns)}\n\n"
607
+
608
+ result += "| Feature | Mean | Std | Min | Max |\n"
609
+ result += "|---------|------|-----|-----|-----|\n"
610
+
611
+ for col in result_df.columns:
612
+ mean = result_df[col].mean()
613
+ std = result_df[col].std()
614
+ min_val = result_df[col].min()
615
+ max_val = result_df[col].max()
616
+ result += f"| {col} | {mean:.2f} | {std:.2f} | {min_val:.2f} | {max_val:.2f} |\n"
617
+
618
+ # 상관관계 매트릭스 (주가와의 상관관계)
619
+ result += "\n## Correlation with Target Price\n"
620
+ target_col = f"{symbol}_close"
621
+ correlations = result_df.corr()[target_col].sort_values(ascending=False)
622
+
623
+ result += "| Feature | Correlation |\n"
624
+ result += "|---------|-------------|\n"
625
+ for feat, corr in correlations.items():
626
+ if feat != target_col:
627
+ result += f"| {feat} | {corr:+.3f} |\n"
628
+
629
+ result += "\n## Data Quality\n"
630
+ missing = result_df.isnull().sum().sum()
631
+ result += f"- Missing values after forward fill: {missing}\n"
632
+ result += f"- Data completeness: {(1 - missing/(len(result_df)*len(result_df.columns)))*100:.1f}%\n"
633
+
634
+ return result
635
+
636
+ except Exception as e:
637
+ return f"Error collecting features: {str(e)}"
638
+
639
+
640
+ # ============================================================================
641
+ # 딥러닝 기반 앙상블 예측 (NeuralForecast)
642
+ # ============================================================================
643
+
644
+ def _check_neuralforecast_available():
645
+ """NeuralForecast 라이브러리 가용성 확인"""
646
+ try:
647
+ from neuralforecast import NeuralForecast
648
+ from neuralforecast.models import DLinear, NHITS, NBEATS
649
+ return True
650
+ except ImportError:
651
+ return False
652
+
653
+
654
+ class EnsembleForecaster:
655
+ """메모리 효율적인 딥러닝 앙상블 시계열 예측기"""
656
+
657
+ def __init__(
658
+ self,
659
+ input_size: int = 30,
660
+ horizon: int = 5,
661
+ batch_size: int = 32,
662
+ max_steps: int = 100,
663
+ ):
664
+ self.input_size = input_size
665
+ self.horizon = horizon
666
+ self.batch_size = batch_size
667
+ self.max_steps = max_steps
668
+ self.weights = {
669
+ 'DLinear': 0.3,
670
+ 'NHITS': 0.4,
671
+ 'NBEATS': 0.3
672
+ }
673
+
674
+ def prepare_data(self, symbol: str, end_date: str, lookback_days: int = 365) -> pd.DataFrame:
675
+ """yfinance에서 데이터를 NeuralForecast 형식으로 변환"""
676
+ end_dt = pd.to_datetime(end_date)
677
+ start_dt = end_dt - timedelta(days=lookback_days)
678
+
679
+ ticker = yf.Ticker(symbol)
680
+ df = ticker.history(start=start_dt.strftime("%Y-%m-%d"), end=end_dt.strftime("%Y-%m-%d"))
681
+
682
+ if df.empty:
683
+ raise ValueError(f"No data found for {symbol}")
684
+
685
+ nf_df = pd.DataFrame({
686
+ 'unique_id': symbol,
687
+ 'ds': df.index.tz_localize(None),
688
+ 'y': df['Close'].values
689
+ })
690
+
691
+ return nf_df
692
+
693
+ def _run_single_model(self, model_class, model_name: str, df: pd.DataFrame) -> pd.DataFrame:
694
+ """단일 모델 실행 후 메모리 해제"""
695
+ from neuralforecast import NeuralForecast
696
+ from neuralforecast.models import DLinear, NHITS, NBEATS
697
+
698
+ if model_name == 'DLinear':
699
+ model = model_class(
700
+ h=self.horizon,
701
+ input_size=self.input_size,
702
+ max_steps=self.max_steps,
703
+ batch_size=self.batch_size,
704
+ scaler_type='standard',
705
+ random_seed=42,
706
+ )
707
+ elif model_name == 'NHITS':
708
+ model = model_class(
709
+ h=self.horizon,
710
+ input_size=self.input_size,
711
+ max_steps=self.max_steps,
712
+ batch_size=self.batch_size,
713
+ n_pool_kernel_size=[2, 2, 1],
714
+ n_freq_downsample=[4, 2, 1],
715
+ scaler_type='standard',
716
+ random_seed=42,
717
+ )
718
+ elif model_name == 'NBEATS':
719
+ model = model_class(
720
+ h=self.horizon,
721
+ input_size=self.input_size,
722
+ max_steps=self.max_steps,
723
+ batch_size=self.batch_size,
724
+ scaler_type='standard',
725
+ random_seed=42,
726
+ )
727
+
728
+ nf = NeuralForecast(models=[model], freq='B')
729
+ nf.fit(df=df)
730
+ forecast = nf.predict()
731
+
732
+ del nf, model
733
+ gc.collect()
734
+
735
+ return forecast
736
+
737
+ def forecast(self, symbol: str, end_date: str, lookback_days: int = 365,
738
+ use_single_model: bool = True) -> dict:
739
+ """앙상블 예측 실행
740
+
741
+ Args:
742
+ use_single_model: True면 DLinear만 사용 (빠름, 2초), False면 3개 모델 사용 (8초)
743
+ """
744
+ from neuralforecast.models import DLinear, NHITS, NBEATS
745
+
746
+ df = self.prepare_data(symbol, end_date, lookback_days)
747
+
748
+ # 최적화: DLinear만 사용 (논문에 따르면 복잡한 모델과 비슷하거나 더 나은 성능)
749
+ if use_single_model:
750
+ models_to_run = [
751
+ (DLinear, 'DLinear'),
752
+ ]
753
+ else:
754
+ models_to_run = [
755
+ (DLinear, 'DLinear'),
756
+ (NHITS, 'NHITS'),
757
+ (NBEATS, 'NBEATS'),
758
+ ]
759
+
760
+ forecasts = {}
761
+ for model_class, model_name in models_to_run:
762
+ try:
763
+ forecast = self._run_single_model(model_class, model_name, df)
764
+ forecasts[model_name] = forecast
765
+ except Exception as e:
766
+ continue
767
+
768
+ # 앙상블 결합
769
+ ensemble_pred = None
770
+ total_weight = 0
771
+
772
+ for model_name, forecast in forecasts.items():
773
+ pred_col = model_name
774
+ if pred_col in forecast.columns:
775
+ weight = self.weights.get(model_name, 0.33)
776
+ if ensemble_pred is None:
777
+ ensemble_pred = forecast[pred_col].values * weight
778
+ else:
779
+ ensemble_pred += forecast[pred_col].values * weight
780
+ total_weight += weight
781
+
782
+ if ensemble_pred is not None and total_weight > 0:
783
+ ensemble_pred = ensemble_pred / total_weight
784
+
785
+ current_price = df['y'].iloc[-1]
786
+
787
+ # 각 모델별 예측 요약
788
+ model_predictions = {}
789
+ for model_name, forecast in forecasts.items():
790
+ pred_col = model_name
791
+ if pred_col in forecast.columns:
792
+ preds = forecast[pred_col].values
793
+ model_predictions[model_name] = {
794
+ 'predictions': preds.tolist(),
795
+ 'final_price': float(preds[-1]),
796
+ 'change_pct': float((preds[-1] / current_price - 1) * 100)
797
+ }
798
+
799
+ # 결과 생성
800
+ result = {
801
+ 'symbol': symbol,
802
+ 'current_price': float(current_price),
803
+ 'forecast_dates': self._get_forecast_dates(end_date),
804
+ 'ensemble_predictions': ensemble_pred.tolist() if ensemble_pred is not None else [],
805
+ 'ensemble_final_price': float(ensemble_pred[-1]) if ensemble_pred is not None else current_price,
806
+ 'ensemble_change_pct': float((ensemble_pred[-1] / current_price - 1) * 100) if ensemble_pred is not None else 0,
807
+ 'model_predictions': model_predictions,
808
+ 'signal': self._generate_signal(ensemble_pred, current_price),
809
+ 'confidence': self._calculate_confidence(model_predictions),
810
+ }
811
+
812
+ return result
813
+
814
+ def _get_forecast_dates(self, end_date: str) -> list:
815
+ end_dt = pd.to_datetime(end_date)
816
+ dates = pd.bdate_range(start=end_dt + timedelta(days=1), periods=self.horizon)
817
+ return [d.strftime('%Y-%m-%d') for d in dates]
818
+
819
+ def _generate_signal(self, predictions: np.ndarray, current_price: float) -> dict:
820
+ if predictions is None or len(predictions) == 0:
821
+ return {'action': 'HOLD', 'strength': 0, 'reason': 'No valid predictions'}
822
+
823
+ final_pred = predictions[-1]
824
+ change_pct = (final_pred / current_price - 1) * 100
825
+ trend_direction = np.mean(np.diff(predictions))
826
+
827
+ if change_pct > 3:
828
+ action = 'STRONG_BUY'
829
+ strength = min(100, int(change_pct * 10))
830
+ elif change_pct > 1:
831
+ action = 'BUY'
832
+ strength = min(80, int(change_pct * 15))
833
+ elif change_pct < -3:
834
+ action = 'STRONG_SELL'
835
+ strength = min(100, int(abs(change_pct) * 10))
836
+ elif change_pct < -1:
837
+ action = 'SELL'
838
+ strength = min(80, int(abs(change_pct) * 15))
839
+ else:
840
+ action = 'HOLD'
841
+ strength = 50
842
+
843
+ return {
844
+ 'action': action,
845
+ 'strength': strength,
846
+ 'expected_change_pct': round(change_pct, 2),
847
+ 'trend': 'UPWARD' if trend_direction > 0 else 'DOWNWARD' if trend_direction < 0 else 'SIDEWAYS',
848
+ 'reason': f"Ensemble predicts {change_pct:+.2f}% change over {self.horizon} days"
849
+ }
850
+
851
+ def _calculate_confidence(self, model_predictions: dict) -> dict:
852
+ if not model_predictions:
853
+ return {'score': 0, 'agreement': 'NONE'}
854
+
855
+ changes = [p['change_pct'] for p in model_predictions.values()]
856
+
857
+ if len(changes) < 2:
858
+ return {'score': 50, 'agreement': 'SINGLE_MODEL'}
859
+
860
+ std = np.std(changes)
861
+ all_positive = all(c > 0 for c in changes)
862
+ all_negative = all(c < 0 for c in changes)
863
+ direction_agreement = all_positive or all_negative
864
+
865
+ if std < 0.5 and direction_agreement:
866
+ score = 90
867
+ agreement = 'VERY_HIGH'
868
+ elif std < 1.0 and direction_agreement:
869
+ score = 75
870
+ agreement = 'HIGH'
871
+ elif direction_agreement:
872
+ score = 60
873
+ agreement = 'MODERATE'
874
+ else:
875
+ score = 40
876
+ agreement = 'LOW'
877
+
878
+ return {
879
+ 'score': score,
880
+ 'agreement': agreement,
881
+ 'std_deviation': round(std, 2),
882
+ 'direction_aligned': direction_agreement
883
+ }
884
+
885
+
886
+ @tool
887
+ def run_ensemble_forecast(
888
+ symbol: Annotated[str, "The stock ticker symbol to forecast"],
889
+ end_date: Annotated[str, "The forecast start date, YYYY-MM-DD format"],
890
+ forecast_days: Annotated[int, "Number of days to forecast (default: 5)"] = 5,
891
+ input_size: Annotated[int, "Number of historical days to use as input (default: 30)"] = 30,
892
+ use_single_model: Annotated[bool, "Use only DLinear for faster prediction (default: True)"] = True,
893
+ ) -> str:
894
+ """
895
+ 딥러닝 모델을 사용한 시계열 예측을 수��합니다.
896
+ 기본값: DLinear 단일 모델 사용 (빠름, ~2초)
897
+ use_single_model=False: 3개 모델 앙상블 (느림, ~8초)
898
+
899
+ 모델 설명:
900
+ - DLinear: 선형 분해 기반 경량 모델 (빠르고 안정적, 논문에서 SOTA급 성능)
901
+ - NHITS: 계층적 시간 인터폴레이션 (장기 패턴 포착)
902
+ - NBEATS: 신경망 기반 분해 (높은 정확도)
903
+
904
+ Args:
905
+ symbol: 예측할 종목 티커
906
+ end_date: 예측 시작 날짜
907
+ forecast_days: 예측 기간 (일)
908
+ input_size: 입력 시퀀스 길이 (일)
909
+ use_single_model: True면 DLinear만 사용 (권장)
910
+
911
+ Returns:
912
+ 예측 결과 문자열
913
+ """
914
+ # NeuralForecast 가용성 확인
915
+ if not _check_neuralforecast_available():
916
+ return "NeuralForecast library not available. Please install: pip install neuralforecast scipy"
917
+
918
+ try:
919
+ forecaster = EnsembleForecaster(
920
+ input_size=input_size,
921
+ horizon=forecast_days,
922
+ batch_size=32,
923
+ max_steps=100,
924
+ )
925
+
926
+ result = forecaster.forecast(symbol, end_date, lookback_days=365,
927
+ use_single_model=use_single_model)
928
+
929
+ # 포맷팅된 결과 생성
930
+ report = []
931
+ report.append(f"# Deep Learning Ensemble Forecast: {result['symbol']}")
932
+ report.append(f"Models: DLinear (30%) + NHITS (40%) + NBEATS (30%)\n")
933
+
934
+ report.append(f"## Current Status")
935
+ report.append(f"- **Current Price**: ${result['current_price']:.2f}")
936
+
937
+ report.append(f"\n## Ensemble Prediction ({forecast_days}-Day Forecast)")
938
+ report.append(f"- **Predicted Price**: ${result['ensemble_final_price']:.2f}")
939
+ report.append(f"- **Expected Change**: {result['ensemble_change_pct']:+.2f}%")
940
+
941
+ report.append(f"\n## Daily Predictions")
942
+ for i, (date, pred) in enumerate(zip(result['forecast_dates'], result['ensemble_predictions'])):
943
+ change = (pred / result['current_price'] - 1) * 100
944
+ report.append(f" Day {i+1} ({date}): ${pred:.2f} ({change:+.2f}%)")
945
+
946
+ report.append(f"\n## Individual Model Predictions")
947
+ for model_name, pred in result['model_predictions'].items():
948
+ report.append(f" - {model_name}: ${pred['final_price']:.2f} ({pred['change_pct']:+.2f}%)")
949
+
950
+ signal = result['signal']
951
+ report.append(f"\n## Trading Signal")
952
+ report.append(f" - **Action**: {signal['action']}")
953
+ report.append(f" - **Strength**: {signal['strength']}/100")
954
+ report.append(f" - **Trend**: {signal['trend']}")
955
+ report.append(f" - **Reason**: {signal['reason']}")
956
+
957
+ conf = result['confidence']
958
+ report.append(f"\n## Model Confidence")
959
+ report.append(f" - **Score**: {conf['score']}/100")
960
+ report.append(f" - **Agreement**: {conf['agreement']}")
961
+ if 'direction_aligned' in conf:
962
+ report.append(f" - **Direction Aligned**: {'Yes ✓' if conf['direction_aligned'] else 'No ✗'}")
963
+
964
+ report.append(f"\n⚠️ *Deep learning forecast. Not financial advice.*")
965
+
966
+ return '\n'.join(report)
967
+
968
+ except Exception as e:
969
+ return f"Error running ensemble forecast: {str(e)}"
970
+
971
+
972
+ # 도구 목록 (에이전트에서 사용)
973
+ TIMESERIES_TOOLS = [
974
+ get_macro_indicators,
975
+ get_sector_performance,
976
+ get_correlated_assets,
977
+ analyze_stock_for_forecasting,
978
+ run_simple_forecast,
979
+ collect_forecast_features,
980
+ run_ensemble_forecast, # 새로운 딥러닝 앙상블 도구
981
+ ]
tradingagents/dataflows/__init__.py ADDED
File without changes
tradingagents/dataflows/alpha_vantage.py ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ # Import functions from specialized modules
2
+ from .alpha_vantage_stock import get_stock
3
+ from .alpha_vantage_indicator import get_indicator
4
+ from .alpha_vantage_fundamentals import get_fundamentals, get_balance_sheet, get_cashflow, get_income_statement
5
+ from .alpha_vantage_news import get_news, get_insider_transactions
tradingagents/dataflows/alpha_vantage_common.py ADDED
@@ -0,0 +1,122 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import requests
3
+ import pandas as pd
4
+ import json
5
+ from datetime import datetime
6
+ from io import StringIO
7
+
8
+ API_BASE_URL = "https://www.alphavantage.co/query"
9
+
10
+ def get_api_key() -> str:
11
+ """Retrieve the API key for Alpha Vantage from environment variables."""
12
+ api_key = os.getenv("ALPHA_VANTAGE_API_KEY")
13
+ if not api_key:
14
+ raise ValueError("ALPHA_VANTAGE_API_KEY environment variable is not set.")
15
+ return api_key
16
+
17
+ def format_datetime_for_api(date_input) -> str:
18
+ """Convert various date formats to YYYYMMDDTHHMM format required by Alpha Vantage API."""
19
+ if isinstance(date_input, str):
20
+ # If already in correct format, return as-is
21
+ if len(date_input) == 13 and 'T' in date_input:
22
+ return date_input
23
+ # Try to parse common date formats
24
+ try:
25
+ dt = datetime.strptime(date_input, "%Y-%m-%d")
26
+ return dt.strftime("%Y%m%dT0000")
27
+ except ValueError:
28
+ try:
29
+ dt = datetime.strptime(date_input, "%Y-%m-%d %H:%M")
30
+ return dt.strftime("%Y%m%dT%H%M")
31
+ except ValueError:
32
+ raise ValueError(f"Unsupported date format: {date_input}")
33
+ elif isinstance(date_input, datetime):
34
+ return date_input.strftime("%Y%m%dT%H%M")
35
+ else:
36
+ raise ValueError(f"Date must be string or datetime object, got {type(date_input)}")
37
+
38
+ class AlphaVantageRateLimitError(Exception):
39
+ """Exception raised when Alpha Vantage API rate limit is exceeded."""
40
+ pass
41
+
42
+ def _make_api_request(function_name: str, params: dict) -> dict | str:
43
+ """Helper function to make API requests and handle responses.
44
+
45
+ Raises:
46
+ AlphaVantageRateLimitError: When API rate limit is exceeded
47
+ """
48
+ # Create a copy of params to avoid modifying the original
49
+ api_params = params.copy()
50
+ api_params.update({
51
+ "function": function_name,
52
+ "apikey": get_api_key(),
53
+ "source": "trading_agents",
54
+ })
55
+
56
+ # Handle entitlement parameter if present in params or global variable
57
+ current_entitlement = globals().get('_current_entitlement')
58
+ entitlement = api_params.get("entitlement") or current_entitlement
59
+
60
+ if entitlement:
61
+ api_params["entitlement"] = entitlement
62
+ elif "entitlement" in api_params:
63
+ # Remove entitlement if it's None or empty
64
+ api_params.pop("entitlement", None)
65
+
66
+ response = requests.get(API_BASE_URL, params=api_params)
67
+ response.raise_for_status()
68
+
69
+ response_text = response.text
70
+
71
+ # Check if response is JSON (error responses are typically JSON)
72
+ try:
73
+ response_json = json.loads(response_text)
74
+ # Check for rate limit error
75
+ if "Information" in response_json:
76
+ info_message = response_json["Information"]
77
+ if "rate limit" in info_message.lower() or "api key" in info_message.lower():
78
+ raise AlphaVantageRateLimitError(f"Alpha Vantage rate limit exceeded: {info_message}")
79
+ except json.JSONDecodeError:
80
+ # Response is not JSON (likely CSV data), which is normal
81
+ pass
82
+
83
+ return response_text
84
+
85
+
86
+
87
+ def _filter_csv_by_date_range(csv_data: str, start_date: str, end_date: str) -> str:
88
+ """
89
+ Filter CSV data to include only rows within the specified date range.
90
+
91
+ Args:
92
+ csv_data: CSV string from Alpha Vantage API
93
+ start_date: Start date in yyyy-mm-dd format
94
+ end_date: End date in yyyy-mm-dd format
95
+
96
+ Returns:
97
+ Filtered CSV string
98
+ """
99
+ if not csv_data or csv_data.strip() == "":
100
+ return csv_data
101
+
102
+ try:
103
+ # Parse CSV data
104
+ df = pd.read_csv(StringIO(csv_data))
105
+
106
+ # Assume the first column is the date column (timestamp)
107
+ date_col = df.columns[0]
108
+ df[date_col] = pd.to_datetime(df[date_col])
109
+
110
+ # Filter by date range
111
+ start_dt = pd.to_datetime(start_date)
112
+ end_dt = pd.to_datetime(end_date)
113
+
114
+ filtered_df = df[(df[date_col] >= start_dt) & (df[date_col] <= end_dt)]
115
+
116
+ # Convert back to CSV string
117
+ return filtered_df.to_csv(index=False)
118
+
119
+ except Exception as e:
120
+ # If filtering fails, return original data with a warning
121
+ print(f"Warning: Failed to filter CSV data by date range: {e}")
122
+ return csv_data
tradingagents/dataflows/alpha_vantage_fundamentals.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from .alpha_vantage_common import _make_api_request
2
+
3
+
4
+ def get_fundamentals(ticker: str, curr_date: str = None) -> str:
5
+ """
6
+ Retrieve comprehensive fundamental data for a given ticker symbol using Alpha Vantage.
7
+
8
+ Args:
9
+ ticker (str): Ticker symbol of the company
10
+ curr_date (str): Current date you are trading at, yyyy-mm-dd (not used for Alpha Vantage)
11
+
12
+ Returns:
13
+ str: Company overview data including financial ratios and key metrics
14
+ """
15
+ params = {
16
+ "symbol": ticker,
17
+ }
18
+
19
+ return _make_api_request("OVERVIEW", params)
20
+
21
+
22
+ def get_balance_sheet(ticker: str, freq: str = "quarterly", curr_date: str = None) -> str:
23
+ """
24
+ Retrieve balance sheet data for a given ticker symbol using Alpha Vantage.
25
+
26
+ Args:
27
+ ticker (str): Ticker symbol of the company
28
+ freq (str): Reporting frequency: annual/quarterly (default quarterly) - not used for Alpha Vantage
29
+ curr_date (str): Current date you are trading at, yyyy-mm-dd (not used for Alpha Vantage)
30
+
31
+ Returns:
32
+ str: Balance sheet data with normalized fields
33
+ """
34
+ params = {
35
+ "symbol": ticker,
36
+ }
37
+
38
+ return _make_api_request("BALANCE_SHEET", params)
39
+
40
+
41
+ def get_cashflow(ticker: str, freq: str = "quarterly", curr_date: str = None) -> str:
42
+ """
43
+ Retrieve cash flow statement data for a given ticker symbol using Alpha Vantage.
44
+
45
+ Args:
46
+ ticker (str): Ticker symbol of the company
47
+ freq (str): Reporting frequency: annual/quarterly (default quarterly) - not used for Alpha Vantage
48
+ curr_date (str): Current date you are trading at, yyyy-mm-dd (not used for Alpha Vantage)
49
+
50
+ Returns:
51
+ str: Cash flow statement data with normalized fields
52
+ """
53
+ params = {
54
+ "symbol": ticker,
55
+ }
56
+
57
+ return _make_api_request("CASH_FLOW", params)
58
+
59
+
60
+ def get_income_statement(ticker: str, freq: str = "quarterly", curr_date: str = None) -> str:
61
+ """
62
+ Retrieve income statement data for a given ticker symbol using Alpha Vantage.
63
+
64
+ Args:
65
+ ticker (str): Ticker symbol of the company
66
+ freq (str): Reporting frequency: annual/quarterly (default quarterly) - not used for Alpha Vantage
67
+ curr_date (str): Current date you are trading at, yyyy-mm-dd (not used for Alpha Vantage)
68
+
69
+ Returns:
70
+ str: Income statement data with normalized fields
71
+ """
72
+ params = {
73
+ "symbol": ticker,
74
+ }
75
+
76
+ return _make_api_request("INCOME_STATEMENT", params)
77
+
tradingagents/dataflows/alpha_vantage_indicator.py ADDED
@@ -0,0 +1,222 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from .alpha_vantage_common import _make_api_request
2
+
3
+ def get_indicator(
4
+ symbol: str,
5
+ indicator: str,
6
+ curr_date: str,
7
+ look_back_days: int,
8
+ interval: str = "daily",
9
+ time_period: int = 14,
10
+ series_type: str = "close"
11
+ ) -> str:
12
+ """
13
+ Returns Alpha Vantage technical indicator values over a time window.
14
+
15
+ Args:
16
+ symbol: ticker symbol of the company
17
+ indicator: technical indicator to get the analysis and report of
18
+ curr_date: The current trading date you are trading on, YYYY-mm-dd
19
+ look_back_days: how many days to look back
20
+ interval: Time interval (daily, weekly, monthly)
21
+ time_period: Number of data points for calculation
22
+ series_type: The desired price type (close, open, high, low)
23
+
24
+ Returns:
25
+ String containing indicator values and description
26
+ """
27
+ from datetime import datetime
28
+ from dateutil.relativedelta import relativedelta
29
+
30
+ supported_indicators = {
31
+ "close_50_sma": ("50 SMA", "close"),
32
+ "close_200_sma": ("200 SMA", "close"),
33
+ "close_10_ema": ("10 EMA", "close"),
34
+ "macd": ("MACD", "close"),
35
+ "macds": ("MACD Signal", "close"),
36
+ "macdh": ("MACD Histogram", "close"),
37
+ "rsi": ("RSI", "close"),
38
+ "boll": ("Bollinger Middle", "close"),
39
+ "boll_ub": ("Bollinger Upper Band", "close"),
40
+ "boll_lb": ("Bollinger Lower Band", "close"),
41
+ "atr": ("ATR", None),
42
+ "vwma": ("VWMA", "close")
43
+ }
44
+
45
+ indicator_descriptions = {
46
+ "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.",
47
+ "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.",
48
+ "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.",
49
+ "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.",
50
+ "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.",
51
+ "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.",
52
+ "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.",
53
+ "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.",
54
+ "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.",
55
+ "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.",
56
+ "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.",
57
+ "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."
58
+ }
59
+
60
+ if indicator not in supported_indicators:
61
+ raise ValueError(
62
+ f"Indicator {indicator} is not supported. Please choose from: {list(supported_indicators.keys())}"
63
+ )
64
+
65
+ curr_date_dt = datetime.strptime(curr_date, "%Y-%m-%d")
66
+ before = curr_date_dt - relativedelta(days=look_back_days)
67
+
68
+ # Get the full data for the period instead of making individual calls
69
+ _, required_series_type = supported_indicators[indicator]
70
+
71
+ # Use the provided series_type or fall back to the required one
72
+ if required_series_type:
73
+ series_type = required_series_type
74
+
75
+ try:
76
+ # Get indicator data for the period
77
+ if indicator == "close_50_sma":
78
+ data = _make_api_request("SMA", {
79
+ "symbol": symbol,
80
+ "interval": interval,
81
+ "time_period": "50",
82
+ "series_type": series_type,
83
+ "datatype": "csv"
84
+ })
85
+ elif indicator == "close_200_sma":
86
+ data = _make_api_request("SMA", {
87
+ "symbol": symbol,
88
+ "interval": interval,
89
+ "time_period": "200",
90
+ "series_type": series_type,
91
+ "datatype": "csv"
92
+ })
93
+ elif indicator == "close_10_ema":
94
+ data = _make_api_request("EMA", {
95
+ "symbol": symbol,
96
+ "interval": interval,
97
+ "time_period": "10",
98
+ "series_type": series_type,
99
+ "datatype": "csv"
100
+ })
101
+ elif indicator == "macd":
102
+ data = _make_api_request("MACD", {
103
+ "symbol": symbol,
104
+ "interval": interval,
105
+ "series_type": series_type,
106
+ "datatype": "csv"
107
+ })
108
+ elif indicator == "macds":
109
+ data = _make_api_request("MACD", {
110
+ "symbol": symbol,
111
+ "interval": interval,
112
+ "series_type": series_type,
113
+ "datatype": "csv"
114
+ })
115
+ elif indicator == "macdh":
116
+ data = _make_api_request("MACD", {
117
+ "symbol": symbol,
118
+ "interval": interval,
119
+ "series_type": series_type,
120
+ "datatype": "csv"
121
+ })
122
+ elif indicator == "rsi":
123
+ data = _make_api_request("RSI", {
124
+ "symbol": symbol,
125
+ "interval": interval,
126
+ "time_period": str(time_period),
127
+ "series_type": series_type,
128
+ "datatype": "csv"
129
+ })
130
+ elif indicator in ["boll", "boll_ub", "boll_lb"]:
131
+ data = _make_api_request("BBANDS", {
132
+ "symbol": symbol,
133
+ "interval": interval,
134
+ "time_period": "20",
135
+ "series_type": series_type,
136
+ "datatype": "csv"
137
+ })
138
+ elif indicator == "atr":
139
+ data = _make_api_request("ATR", {
140
+ "symbol": symbol,
141
+ "interval": interval,
142
+ "time_period": str(time_period),
143
+ "datatype": "csv"
144
+ })
145
+ elif indicator == "vwma":
146
+ # Alpha Vantage doesn't have direct VWMA, so we'll return an informative message
147
+ # In a real implementation, this would need to be calculated from OHLCV data
148
+ 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.')}"
149
+ else:
150
+ return f"Error: Indicator {indicator} not implemented yet."
151
+
152
+ # Parse CSV data and extract values for the date range
153
+ lines = data.strip().split('\n')
154
+ if len(lines) < 2:
155
+ return f"Error: No data returned for {indicator}"
156
+
157
+ # Parse header and data
158
+ header = [col.strip() for col in lines[0].split(',')]
159
+ try:
160
+ date_col_idx = header.index('time')
161
+ except ValueError:
162
+ return f"Error: 'time' column not found in data for {indicator}. Available columns: {header}"
163
+
164
+ # Map internal indicator names to expected CSV column names from Alpha Vantage
165
+ col_name_map = {
166
+ "macd": "MACD", "macds": "MACD_Signal", "macdh": "MACD_Hist",
167
+ "boll": "Real Middle Band", "boll_ub": "Real Upper Band", "boll_lb": "Real Lower Band",
168
+ "rsi": "RSI", "atr": "ATR", "close_10_ema": "EMA",
169
+ "close_50_sma": "SMA", "close_200_sma": "SMA"
170
+ }
171
+
172
+ target_col_name = col_name_map.get(indicator)
173
+
174
+ if not target_col_name:
175
+ # Default to the second column if no specific mapping exists
176
+ value_col_idx = 1
177
+ else:
178
+ try:
179
+ value_col_idx = header.index(target_col_name)
180
+ except ValueError:
181
+ return f"Error: Column '{target_col_name}' not found for indicator '{indicator}'. Available columns: {header}"
182
+
183
+ result_data = []
184
+ for line in lines[1:]:
185
+ if not line.strip():
186
+ continue
187
+ values = line.split(',')
188
+ if len(values) > value_col_idx:
189
+ try:
190
+ date_str = values[date_col_idx].strip()
191
+ # Parse the date
192
+ date_dt = datetime.strptime(date_str, "%Y-%m-%d")
193
+
194
+ # Check if date is in our range
195
+ if before <= date_dt <= curr_date_dt:
196
+ value = values[value_col_idx].strip()
197
+ result_data.append((date_dt, value))
198
+ except (ValueError, IndexError):
199
+ continue
200
+
201
+ # Sort by date and format output
202
+ result_data.sort(key=lambda x: x[0])
203
+
204
+ ind_string = ""
205
+ for date_dt, value in result_data:
206
+ ind_string += f"{date_dt.strftime('%Y-%m-%d')}: {value}\n"
207
+
208
+ if not ind_string:
209
+ ind_string = "No data available for the specified date range.\n"
210
+
211
+ result_str = (
212
+ f"## {indicator.upper()} values from {before.strftime('%Y-%m-%d')} to {curr_date}:\n\n"
213
+ + ind_string
214
+ + "\n\n"
215
+ + indicator_descriptions.get(indicator, "No description available.")
216
+ )
217
+
218
+ return result_str
219
+
220
+ except Exception as e:
221
+ print(f"Error getting Alpha Vantage indicator data for {indicator}: {e}")
222
+ return f"Error retrieving {indicator} data: {str(e)}"
tradingagents/dataflows/alpha_vantage_news.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from .alpha_vantage_common import _make_api_request, format_datetime_for_api
2
+
3
+ def get_news(ticker, start_date, end_date) -> dict[str, str] | str:
4
+ """Returns live and historical market news & sentiment data from premier news outlets worldwide.
5
+
6
+ Covers stocks, cryptocurrencies, forex, and topics like fiscal policy, mergers & acquisitions, IPOs.
7
+
8
+ Args:
9
+ ticker: Stock symbol for news articles.
10
+ start_date: Start date for news search.
11
+ end_date: End date for news search.
12
+
13
+ Returns:
14
+ Dictionary containing news sentiment data or JSON string.
15
+ """
16
+
17
+ params = {
18
+ "tickers": ticker,
19
+ "time_from": format_datetime_for_api(start_date),
20
+ "time_to": format_datetime_for_api(end_date),
21
+ "sort": "LATEST",
22
+ "limit": "50",
23
+ }
24
+
25
+ return _make_api_request("NEWS_SENTIMENT", params)
26
+
27
+ def get_insider_transactions(symbol: str) -> dict[str, str] | str:
28
+ """Returns latest and historical insider transactions by key stakeholders.
29
+
30
+ Covers transactions by founders, executives, board members, etc.
31
+
32
+ Args:
33
+ symbol: Ticker symbol. Example: "IBM".
34
+
35
+ Returns:
36
+ Dictionary containing insider transaction data or JSON string.
37
+ """
38
+
39
+ params = {
40
+ "symbol": symbol,
41
+ }
42
+
43
+ return _make_api_request("INSIDER_TRANSACTIONS", params)
tradingagents/dataflows/alpha_vantage_stock.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from datetime import datetime
2
+ from .alpha_vantage_common import _make_api_request, _filter_csv_by_date_range
3
+
4
+ def get_stock(
5
+ symbol: str,
6
+ start_date: str,
7
+ end_date: str
8
+ ) -> str:
9
+ """
10
+ Returns raw daily OHLCV values, adjusted close values, and historical split/dividend events
11
+ filtered to the specified date range.
12
+
13
+ Args:
14
+ symbol: The name of the equity. For example: symbol=IBM
15
+ start_date: Start date in yyyy-mm-dd format
16
+ end_date: End date in yyyy-mm-dd format
17
+
18
+ Returns:
19
+ CSV string containing the daily adjusted time series data filtered to the date range.
20
+ """
21
+ # Parse dates to determine the range
22
+ start_dt = datetime.strptime(start_date, "%Y-%m-%d")
23
+ today = datetime.now()
24
+
25
+ # Choose outputsize based on whether the requested range is within the latest 100 days
26
+ # Compact returns latest 100 data points, so check if start_date is recent enough
27
+ days_from_today_to_start = (today - start_dt).days
28
+ outputsize = "compact" if days_from_today_to_start < 100 else "full"
29
+
30
+ params = {
31
+ "symbol": symbol,
32
+ "outputsize": outputsize,
33
+ "datatype": "csv",
34
+ }
35
+
36
+ response = _make_api_request("TIME_SERIES_DAILY_ADJUSTED", params)
37
+
38
+ return _filter_csv_by_date_range(response, start_date, end_date)