RL-HMM Trading System: Reinforcement Learning with Hidden Markov Models for Financial Trading
Project description
RL-HMM 交易系统库
一个模块化的强化学习结合隐马尔可夫模型的交易系统库,支持多时间框架分析。
安装
从 PyPI 安装(推荐)
pip install rl-hmm
安装完整版本(含强化学习依赖)
pip install rl-hmm[gym]
从源码安装
git clone https://github.com/yourusername/rl-hmm.git
cd rl-hmm
pip install -e .
功能特性
- 多时间框架支持:支持 5分钟、15分钟、1小时等多种时间框架
- 特征工程:内置多种技术指标(RSI、MACD、ATR、布林带等)
- HMM状态建模:隐马尔可夫模型捕捉市场状态
- 强化学习:支持 PPO 算法的交易策略学习
- 模块化设计:灵活配置特征、奖励机制、动作空间等
项目结构
rl_hmm/
├── __init__.py
├── config/ # 配置模块
│ ├── __init__.py
│ └── default_config.py
├── features/ # 特征工程模块
│ ├── __init__.py
│ └── base.py
├── hmm/ # HMM模块
│ ├── __init__.py
│ ├── hmm_handler.py
│ └── multi_timeframe.py
├── environment/ # Gym环境模块
│ ├── __init__.py
│ └── trading_env.py
├── agents/ # RL Agent模块
│ ├── __init__.py
│ └── ppo_agent.py
├── data/ # 数据加载模块
│ ├── __init__.py
│ └── loader.py
├── evaluation/ # 评估模块
│ ├── __init__.py
│ └── metrics.py
└── system/ # 交易系统主类
├── __init__.py
└── trading_system.py
快速开始
基础使用
from rl_hmm import TradingSystem, Config
config = Config()
system = TradingSystem(config)
system.run()
自定义配置
from rl_hmm import TradingSystem, Config
config = Config()
config.ticker = "AAPL"
config.total_timesteps = 100000
config.n_components = 4
config.win = 100
config.loss = -80
system = TradingSystem(config)
system.run()
模块化使用
from rl_hmm import Config, DataLoader, FeatureEngineer, HMMHandler
import numpy as np
config = Config()
loader = DataLoader(config)
df_15m, df_1h = loader.load_data()
hmm = HMMHandler(config.n_components)
features = df_15m['return'].values.reshape(-1, 1)
hmm.fit(features)
prediction = hmm.predict(features[-1:])
核心模块说明
Config
配置类,包含所有系统参数:
- ticker: 交易代码
- interval_15m: 15分钟K线间隔
- interval_1h: 1小时K线间隔
- train_window: 训练窗口大小
- total_timesteps: RL训练总步数
- n_components: HMM组件数量
- n_env: 并行环境数量
- win: 获胜奖励
- loss: 失败惩罚
FeatureEngineer
特征工程类,提供技术指标提取功能:
- add_all_features(): 添加所有特征
- get_feature_names(): 获取特征名称列表
HMMHandler
单个HMM处理器:
- fit(): 训练HMM模型
- predict(): 预测状态
- predict_proba(): 预测状态概率
MultiTimeframeHMM
多时间框架HMM处理器:
- update(): 更新多时间框架HMM模型
- get_regime_filter(): 获取市场状态过滤
TradingEnv
Gym交易环境,用于RL训练:
- reset(): 重置环境
- step(): 执行一步交易
- _get_observation(): 获取观察状态
PPOAgent
PPO强化学习Agent:
- train(): 训练Agent
- predict(): 预测动作
- save(): 保存模型
- load(): 加载模型
DataLoader
数据加载器:
- load_data(): 下载并加载数据
Evaluator
评估器:
- evaluate(): 评估模型性能
- plot_results(): 绘制结果
TradingSystem
交易系统主类,协调所有模块:
- run(): 运行完整的交易系统
特点
- 模块化设计:各功能模块独立,便于扩展和维护
- 面向对象:清晰的类结构和职责划分
- 易于调用:提供简单易用的API
- 多时间框架:支持多时间框架分析
- 灵活配置:可自定义各种参数
依赖项
- numpy
- pandas
- yfinance
- hmmlearn
- gym
- stable-baselines3
- pandas-ta
- matplotlib
许可证
MIT License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
rl_hmm-0.2.1.tar.gz
(17.1 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
rl_hmm-0.2.1-py3-none-any.whl
(18.4 kB
view details)
File details
Details for the file rl_hmm-0.2.1.tar.gz.
File metadata
- Download URL: rl_hmm-0.2.1.tar.gz
- Upload date:
- Size: 17.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0cb21428a8bf89ed155037afdcd72017762846861f5cde63cda6d2d5c24b7d10
|
|
| MD5 |
8e48a60b3beb5acfaeb1fbba94327fae
|
|
| BLAKE2b-256 |
2a367ed2c2c51d12b4df2f70c4c0840ae4582f4f5b5059ae04934d18ca9cf908
|
File details
Details for the file rl_hmm-0.2.1-py3-none-any.whl.
File metadata
- Download URL: rl_hmm-0.2.1-py3-none-any.whl
- Upload date:
- Size: 18.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e43900cc53016408bf06d03b83c274ae6faf0e5d5b04e6e9646f0791b8187c18
|
|
| MD5 |
aa73a0403da98be6e16a0c2b66d3db85
|
|
| BLAKE2b-256 |
14cd4a3ada7ada17186975ddd4d0320fb067cd6cacebcf6cedb16414eb4a0019
|