mirror of
https://gitee.com/fasiondog/hikyuu.git
synced 2024-11-30 10:59:43 +08:00
Merge the examples project into the "hikyuu/examples/notebook" subdirectory
This commit is contained in:
parent
89ac67823b
commit
c02705f8d9
1
.gitignore
vendored
1
.gitignore
vendored
@ -20,6 +20,7 @@
|
|||||||
.pyproject
|
.pyproject
|
||||||
.vscode
|
.vscode
|
||||||
.vs
|
.vs
|
||||||
|
.ipynb_checkpoints
|
||||||
temp.txt
|
temp.txt
|
||||||
release.x64.version.h
|
release.x64.version.h
|
||||||
release.x86.version.h
|
release.x86.version.h
|
||||||
|
75
hikyuu/examples/notebook/000-Index.ipynb
Normal file
75
hikyuu/examples/notebook/000-Index.ipynb
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"<img src=\"http://hikyuu.readthedocs.io/en/latest/_images/00000-title.png\" align=\"left\">"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Hikyuu Quant Framework是一款基于C++/Python的开源量化交易研究框架,用于策略分析及回测。其核心思想基于当前成熟的系统化交易方法,将整个系统化交易策略抽象为由市场环境判断策略、系统有效条件、信号指示器、止损/止盈策略、资金管理策略、盈利目标策略、移滑价差算法七大组件,你可以分别构建这些组件的策略资产库,在实际研究中对它们自由组合来观察系统的有效性、稳定性以及单一种类策略的效果。在系统策略之上,对交易对象选择、系统策略选择、资产组合资金分配进行了进一步封装,能够灵活支持更高层级的策略组合。\n",
|
||||||
|
"\n",
|
||||||
|
"更多信息,请参见:<https://hikyuu.org>"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## 入门篇\n",
|
||||||
|
"\n",
|
||||||
|
"* [001 交互式工具示例](001-overview.ipynb?flush_cache=True)\n",
|
||||||
|
"* [002 获取股票对象](002-HowToGetStock.ipynb?flush_cache=True)\n",
|
||||||
|
"* [003 获取并绘制K线数据](003-HowToGetKDataAndDraw.ipynb?flush_cache=True)\n",
|
||||||
|
"* [004 计算并绘制技术指标](004-IndicatorOverview.ipynb?flush_cache=True)\n",
|
||||||
|
"* [005 绘制组合图形](005-Drawplot.ipynb?flush_cache=True)\n",
|
||||||
|
"* [006 TradeManager应用](006-TradeManager.ipynb?flush_cache=True)\n",
|
||||||
|
"* [007 系统策略演示](007-SystemDetails.ipynb?flush_cache=True)\n",
|
||||||
|
"* [008 序列化说明](008-Pickle.ipynb?flush_cache=True)\n",
|
||||||
|
"* [009_获取实时日线数据](009-RealData.ipynb?flush_cache=True)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## 示例\n",
|
||||||
|
"\n",
|
||||||
|
"* [Demo1](Demo/Demo1.ipynb?flush_cache=True)\n",
|
||||||
|
"* [Demo2](Demo/Demo2.ipynb?flush_cache=True)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Python 3",
|
||||||
|
"language": "python",
|
||||||
|
"name": "python3"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"codemirror_mode": {
|
||||||
|
"name": "ipython",
|
||||||
|
"version": 3
|
||||||
|
},
|
||||||
|
"file_extension": ".py",
|
||||||
|
"mimetype": "text/x-python",
|
||||||
|
"name": "python",
|
||||||
|
"nbconvert_exporter": "python",
|
||||||
|
"pygments_lexer": "ipython3",
|
||||||
|
"version": "3.7.1"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 1
|
||||||
|
}
|
339
hikyuu/examples/notebook/001-overview.ipynb
Normal file
339
hikyuu/examples/notebook/001-overview.ipynb
Normal file
File diff suppressed because one or more lines are too long
280
hikyuu/examples/notebook/002-HowToGetStock.ipynb
Normal file
280
hikyuu/examples/notebook/002-HowToGetStock.ipynb
Normal file
@ -0,0 +1,280 @@
|
|||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 1,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"std::cout are redirected to python::stdout\n",
|
||||||
|
"std::cerr are redirected to python::stderr\n",
|
||||||
|
"[2019-03-27 21:33:13.813] [info] Loading market information...\n",
|
||||||
|
"[2019-03-27 21:33:13.816] [info] Loading stock type information...\n",
|
||||||
|
"[2019-03-27 21:33:13.818] [info] Loading stock information...\n",
|
||||||
|
"[2019-03-27 21:33:18.709] [info] Loading KData...\n",
|
||||||
|
"[2019-03-27 21:33:18.720] [info] Preloading all day kdata to buffer!\n",
|
||||||
|
"[2019-03-27 21:33:27.778] [info] 9.068s Loaded Data.\n",
|
||||||
|
"Wall time: 16.6 s\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"%matplotlib inline\n",
|
||||||
|
"%time from hikyuu.interactive.interactive import *"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"1 全局获取股票对象\n",
|
||||||
|
"==========\n",
|
||||||
|
"\n",
|
||||||
|
"1.1 获取股票对象\n",
|
||||||
|
"-----------------\n",
|
||||||
|
"\n",
|
||||||
|
"通过全局管理对象 sm,或使用函数 getStock。股票标识格式“市场标识+股票代码”,市场标识:沪市sh,深市sz。"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 2,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"Stock(SH, 000001, 上证指数, 指数, 1, 1990-12-19 0:0:0, +infinity)\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"#s = getStock('sh000001')\n",
|
||||||
|
"s = sm['sh000001']\n",
|
||||||
|
"print(s)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"1.2 遍历所有股票\n",
|
||||||
|
"-----------------"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 3,
|
||||||
|
"metadata": {
|
||||||
|
"scrolled": false
|
||||||
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"全部数量: 5517\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"5517"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 3,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"i = 0\n",
|
||||||
|
"#遍历所有股票\n",
|
||||||
|
"for s in sm:\n",
|
||||||
|
" i += 1\n",
|
||||||
|
" #print(s)\n",
|
||||||
|
"print(\"全部数量:\", i)\n",
|
||||||
|
"\n",
|
||||||
|
"len(sm)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"2 通过板块(Block)遍历股票对象\n",
|
||||||
|
"================\n",
|
||||||
|
"\n",
|
||||||
|
"2.1 通过 sm.getStock(\"板块分类\", \"板块名称\") 获取相应板块\n",
|
||||||
|
"------------------------------------------------------------"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 4,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"Stock(SH, 600270, 外运发展, A股, 0, 2000-12-28 0:0:0, +infinity)\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"blk = sm.getBlock(\"指数板块\", \"上证380\")\n",
|
||||||
|
"for s in blk:\n",
|
||||||
|
" if not s.valid:\n",
|
||||||
|
" print(s)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"2.1 获取自定义板块\n",
|
||||||
|
"------------------\n",
|
||||||
|
"\n",
|
||||||
|
"自定义板块的板块分类固定为 “self”"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 5,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"Stock(SZ, 002685, 华东重机, A股, 1, 2012-6-12 0:0:0, +infinity)\n",
|
||||||
|
"Stock(SZ, 002339, 积成电子, A股, 1, 2010-1-22 0:0:0, +infinity)\n",
|
||||||
|
"Stock(SZ, 000728, 国元证券, A股, 1, 1997-5-22 0:0:0, +infinity)\n",
|
||||||
|
"Stock(SZ, 000958, 东方能源, A股, 1, 1999-12-23 0:0:0, +infinity)\n",
|
||||||
|
"Stock(SZ, 000001, 平安银行, A股, 1, 1991-1-2 0:0:0, +infinity)\n",
|
||||||
|
"Stock(SH, 600601, 方正科技, A股, 1, 1990-12-19 0:0:0, +infinity)\n",
|
||||||
|
"Stock(SH, 600050, 中国联通, A股, 1, 2002-10-9 0:0:0, +infinity)\n",
|
||||||
|
"Stock(SH, 601018, 宁波港, A股, 1, 2010-9-28 0:0:0, +infinity)\n",
|
||||||
|
"Stock(SH, 601098, 中南传媒, A股, 1, 2010-10-28 0:0:0, +infinity)\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"blk = sm.getBlock(\"self\", \"1\")\n",
|
||||||
|
"for s in blk:\n",
|
||||||
|
" print(s)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"2.2 板块信息的配置\n",
|
||||||
|
"-------------------\n",
|
||||||
|
"\n",
|
||||||
|
"板块信息在数据存放路径中 “block” 子目录下,目前采用的是钱龙的格式,你也可从钱龙相应的目录下拷贝最新的板块配置信息。\n",
|
||||||
|
"\n",
|
||||||
|
"![板块配置](images/002_01_block_config.png)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"3 查看权息信息\n",
|
||||||
|
"======="
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 6,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"Weight(1991-4-3 0:0:0, 0, 0, 0, 0, 0, 150, 68)\n",
|
||||||
|
"Weight(1993-5-24 0:0:0, 3.5, 1, 16, 3, 5, 26941, 17912)\n",
|
||||||
|
"Weight(1994-7-11 0:0:0, 3, 1, 5, 5, 2, 43106, 28659)\n",
|
||||||
|
"Weight(1994-9-2 0:0:0, 0, 0, 0, 0, 0, 43106, 29707)\n",
|
||||||
|
"Weight(1995-9-25 0:0:0, 2, 0, 0, 3, 0, 51728, 35721)\n",
|
||||||
|
"Weight(1996-5-27 0:0:0, 5, 0, 0, 0, 5, 103456, 71393)\n",
|
||||||
|
"Weight(1997-8-25 0:0:0, 5, 0, 0, 2, 0, 155184, 107163)\n",
|
||||||
|
"Weight(1999-10-18 0:0:0, 0, 0, 0, 6, 0, 155184, 107163)\n",
|
||||||
|
"Weight(2000-11-6 0:0:0, 0, 3, 8, 0, 0, 194582, 139312)\n",
|
||||||
|
"Weight(2002-7-23 0:0:0, 0, 0, 0, 1.5, 0, 194582, 140936)\n",
|
||||||
|
"Weight(2003-9-29 0:0:0, 0, 0, 0, 1.5, 0, 194582, 140936)\n",
|
||||||
|
"Weight(2007-6-20 0:0:0, 1, 0, 0, 0, 0, 208676, 155019)\n",
|
||||||
|
"Weight(2007-12-31 0:0:0, 0, 0, 0, 0, 0, 229341, 175682)\n",
|
||||||
|
"Weight(2008-1-21 0:0:0, 0, 0, 0, 0, 0, 229341, 175682)\n",
|
||||||
|
"Weight(2008-6-26 0:0:0, 0, 0, 0, 0, 0, 229341, 204652)\n",
|
||||||
|
"Weight(2008-6-27 0:0:0, 0, 0, 0, 0, 0, 238880, 214200)\n",
|
||||||
|
"Weight(2008-10-31 0:0:0, 3, 0, 0, 0.335, 0, 310543, 278461)\n",
|
||||||
|
"Weight(2009-6-22 0:0:0, 0, 0, 0, 0, 0, 310543, 292367)\n",
|
||||||
|
"Weight(2009-6-30 0:0:0, 0, 0, 0, 0, 0, 310543, 292376)\n",
|
||||||
|
"Weight(2009-10-15 0:0:0, 0, 0, 0, 0, 0, 310543, 292411)\n",
|
||||||
|
"Weight(2010-6-28 0:0:0, 0, 0, 0, 0, 0, 310543, 310537)\n",
|
||||||
|
"Weight(2010-9-17 0:0:0, 0, 0, 0, 0, 0, 348501, 310537)\n",
|
||||||
|
"Weight(2011-8-5 0:0:0, 0, 0, 0, 0, 0, 512335, 310536)\n",
|
||||||
|
"Weight(2011-12-31 0:0:0, 0, 0, 0, 0, 0, 512335, 310536)\n",
|
||||||
|
"Weight(2012-10-19 0:0:0, 0, 0, 0, 1, 0, 512335, 310536)\n",
|
||||||
|
"Weight(2012-12-31 0:0:0, 0, 0, 0, 0, 0, 512335, 310536)\n",
|
||||||
|
"Weight(2013-6-20 0:0:0, 6, 0, 0, 1.7, 0, 819736, 496857)\n",
|
||||||
|
"Weight(2013-11-12 0:0:0, 0, 0, 0, 0, 0, 819736, 557590)\n",
|
||||||
|
"Weight(2014-1-9 0:0:0, 0, 0, 0, 0, 0, 952075, 557590)\n",
|
||||||
|
"Weight(2014-6-12 0:0:0, 0, 0, 0, 1.6, 2, 1.14249e+06, 669106)\n",
|
||||||
|
"Weight(2014-9-1 0:0:0, 0, 0, 0, 0, 0, 1.14249e+06, 983671)\n",
|
||||||
|
"Weight(2015-4-13 0:0:0, 0, 0, 0, 1.74, 2, 1.37099e+06, 1.18041e+06)\n",
|
||||||
|
"Weight(2015-5-21 0:0:0, 0, 0, 0, 0, 0, 1.43087e+06, 1.18041e+06)\n",
|
||||||
|
"Weight(2016-5-23 0:0:0, 0, 0, 0, 0, 0, 1.43087e+06, 1.21927e+06)\n",
|
||||||
|
"Weight(2016-6-16 0:0:0, 0, 0, 0, 1.53, 2, 1.71704e+06, 1.46312e+06)\n",
|
||||||
|
"Weight(2017-1-9 0:0:0, 0, 0, 0, 0, 0, 1.71704e+06, 1.6918e+06)\n",
|
||||||
|
"Weight(2017-7-21 0:0:0, 0, 0, 0, 1.58, 0, 1.71704e+06, 1.6918e+06)\n",
|
||||||
|
"Weight(2017-12-31 0:0:0, 0, 0, 0, 0, 0, 1.71704e+06, 1.6918e+06)\n",
|
||||||
|
"Weight(2018-5-21 0:0:0, 0, 0, 0, 0, 0, 1.71704e+06, 1.71703e+06)\n",
|
||||||
|
"Weight(2018-7-12 0:0:0, 0, 0, 0, 1.36, 0, 1.71704e+06, 1.71703e+06)\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"ws = sm['sz000001'].getWeight()\n",
|
||||||
|
"for w in ws:\n",
|
||||||
|
" print(w)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Python 3",
|
||||||
|
"language": "python",
|
||||||
|
"name": "python3"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"codemirror_mode": {
|
||||||
|
"name": "ipython",
|
||||||
|
"version": 3
|
||||||
|
},
|
||||||
|
"file_extension": ".py",
|
||||||
|
"mimetype": "text/x-python",
|
||||||
|
"name": "python",
|
||||||
|
"nbconvert_exporter": "python",
|
||||||
|
"pygments_lexer": "ipython3",
|
||||||
|
"version": "3.7.1"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 1
|
||||||
|
}
|
380
hikyuu/examples/notebook/003-HowToGetKDataAndDraw.ipynb
Normal file
380
hikyuu/examples/notebook/003-HowToGetKDataAndDraw.ipynb
Normal file
File diff suppressed because one or more lines are too long
422
hikyuu/examples/notebook/004-IndicatorOverview.ipynb
Normal file
422
hikyuu/examples/notebook/004-IndicatorOverview.ipynb
Normal file
File diff suppressed because one or more lines are too long
354
hikyuu/examples/notebook/005-Drawplot.ipynb
Normal file
354
hikyuu/examples/notebook/005-Drawplot.ipynb
Normal file
File diff suppressed because one or more lines are too long
330
hikyuu/examples/notebook/006-TradeManager.ipynb
Normal file
330
hikyuu/examples/notebook/006-TradeManager.ipynb
Normal file
@ -0,0 +1,330 @@
|
|||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 1,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"std::cout are redirected to python::stdout\n",
|
||||||
|
"std::cerr are redirected to python::stderr\n",
|
||||||
|
"[2019-03-27 21:36:20.830] [info] Loading market information...\n",
|
||||||
|
"[2019-03-27 21:36:20.834] [info] Loading stock type information...\n",
|
||||||
|
"[2019-03-27 21:36:20.836] [info] Loading stock information...\n",
|
||||||
|
"[2019-03-27 21:36:25.799] [info] Loading KData...\n",
|
||||||
|
"[2019-03-27 21:36:25.810] [info] Preloading all day kdata to buffer!\n",
|
||||||
|
"[2019-03-27 21:36:35.496] [info] 9.69635s Loaded Data.\n",
|
||||||
|
"Wall time: 17.6 s\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"%matplotlib inline\n",
|
||||||
|
"%time from hikyuu.interactive.interactive import *"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"# 1 利用 TM 实现简单的记账本\n",
|
||||||
|
"\n",
|
||||||
|
"TradeManager对象可以理解为一个模拟的交易账户,负责交易的买/卖操作、记录交易记录以及持仓情况,也可以通过修改其买/卖操作的接口实现实盘接入。创建一个模拟交易账户,通常使用快捷创建函数 crtTM。TM对象的基本操作:\n",
|
||||||
|
"\n",
|
||||||
|
"- buy 买入\n",
|
||||||
|
"- sell 卖出\n",
|
||||||
|
"- checkin 存入现金\n",
|
||||||
|
"- checkout 取出现金\n",
|
||||||
|
"\n",
|
||||||
|
"可以利用 TM 实现简单的记账本,手工记录自己的操作情况,例如:"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 2,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"TradeManager {\n",
|
||||||
|
" params: params[precision(int): 2, reinvest(bool): 0, save_action(bool): 1, support_borrow_cash(bool): 0, support_borrow_stock(bool): 0, ],\n",
|
||||||
|
" name: SYS,\n",
|
||||||
|
" init_date: 2017-1-1 0:0:0,\n",
|
||||||
|
" init_cash: 100000.00,\n",
|
||||||
|
" firstDatetime: 2017-1-3 0:0:0,\n",
|
||||||
|
" lastDatetime: 2017-1-3 0:0:0,\n",
|
||||||
|
" TradeCostFunc: TradeCostFunc(TC_Zero, params[]),\n",
|
||||||
|
" current cash: 99089.00,\n",
|
||||||
|
" current market_value: 916.00,\n",
|
||||||
|
" current short_market_value: 0.00,\n",
|
||||||
|
" current base_cash: 100000.00,\n",
|
||||||
|
" current base_asset: 0.00,\n",
|
||||||
|
" current borrow_cash: 0.00,\n",
|
||||||
|
" current borrow_asset: 0.00,\n",
|
||||||
|
" Position: \n",
|
||||||
|
" SZ000001 平安银行 2017-1-3 0:0:0 543 100 911.00 1238.00 327.00 35.89% 0.33%\n",
|
||||||
|
" Short Position: \n",
|
||||||
|
" Borrow Stock: \n",
|
||||||
|
"}\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"#创建一个初始资金10万元,起始日期2017年1月1日的模拟账户\n",
|
||||||
|
"my_tm = crtTM(initCash=100000, datetime=Datetime(201701010000))\n",
|
||||||
|
"\n",
|
||||||
|
"#2017年1月3日以9.11的价格买入100股\n",
|
||||||
|
"td = my_tm.buy(Datetime(201701030000), sm['sz000001'], 9.11, 100)\n",
|
||||||
|
"\n",
|
||||||
|
"#查看当前资金及持仓情况\n",
|
||||||
|
"print(my_tm)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 3,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/html": [
|
||||||
|
"<div>\n",
|
||||||
|
"<style scoped>\n",
|
||||||
|
" .dataframe tbody tr th:only-of-type {\n",
|
||||||
|
" vertical-align: middle;\n",
|
||||||
|
" }\n",
|
||||||
|
"\n",
|
||||||
|
" .dataframe tbody tr th {\n",
|
||||||
|
" vertical-align: top;\n",
|
||||||
|
" }\n",
|
||||||
|
"\n",
|
||||||
|
" .dataframe thead th {\n",
|
||||||
|
" text-align: right;\n",
|
||||||
|
" }\n",
|
||||||
|
"</style>\n",
|
||||||
|
"<table border=\"1\" class=\"dataframe\">\n",
|
||||||
|
" <thead>\n",
|
||||||
|
" <tr style=\"text-align: right;\">\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th>证券名称</th>\n",
|
||||||
|
" <th>买入日期</th>\n",
|
||||||
|
" <th>已持仓天数</th>\n",
|
||||||
|
" <th>持仓数量</th>\n",
|
||||||
|
" <th>投入金额</th>\n",
|
||||||
|
" <th>当前市值</th>\n",
|
||||||
|
" <th>盈亏金额</th>\n",
|
||||||
|
" <th>盈亏比例</th>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>证券代码</th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" </thead>\n",
|
||||||
|
" <tbody>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>SZ000001</th>\n",
|
||||||
|
" <td>平安银行</td>\n",
|
||||||
|
" <td>2017-01-03</td>\n",
|
||||||
|
" <td>543</td>\n",
|
||||||
|
" <td>100</td>\n",
|
||||||
|
" <td>911.0</td>\n",
|
||||||
|
" <td>1238.0</td>\n",
|
||||||
|
" <td>327.0</td>\n",
|
||||||
|
" <td>35.894621</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" </tbody>\n",
|
||||||
|
"</table>\n",
|
||||||
|
"</div>"
|
||||||
|
],
|
||||||
|
"text/plain": [
|
||||||
|
" 证券名称 买入日期 已持仓天数 持仓数量 投入金额 当前市值 盈亏金额 盈亏比例\n",
|
||||||
|
"证券代码 \n",
|
||||||
|
"SZ000001 平安银行 2017-01-03 543 100 911.0 1238.0 327.0 35.894621"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 3,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"#转化为pandas的DataFrame显示当前持仓情况 \n",
|
||||||
|
"position = my_tm.getPositionList()\n",
|
||||||
|
"position.to_df()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 4,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"<TradeManager {\n",
|
||||||
|
" params: params[precision(int): 2, reinvest(bool): 0, save_action(bool): 1, support_borrow_cash(bool): 0, support_borrow_stock(bool): 0, ],\n",
|
||||||
|
" name: SYS,\n",
|
||||||
|
" init_date: 2017-1-1 0:0:0,\n",
|
||||||
|
" init_cash: 100000.00,\n",
|
||||||
|
" firstDatetime: 2017-1-3 0:0:0,\n",
|
||||||
|
" lastDatetime: 2017-2-21 0:0:0,\n",
|
||||||
|
" TradeCostFunc: TradeCostFunc(TC_Zero, params[]),\n",
|
||||||
|
" current cash: 100049.00,\n",
|
||||||
|
" current market_value: 0.00,\n",
|
||||||
|
" current short_market_value: 0.00,\n",
|
||||||
|
" current base_cash: 100000.00,\n",
|
||||||
|
" current base_asset: 0.00,\n",
|
||||||
|
" current borrow_cash: 0.00,\n",
|
||||||
|
" current borrow_asset: 0.00,\n",
|
||||||
|
" Position: \n",
|
||||||
|
" Short Position: \n",
|
||||||
|
" Borrow Stock: \n",
|
||||||
|
"}>"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 4,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"#2017年2月21日以9.60的价格卖出100股\n",
|
||||||
|
"td = my_tm.sell(Datetime(201702210000), sm['sz000001'], 9.60)\n",
|
||||||
|
"\n",
|
||||||
|
"my_tm"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# 2 利用 Excel 查看交易详情\n",
|
||||||
|
"\n",
|
||||||
|
"使用 tocsv 方法将 TM 的交易记录、当前持仓及已平仓详细情况分别保存为 csv 文件,以便用 Excel 查看详情。\n",
|
||||||
|
"\n",
|
||||||
|
"tocsv方法参数为一个指定的目录,目录必须以存在。其输出会在指定目录中,生成三个文件,“TM名称_交易记录.csv”、“TM名称_未平仓记录.csv”、“TM名称_已平仓记录.csv”。TM名称可在crtTM创建TM对象时指定,默认为“SYS”,如下图所示。\n",
|
||||||
|
"\n",
|
||||||
|
"<img src=\"images/008_01_tocsv.png\" align='left'>"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 5,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"#在 hikyuu_XXX.ini 文件中配置的临时路径中输出\n",
|
||||||
|
"my_tm.tocsv(sm.tmpdir())"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"使用 Excel 查看 csv,如:\n",
|
||||||
|
"\n",
|
||||||
|
"<img src=\"images/008_02_tocsv_look.png\" align=\"left\">"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# 3 使用序列化保存或重新载入已有TM对象"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 6,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"Can't open file(c:\\stock/tmp/my_trade/my_trade_record_2019-03-27.xml)!\n",
|
||||||
|
"output stream error\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"#保存至指定文件\n",
|
||||||
|
"from datetime import date\n",
|
||||||
|
"filename = \"{}/my_trade/my_trade_record_{}.xml\".format(sm.tmpdir(), date.today());\n",
|
||||||
|
"hku_save(my_tm, filename)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 7,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"Can't open file(c:\\stock/tmp/my_trade/my_trade_record_2019-03-27.xml)!\n",
|
||||||
|
"unrecognized XML syntax\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"#载入已保存的TM对象\n",
|
||||||
|
"#filename = \"{}/my_trade/my_trade_record_{}.xml\".format(sm.tmpdir(), date.today())\n",
|
||||||
|
"new_my_tm = crtTM()\n",
|
||||||
|
"hku_load(new_my_tm, filename)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"使用 hku_save 保存的对象,其格式为XML文件,可直接使用 XML 工具或浏览器查看:\n",
|
||||||
|
"\n",
|
||||||
|
"<img src=\"images/008_03_pickle.png\" align=\"left\">"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Python 3",
|
||||||
|
"language": "python",
|
||||||
|
"name": "python3"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"codemirror_mode": {
|
||||||
|
"name": "ipython",
|
||||||
|
"version": 3
|
||||||
|
},
|
||||||
|
"file_extension": ".py",
|
||||||
|
"mimetype": "text/x-python",
|
||||||
|
"name": "python",
|
||||||
|
"nbconvert_exporter": "python",
|
||||||
|
"pygments_lexer": "ipython3",
|
||||||
|
"version": "3.7.1"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 1
|
||||||
|
}
|
290
hikyuu/examples/notebook/007-SystemDetails.ipynb
Normal file
290
hikyuu/examples/notebook/007-SystemDetails.ipynb
Normal file
File diff suppressed because one or more lines are too long
46
hikyuu/examples/notebook/008-Pickle.ipynb
Normal file
46
hikyuu/examples/notebook/008-Pickle.ipynb
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"%matplotlib inline\n",
|
||||||
|
"%time from hikyuu.interactive.interactive import *"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Python [default]",
|
||||||
|
"language": "python",
|
||||||
|
"name": "python3"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"codemirror_mode": {
|
||||||
|
"name": "ipython",
|
||||||
|
"version": 3
|
||||||
|
},
|
||||||
|
"file_extension": ".py",
|
||||||
|
"mimetype": "text/x-python",
|
||||||
|
"name": "python",
|
||||||
|
"nbconvert_exporter": "python",
|
||||||
|
"pygments_lexer": "ipython3",
|
||||||
|
"version": "3.5.3"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 0
|
||||||
|
}
|
124
hikyuu/examples/notebook/009-RealData.ipynb
Normal file
124
hikyuu/examples/notebook/009-RealData.ipynb
Normal file
@ -0,0 +1,124 @@
|
|||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 1,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"Wall time: 6.67 s\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"%matplotlib inline\n",
|
||||||
|
"%time from hikyuu.interactive.interactive import *"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# 获取实时日线数据\n",
|
||||||
|
"\n",
|
||||||
|
"目前仅支持获取实时日线数据,使用函数 realtimeUpdate(source, delta=60)。其中,source支持 'sina' | 'qq' | 'tushare',默认使用 tushare。\n",
|
||||||
|
"\n",
|
||||||
|
"tushare 需安装 Python tushare 库,pip install tushare.\n",
|
||||||
|
"\n",
|
||||||
|
"**使用 sina 或 qq 时,应注意控制两次获取数据之间的间隔时长(使用参数delta,默认时长60s),以免 ip 被 sina 或 qq 列入黑名单。**"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 2,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"Wall time: 4.42 s\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"%time realtimeUpdate('tushare')"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 3,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"Wall time: 2.46 s\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"%time realtimeUpdate('sina')"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 4,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"Wall time: 20.3 s\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"%time realtimeUpdate('qq')"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Python [conda root]",
|
||||||
|
"language": "python",
|
||||||
|
"name": "conda-root-py"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"codemirror_mode": {
|
||||||
|
"name": "ipython",
|
||||||
|
"version": 3
|
||||||
|
},
|
||||||
|
"file_extension": ".py",
|
||||||
|
"mimetype": "text/x-python",
|
||||||
|
"name": "python",
|
||||||
|
"nbconvert_exporter": "python",
|
||||||
|
"pygments_lexer": "ipython3",
|
||||||
|
"version": "3.5.3"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 2
|
||||||
|
}
|
594
hikyuu/examples/notebook/Demo/Demo1.ipynb
Normal file
594
hikyuu/examples/notebook/Demo/Demo1.ipynb
Normal file
File diff suppressed because one or more lines are too long
722
hikyuu/examples/notebook/Demo/Demo2.ipynb
Normal file
722
hikyuu/examples/notebook/Demo/Demo2.ipynb
Normal file
File diff suppressed because one or more lines are too long
20
hikyuu/examples/notebook/LICENSE
Normal file
20
hikyuu/examples/notebook/LICENSE
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright (c) 2017 fasiondog
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||||
|
this software and associated documentation files (the "Software"), to deal in
|
||||||
|
the Software without restriction, including without limitation the rights to
|
||||||
|
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||||
|
the Software, and to permit persons to whom the Software is furnished to do so,
|
||||||
|
subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||||
|
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||||
|
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||||
|
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||||
|
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
1
hikyuu/examples/notebook/README.md
Normal file
1
hikyuu/examples/notebook/README.md
Normal file
@ -0,0 +1 @@
|
|||||||
|
#hikyuu_examples
|
BIN
hikyuu/examples/notebook/images/002_01_block_config.png
Normal file
BIN
hikyuu/examples/notebook/images/002_01_block_config.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 37 KiB |
BIN
hikyuu/examples/notebook/images/008_01_tocsv.png
Normal file
BIN
hikyuu/examples/notebook/images/008_01_tocsv.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 14 KiB |
BIN
hikyuu/examples/notebook/images/008_02_tocsv_look.png
Normal file
BIN
hikyuu/examples/notebook/images/008_02_tocsv_look.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 47 KiB |
BIN
hikyuu/examples/notebook/images/008_03_pickle.png
Normal file
BIN
hikyuu/examples/notebook/images/008_03_pickle.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 35 KiB |
Loading…
Reference in New Issue
Block a user