提交 cfe54f13 编写于 作者: Z zengbin93

example: 新增绘图

上级 483298d1
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Name: pyecharts\n",
"Version: 1.8.1\n",
"Summary: Python options, make charting easier\n",
"Home-page: https://github.com/pyecharts/pyecharts\n",
"Author: chenjiandongx\n",
"Author-email: chenjiandongx@qq.com\n",
"License: MIT\n",
"Location: c:\\users\\zengb\\anaconda3\\lib\\site-packages\n",
"Requires: simplejson, prettytable, jinja2\n",
"Required-by: \n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"系统找不到指定的路径。\n"
]
}
],
"source": [
"!pip show pyecharts \n",
"\n",
"import warnings\n",
"warnings.filterwarnings(\"ignore\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## pyecharts 可视化分析结果\n",
"---"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.5.4\n"
]
}
],
"source": [
"import sys\n",
"sys.path.insert(0, \".\")\n",
"sys.path.insert(0, \"..\")\n",
"\n",
"import czsc\n",
"print(czsc.__version__)\n",
"\n",
"from datetime import datetime\n",
"from typing import List\n",
"import traceback\n",
"import pandas as pd\n",
"import numpy as np\n",
"from tqdm import tqdm_notebook as tqdm\n",
"from czsc.analyze import KlineAnalyze\n",
"from pyecharts import options as opts\n",
"from pyecharts.commons.utils import JsCode\n",
"from pyecharts.charts import Kline, Line, Bar, Grid\n",
"\n",
"# 如果需要实盘行情,导入聚宽数据\n",
"# from czsc.data.jq import *\n",
"\n",
"# 导入 tushare 数据\n",
"from czsc.data.ts import *"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"kline = get_kline(\"300803.SZ-E\", end_date=\"20200913\", freq='30min')\n",
"ka = KlineAnalyze(kline, name=\"30分钟\", bi_mode=\"new\", max_xd_len=20, ma_params=(5, 34, 120), verbose=False)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## pyecharts 绘图\n",
"---"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def plot_kline(kline):\n",
" \n",
" x_data = [x['dt'] for x in kline]\n",
" y_data = [[x['']]]\n",
" # 基础配置\n",
" bar_opts = opts.ItemStyleOpts(\n",
" color=\"#ef232a\",\n",
" color0=\"#14b143\",\n",
" border_color=\"#ef232a\",\n",
" border_color0=\"#14b143\",\n",
" )\n",
" \n",
" mark_opts = \n",
" \n",
" \n",
" draw = Kline()\n",
" draw.add_xaxis(xaxis_data=data[\"times\"])\n",
" draw.add_yaxis()\n",
" \n",
" draw.add_yaxis(series_name=\"K线\",\n",
" y_axis=data[\"datas\"],\n",
" itemstyle_opts=opts.ItemStyleOpts(\n",
" color=\"#ef232a\",\n",
" color0=\"#14b143\",\n",
" border_color=\"#ef232a\",\n",
" border_color0=\"#14b143\",\n",
" ),\n",
" markpoint_opts=opts.MarkPointOpts(\n",
" data=[\n",
" opts.MarkPointItem(type_=\"max\", name=\"最大值\"),\n",
" opts.MarkPointItem(type_=\"min\", name=\"最小值\"),\n",
" ]\n",
" ),\n",
" markline_opts=opts.MarkLineOpts(\n",
" label_opts=opts.LabelOpts(\n",
" position=\"middle\", color=\"blue\", font_size=15\n",
" ),\n",
" data=split_data_part(),\n",
" symbol=[\"circle\", \"none\"],\n",
" ),\n",
" )\n",
" .set_series_opts(\n",
" markarea_opts=opts.MarkAreaOpts(is_silent=True, data=split_data_part())\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<czsc.analyze.KlineAnalyze at 0x175566e8d30>"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ka"
]
},
{
"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.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
......@@ -31,7 +31,15 @@
"sys.path.insert(0, \"..\")\n",
"\n",
"import czsc\n",
"print(czsc.__version__)"
"print(czsc.__version__)\n",
"\n",
"from datetime import datetime\n",
"from typing import List\n",
"import traceback\n",
"import pandas as pd\n",
"import numpy as np\n",
"from tqdm import tqdm_notebook as tqdm\n",
"from czsc.analyze import KlineAnalyze"
]
},
{
......@@ -40,18 +48,16 @@
"metadata": {},
"outputs": [],
"source": [
"from datetime import datetime\n",
"from typing import List\n",
"import traceback\n",
"import pandas as pd\n",
"import numpy as np\n",
"from tqdm import tqdm_notebook as tqdm\n",
"from czsc.analyze import KlineAnalyze"
"# 导入 Tushare 数据(推荐使用)\n",
"from czsc.data.ts import *\n",
"\n",
"# 如果是第一次使用需要设置 token\n",
"# set_token(\"trushare.pro token\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": null,
"metadata": {
"pycharm": {
"name": "#%%\n"
......@@ -63,7 +69,7 @@
"from czsc.data.jq import *\n",
"\n",
"# 如果是第一次使用需要设置 token\n",
"# set_token(\"手机号\", \"密码\")\n"
"# set_token(\"手机号\", \"密码\")"
]
},
{
......@@ -83,7 +89,7 @@
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
......@@ -93,7 +99,7 @@
" for symbol in tqdm(symbols, desc=\"缠论日线笔中枢三买选股\"):\n",
" try:\n",
" kline = get_kline(symbol=symbol, end_date=datetime.now(), freq=\"D\", count=1000)\n",
" ka = KlineAnalyze(kline, ma_params=(5, 34, 120, 233))\n",
" ka = KlineAnalyze(kline, ma_params=(5, 34, 120, 233), bi_mode=\"new\")\n",
" points = ka.bi_list[-6:]\n",
" \n",
" if len(points) == 6 and points[-1]['fx_mark'] == \"d\":\n",
......@@ -104,46 +110,11 @@
" res.append(symbol)\n",
" \n",
" except:\n",
" print(\"{} 分析失败\".format(symbol))\n",
" traceback.print_exc()\n",
" return res"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "b33033a1bccb49338a8bedfb36f9ae1b",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"HBox(children=(IntProgress(value=0, description='缠论日线笔中枢三买选股', style=ProgressStyle(description_width='initial'…"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"选股结果: ['300133.XSHE']\n"
]
}
],
"source": [
"# 在创业板中选股\n",
"symbols = get_index_stocks(\"399006.XSHE\")\n",
"selected = selector(symbols)\n",
"\n",
"print(\"选股结果:\", selected)"
]
},
{
"cell_type": "markdown",
"metadata": {},
......@@ -161,7 +132,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 4,
"metadata": {},
"outputs": [
{
......@@ -190,7 +161,7 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
......@@ -200,7 +171,7 @@
" for symbol in tqdm(symbols, desc=\"缠论选股\"):\n",
" try:\n",
" kline = get_kline(symbol=symbol, end_date=datetime.now(), freq=\"D\", count=1000)\n",
" ka = KlineAnalyze(kline, ma_params=(5, 34, 60, 250))\n",
" ka = KlineAnalyze(kline, ma_params=(5, 34, 60, 250), bi_mode=\"new\")\n",
"\n",
" if ka.ma[-1]['ma60'] >= ka.latest_price >= ka.ma[-1]['ma250']:\n",
" # print(\"{} 满足条件1:ma60 > close > ma233\".format(symbol))\n",
......@@ -218,44 +189,102 @@
" num = cross_number(low, ma_)\n",
" res.append({\"symbol\": symbol, \"cross_num\": num})\n",
" except:\n",
" print(\"{} 分析失败\".format(symbol))\n",
" traceback.print_exc()\n",
" return res"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 执行选股\n",
"---"
]
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 6,
"metadata": {
"scrolled": false
"scrolled": true
},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "8da8cca5b5b34c53b4cd3bb4f25da4f6",
"model_id": "51fcbc905a954c51b8ce864d8897821e",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"HBox(children=(IntProgress(value=0, description='缠论选股', style=ProgressStyle(description_width='initial')), HTM…"
"HBox(children=(IntProgress(value=0, description='缠论选股', max=795, style=ProgressStyle(description_width='initia…"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"..\\czsc\\data\\ts.py:175: SettingWithCopyWarning: \n",
"A value is trying to be set on a copy of a slice from a DataFrame.\n",
"Try using .loc[row_indexer,col_indexer] = value instead\n",
"\n",
"See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
" k[col] = k[col].apply(round, args=(2,))\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"300793.SZ-E 分析失败\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"Traceback (most recent call last):\n",
" File \"<ipython-input-5-14c245f5b689>\", line 7, in selector\n",
" ka = KlineAnalyze(kline, ma_params=(5, 34, 60, 250), bi_mode=\"new\")\n",
" File \"..\\czsc\\analyze.py\", line 327, in __init__\n",
" self._update_xd_list()\n",
" File \"..\\czsc\\analyze.py\", line 633, in _update_xd_list\n",
" self._xd_after_process()\n",
" File \"..\\czsc\\analyze.py\", line 627, in _xd_after_process\n",
" if (self.xd_list[-1]['fx_mark'] == 'd' and self.bi_list[-1]['bi'] < self.xd_list[-1]['xd']) \\\n",
"IndexError: list index out of range\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"选股结果: [{'symbol': '300003.XSHE', 'cross_num': 0}, {'symbol': '300223.XSHE', 'cross_num': 0}, {'symbol': '300383.XSHE', 'cross_num': 0}, {'symbol': '300567.XSHE', 'cross_num': 2}, {'symbol': '300633.XSHE', 'cross_num': 0}, {'symbol': '300661.XSHE', 'cross_num': 0}]\n"
"选股结果: [{'symbol': '300246.SZ-E', 'cross_num': 0}, {'symbol': '300206.SZ-E', 'cross_num': 0}, {'symbol': '300122.SZ-E', 'cross_num': 0}, {'symbol': '300573.SZ-E', 'cross_num': 0}, {'symbol': '300530.SZ-E', 'cross_num': 0}, {'symbol': '300529.SZ-E', 'cross_num': 0}, {'symbol': '300496.SZ-E', 'cross_num': 0}, {'symbol': '300489.SZ-E', 'cross_num': 0}, {'symbol': '300750.SZ-E', 'cross_num': 0}, {'symbol': '300699.SZ-E', 'cross_num': 0}, {'symbol': '300677.SZ-E', 'cross_num': 0}, {'symbol': '300661.SZ-E', 'cross_num': 0}, {'symbol': '300635.SZ-E', 'cross_num': 0}, {'symbol': '300624.SZ-E', 'cross_num': 0}, {'symbol': '300618.SZ-E', 'cross_num': 0}, {'symbol': '300601.SZ-E', 'cross_num': 0}, {'symbol': '300595.SZ-E', 'cross_num': 0}, {'symbol': '300454.SZ-E', 'cross_num': 0}, {'symbol': '300014.SZ-E', 'cross_num': 0}]\n"
]
}
],
"source": [
"# 在创业板中选股\n",
"symbols = get_index_stocks(\"399006.XSHE\")\n",
"# symbols = get_index_stocks(\"000001.XSHG\")\n",
"# 使用 tushare 数据在创业板综指上选股\n",
"symbols = get_index_stocks(\"399102.SZ\")\n",
"selected = selector(symbols)\n",
"\n",
"print(\"选股结果:\", selected)\n",
"# df = pd.DataFrame(selected)\n",
"# df.to_excel(\"选股结果.xlsx\", index=False)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 使用聚宽数据在创业板综指上选股\n",
"symbols = get_index_stocks(\"399102.XSHE\")\n",
"selected = selector(symbols)\n",
"\n",
"print(\"选股结果:\", selected)\n",
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册