Spaces:
Runtime error
Runtime error
lizhen commited on
Commit ·
267ee3f
1
Parent(s): 03b400c
股市数据分析
Browse files- market_baostock.ipynb +179 -0
- market_tushare.ipynb +95 -0
market_baostock.ipynb
ADDED
|
@@ -0,0 +1,179 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": 71,
|
| 6 |
+
"metadata": {},
|
| 7 |
+
"outputs": [],
|
| 8 |
+
"source": [
|
| 9 |
+
"# http://baostock.com/baostock/index.php/%E9%A6%96%E9%A1%B5"
|
| 10 |
+
]
|
| 11 |
+
},
|
| 12 |
+
{
|
| 13 |
+
"cell_type": "code",
|
| 14 |
+
"execution_count": 72,
|
| 15 |
+
"metadata": {},
|
| 16 |
+
"outputs": [
|
| 17 |
+
{
|
| 18 |
+
"name": "stdout",
|
| 19 |
+
"output_type": "stream",
|
| 20 |
+
"text": [
|
| 21 |
+
"login success!\n"
|
| 22 |
+
]
|
| 23 |
+
}
|
| 24 |
+
],
|
| 25 |
+
"source": [
|
| 26 |
+
"import baostock as bs\n",
|
| 27 |
+
"import pandas as pd\n",
|
| 28 |
+
"\n",
|
| 29 |
+
"#### 登陆系统 ####\n",
|
| 30 |
+
"lg = bs.login()"
|
| 31 |
+
]
|
| 32 |
+
},
|
| 33 |
+
{
|
| 34 |
+
"cell_type": "code",
|
| 35 |
+
"execution_count": 73,
|
| 36 |
+
"metadata": {},
|
| 37 |
+
"outputs": [
|
| 38 |
+
{
|
| 39 |
+
"name": "stdout",
|
| 40 |
+
"output_type": "stream",
|
| 41 |
+
"text": [
|
| 42 |
+
"2023-05-03 2023-05-11\n"
|
| 43 |
+
]
|
| 44 |
+
}
|
| 45 |
+
],
|
| 46 |
+
"source": [
|
| 47 |
+
"from datetime import date , datetime , timedelta\n",
|
| 48 |
+
"\n",
|
| 49 |
+
"end_date = datetime.now() + timedelta(days=1)\n",
|
| 50 |
+
"begin_date = datetime.now() - timedelta(days=7)\n",
|
| 51 |
+
"\n",
|
| 52 |
+
"end_date = end_date.strftime(\"%Y-%m-%d\")\n",
|
| 53 |
+
"begin_date = begin_date.strftime(\"%Y-%m-%d\")\n",
|
| 54 |
+
"\n",
|
| 55 |
+
"\n",
|
| 56 |
+
"print(begin_date, end_date)"
|
| 57 |
+
]
|
| 58 |
+
},
|
| 59 |
+
{
|
| 60 |
+
"cell_type": "code",
|
| 61 |
+
"execution_count": 74,
|
| 62 |
+
"metadata": {},
|
| 63 |
+
"outputs": [],
|
| 64 |
+
"source": [
|
| 65 |
+
"from datetime import date\n",
|
| 66 |
+
"\n",
|
| 67 |
+
"#### 获取历史K线数据 ####\n",
|
| 68 |
+
"# 详细指标参数,参见“历史行情指标参数”章节\n",
|
| 69 |
+
"rs = bs.query_history_k_data_plus(\"sh.688027\",\n",
|
| 70 |
+
" \"date,code,open,high,low,close,preclose,volume,amount,adjustflag,turn,tradestatus,pctChg,peTTM,pbMRQ,psTTM,pcfNcfTTM,isST\",\n",
|
| 71 |
+
" start_date=begin_date, end_date=end_date, \n",
|
| 72 |
+
" frequency=\"d\", adjustflag=\"3\") #frequency=\"d\"取日k线,adjustflag=\"3\"默认不复权"
|
| 73 |
+
]
|
| 74 |
+
},
|
| 75 |
+
{
|
| 76 |
+
"cell_type": "code",
|
| 77 |
+
"execution_count": 75,
|
| 78 |
+
"metadata": {},
|
| 79 |
+
"outputs": [],
|
| 80 |
+
"source": [
|
| 81 |
+
"\n",
|
| 82 |
+
"#### 打印结果集 ####\n",
|
| 83 |
+
"data_list = []\n",
|
| 84 |
+
"while (rs.error_code == '0') & rs.next():\n",
|
| 85 |
+
" # 获取一条记录,将记录合并在一起\n",
|
| 86 |
+
" data_list.append(rs.get_row_data())\n",
|
| 87 |
+
"result = pd.DataFrame(data_list, columns=rs.fields)"
|
| 88 |
+
]
|
| 89 |
+
},
|
| 90 |
+
{
|
| 91 |
+
"cell_type": "code",
|
| 92 |
+
"execution_count": 76,
|
| 93 |
+
"metadata": {},
|
| 94 |
+
"outputs": [
|
| 95 |
+
{
|
| 96 |
+
"name": "stdout",
|
| 97 |
+
"output_type": "stream",
|
| 98 |
+
"text": [
|
| 99 |
+
" date code open high low close preclose \n",
|
| 100 |
+
"0 2023-05-04 sh.688027 152.9600 155.0000 146.5800 149.4000 154.3000 \\\n",
|
| 101 |
+
"1 2023-05-05 sh.688027 148.1700 150.7000 144.6000 147.0000 149.4000 \n",
|
| 102 |
+
"2 2023-05-08 sh.688027 146.0000 166.0000 146.0000 164.7500 147.0000 \n",
|
| 103 |
+
"3 2023-05-09 sh.688027 164.7500 167.7700 158.4500 159.1000 164.7500 \n",
|
| 104 |
+
"\n",
|
| 105 |
+
" volume amount adjustflag turn tradestatus pctChg \n",
|
| 106 |
+
"0 1254321 186963777.1300 3 2.670400 1 -3.175600 \\\n",
|
| 107 |
+
"1 808390 118881650.0400 3 1.721000 1 -1.606400 \n",
|
| 108 |
+
"2 2704449 426179310.0000 3 5.757700 1 12.074800 \n",
|
| 109 |
+
"3 1778358 287712315.1700 3 3.786100 1 -3.429400 \n",
|
| 110 |
+
"\n",
|
| 111 |
+
" peTTM pbMRQ psTTM pcfNcfTTM isST \n",
|
| 112 |
+
"0 -144.716648 7.394363 77.147007 408.064550 0 \n",
|
| 113 |
+
"1 -142.391882 7.275578 75.907697 401.509296 0 \n",
|
| 114 |
+
"2 -159.585460 8.154091 85.073423 449.990861 0 \n",
|
| 115 |
+
"3 -154.112575 7.874452 82.155882 434.558701 0 \n"
|
| 116 |
+
]
|
| 117 |
+
}
|
| 118 |
+
],
|
| 119 |
+
"source": [
|
| 120 |
+
"\n",
|
| 121 |
+
"#### 结果集输出到csv文件 ####\n",
|
| 122 |
+
"result.to_csv(\"D:/history_k_data.csv\", encoding=\"gbk\", index=False)\n",
|
| 123 |
+
"print(result)"
|
| 124 |
+
]
|
| 125 |
+
},
|
| 126 |
+
{
|
| 127 |
+
"cell_type": "code",
|
| 128 |
+
"execution_count": 77,
|
| 129 |
+
"metadata": {},
|
| 130 |
+
"outputs": [
|
| 131 |
+
{
|
| 132 |
+
"name": "stdout",
|
| 133 |
+
"output_type": "stream",
|
| 134 |
+
"text": [
|
| 135 |
+
"logout success!\n",
|
| 136 |
+
"logout failed!\n"
|
| 137 |
+
]
|
| 138 |
+
},
|
| 139 |
+
{
|
| 140 |
+
"data": {
|
| 141 |
+
"text/plain": [
|
| 142 |
+
"<baostock.data.resultset.ResultData at 0x1ed19353110>"
|
| 143 |
+
]
|
| 144 |
+
},
|
| 145 |
+
"execution_count": 77,
|
| 146 |
+
"metadata": {},
|
| 147 |
+
"output_type": "execute_result"
|
| 148 |
+
}
|
| 149 |
+
],
|
| 150 |
+
"source": [
|
| 151 |
+
"#### 登出系统 ####\n",
|
| 152 |
+
"bs.logout()#### 登出系统 ####\n",
|
| 153 |
+
"bs.logout()"
|
| 154 |
+
]
|
| 155 |
+
}
|
| 156 |
+
],
|
| 157 |
+
"metadata": {
|
| 158 |
+
"kernelspec": {
|
| 159 |
+
"display_name": "Python 3",
|
| 160 |
+
"language": "python",
|
| 161 |
+
"name": "python3"
|
| 162 |
+
},
|
| 163 |
+
"language_info": {
|
| 164 |
+
"codemirror_mode": {
|
| 165 |
+
"name": "ipython",
|
| 166 |
+
"version": 3
|
| 167 |
+
},
|
| 168 |
+
"file_extension": ".py",
|
| 169 |
+
"mimetype": "text/x-python",
|
| 170 |
+
"name": "python",
|
| 171 |
+
"nbconvert_exporter": "python",
|
| 172 |
+
"pygments_lexer": "ipython3",
|
| 173 |
+
"version": "3.11.3"
|
| 174 |
+
},
|
| 175 |
+
"orig_nbformat": 4
|
| 176 |
+
},
|
| 177 |
+
"nbformat": 4,
|
| 178 |
+
"nbformat_minor": 2
|
| 179 |
+
}
|
market_tushare.ipynb
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": 3,
|
| 6 |
+
"metadata": {},
|
| 7 |
+
"outputs": [],
|
| 8 |
+
"source": [
|
| 9 |
+
"# https://tushare.pro/document/1?doc_id=8\n",
|
| 10 |
+
"# pip install tushare --upgrade"
|
| 11 |
+
]
|
| 12 |
+
},
|
| 13 |
+
{
|
| 14 |
+
"cell_type": "code",
|
| 15 |
+
"execution_count": 4,
|
| 16 |
+
"metadata": {},
|
| 17 |
+
"outputs": [
|
| 18 |
+
{
|
| 19 |
+
"name": "stdout",
|
| 20 |
+
"output_type": "stream",
|
| 21 |
+
"text": [
|
| 22 |
+
"1.2.89\n"
|
| 23 |
+
]
|
| 24 |
+
}
|
| 25 |
+
],
|
| 26 |
+
"source": [
|
| 27 |
+
"import tushare as ts\n",
|
| 28 |
+
"\n",
|
| 29 |
+
"print(ts.__version__)"
|
| 30 |
+
]
|
| 31 |
+
},
|
| 32 |
+
{
|
| 33 |
+
"cell_type": "code",
|
| 34 |
+
"execution_count": 5,
|
| 35 |
+
"metadata": {},
|
| 36 |
+
"outputs": [],
|
| 37 |
+
"source": [
|
| 38 |
+
"ts.set_token('f09dfd341746eee390788f2f34db5d1e5150689b2310697e5ee73d7e')"
|
| 39 |
+
]
|
| 40 |
+
},
|
| 41 |
+
{
|
| 42 |
+
"cell_type": "code",
|
| 43 |
+
"execution_count": 6,
|
| 44 |
+
"metadata": {},
|
| 45 |
+
"outputs": [],
|
| 46 |
+
"source": [
|
| 47 |
+
"pro = ts.pro_api()"
|
| 48 |
+
]
|
| 49 |
+
},
|
| 50 |
+
{
|
| 51 |
+
"cell_type": "code",
|
| 52 |
+
"execution_count": 7,
|
| 53 |
+
"metadata": {},
|
| 54 |
+
"outputs": [
|
| 55 |
+
{
|
| 56 |
+
"ename": "Exception",
|
| 57 |
+
"evalue": "抱歉,您没有访问该接口的权限,权限的具体详情访问:https://tushare.pro/document/1?doc_id=108。",
|
| 58 |
+
"output_type": "error",
|
| 59 |
+
"traceback": [
|
| 60 |
+
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
|
| 61 |
+
"\u001b[1;31mException\u001b[0m Traceback (most recent call last)",
|
| 62 |
+
"Cell \u001b[1;32mIn[7], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m df \u001b[39m=\u001b[39m pro\u001b[39m.\u001b[39;49mtrade_cal(exchange\u001b[39m=\u001b[39;49m\u001b[39m'\u001b[39;49m\u001b[39m'\u001b[39;49m, start_date\u001b[39m=\u001b[39;49m\u001b[39m'\u001b[39;49m\u001b[39m20230101\u001b[39;49m\u001b[39m'\u001b[39;49m, end_date\u001b[39m=\u001b[39;49m\u001b[39m'\u001b[39;49m\u001b[39m20230510\u001b[39;49m\u001b[39m'\u001b[39;49m, fields\u001b[39m=\u001b[39;49m\u001b[39m'\u001b[39;49m\u001b[39mexchange,cal_date,is_open,pretrade_date\u001b[39;49m\u001b[39m'\u001b[39;49m, is_open\u001b[39m=\u001b[39;49m\u001b[39m'\u001b[39;49m\u001b[39m0\u001b[39;49m\u001b[39m'\u001b[39;49m)\n",
|
| 63 |
+
"File \u001b[1;32m~\\AppData\\Roaming\\Python\\Python311\\site-packages\\tushare\\pro\\client.py:45\u001b[0m, in \u001b[0;36mDataApi.query\u001b[1;34m(self, api_name, fields, **kwargs)\u001b[0m\n\u001b[0;32m 43\u001b[0m result \u001b[39m=\u001b[39m json\u001b[39m.\u001b[39mloads(res\u001b[39m.\u001b[39mtext)\n\u001b[0;32m 44\u001b[0m \u001b[39mif\u001b[39;00m result[\u001b[39m'\u001b[39m\u001b[39mcode\u001b[39m\u001b[39m'\u001b[39m] \u001b[39m!=\u001b[39m \u001b[39m0\u001b[39m:\n\u001b[1;32m---> 45\u001b[0m \u001b[39mraise\u001b[39;00m \u001b[39mException\u001b[39;00m(result[\u001b[39m'\u001b[39m\u001b[39mmsg\u001b[39m\u001b[39m'\u001b[39m])\n\u001b[0;32m 46\u001b[0m data \u001b[39m=\u001b[39m result[\u001b[39m'\u001b[39m\u001b[39mdata\u001b[39m\u001b[39m'\u001b[39m]\n\u001b[0;32m 47\u001b[0m columns \u001b[39m=\u001b[39m data[\u001b[39m'\u001b[39m\u001b[39mfields\u001b[39m\u001b[39m'\u001b[39m]\n",
|
| 64 |
+
"\u001b[1;31mException\u001b[0m: 抱歉,您没有访问该接口的权限,权限的具体详情访问:https://tushare.pro/document/1?doc_id=108。"
|
| 65 |
+
]
|
| 66 |
+
}
|
| 67 |
+
],
|
| 68 |
+
"source": [
|
| 69 |
+
"df = pro.trade_cal(exchange='', start_date='20230101', end_date='20230510', fields='exchange,cal_date,is_open,pretrade_date', is_open='0')"
|
| 70 |
+
]
|
| 71 |
+
}
|
| 72 |
+
],
|
| 73 |
+
"metadata": {
|
| 74 |
+
"kernelspec": {
|
| 75 |
+
"display_name": "Python 3",
|
| 76 |
+
"language": "python",
|
| 77 |
+
"name": "python3"
|
| 78 |
+
},
|
| 79 |
+
"language_info": {
|
| 80 |
+
"codemirror_mode": {
|
| 81 |
+
"name": "ipython",
|
| 82 |
+
"version": 3
|
| 83 |
+
},
|
| 84 |
+
"file_extension": ".py",
|
| 85 |
+
"mimetype": "text/x-python",
|
| 86 |
+
"name": "python",
|
| 87 |
+
"nbconvert_exporter": "python",
|
| 88 |
+
"pygments_lexer": "ipython3",
|
| 89 |
+
"version": "3.11.3"
|
| 90 |
+
},
|
| 91 |
+
"orig_nbformat": 4
|
| 92 |
+
},
|
| 93 |
+
"nbformat": 4,
|
| 94 |
+
"nbformat_minor": 2
|
| 95 |
+
}
|