快速开始
本指南将在5分钟内让您开始使用TradingAgents。我们将通过一个基础示例来分析股票并做出交易决策。
前提条件
开始之前,请确保您已经:
- ✅ 完成了安装
- ✅ 设置了API密钥(FinnHub和OpenAI)
- ✅ 激活了虚拟环境
基础使用示例
以下是一个开始使用TradingAgents的简单示例:
from tradingagents.graph.trading_graph import TradingAgentsGraph
from tradingagents.default_config import DEFAULT_CONFIG
# 使用默认配置初始化TradingAgents
ta = TradingAgentsGraph(debug=True, config=DEFAULT_CONFIG.copy())
# 分析NVIDIA股票的特定日期
symbol = "NVDA"
date = "2024-05-10"
# 运行多智能体分析
state, decision = ta.propagate(symbol, date)
# 打印交易决策
print(f"{date}对{symbol}的交易决策:")
print(decision)
理解输出
propagate()
方法返回两个值:
- State:包含不同智能体的所有中间分析
- Decision:最终交易建议
典型的决策输出包括:
- Action:BUY、SELL或HOLD
- Confidence:决策置信度评分
- Reasoning:分析摘要
- Risk Assessment:识别的风险和缓解策略
自定义配置
您可以通过修改配置来自定义框架行为:
from tradingagents.graph.trading_graph import TradingAgentsGraph
from tradingagents.default_config import DEFAULT_CONFIG
# 创建自定义配置
custom_config = DEFAULT_CONFIG.copy()
# 使用不同的LLM模型(测试的成本效益选择)
custom_config["deep_think_llm"] = "gpt-4o-mini"
custom_config["quick_think_llm"] = "gpt-4o-mini"
# 调整辩论轮数(更少轮数=更快执行)
custom_config["max_debate_rounds"] = 1
# 启用/禁用在线数据工具
custom_config["online_tools"] = True
# 使用自定义配置初始化
ta = TradingAgentsGraph(debug=True, config=custom_config)
# 运行分析
_, decision = ta.propagate("AAPL", "2024-05-10")
print(decision)
配置选项
您可以调整的关键配置参数:
参数 | 描述 | 默认值 | 选项 |
---|---|---|---|
deep_think_llm | 复杂分析模型 | o1-preview | o1-preview , gpt-4o , gpt-4o-mini |
quick_think_llm | 快速响应模型 | gpt-4o | gpt-4o , gpt-4o-mini |
max_debate_rounds | 研究员辩论轮数 | 3 | 1-5 |
online_tools | 使用实时vs缓存数据 | False | True , False |
数据源
在线工具(推荐用于实验)
当online_tools = True
时,智能体访问:
- 通过FinnHub API的实时市场数据
- 当前新闻和情绪数据
- 实时技术指标
缓存数据(默认)
当online_tools = False
时,智能体使用:
- 来自Tauric TradingDB的精选历史数据
- 预处理的技术指标
- 历史新闻和情绪数据
常见用例
1. 单股分析
# 分析特定日期的特定股票
_, decision = ta.propagate("TSLA", "2024-03-15")
2. 多股比较
stocks = ["AAPL", "GOOGL", "MSFT", "NVDA"]
date = "2024-05-10"
results = {}
for stock in stocks:
_, decision = ta.propagate(stock, date)
results[stock] = decision
# 比较决策
for stock, decision in results.items():
print(f"{stock}: {decision['action']} (置信度: {decision['confidence']})")
3. 时间序列分析
import datetime
symbol = "AAPL"
dates = ["2024-05-01", "2024-05-02", "2024-05-03"]
for date in dates:
_, decision = ta.propagate(symbol, date)
print(f"{date}: {decision['action']}")
性能提示
用于测试和开发
- 使用
gpt-4o-mini
模型降低API成本 - 设置
max_debate_rounds = 1
以更快执行 - 启用
debug=True
查看详细日志
用于生产分析
- 使用
o1-preview
进行深度分析任务 - 增加
max_debate_rounds
进行更深入的辩论 - 启用
online_tools
获取实时数据
下一步
现在您已经开始运行:
故障排除
如果遇到问题: