hikyuu2/hikyuu/examples/notebook/002-HowToGetStock.ipynb
2021-02-10 23:01:09 +08:00

290 lines
9.3 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{
"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",
"2021-02-10 22:54:27.023 [HKU-I] - Using SQLITE3 BaseInfoDriver (BaseInfoDriver.cpp:58)\n",
"2021-02-10 22:54:27.025 [HKU-I] - Loading market information... (StockManager.cpp:473)\n",
"2021-02-10 22:54:27.026 [HKU-I] - Loading stock type information... (StockManager.cpp:486)\n",
"2021-02-10 22:54:27.027 [HKU-I] - Loading stock information... (StockManager.cpp:422)\n",
"2021-02-10 22:54:27.130 [HKU-I] - Loading stock weight... (StockManager.cpp:503)\n",
"2021-02-10 22:54:28.440 [HKU-I] - Loading KData... (StockManager.cpp:138)\n",
"2021-02-10 22:54:28.446 [HKU-I] - Preloading all day kdata to buffer! (StockManager.cpp:161)\n",
"2021-02-10 22:54:28.468 [HKU-I] - 0.03s Loaded Data. (StockManager.cpp:149)\n",
"Wall time: 2.72 s\n"
]
}
],
"source": [
"%matplotlib inline\n",
"%time from hikyuu.interactive import *"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"1 全局获取股票对象\n",
"==========\n",
"\n",
"1.1 获取股票对象\n",
"-----------------\n",
"\n",
"通过全局管理对象 sm或使用函数 get_stock。股票标识格式“市场标识+股票代码”市场标识沪市sh深市sz。"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Stock(SH, 000001, 上证指数, 指数, 1, 1990-12-19 00:00:00, +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": [
"全部数量: 6067\n"
]
},
{
"data": {
"text/plain": [
"6067"
]
},
"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.get_stock(\"板块分类\", \"板块名称\") 获取相应板块\n",
"------------------------------------------------------------"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Stock(SH, 600175, 美都能源, A股, 0, 1999-04-08 00:00:00, +infinity)\n",
"Stock(SH, 600240, *ST华业, A股, 0, 2000-06-28 00:00:00, +infinity)\n",
"Stock(SH, 600270, 外运发展, A股, 0, 2000-12-28 00:00:00, +infinity)\n"
]
}
],
"source": [
"blk = sm.get_block(\"指数板块\", \"上证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-06-12 00:00:00, +infinity)\n",
"Stock(SZ, 002339, 积成电子, A股, 1, 2010-01-22 00:00:00, +infinity)\n",
"Stock(SZ, 000728, 国元证券, A股, 1, 1997-05-22 00:00:00, +infinity)\n",
"Stock(SZ, 000958, 东方能源, A股, 1, 1999-12-23 00:00:00, +infinity)\n",
"Stock(SZ, 000001, 平安银行, A股, 1, 1991-01-02 00:00:00, +infinity)\n",
"Stock(SH, 600601, 方正科技, A股, 1, 1990-12-19 00:00:00, +infinity)\n",
"Stock(SH, 600050, 中国联通, A股, 1, 2002-10-09 00:00:00, +infinity)\n",
"Stock(SH, 601098, 中南传媒, A股, 1, 2010-10-28 00:00:00, +infinity)\n",
"Stock(SH, 601018, 宁波港, A股, 1, 2010-09-28 00:00:00, +infinity)\n"
]
}
],
"source": [
"blk = sm.get_block(\"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-04-03 00:00:00, 0, 0, 0, 0, 0, 150, 68)\n",
"Weight(1993-05-24 00:00:00, 3.5, 1, 16, 3, 5, 26941, 17912)\n",
"Weight(1994-07-11 00:00:00, 3, 1, 5, 5, 2, 43106, 28659)\n",
"Weight(1994-09-02 00:00:00, 0, 0, 0, 0, 0, 43106, 29707)\n",
"Weight(1995-09-25 00:00:00, 2, 0, 0, 3, 0, 51728, 35721)\n",
"Weight(1996-05-27 00:00:00, 5, 0, 0, 0, 5, 103456, 71393)\n",
"Weight(1997-08-25 00:00:00, 5, 0, 0, 2, 0, 155184, 107163)\n",
"Weight(1999-10-18 00:00:00, 0, 0, 0, 6, 0, 155184, 107163)\n",
"Weight(2000-11-06 00:00:00, 0, 3, 8, 0, 0, 194582, 139312)\n",
"Weight(2002-07-23 00:00:00, 0, 0, 0, 1.5, 0, 194582, 140936)\n",
"Weight(2003-09-29 00:00:00, 0, 0, 0, 1.5, 0, 194582, 140936)\n",
"Weight(2007-06-20 00:00:00, 1, 0, 0, 0, 0, 208676, 155019)\n",
"Weight(2007-12-31 00:00:00, 0, 0, 0, 0, 0, 229341, 175682)\n",
"Weight(2008-01-21 00:00:00, 0, 0, 0, 0, 0, 229341, 175682)\n",
"Weight(2008-06-26 00:00:00, 0, 0, 0, 0, 0, 229341, 204652)\n",
"Weight(2008-06-27 00:00:00, 0, 0, 0, 0, 0, 238880, 214200)\n",
"Weight(2008-10-31 00:00:00, 3, 0, 0, 0.335, 0, 310543, 278461)\n",
"Weight(2009-06-22 00:00:00, 0, 0, 0, 0, 0, 310543, 292367)\n",
"Weight(2009-06-30 00:00:00, 0, 0, 0, 0, 0, 310543, 292376)\n",
"Weight(2009-10-15 00:00:00, 0, 0, 0, 0, 0, 310543, 292411)\n",
"Weight(2010-06-28 00:00:00, 0, 0, 0, 0, 0, 310543, 310537)\n",
"Weight(2010-09-17 00:00:00, 0, 0, 0, 0, 0, 348501, 310537)\n",
"Weight(2011-08-05 00:00:00, 0, 0, 0, 0, 0, 512335, 310536)\n",
"Weight(2011-12-31 00:00:00, 0, 0, 0, 0, 0, 512335, 310536)\n",
"Weight(2012-10-19 00:00:00, 0, 0, 0, 1, 0, 512335, 310536)\n",
"Weight(2012-12-31 00:00:00, 0, 0, 0, 0, 0, 512335, 310536)\n",
"Weight(2013-06-20 00:00:00, 6, 0, 0, 1.7, 0, 819736, 496857)\n",
"Weight(2013-11-12 00:00:00, 0, 0, 0, 0, 0, 819736, 557590)\n",
"Weight(2014-01-09 00:00:00, 0, 0, 0, 0, 0, 952075, 557590)\n",
"Weight(2014-06-12 00:00:00, 0, 0, 0, 1.6, 2, 1.14249e+06, 669106)\n",
"Weight(2014-09-01 00:00:00, 0, 0, 0, 0, 0, 1.14249e+06, 983671)\n",
"Weight(2015-04-13 00:00:00, 0, 0, 0, 1.74, 2, 1.37099e+06, 1.1804e+06)\n",
"Weight(2015-05-21 00:00:00, 0, 0, 0, 0, 0, 1.43087e+06, 1.1804e+06)\n",
"Weight(2016-05-23 00:00:00, 0, 0, 0, 0, 0, 1.43087e+06, 1.21926e+06)\n",
"Weight(2016-06-16 00:00:00, 0, 0, 0, 1.53, 2, 1.71704e+06, 1.46312e+06)\n",
"Weight(2017-01-09 00:00:00, 0, 0, 0, 0, 0, 1.71704e+06, 1.6918e+06)\n",
"Weight(2017-07-21 00:00:00, 0, 0, 0, 1.58, 0, 1.71704e+06, 1.6918e+06)\n",
"Weight(2017-12-31 00:00:00, 0, 0, 0, 0, 0, 1.71704e+06, 1.6918e+06)\n",
"Weight(2018-05-21 00:00:00, 0, 0, 0, 0, 0, 1.71704e+06, 1.71702e+06)\n",
"Weight(2018-07-12 00:00:00, 0, 0, 0, 1.36, 0, 1.71704e+06, 1.71702e+06)\n",
"Weight(2019-06-26 00:00:00, 0, 0, 0, 1.45, 0, 1.71704e+06, 1.71702e+06)\n",
"Weight(2019-06-30 00:00:00, 0, 0, 0, 0, 0, 1.71704e+06, 1.71702e+06)\n",
"Weight(2019-09-18 00:00:00, 0, 0, 0, 0, 0, 1.94059e+06, 1.94058e+06)\n",
"Weight(2020-05-28 00:00:00, 0, 0, 0, 2.18, 0, 1.94059e+06, 1.94058e+06)\n",
"Weight(2020-12-31 00:00:00, 0, 0, 0, 0, 0, 1.94059e+06, 1.94058e+06)\n"
]
}
],
"source": [
"ws = sm['sz000001'].get_weight()\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.8.3"
}
},
"nbformat": 4,
"nbformat_minor": 1
}