135 lines
4.8 KiB
Python
135 lines
4.8 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
|
|
from tradingagents.agents.utils.prompt_templates import (
|
|
BASE_COLLABORATIVE_BOILERPLATE,
|
|
get_date_awareness_section,
|
|
)
|
|
|
|
|
|
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 = f"""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).
|
|
|
|
{get_date_awareness_section(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 ONCE** to get a comprehensive technical report (includes RSI, MACD, Moving Averages, Bollinger Bands, ATR, etc.)
|
|
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: TOOL USAGE
|
|
- ✅ DO call `get_indicators(symbol=ticker, curr_date=current_date)` ONCE
|
|
→ This returns ALL indicators (RSI, MACD, Bollinger Bands, ATR, etc.) in one call
|
|
- ❌ DO NOT try to pass `indicator="rsi"` parameter - the tool doesn't support that
|
|
- ❌ DO NOT call get_indicators multiple times - one call gives you everything
|
|
|
|
## CRITICAL RULES
|
|
- ❌ 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}"""
|
|
|
|
tool_names_str = ", ".join([tool.name for tool in tools])
|
|
full_system_message = (
|
|
f"{BASE_COLLABORATIVE_BOILERPLATE}\n\n{system_message}\n\n"
|
|
f"Context: {ticker} | Date: {current_date} | Tools: {tool_names_str}"
|
|
)
|
|
|
|
prompt = ChatPromptTemplate.from_messages(
|
|
[
|
|
("system", full_system_message),
|
|
MessagesPlaceholder(variable_name="messages"),
|
|
]
|
|
)
|
|
|
|
|
|
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
|