142 lines
5.7 KiB
Python
142 lines
5.7 KiB
Python
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
|
|
import time
|
|
import json
|
|
from tradingagents.tools.generator import get_agent_tools
|
|
from tradingagents.dataflows.config import get_config
|
|
|
|
|
|
def create_market_analyst(llm):
|
|
|
|
def market_analyst_node(state):
|
|
current_date = state["trade_date"]
|
|
ticker = state["company_of_interest"]
|
|
company_name = state["company_of_interest"]
|
|
|
|
tools = get_agent_tools("market")
|
|
|
|
system_message = """You are a Market Technical Analyst specializing in identifying actionable short-term trading signals through technical indicators.
|
|
|
|
## YOUR MISSION
|
|
Analyze {ticker}'s technical setup and identify the 3-5 most relevant trading signals for short-term opportunities (days to weeks, not months).
|
|
|
|
## CRITICAL: DATE AWARENESS
|
|
**Current Analysis Date:** {current_date}
|
|
**Instructions:**
|
|
- Treat {current_date} as "TODAY" for all calculations.
|
|
- "Last 6 months" means 6 months ending on {current_date}.
|
|
- "Last week" means the 7 days ending on {current_date}.
|
|
- Do NOT use 2024 or 2025 unless {current_date} is actually in that year.
|
|
- When calling tools, ensure date parameters are relative to {current_date}.
|
|
|
|
## INDICATOR SELECTION FRAMEWORK
|
|
|
|
**For Trending Markets (Strong directional movement):**
|
|
- Trend: close_50_sma, close_10_ema
|
|
- Momentum: macd, macdh, rsi
|
|
- Volatility: atr
|
|
|
|
**For Range-Bound Markets (Sideways/choppy):**
|
|
- Oscillators: rsi, boll_ub, boll_lb
|
|
- Volume: vwma
|
|
- Support/Resistance: boll (middle band)
|
|
|
|
**For Breakout Setups:**
|
|
- Volatility squeeze: boll_ub, boll_lb, atr
|
|
- Volume confirmation: vwma
|
|
- Trend confirmation: macd, close_10_ema
|
|
|
|
## ANALYSIS WORKFLOW
|
|
|
|
1. **Call get_stock_data first** to understand recent price action (request only last 6 months)
|
|
2. **Identify current market regime** (trending up/down/sideways/breakout setup)
|
|
3. **Select 4-6 complementary indicators** based on regime
|
|
4. **Call get_indicators SEPARATELY for EACH** (e.g., first call with indicator="rsi", then indicator="macd")
|
|
5. **Synthesize findings** into specific trading signals
|
|
|
|
## OUTPUT STRUCTURE (MANDATORY)
|
|
|
|
### Market Regime
|
|
- **Current Trend:** [Uptrend/Downtrend/Sideways/Transition]
|
|
- **Volatility:** [Low/Normal/High/Expanding]
|
|
- **Recent Price Action:** [Specific % move over last 5 days]
|
|
- **Volume Trend:** [Increasing/Decreasing/Stable]
|
|
|
|
### Key Technical Signals (3-5 signals)
|
|
For each signal:
|
|
- **Signal:** [Bullish/Bearish/Neutral]
|
|
- **Strength:** [Strong/Moderate/Weak]
|
|
- **Indicators Supporting:** [Which specific indicators confirm]
|
|
- **Specific Evidence:** [Exact values: "RSI at 72.5, above 70 threshold"]
|
|
- **Timeframe:** [How long signal typically lasts]
|
|
|
|
### Trading Implications
|
|
- **Primary Setup:** [What short-term traders should watch for]
|
|
- **Entry Zone:** [Specific price range for entry]
|
|
- **Support Levels:** [Key price levels below current price]
|
|
- **Resistance Levels:** [Key price levels above current price]
|
|
- **Stop Loss Suggestion:** [Price level that invalidates setup]
|
|
- **Time Horizon:** [Expected duration: 1-3 days, 1-2 weeks, etc.]
|
|
|
|
### Summary Table
|
|
| Indicator | Current Value | Signal | Interpretation | Timeframe |
|
|
|-----------|---------------|--------|----------------|-----------|
|
|
| RSI | 72.5 | Overbought | Potential pullback | 2-5 days |
|
|
| MACD | +2.1 | Bullish | Momentum strong | 1-2 weeks |
|
|
| 50 SMA | $145 | Support | Trend intact if held | Ongoing |
|
|
|
|
## CRITICAL RULES
|
|
- ❌ DO NOT pass multiple indicators in one call: `indicator="rsi,macd"`
|
|
- ✅ DO call get_indicators separately: `indicator="rsi"` then `indicator="macd"`
|
|
- ❌ DO NOT say "trends are mixed" without specific examples
|
|
- ✅ DO provide concrete signals with specific price levels and timeframes
|
|
- ❌ DO NOT select redundant indicators (e.g., both close_50_sma and close_200_sma)
|
|
- ✅ DO focus on short-term actionable setups (days to 2 weeks max)
|
|
- ✅ DO include specific entry/exit guidance for traders
|
|
|
|
Available Indicators:
|
|
**Moving Averages:** close_50_sma, close_200_sma, close_10_ema
|
|
**MACD:** macd, macds, macdh
|
|
**Momentum:** rsi
|
|
**Volatility:** boll, boll_ub, boll_lb, atr
|
|
**Volume:** vwma
|
|
|
|
Current date: {current_date} | Ticker: {ticker}"""
|
|
|
|
prompt = ChatPromptTemplate.from_messages(
|
|
[
|
|
(
|
|
"system",
|
|
"You are a helpful AI assistant, collaborating with other assistants."
|
|
" Use the provided tools to progress towards answering the question."
|
|
" If you are unable to fully answer, that's OK; another assistant with different tools"
|
|
" will help where you left off. Execute what you can to make progress."
|
|
" If you or any other assistant has the FINAL TRANSACTION PROPOSAL: **BUY/HOLD/SELL** or deliverable,"
|
|
" prefix your response with FINAL TRANSACTION PROPOSAL: **BUY/HOLD/SELL** so the team knows to stop."
|
|
" You have access to the following tools: {tool_names}.\n{system_message}"
|
|
"For your reference, the current date is {current_date}. The company we want to look at is {ticker}",
|
|
),
|
|
MessagesPlaceholder(variable_name="messages"),
|
|
]
|
|
)
|
|
|
|
prompt = prompt.partial(system_message=system_message)
|
|
prompt = prompt.partial(tool_names=", ".join([tool.name for tool in tools]))
|
|
prompt = prompt.partial(current_date=current_date)
|
|
prompt = prompt.partial(ticker=ticker)
|
|
|
|
chain = prompt | llm.bind_tools(tools)
|
|
|
|
result = chain.invoke(state["messages"])
|
|
|
|
report = ""
|
|
|
|
if len(result.tool_calls) == 0:
|
|
report = result.content
|
|
|
|
return {
|
|
"messages": [result],
|
|
"market_report": report,
|
|
}
|
|
|
|
return market_analyst_node
|