跳到主要内容

快速开始

本指南将在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()方法返回两个值:

  1. State:包含不同智能体的所有中间分析
  2. 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-previewo1-preview, gpt-4o, gpt-4o-mini
quick_think_llm快速响应模型gpt-4ogpt-4o, gpt-4o-mini
max_debate_rounds研究员辩论轮数31-5
online_tools使用实时vs缓存数据FalseTrue, 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获取实时数据

下一步

现在您已经开始运行:

  1. 探索智能体详情:了解个别智能体
  2. 尝试CLI:使用命令行界面
  3. 查看示例:查看高级示例
  4. 理解配置:阅读API文档

故障排除

如果遇到问题: