MoNiBS
模拟交易回测系统 - 提供完整的订单管理、账户管理、数据管理、因子分析功能
注意: 包在PyPI上的名称是 liwancai-MoNiBS,但导入时使用 MoNiBS
安装
pip install liwancai-MoNiBS
快速开始
from MoNiBS import *
# 配置回测参数
cfg = {
"do_winsor": {"mathed": "MADWIN", "arges": {"k": 5, "lower_quantile": 0.01, "upper_quantile": 0.99}},
"do_neutral": {"mathed": "INDCAP"},
"do_std": True,
"col_strategy": "overwrite"
}
# 初始化回测引擎
engine = MoNiBS(cfg)
# 使用动态注册的交易方法
engine.KB(code="000001", Price=10.0, Qty=100) # 开多
engine.KS(code="000001", Price=10.0, Qty=100) # 开空
engine.PB(code="000001") # 平多
engine.PS(code="000001") # 平空
核心模块
1. 回测引擎 (MoNiBS)
主框架类,集成所有核心模块,提供动态交易方法注册:
from MoNiBS import MoNiBS
engine = MoNiBS(cfg)
engine.OnOrderBS(KLs, TKs) # 执行订单处理
特性:
- 动态注册交易方法 (KB, KS, PB, PS, KBB, KBS, KSB, KSS, PBB, PBS, PSB, PSS)
- 因子预处理流水线 (去极值 → 中性化 → 标准化)
- 订单处理流程: 先平仓 → 更新账户 → 再开仓
2. 账户管理 (Account)
多账户资金管理模块:
from MoNiBS import Account, AccountHub
# 创建账户
acc = Account(AccName="测试账户", initfunds=1000000)
# 账户属性
acc.FundsSum # 总资产
acc.FundsAble # 可用资金
acc.FundsHold # 持仓资金/占用保证金
acc.HoldEarn # 浮动盈亏
acc.FundsEarnSum # 清仓结算盈亏
acc.FeesSum # 总手续费
3. 订单管理 (OrderHold)
完整的订单生命周期管理:
from MoNiBS import Order, OrderStatus, BSType
# 创建订单
order = Order(
AccountID="001",
kline=kline,
BSType=BSType.KB,
Method=OrderMethod.Market,
Price=10.0,
Qty=100
)
交易类型 (BSType):
KB: 开多,KS: 开空PB: 平多,PS: 平空KBB/KBS: 开看多买方/卖方KSB/KSS: 开看空买方/卖方PBB/PBS: 平看多买方/卖方PSB/PSS: 平看空买方/卖方
订单方式 (OrderMethod):
Market: 市价单Limit: 限价单Buy1-5/Sell1-5: 盘口价格UpperLimit/LowerLimit: 涨跌停价
4. 数据管理 (MgrData)
行情数据获取与费用计算:
from MoNiBS import MgrDataHub
mgr = MgrDataHub(cfg)
fees = mgr.fetch_Fees(code) # 获取费用信息
5. 数据存储 (DBSave)
基于 DuckDB 的高效数据存储:
from MoNiBS import DBSaveHub
db = DBSaveHub(cfg)
6. 因子分析 (Factor)
6.1 截面因子分析 (RankSaS)
截面因子统计分析引擎:
from MoNiBS.Factor.RankSaS.Core import FactorBase
cfg = {
"account": "your_account",
"password": "your_password",
"base_url": "api_url",
"Factor_col": "close",
"REBChgPerN": ["REBChgPer1", "REBChgPer20"],
"group_num": 10,
"do_winsor": {"mathed": "MADWIN", "arges": {"k": 5}},
"do_neutral": {"mathed": "INDCAP"},
"do_std": True
}
factor_engine = FactorBase(cfg)
功能模块:
PreProcess: 因子预处理 (去极值/中性化/标准化)ICIR: IC/IR 统计分析Report: 因子分析报告生成Core: 核心因子分析引擎
6.2 类别因子分析 (ClassSAS)
类别因子统计分析引擎:
from MoNiBS.Factor.ClassSAS.Core import ClassBase
cfg = {
"account": "your_account",
"password": "your_password",
"base_url": "api_url",
"CATEGORY_SCALES": [1, 2, 5, 10, 25, 50],
"CATEGORY_CLASSES": [1, 2, 3, 4, -1, -2, -3, -4],
"RETURN_PERIODS": [1, 2, 3, 5, 10, 20, 60, 120]
}
class_engine = ClassBase(cfg)
子模块:
Method/: 统计方法库 (CAPM/复合/跨期/对冲/推断/线性/周期/RankIC/风险)compute/: 计算引擎 (跨期/市场/指标/净值/选择器/统计)output/: 输出模块 (报告/保存)
7. Web API (WebApi)
RESTful API 接口,支持远程调用:
from MoNiBS.WebApi.ApiBase import base_router
# 可用接口
# GET /strategies - 获取策略列表
# GET /databases - 获取数据库列表
# GET /tables - 获取表列表
# GET /accounts - 获取账户信息
# GET /holds - 获取持仓信息
# GET /orders - 获取订单信息
8. 数据类型 (utils)
from MoNiBS import KLine, KLineN, TickN
# K线数据
kline = KLine(
code="000001",
period="1d",
open=10.0,
close=10.5,
high=11.0,
low=9.8,
volume=1000000,
amount=10500000,
trade_time="2024-01-01 09:30:00",
trade_date="2024-01-01"
)
# Tick数据
tick = TickN(
Code="000001",
LastPrice=10.5,
Volume=1000,
AskPrice=[10.51, 10.52, 10.53, 10.54, 10.55],
BidPrice=[10.49, 10.48, 10.47, 10.46, 10.45]
)
项目结构
MoNiBS/
├── Account/ # 账户管理模块
│ ├── Account.py # 账户数据模型
│ ├── AccountHub.py # 账户管理器
│ └── __init__.py
├── DBSave/ # 数据存储模块
│ ├── DBSaveHub.py # 数据库管理器
│ └── __init__.py
├── Factor/ # 因子分析模块
│ ├── ClassSAS/ # 类别因子分析
│ │ ├── Core.py # 类别因子基类
│ │ ├── Method/ # 统计方法库
│ │ │ ├── ClassStatsCAPM.py
│ │ │ ├── ClassStatsComposite.py
│ │ │ ├── ClassStatsCrossPeriod.py
│ │ │ ├── ClassStatsHedge.py
│ │ │ ├── ClassStatsInference.py
│ │ │ ├── ClassStatsLine.py
│ │ │ ├── ClassStatsPeriodic.py
│ │ │ ├── ClassStatsRankIC.py
│ │ │ └── ClassStatsRisk.py
│ │ ├── compute/ # 计算引擎
│ │ │ ├── cross_period.py
│ │ │ ├── market.py
│ │ │ ├── metrics.py
│ │ │ ├── nav.py
│ │ │ ├── selector.py
│ │ │ └── stats.py
│ │ └── output/ # 输出模块
│ │ ├── reporter.py
│ │ └── saver.py
│ ├── RankSaS/ # 截面因子分析
│ │ ├── Core.py # 因子分析基类
│ │ ├── ICIR.py # IC/IR分析
│ │ ├── PreProcess.py # 数据预处理
│ │ └── Report.py # 报告生成
│ └── __init__.py
├── MgrData/ # 数据管理模块
│ ├── Cache.py # 数据缓存
│ ├── Client.py # 数据客户端
│ ├── CodeFees.py # 费用计算
│ ├── MgrDataHub.py # 数据管理器
│ └── __init__.py
├── OrderHold/ # 订单持仓模块
│ ├── Help.py # 订单帮助文档
│ ├── Hold.py # 持仓管理
│ ├── Order.py # 订单模型
│ ├── OrderDeal.py # 订单处理
│ ├── OrderHub.py # 订单管理器
│ ├── OrderPGFH.py # 分红配股处理
│ ├── Type.py # 类型定义
│ └── __init__.py
├── Strategys/ # 策略模块
│ └── __init__.py
├── WebApi/ # Web API接口
│ ├── ApiAccs.py # 账户数据API
│ ├── ApiBase.py # 基础数据API
│ ├── ApiHold.py # 持仓数据API
│ ├── ApiOrder.py # 订单数据API
│ ├── DBConBase.py # 数据库连接基类
│ ├── Evaluate.py # 评估数据模型
│ └── __init__.py
├── utils/ # 工具模块
│ ├── DataType.py # 数据类型定义
│ ├── Tools.py # 工具函数
│ └── __init__.py
├── MoNiBS.py # 主框架类
└── __init__.py
依赖包
liwancai-PyLOG>=1.0.0: 日志记录liwancai-PyTools>=1.0.0: 工具函数fastapi>=0.68.0: Web API框架uvicorn>=0.15.0: ASGI服务器pandas>=1.0.0: 数据处理numpy>=1.19.0: 数值计算pydantic>=1.8.0: 数据验证duckdb>=0.5.0: 数据库存储
版本信息
- 当前版本: 1.0.71
- 作者: liwancai
- 许可证: MIT
📄 许可证
MIT License
Metadata
Release files for liwancai-MoNiBS 1.0.71
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| liwancai_monibs-1.0.71.tar.gz | 13.8 MB | Without provenance | Details for liwancai_monibs-1.0.71.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| liwancai_monibs-1.0.71-py3-none-any.whl | Python 3 | none | any | Details for liwancai_monibs-1.0.71-py3-none-any.whl |
Total release size: 27.7 MB
Release files / liwancai_monibs-1.0.71.tar.gz
| Download URL | liwancai_monibs-1.0.71.tar.gz |
|---|---|
| Size | 13.8 MB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ba3eb9f8bb55e73362eb2efc5757ba33a5606a418ec5cfb3d7ccff89fe25c876
|
|
BLAKE2b-256 checksum How to use checksums |
b1c5053161ceb3003ee4247ff9ef3b0f90edf147fcee014dcb75c018e551eb7f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.3
|
Release files / liwancai_monibs-1.0.71-py3-none-any.whl
| Download URL | liwancai_monibs-1.0.71-py3-none-any.whl |
|---|---|
| Size | 13.9 MB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
4134bc2235a0f77db060b15dc846d2bca502b9040fa15c45b52c76d07bc7a39b
|
|
BLAKE2b-256 checksum How to use checksums |
86709caae90a9202d2e43580cc939523daba9244a540409ff4c0332a0780826d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.3
|
Security for liwancai-MoNiBS 1.0.71
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.