跳到主要内容

环境变量

本文档列出了 OpenBB 平台支持的所有环境变量及其用途。

API 密钥

数据提供商 API 密钥

# Alpha Vantage
export OPENBB_ALPHA_VANTAGE_API_KEY="your_api_key"

# FRED (Federal Reserve Economic Data)
export OPENBB_FRED_API_KEY="your_api_key"

# Polygon
export OPENBB_POLYGON_API_KEY="your_api_key"

# Quandl
export OPENBB_QUANDL_API_KEY="your_api_key"

# Financial Modeling Prep
export OPENBB_FMP_API_KEY="your_api_key"

# Intrinio
export OPENBB_INTRINIO_API_KEY="your_api_key"

# IEX Cloud
export OPENBB_IEX_TOKEN="your_token"

新闻和社交媒体

# News API
export OPENBB_NEWS_API_KEY="your_api_key"

# Twitter Bearer Token
export OPENBB_TWITTER_BEARER_TOKEN="your_token"

# Reddit API
export OPENBB_REDDIT_CLIENT_ID="your_client_id"
export OPENBB_REDDIT_CLIENT_SECRET="your_client_secret"
export OPENBB_REDDIT_USERNAME="your_username"
export OPENBB_REDDIT_PASSWORD="your_password"
export OPENBB_REDDIT_USER_AGENT="your_user_agent"

系统配置

缓存设置

# 启用/禁用缓存
export OPENBB_CACHE_ENABLED="true"

# 缓存目录
export OPENBB_CACHE_DIRECTORY="/path/to/cache"

# 缓存过期时间(秒)
export OPENBB_CACHE_EXPIRATION="3600"

日志设置

# 日志级别 (DEBUG, INFO, WARNING, ERROR, CRITICAL)
export OPENBB_LOG_LEVEL="INFO"

# 日志文件路径
export OPENBB_LOG_FILE="/path/to/logfile.log"

# 启用控制台日志
export OPENBB_LOG_CONSOLE="true"

网络设置

# HTTP 代理
export OPENBB_HTTP_PROXY="http://proxy.example.com:8080"

# HTTPS 代理
export OPENBB_HTTPS_PROXY="https://proxy.example.com:8080"

# 请求超时(秒)
export OPENBB_REQUEST_TIMEOUT="30"

# 重试次数
export OPENBB_MAX_RETRIES="3"

数据库配置

本地数据库

# SQLite 数据库路径
export OPENBB_DATABASE_URL="sqlite:///path/to/database.db"

远程数据库

# PostgreSQL
export OPENBB_DATABASE_URL="postgresql://user:password@host:port/database"

# MySQL
export OPENBB_DATABASE_URL="mysql://user:password@host:port/database"

开发设置

调试模式

# 启用调试模式
export OPENBB_DEBUG="true"

# 启用详细输出
export OPENBB_VERBOSE="true"

# 开发模式
export OPENBB_DEV_MODE="true"

测试设置

# 测试环境
export OPENBB_TEST_MODE="true"

# 测试数据目录
export OPENBB_TEST_DATA_DIR="/path/to/test/data"

设置环境变量

Linux/macOS

临时设置(当前会话)

export OPENBB_ALPHA_VANTAGE_API_KEY="your_api_key"

永久设置

~/.bashrc~/.zshrc 中添加:

echo 'export OPENBB_ALPHA_VANTAGE_API_KEY="your_api_key"' >> ~/.bashrc
source ~/.bashrc

Windows

临时设置(当前会话)

set OPENBB_ALPHA_VANTAGE_API_KEY=your_api_key

永久设置

setx OPENBB_ALPHA_VANTAGE_API_KEY "your_api_key"

Python 中设置

import os

# 设置环境变量
os.environ["OPENBB_ALPHA_VANTAGE_API_KEY"] = "your_api_key"

# 或者使用 OpenBB 的配置系统
import openbb
openbb.account.credentials.alpha_vantage_api_key = "your_api_key"

.env 文件

创建 .env 文件来管理环境变量:

# .env 文件
OPENBB_ALPHA_VANTAGE_API_KEY=your_api_key
OPENBB_FRED_API_KEY=your_fred_key
OPENBB_POLYGON_API_KEY=your_polygon_key
OPENBB_LOG_LEVEL=INFO
OPENBB_CACHE_ENABLED=true

在 Python 中加载:

from dotenv import load_dotenv
load_dotenv()

import openbb
# 环境变量会自动加载

验证配置

import openbb
import os

# 检查环境变量
print("Alpha Vantage API Key:", os.getenv("OPENBB_ALPHA_VANTAGE_API_KEY"))

# 检查 OpenBB 配置
print("Credentials:", openbb.account.credentials)

安全注意事项

  1. 不要在代码中硬编码 API 密钥
  2. 使用环境变量或配置文件
  3. 不要将 API 密钥提交到版本控制
  4. 定期轮换 API 密钥
  5. 限制 API 密钥的权限