TradingAgents/tradingagents/agents/analysts/market_analyst.py

138 lines
11 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
import time
import json
def create_market_analyst(llm, toolkit):
def market_analyst_node(state):
current_date = state["trade_date"]
ticker = state["company_of_interest"]
company_name = state["company_of_interest"]
if toolkit.config["online_tools"]:
tools = [
toolkit.get_YFin_data_online,
toolkit.get_stockstats_indicators_report_online,
]
else:
tools = [
toolkit.get_YFin_data,
toolkit.get_stockstats_indicators_report,
]
# 根据配置选择语言
if toolkit.config.get("output_language", "english") == "chinese":
system_message = (
"""你是一个负责分析金融市场的交易助手。你的任务是从以下列表中为给定的市场条件或交易策略选择**最相关的指标**。目标是选择最多**8个指标**,这些指标能够提供互补的洞察而不重复。分类和每个分类的指标如下:
移动平均线:
- close_50_sma50日简单移动平均线中期趋势指标。用途识别趋势方向并作为动态支撑/阻力。提示:它滞后于价格;结合更快的指标获得及时信号。
- close_200_sma200日简单移动平均线长期趋势基准。用途确认整体市场趋势并识别金叉/死叉设置。提示:反应缓慢;最适合战略趋势确认而非频繁交易入场。
- close_10_ema10日指数移动平均线响应性强的短期平均线。用途捕捉动量的快速变化和潜在入场点。提示在震荡市场中容易产生噪音与更长的平均线一起使用以过滤虚假信号。
MACD相关
- macdMACD通过EMA差异计算动量。用途寻找交叉和背离作为趋势变化的信号。提示在低波动性或横盘市场中与其他指标确认。
- macdsMACD信号线MACD线的EMA平滑。用途使用与MACD线的交叉来触发交易。提示应该是更广泛策略的一部分以避免虚假信号。
- macdhMACD柱状图显示MACD线与其信号线之间的差距。用途可视化动量强度并早期发现背离。提示可能波动较大在快速移动的市场中与额外过滤器结合使用。
动量指标:
- rsiRSI测量动量以标记超买/超卖条件。用途应用70/30阈值并观察背离以信号反转。提示在强趋势中RSI可能保持极端始终与趋势分析交叉检查。
波动性指标:
- boll布林中轨作为布林带基础的20日SMA。用途作为价格运动的动态基准。提示与上下轨结合使用以有效发现突破或反转。
- boll_ub布林上轨通常是中轨上方2个标准差。用途信号潜在超买条件和突破区域。提示用其他工具确认信号在强趋势中价格可能沿着带运行。
- boll_lb布林下轨通常是中轨下方2个标准差。用途指示潜在超卖条件。提示使用额外分析避免虚假反转信号。
- atrATR平均真实范围以测量波动性。用途基于当前市场波动性设置止损水平和调整仓位大小。提示这是一个反应性指标所以将其作为更广泛风险管理策略的一部分使用。
成交量指标:
- vwmaVWMA按成交量加权的移动平均线。用途通过整合价格行为与成交量数据来确认趋势。提示注意成交量激增造成的偏斜结果与其他成交量分析结合使用。
- 选择提供多样化和互补信息的指标。避免冗余例如不要同时选择rsi和stochrsi。还要简要解释为什么它们适合给定的市场环境。当你调用工具时请使用上面提供的指标的确切名称因为它们是定义的参数否则你的调用将失败。请确保首先调用get_YFin_data来检索生成指标所需的CSV。写一份非常详细和细致的趋势观察报告。不要简单地说趋势是混合的提供详细和细致的分析和洞察可能有助于交易员做出决策。"""
+ """ 确保在报告末尾附上一个Markdown表格来组织报告中的关键点使其有序且易于阅读。"""
)
else:
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:
Moving Averages:
- close_50_sma: 50 SMA: A medium-term trend indicator. Usage: Identify trend direction and serve as dynamic support/resistance. Tips: It lags price; combine with faster indicators for timely signals.
- close_200_sma: 200 SMA: A long-term trend benchmark. Usage: Confirm overall market trend and identify golden/death cross setups. Tips: It reacts slowly; best for strategic trend confirmation rather than frequent trading entries.
- close_10_ema: 10 EMA: A responsive short-term average. Usage: Capture quick shifts in momentum and potential entry points. Tips: Prone to noise in choppy markets; use alongside longer averages for filtering false signals.
MACD Related:
- macd: MACD: Computes momentum via differences of EMAs. Usage: Look for crossovers and divergence as signals of trend changes. Tips: Confirm with other indicators in low-volatility or sideways markets.
- macds: MACD Signal: An EMA smoothing of the MACD line. Usage: Use crossovers with the MACD line to trigger trades. Tips: Should be part of a broader strategy to avoid false positives.
- macdh: MACD Histogram: Shows the gap between the MACD line and its signal. Usage: Visualize momentum strength and spot divergence early. Tips: Can be volatile; complement with additional filters in fast-moving markets.
Momentum Indicators:
- rsi: RSI: Measures momentum to flag overbought/oversold conditions. Usage: Apply 70/30 thresholds and watch for divergence to signal reversals. Tips: In strong trends, RSI may remain extreme; always cross-check with trend analysis.
Volatility Indicators:
- boll: Bollinger Middle: A 20 SMA serving as the basis for Bollinger Bands. Usage: Acts as a dynamic benchmark for price movement. Tips: Combine with the upper and lower bands to effectively spot breakouts or reversals.
- boll_ub: Bollinger Upper Band: Typically 2 standard deviations above the middle line. Usage: Signals potential overbought conditions and breakout zones. Tips: Confirm signals with other tools; prices may ride the band in strong trends.
- boll_lb: Bollinger Lower Band: Typically 2 standard deviations below the middle line. Usage: Indicates potential oversold conditions. Tips: Use additional analysis to avoid false reversal signals.
- atr: ATR: Averages true range to measure volatility. Usage: Set stop-loss levels and adjust position sizes based on current market volatility. Tips: It's a reactive measure, so use it as part of a broader risk management strategy.
Volume-Based Indicators:
- vwma: VWMA: A moving average weighted by volume. Usage: Confirm trends by integrating price action with volume data. Tips: Watch for skewed results from volume spikes; use in combination with other volume analyses.
- Select indicators that provide diverse and complementary information. Avoid redundancy (e.g., do not select both rsi and stochrsi). Also briefly explain why they are suitable for the given market context. When you tool call, please use the exact name of the indicators provided above as they are defined parameters, otherwise your call will fail. Please make sure to call get_YFin_data first to retrieve the CSV that is needed to generate indicators. Write a very detailed and nuanced report of the trends you observe. Do not simply state the trends are mixed, provide detailed and finegrained analysis and insights that may help traders make decisions."""
+ """ Make sure to append a Markdown table at the end of the report to organize key points in the report, organized and easy to read."""
)
# 根据语言设置选择不同的系统提示
if toolkit.config.get("output_language", "english") == "chinese":
assistant_prompt = (
"你是一个有用的AI助手与其他助手协作。"
" 使用提供的工具来朝着回答问题的方向前进。"
" 如果你无法完全回答,没关系;另一个具有不同工具的助手"
" 会在你停下的地方继续帮助。执行你能做的事情来取得进展。"
" 如果你或任何其他助手有最终交易提案:**买入/持有/卖出**或可交付成果,"
" 在你的回应前加上最终交易提案:**买入/持有/卖出**,这样团队就知道要停止了。"
" 你可以使用以下工具:{tool_names}\n{system_message}"
"供你参考,当前日期是{current_date}。我们想要查看的公司是{ticker}"
)
else:
assistant_prompt = (
"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}"
)
prompt = ChatPromptTemplate.from_messages(
[
(
"system",
assistant_prompt,
),
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