From 692a55dfd565f05ef6666e683c2320bcf48817e6 Mon Sep 17 00:00:00 2001 From: hyejwon Date: Thu, 12 Mar 2026 15:55:51 +0900 Subject: [PATCH] update krx function --- .env.example | 6 ---- .../agents/analysts/fundamentals_analyst.py | 33 +++++++++++++++---- .../agents/analysts/market_analyst.py | 20 ++++++++--- tradingagents/agents/analysts/news_analyst.py | 26 ++++++++++++--- tradingagents/dataflows/screening_data.py | 28 ++++++++++++---- 5 files changed, 84 insertions(+), 29 deletions(-) delete mode 100644 .env.example diff --git a/.env.example b/.env.example deleted file mode 100644 index 1328b838..00000000 --- a/.env.example +++ /dev/null @@ -1,6 +0,0 @@ -# LLM Providers (set the one you use) -OPENAI_API_KEY= -GOOGLE_API_KEY= -ANTHROPIC_API_KEY= -XAI_API_KEY= -OPENROUTER_API_KEY= diff --git a/tradingagents/agents/analysts/fundamentals_analyst.py b/tradingagents/agents/analysts/fundamentals_analyst.py index a2d620b0..80385f5d 100644 --- a/tradingagents/agents/analysts/fundamentals_analyst.py +++ b/tradingagents/agents/analysts/fundamentals_analyst.py @@ -1,7 +1,16 @@ from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder import time import json -from tradingagents.agents.utils.agent_utils import get_fundamentals, get_balance_sheet, get_cashflow, get_income_statement, get_insider_transactions +from tradingagents.agents.utils.agent_utils import ( + get_fundamentals, + get_balance_sheet, + get_cashflow, + get_income_statement, + get_insider_transactions, + get_krx_fundamentals, + get_dart_financials, + get_dart_shareholders, +) from tradingagents.agents.utils.korean_prompt import ( KOREAN_INVESTOR_GUIDE, KOREAN_REPORT_FORMAT_GUIDE, @@ -17,12 +26,22 @@ def create_fundamentals_analyst(llm): ticker = state["company_of_interest"] company_name = state["company_of_interest"] - tools = [ - get_fundamentals, - get_balance_sheet, - get_cashflow, - get_income_statement, - ] + config = get_config() + market = config.get("market", "US") if config else "US" + + if market == "KRX": + tools = [ + get_krx_fundamentals, + get_dart_financials, + get_dart_shareholders, + ] + else: + tools = [ + get_fundamentals, + get_balance_sheet, + get_cashflow, + get_income_statement, + ] system_message = ( "You are a researcher tasked with analyzing fundamental information over the past week about a company. Please write a comprehensive report of the company's fundamental information such as financial documents, company profile, basic company financials, and company financial history to gain a full view of the company's fundamental information to inform traders. Make sure to include as much detail as possible. Do not simply state the trends are mixed, provide detailed and finegrained analysis and insights that may help traders make decisions." diff --git a/tradingagents/agents/analysts/market_analyst.py b/tradingagents/agents/analysts/market_analyst.py index 9a779108..78112634 100644 --- a/tradingagents/agents/analysts/market_analyst.py +++ b/tradingagents/agents/analysts/market_analyst.py @@ -26,10 +26,22 @@ def create_market_analyst(llm): ticker = state["company_of_interest"] company_name = state["company_of_interest"] - tools = [ - get_stock_data, - get_indicators, - ] + config = get_config() + market = config.get("market", "US") if config else "US" + + if market == "KRX": + tools = [ + get_krx_stock_data, + get_krx_indicators, + get_exchange_rate, + get_korea_index, + get_investor_trading, + ] + else: + tools = [ + get_stock_data, + get_indicators, + ] system_message = ( """You are a trading assistant tasked with analyzing financial markets. Your role is to select the **most relevant indicators** for a given market condition or trading strategy from the following list. The goal is to choose up to **8 indicators** that provide complementary insights without redundancy. Categories and each category's indicators are: diff --git a/tradingagents/agents/analysts/news_analyst.py b/tradingagents/agents/analysts/news_analyst.py index ee0bcc1e..4006fb7e 100644 --- a/tradingagents/agents/analysts/news_analyst.py +++ b/tradingagents/agents/analysts/news_analyst.py @@ -1,7 +1,13 @@ from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder import time import json -from tradingagents.agents.utils.agent_utils import get_news, get_global_news +from tradingagents.agents.utils.agent_utils import ( + get_news, + get_global_news, + get_korean_news, + get_korean_global_news, + get_dart_disclosures, +) from tradingagents.agents.utils.korean_prompt import ( KOREAN_INVESTOR_GUIDE, KOREAN_REPORT_FORMAT_GUIDE, @@ -16,10 +22,20 @@ def create_news_analyst(llm): current_date = state["trade_date"] ticker = state["company_of_interest"] - tools = [ - get_news, - get_global_news, - ] + config = get_config() + market = config.get("market", "US") if config else "US" + + if market == "KRX": + tools = [ + get_korean_news, + get_korean_global_news, + get_dart_disclosures, + ] + else: + tools = [ + get_news, + get_global_news, + ] system_message = ( "You are a news researcher tasked with analyzing recent news and trends over the past week. Please write a comprehensive report of the current state of the world that is relevant for trading and macroeconomics. Use the available tools: get_news(query, start_date, end_date) for company-specific or targeted news searches, and get_global_news(curr_date, look_back_days, limit) for broader macroeconomic news. Do not simply state the trends are mixed, provide detailed and finegrained analysis and insights that may help traders make decisions." diff --git a/tradingagents/dataflows/screening_data.py b/tradingagents/dataflows/screening_data.py index 8df1013d..d3b1159b 100644 --- a/tradingagents/dataflows/screening_data.py +++ b/tradingagents/dataflows/screening_data.py @@ -189,14 +189,25 @@ def get_bulk_ohlcv( if market == "KRX": try: - import FinanceDataReader as fdr + from pykrx import stock as krx_stock except ImportError: - raise ImportError("FinanceDataReader required for KRX data") + raise ImportError("pykrx required for KRX data: pip install pykrx") + + # pykrx uses YYYYMMDD format + start_fmt = start_date.replace("-", "") + end_fmt = end_date.replace("-", "") for ticker in tickers: try: - data = fdr.DataReader(ticker, start_date, end_date) + ticker_padded = ticker.zfill(6) + data = krx_stock.get_market_ohlcv(start_fmt, end_fmt, ticker_padded) if data is not None and not data.empty: + # Normalize column names to match expected format + col_map = { + "시가": "Open", "고가": "High", "저가": "Low", + "종가": "Close", "거래량": "Volume", + } + data = data.rename(columns=col_map) result[ticker] = data except Exception as e: logger.warning(f"Failed to get OHLCV for {ticker}: {e}") @@ -263,9 +274,12 @@ def compute_screening_indicators(df: pd.DataFrame) -> dict: # Volume analysis if volume is not None and len(volume) >= 20: indicators["volume_current"] = volume.iloc[-1] - indicators["volume_avg_20"] = volume.rolling(20).mean().iloc[-1] - vol_ratio = volume.iloc[-1] / volume.rolling(20).mean().iloc[-1] - indicators["volume_ratio"] = vol_ratio + vol_avg_20 = volume.rolling(20).mean().iloc[-1] + indicators["volume_avg_20"] = vol_avg_20 + if vol_avg_20 and vol_avg_20 > 0: + indicators["volume_ratio"] = volume.iloc[-1] / vol_avg_20 + else: + indicators["volume_ratio"] = 0.0 # Bollinger Bands if len(close) >= 20: @@ -370,7 +384,7 @@ def _get_naver_krx_universe( if not code_match: continue - code = code_match.group(1) + code = code_match.group(1).zfill(6) name = link.text.strip() # Parse numeric values (remove commas)