# moomoo OpenAPI
### 简介
[**moomoo OpenAPI**](https://openapi.moomoo.com/moomoo-api-doc/)开源项目可以满足使用[**Futu OpenAPI**](https://www.futunn.com/OpenAPI)进行量化投资的需求, 并提供包括Python、Json/Protobuf协议的行情及交易接口。
- [官方在线文档](https://openapi.moomoo.com/moomoo-api-doc/)
-------------------
### 安装
```
pip install moomoo-api
```
###### 注: 本API支持Python2.7/Python3.x, 推荐安装anaconda2或anaconda3环境,方便快捷。
---
### 快速上手
```
# 导入moomoo-api
import moomoo as ft
# 实例化行情上下文对象
quote_ctx = ft.OpenQuoteContext(host="127.0.0.1", port=11111)
# 上下文控制
quote_ctx.start() # 开启异步数据接收
quote_ctx.set_handler(ft.TickerHandlerBase()) # 设置用于异步处理数据的回调对象(可派生支持自定义)
# 低频数据接口
market = ft.Market.HK
code = 'HK.00123'
code_list = [code]
plate = 'HK.BK1107'
print(quote_ctx.get_trading_days(market, start=None, end=None)) # 获取交易日
print(quote_ctx.get_stock_basicinfo(market, stock_type=ft.SecurityType.STOCK)) # 获取股票信息
print(quote_ctx.get_autype_list(code_list)) # 获取复权因子
print(quote_ctx.get_market_snapshot(code_list)) # 获取市场快照
print(quote_ctx.get_plate_list(market, ft.Plate.ALL)) # 获取板块集合下的子板块列表
print(quote_ctx.get_plate_stock(plate)) # 获取板块下的股票列表
# 高频数据接口
quote_ctx.subscribe(code, [ft.SubType.QUOTE, ft.SubType.TICKER, ft.SubType.K_DAY, ft.SubType.ORDER_BOOK, ft.SubType.RT_DATA, ft.SubType.BROKER])
print(quote_ctx.get_stock_quote(code)) # 获取报价
print(quote_ctx.get_rt_ticker(code)) # 获取逐笔
print(quote_ctx.get_cur_kline(code, num=100, ktype=ft.KLType.K_DAY)) #获取当前K线
print(quote_ctx.get_order_book(code)) # 获取摆盘
print(quote_ctx.get_rt_data(code)) # 获取分时数据
print(quote_ctx.get_broker_queue(code)) # 获取经纪队列
# 停止异步数据接收
quote_ctx.stop()
# 关闭对象
quote_ctx.close()
# 实例化港股交易上下文对象
trade_hk_ctx = ft.OpenHKTradeContext(host="127.0.0.1", port=11111)
# 交易接口列表
print(trade_hk_ctx.unlock_trade(password='123456')) # 解锁接口
print(trade_hk_ctx.accinfo_query(trd_env=ft.TrdEnv.SIMULATE)) # 查询账户信息
print(trade_hk_ctx.place_order(price=1.1, qty=2000, code=code, trd_side=ft.TrdSide.BUY, order_type=ft.OrderType.NORMAL, trd_env=ft.TrdEnv.SIMULATE)) # 下单接口
print(trade_hk_ctx.order_list_query(trd_env=ft.TrdEnv.SIMULATE)) # 查询订单列表
print(trade_hk_ctx.position_list_query(trd_env=ft.TrdEnv.SIMULATE)) # 查询持仓列表
trade_hk_ctx.close()
```
---
### 示例策略
- 示例策略文件位于目录: (moomoo-api包安装目录)/py-moomoo-api/examples 下,用户可参考实例策略来学习API的使用。
---
### 调试开关和推送记录
- set_futu_debug_model函数可以打开或关闭调试级别的log记录。
- 如果打开记录,则会记录info级别的log并且记录所有逐笔、摆盘、券商经纪的推送记录,以便于后面排查,文件记录在%appdata%(%HOME%)\com.moomoonn.FutuOpenD\Log下面
- examples\analysis下面会有对逐笔、摆盘、券商经纪的推送记录的分析脚本,与我们联系,拿到原始交易所数据后,可以载入比对(beta功能)
---
### 使用须知
- python脚本运行前,需先启动[FutuOpenD](https://www.futunn.com/download/openAPI)网关客户端
### API与FutuOpenD网关客户端的架构
![image](https://futunnopen.github.io/futu-api-doc/_images/API.png)
***
### API及FutuOpenD客户端交流方式
* 富途开放API群(229850364, 108534288)
* 有一定交易额或特殊需求的用户请在入群后联系群主
***
### 使用说明
* 有任何问题可以到 issues 处提出,我们会及时进行解答。
* 使用新版本时请先仔细阅读接口文档,大部分问题都可以在接口文档中找到你想要的答案。
* 欢迎大家提出建议、也可以提出各种需求,我们一定会尽量满足大家的需求。
---
Raw data
{
"_id": null,
"home_page": "https://github.com/MoomooOpen/py-moomoo-api",
"name": "moomoo-api",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": "Moomoo Stock Quant Trading API",
"author": "Moomoo, Inc.",
"author_email": "openapi@moomoo.com",
"download_url": "https://files.pythonhosted.org/packages/4c/2a/9d660d17203e91696fecebcb184036fcbf9e819aed0baeaae65b16d6612d/moomoo-api-8.8.4808.tar.gz",
"platform": null,
"description": "# moomoo OpenAPI\r\n\r\n### \u7b80\u4ecb\r\n\r\n[**moomoo OpenAPI**](https://openapi.moomoo.com/moomoo-api-doc/)\u5f00\u6e90\u9879\u76ee\u53ef\u4ee5\u6ee1\u8db3\u4f7f\u7528[**Futu OpenAPI**](https://www.futunn.com/OpenAPI)\u8fdb\u884c\u91cf\u5316\u6295\u8d44\u7684\u9700\u6c42, \u5e76\u63d0\u4f9b\u5305\u62ecPython\u3001Json/Protobuf\u534f\u8bae\u7684\u884c\u60c5\u53ca\u4ea4\u6613\u63a5\u53e3\u3002\r\n\r\n- [\u5b98\u65b9\u5728\u7ebf\u6587\u6863](https://openapi.moomoo.com/moomoo-api-doc/)\r\n\r\n-------------------\r\n\r\n### \u5b89\u88c5\r\n```\r\npip install moomoo-api\r\n```\r\n\r\n###### \u6ce8: \u672cAPI\u652f\u6301Python2.7/Python3.x, \u63a8\u8350\u5b89\u88c5anaconda2\u6216anaconda3\u73af\u5883\uff0c\u65b9\u4fbf\u5feb\u6377\u3002\r\n\r\n---\r\n\r\n### \u5feb\u901f\u4e0a\u624b\r\n```\r\n\r\n# \u5bfc\u5165moomoo-api\r\nimport moomoo as ft\r\n\r\n# \u5b9e\u4f8b\u5316\u884c\u60c5\u4e0a\u4e0b\u6587\u5bf9\u8c61\r\nquote_ctx = ft.OpenQuoteContext(host=\"127.0.0.1\", port=11111)\r\n\r\n# \u4e0a\u4e0b\u6587\u63a7\u5236\r\nquote_ctx.start() # \u5f00\u542f\u5f02\u6b65\u6570\u636e\u63a5\u6536\r\nquote_ctx.set_handler(ft.TickerHandlerBase()) # \u8bbe\u7f6e\u7528\u4e8e\u5f02\u6b65\u5904\u7406\u6570\u636e\u7684\u56de\u8c03\u5bf9\u8c61(\u53ef\u6d3e\u751f\u652f\u6301\u81ea\u5b9a\u4e49)\r\n\r\n# \u4f4e\u9891\u6570\u636e\u63a5\u53e3\r\nmarket = ft.Market.HK\r\ncode = 'HK.00123'\r\ncode_list = [code]\r\nplate = 'HK.BK1107'\r\nprint(quote_ctx.get_trading_days(market, start=None, end=None)) # \u83b7\u53d6\u4ea4\u6613\u65e5\r\nprint(quote_ctx.get_stock_basicinfo(market, stock_type=ft.SecurityType.STOCK)) # \u83b7\u53d6\u80a1\u7968\u4fe1\u606f\r\nprint(quote_ctx.get_autype_list(code_list)) # \u83b7\u53d6\u590d\u6743\u56e0\u5b50\r\nprint(quote_ctx.get_market_snapshot(code_list)) # \u83b7\u53d6\u5e02\u573a\u5feb\u7167\r\nprint(quote_ctx.get_plate_list(market, ft.Plate.ALL)) # \u83b7\u53d6\u677f\u5757\u96c6\u5408\u4e0b\u7684\u5b50\u677f\u5757\u5217\u8868\r\nprint(quote_ctx.get_plate_stock(plate)) # \u83b7\u53d6\u677f\u5757\u4e0b\u7684\u80a1\u7968\u5217\u8868\r\n\r\n# \u9ad8\u9891\u6570\u636e\u63a5\u53e3\r\nquote_ctx.subscribe(code, [ft.SubType.QUOTE, ft.SubType.TICKER, ft.SubType.K_DAY, ft.SubType.ORDER_BOOK, ft.SubType.RT_DATA, ft.SubType.BROKER])\r\nprint(quote_ctx.get_stock_quote(code)) # \u83b7\u53d6\u62a5\u4ef7\r\nprint(quote_ctx.get_rt_ticker(code)) # \u83b7\u53d6\u9010\u7b14\r\nprint(quote_ctx.get_cur_kline(code, num=100, ktype=ft.KLType.K_DAY)) #\u83b7\u53d6\u5f53\u524dK\u7ebf\r\nprint(quote_ctx.get_order_book(code)) # \u83b7\u53d6\u6446\u76d8\r\nprint(quote_ctx.get_rt_data(code)) # \u83b7\u53d6\u5206\u65f6\u6570\u636e\r\nprint(quote_ctx.get_broker_queue(code)) # \u83b7\u53d6\u7ecf\u7eaa\u961f\u5217\r\n\r\n# \u505c\u6b62\u5f02\u6b65\u6570\u636e\u63a5\u6536\r\nquote_ctx.stop()\r\n\r\n# \u5173\u95ed\u5bf9\u8c61\r\nquote_ctx.close()\r\n\r\n# \u5b9e\u4f8b\u5316\u6e2f\u80a1\u4ea4\u6613\u4e0a\u4e0b\u6587\u5bf9\u8c61\r\ntrade_hk_ctx = ft.OpenHKTradeContext(host=\"127.0.0.1\", port=11111)\r\n\r\n# \u4ea4\u6613\u63a5\u53e3\u5217\u8868\r\nprint(trade_hk_ctx.unlock_trade(password='123456')) # \u89e3\u9501\u63a5\u53e3\r\nprint(trade_hk_ctx.accinfo_query(trd_env=ft.TrdEnv.SIMULATE)) # \u67e5\u8be2\u8d26\u6237\u4fe1\u606f\r\nprint(trade_hk_ctx.place_order(price=1.1, qty=2000, code=code, trd_side=ft.TrdSide.BUY, order_type=ft.OrderType.NORMAL, trd_env=ft.TrdEnv.SIMULATE)) # \u4e0b\u5355\u63a5\u53e3\r\nprint(trade_hk_ctx.order_list_query(trd_env=ft.TrdEnv.SIMULATE)) # \u67e5\u8be2\u8ba2\u5355\u5217\u8868\r\nprint(trade_hk_ctx.position_list_query(trd_env=ft.TrdEnv.SIMULATE)) # \u67e5\u8be2\u6301\u4ed3\u5217\u8868\r\n\r\ntrade_hk_ctx.close()\r\n\r\n```\r\n\r\n---\r\n\r\n### \u793a\u4f8b\u7b56\u7565\r\n\r\n- \u793a\u4f8b\u7b56\u7565\u6587\u4ef6\u4f4d\u4e8e\u76ee\u5f55: (moomoo-api\u5305\u5b89\u88c5\u76ee\u5f55)/py-moomoo-api/examples \u4e0b\uff0c\u7528\u6237\u53ef\u53c2\u8003\u5b9e\u4f8b\u7b56\u7565\u6765\u5b66\u4e60API\u7684\u4f7f\u7528\u3002\r\n\r\n---\r\n\r\n### \u8c03\u8bd5\u5f00\u5173\u548c\u63a8\u9001\u8bb0\u5f55\r\n\r\n- set_futu_debug_model\u51fd\u6570\u53ef\u4ee5\u6253\u5f00\u6216\u5173\u95ed\u8c03\u8bd5\u7ea7\u522b\u7684log\u8bb0\u5f55\u3002\r\n- \u5982\u679c\u6253\u5f00\u8bb0\u5f55\uff0c\u5219\u4f1a\u8bb0\u5f55info\u7ea7\u522b\u7684log\u5e76\u4e14\u8bb0\u5f55\u6240\u6709\u9010\u7b14\u3001\u6446\u76d8\u3001\u5238\u5546\u7ecf\u7eaa\u7684\u63a8\u9001\u8bb0\u5f55\uff0c\u4ee5\u4fbf\u4e8e\u540e\u9762\u6392\u67e5\uff0c\u6587\u4ef6\u8bb0\u5f55\u5728%appdata%(%HOME%)\\com.moomoonn.FutuOpenD\\Log\u4e0b\u9762\r\n- examples\\analysis\u4e0b\u9762\u4f1a\u6709\u5bf9\u9010\u7b14\u3001\u6446\u76d8\u3001\u5238\u5546\u7ecf\u7eaa\u7684\u63a8\u9001\u8bb0\u5f55\u7684\u5206\u6790\u811a\u672c\uff0c\u4e0e\u6211\u4eec\u8054\u7cfb\uff0c\u62ff\u5230\u539f\u59cb\u4ea4\u6613\u6240\u6570\u636e\u540e\uff0c\u53ef\u4ee5\u8f7d\u5165\u6bd4\u5bf9\uff08beta\u529f\u80fd\uff09\r\n\r\n---\r\n\r\n### \u4f7f\u7528\u987b\u77e5\r\n\r\n- python\u811a\u672c\u8fd0\u884c\u524d\uff0c\u9700\u5148\u542f\u52a8[FutuOpenD](https://www.futunn.com/download/openAPI)\u7f51\u5173\u5ba2\u6237\u7aef\r\n\r\n### API\u4e0eFutuOpenD\u7f51\u5173\u5ba2\u6237\u7aef\u7684\u67b6\u6784\r\n\r\n![image](https://futunnopen.github.io/futu-api-doc/_images/API.png)\r\n\r\n***\r\n\r\n\r\n### API\u53caFutuOpenD\u5ba2\u6237\u7aef\u4ea4\u6d41\u65b9\u5f0f\r\n\r\n* \u5bcc\u9014\u5f00\u653eAPI\u7fa4(229850364, 108534288) \r\n* \u6709\u4e00\u5b9a\u4ea4\u6613\u989d\u6216\u7279\u6b8a\u9700\u6c42\u7684\u7528\u6237\u8bf7\u5728\u5165\u7fa4\u540e\u8054\u7cfb\u7fa4\u4e3b\r\n\r\n***\r\n\r\n### \u4f7f\u7528\u8bf4\u660e\r\n\r\n* \u6709\u4efb\u4f55\u95ee\u9898\u53ef\u4ee5\u5230 issues \u5904\u63d0\u51fa\uff0c\u6211\u4eec\u4f1a\u53ca\u65f6\u8fdb\u884c\u89e3\u7b54\u3002\r\n* \u4f7f\u7528\u65b0\u7248\u672c\u65f6\u8bf7\u5148\u4ed4\u7ec6\u9605\u8bfb\u63a5\u53e3\u6587\u6863\uff0c\u5927\u90e8\u5206\u95ee\u9898\u90fd\u53ef\u4ee5\u5728\u63a5\u53e3\u6587\u6863\u4e2d\u627e\u5230\u4f60\u60f3\u8981\u7684\u7b54\u6848\u3002\r\n* \u6b22\u8fce\u5927\u5bb6\u63d0\u51fa\u5efa\u8bae\u3001\u4e5f\u53ef\u4ee5\u63d0\u51fa\u5404\u79cd\u9700\u6c42\uff0c\u6211\u4eec\u4e00\u5b9a\u4f1a\u5c3d\u91cf\u6ee1\u8db3\u5927\u5bb6\u7684\u9700\u6c42\u3002\r\n\r\n---\r\n\r\n\r\n",
"bugtrack_url": null,
"license": "Apache License 2.0",
"summary": "Moomoo Quantitative Trading API",
"version": "8.8.4808",
"project_urls": {
"Homepage": "https://github.com/MoomooOpen/py-moomoo-api"
},
"split_keywords": [
"moomoo",
"stock",
"quant",
"trading",
"api"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "4c2a9d660d17203e91696fecebcb184036fcbf9e819aed0baeaae65b16d6612d",
"md5": "1daf5bda4d1c808df73d0926ddf7cf6c",
"sha256": "ce3f81cce7f3970b6e471bdfd6591d9f3641b988c732072c35207084de5358dc"
},
"downloads": -1,
"filename": "moomoo-api-8.8.4808.tar.gz",
"has_sig": false,
"md5_digest": "1daf5bda4d1c808df73d0926ddf7cf6c",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 237456,
"upload_time": "2024-11-28T08:59:38",
"upload_time_iso_8601": "2024-11-28T08:59:38.158057Z",
"url": "https://files.pythonhosted.org/packages/4c/2a/9d660d17203e91696fecebcb184036fcbf9e819aed0baeaae65b16d6612d/moomoo-api-8.8.4808.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-11-28 08:59:38",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "MoomooOpen",
"github_project": "py-moomoo-api",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"requirements": [],
"lcname": "moomoo-api"
}