# gold_trend_forecast **Repository Path**: bltzmnn/gold_trend_forecast ## Basic Information - **Project Name**: gold_trend_forecast - **Description**: 基于多因子分析和机器学习的黄金价格走势预测系统,包含实时获取金价和政治经济新闻。 - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2026-03-23 - **Last Updated**: 2026-03-25 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 黄金价格走势预测模型 基于多因子分析和机器学习的黄金价格走势预测系统,包含实时获取金价和政治经济新闻。 ## 项目结构 ``` gold_trend_forecast/ ├── src/ # 核心源码 │ ├── __init__.py # 包初始化 │ ├── utils.py # 工具函数 │ ├── advanced_predictor.py # 高级预测器(多因子分析) │ ├── ultimate_predictor.py # 终极预测器(主模型) │ ├── gold_price_fetcher.py # 实时金价获取模块 │ ├── news_fetcher.py # 实时新闻获取模块 │ ├── ultimate_weekly_data.py # 周度数据生成 │ ├── ultimate_weekly_backtest.py # 周度回测引擎 │ ├── iterative_optimizer.py # 迭代优化器 │ └── backtest_report_generator.py # 报告生成器 ├── predict_this_week.py # 本周预测入口 ├── run_ultimate_weekly_backtest.py # 回测入口 ├── requirements.txt # 依赖 └── README.md # 说明文档 ``` ## 模型原理 ### 1. 多因子分析框架 模型综合分析七大类因子: | 因子类型 | 包含指标 | 权重范围 | |---------|---------|---------| | 宏观因子 | 实际利率、美元指数、通胀预期、美联储利率 | 8-15% | | 季节性因子 | 月份效应、节假日效应 | 5-10% | | 技术因子 | MA、MACD、RSI、布林带、KDJ | 10-18% | | 情绪因子 | ETF持仓、VIX指数、市场情绪 | 8-12% | | 相关资产 | 原油、美股、美债收益率 | 8-10% | | 新闻因子 | 实时政治经济新闻、利好利空分析 | 12-18% | | 事件因子 | 地缘政治、突发事件 | 10-12% | ### 2. 新闻因子分析 - **关键词匹配**: 黄金、美联储、利率、美元、地缘政治等50+关键词 - **情感分析**: 自动识别利好/利空消息 - **影响评分**: 计算新闻对金价的影响分数 (-1 到 +1) - **相关度计算**: 评估新闻与黄金的相关程度 ### 3. 动态权重系统 根据市场状态自动调整因子权重: - **牛市**: 增加技术因子和趋势因子权重 - **熊市**: 增加情绪因子和宏观因子权重 - **震荡市**: 增加季节性因子权重 - **转折市**: 增加反转信号检测权重 ### 4. 市场状态识别 | 状态 | 说明 | |------|------| | 牛市 | 多头排列,动量向上 | | 熊市 | 空头排列,动量向下 | | 震荡市 | 均线交织,波动收窄 | | 转折市 | 状态转换中 | ### 5. 预测流程 ``` 实时金价数据 ─┬─→ 因子计算 ─→ 市场状态识别 ─→ 动态权重分配 │ ↓ 实时新闻数据 ─┘ 多模型融合 ─→ 置信度评估 ─→ 预测输出 ``` ## 安装 ```bash pip install -r requirements.txt ``` 主要依赖: - numpy >= 1.24.0 - pandas >= 2.0.0 - requests >= 2.28.0 - scikit-learn (可选,用于机器学习) - torch (可选,用于LSTM) ## 使用方法 ### 1. 预测本周黄金走势 ```bash python predict_this_week.py ``` 输出示例: ``` 【1. 获取实时金价数据】 ✓ 数据源: 新浪财经-纽约黄金期货 ✓ 当前金价: $4353.60 / 盎司 ✓ 黄金ETF获取到 100 个历史价格 【2. 获取实时新闻因子】 ✓ 获取到 X 条相关新闻 ✓ 金价影响分数: +0.32 【预测结果】 本周预测方向: 上涨 ▲ 预测涨跌幅: +0.17% 预测置信度: 55.2% 市场状态: 熊市 综合判断: 偏多看涨 ``` ### 2. 运行历史回测 ```bash python run_ultimate_weekly_backtest.py ``` ### 3. 作为模块使用 ```python from src.ultimate_predictor import UltimateGoldPredictor from src.gold_price_fetcher import GoldPriceFetcher from src.news_fetcher import NewsFetcher from src.advanced_predictor import EventData # 获取实时金价 fetcher = GoldPriceFetcher() result = fetcher.get_current_price() current_price = result['current_price'] # 获取历史价格 prices = fetcher.get_historical_prices(100) # 获取新闻因子 news_fetcher = NewsFetcher() news_factors = news_fetcher.get_news_factors() impact_score = news_fetcher.get_gold_impact_score() # 初始化预测器 predictor = UltimateGoldPredictor() # 准备数据 event = EventData( sentiment_score=news_factors['news_sentiment'], geopolitical_risk=0.35, event_count=news_factors['news_count'], positive_events=news_factors['bullish_count'], negative_events=news_factors['bearish_count'] ) # 执行预测 prediction = predictor.predict( year=2026, month=3, prices=prices, event=event ) print(f"预测方向: {'上涨' if prediction['predicted_change'] > 0 else '下跌'}") print(f"预测涨跌幅: {prediction['predicted_change']*100:.2f}%") print(f"置信度: {prediction['confidence']*100:.1f}%") ``` ## 核心类说明 ### UltimateGoldPredictor 主预测器类,继承自AdvancedGoldPredictor,包含实时新闻因子。 ```python class UltimateGoldPredictor: def predict(self, year, month, prices, event) -> Dict: """ 执行预测 返回: predicted_price: 预测价格 predicted_change: 预测涨跌幅 confidence: 置信度 market_state: 市场状态 reversal_signal: 反转信号 factors: 各因子贡献 """ ``` ### GoldPriceFetcher 实时金价获取器。 ```python class GoldPriceFetcher: def get_current_price(self) -> Dict: """获取当前金价""" def get_historical_prices(self, days=100) -> List[float]: """获取历史价格""" ``` ### NewsFetcher 实时新闻获取器。 ```python class NewsFetcher: def get_all_news(self) -> List[NewsItem]: """获取所有相关新闻""" def get_news_factors(self) -> Dict: """获取新闻因子""" def get_gold_impact_score(self) -> float: """获取新闻对金价的影响分数 (-1 到 1)""" ``` ## 风险提示 1. 本模型仅供学习研究使用,不构成投资建议 2. 预测结果存在不确定性,实际走势可能与预测不符 3. 历史表现不代表未来收益 4. 投资有风险,入市需谨慎 ## 版本历史 - v1.0: 基础预测模型 - v2.0: 添加多因子分析 - v3.0: 终极预测器,动态权重,市场状态识别 - v3.1: 自动获取实时金价数据 - v3.2: 添加实时新闻因子分析 - v3.4: 优化数据源,使用新浪财经+黄金ETF获取金价数据 ## 许可证 MIT License