Skip to main content

RL-HMM Trading System: Reinforcement Learning with Hidden Markov Models for Financial Trading

Project description

RL-HMM 交易系统库

PyPI version License: MIT

一个模块化的强化学习结合隐马尔可夫模型的交易系统库,支持多时间框架分析。

安装

从 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(): 运行完整的交易系统

特点

  1. 模块化设计:各功能模块独立,便于扩展和维护
  2. 面向对象:清晰的类结构和职责划分
  3. 易于调用:提供简单易用的API
  4. 多时间框架:支持多时间框架分析
  5. 灵活配置:可自定义各种参数

依赖项

  • numpy
  • pandas
  • yfinance
  • hmmlearn
  • gym
  • stable-baselines3
  • pandas-ta
  • matplotlib

许可证

MIT License

Project details


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.0.tar.gz (17.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rl_hmm-0.2.0-py3-none-any.whl (18.4 kB view details)

Uploaded Python 3

File details

Details for the file rl_hmm-0.2.0.tar.gz.

File metadata

  • Download URL: rl_hmm-0.2.0.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

Hashes for rl_hmm-0.2.0.tar.gz
Algorithm Hash digest
SHA256 322dfca494f8124f802342d69a773460e1c884f2413de294a4e0677c1c740448
MD5 b38b603ab9c6fe8d1acb7b6bdff2c746
BLAKE2b-256 2e29d7b7da5ec0c26fcb2a8fb41502333c19becd15ab7c4df0c244f9f4fe9404

See more details on using hashes here.

File details

Details for the file rl_hmm-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: rl_hmm-0.2.0-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

Hashes for rl_hmm-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d0e7a34bc8fd0b4558f25c456da3768c3d1b6b7804851f9daeb15f7172ae36ad
MD5 0a50d4ca2d1023ef4707136089b6a752
BLAKE2b-256 76ee88d4ce639128be2971fbf42c9a61d69ccce228ec21b9e1113a4733ace4ae

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page