Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
zengbin93
czsc
提交
44225826
C
czsc
项目概览
zengbin93
/
czsc
通知
23
Star
2
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
4
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
C
czsc
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
4
Issue
4
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
44225826
编写于
8月 15, 2020
作者:
Z
zengbin93
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
0.5.3 删除pyecharts绘制K线
上级
e3dcac10
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
17 addition
and
274 deletion
+17
-274
czsc/analyze.py
czsc/analyze.py
+17
-20
czsc/utils.py
czsc/utils.py
+0
-254
未找到文件。
czsc/analyze.py
浏览文件 @
44225826
...
...
@@ -11,7 +11,7 @@ except ImportError:
import
pandas
as
pd
import
numpy
as
np
from
datetime
import
datetime
from
czsc.utils
import
plot_ka
,
plot_kline
from
czsc.utils
import
plot_ka
def
find_zs
(
points
):
...
...
@@ -287,7 +287,8 @@ class KlineAnalyze:
"fx_mark"
:
"g"
,
"fx"
:
k2
[
'high'
],
"fx_high"
:
k2
[
'high'
],
"fx_low"
:
max
(
k1
[
'low'
],
k3
[
'low'
]),
# "fx_low": min(k1['low'], k3['low']),
"fx_low"
:
k2
[
'low'
],
}
self
.
fx_list
.
append
(
fx
)
...
...
@@ -298,7 +299,8 @@ class KlineAnalyze:
"dt"
:
k2
[
'dt'
],
"fx_mark"
:
"d"
,
"fx"
:
k2
[
'low'
],
"fx_high"
:
min
(
k1
[
'high'
],
k2
[
'high'
]),
# "fx_high": max(k1['high'], k2['high']),
"fx_high"
:
k2
[
'high'
],
"fx_low"
:
k2
[
'low'
],
}
self
.
fx_list
.
append
(
fx
)
...
...
@@ -366,8 +368,8 @@ class KlineAnalyze:
kn_inside
=
[
x
for
x
in
right_kn
if
last_bi
[
'dt'
]
<=
x
[
'dt'
]
<=
bi
[
'dt'
]]
if
len
(
kn_inside
)
>=
self
.
min_bi_k
:
# 确保相邻两个顶底之间不存在包含关系
if
(
last_bi
[
'fx_mark'
]
==
'g'
and
bi
[
'fx_
high
'
]
<
last_bi
[
'fx_low'
])
or
\
(
last_bi
[
'fx_mark'
]
==
'd'
and
bi
[
'fx_
low
'
]
>
last_bi
[
'fx_high'
]):
if
(
last_bi
[
'fx_mark'
]
==
'g'
and
bi
[
'fx_
low
'
]
<
last_bi
[
'fx_low'
])
or
\
(
last_bi
[
'fx_mark'
]
==
'd'
and
bi
[
'fx_
high
'
]
>
last_bi
[
'fx_high'
]):
if
self
.
verbose
:
print
(
"新增笔标记:{}"
.
format
(
bi
))
self
.
bi_list
.
append
(
bi
)
...
...
@@ -544,16 +546,24 @@ class KlineAnalyze:
if
self
.
verbose
:
print
(
"更新结束
\n\n
"
)
def
to_df
(
self
,
ma_params
=
(
5
,
20
),
use_macd
=
False
,
max_count
=
1000
):
def
to_df
(
self
,
ma_params
=
(
5
,
20
),
use_macd
=
False
,
max_count
=
1000
,
mode
=
"raw"
):
"""整理成 df 输出
:param ma_params: tuple of int
均线系统参数
:param use_macd: bool
:param max_count: int
:param mode: str
使用K线类型, raw = 原始K线,new = 去除包含关系的K线
:return: pd.DataFrame
"""
bars
=
self
.
kline_raw
[
-
max_count
:]
if
mode
==
"raw"
:
bars
=
self
.
kline_raw
[
-
max_count
:]
elif
mode
==
"new"
:
bars
=
self
.
kline_raw
[
-
max_count
:]
else
:
raise
ValueError
fx_list
=
{
x
[
"dt"
]:
{
"fx_mark"
:
x
[
"fx_mark"
],
"fx"
:
x
[
'fx'
]}
for
x
in
self
.
fx_list
[
-
(
max_count
//
2
):]}
bi_list
=
{
x
[
"dt"
]:
{
"fx_mark"
:
x
[
"fx_mark"
],
"bi"
:
x
[
'bi'
]}
for
x
in
self
.
bi_list
[
-
(
max_count
//
4
):]}
xd_list
=
{
x
[
"dt"
]:
{
"fx_mark"
:
x
[
"fx_mark"
],
"xd"
:
x
[
'xd'
]}
for
x
in
self
.
xd_list
[
-
(
max_count
//
8
):]}
...
...
@@ -584,19 +594,6 @@ class KlineAnalyze:
df
.
loc
[:,
"macd"
]
=
diff
return
df
def
to_html
(
self
,
file_html
=
"kline.html"
,
width
=
"1400px"
,
height
=
"680px"
):
"""保存成 html
:param file_html: str
html文件名
:param width: str
页面宽度
:param height: str
页面高度
:return:
"""
plot_kline
(
self
,
file_html
=
file_html
,
width
=
width
,
height
=
height
)
def
to_image
(
self
,
file_image
,
mav
=
(
5
,
20
,
120
,
250
),
max_k_count
=
1000
,
dpi
=
50
):
"""保存成图片
...
...
czsc/utils.py
浏览文件 @
44225826
# coding: utf-8
import
pandas
as
pd
from
pyecharts
import
options
as
opts
from
pyecharts.commons.utils
import
JsCode
from
pyecharts.charts
import
Kline
,
Line
,
Bar
,
Grid
,
Scatter
import
mplfinance
as
mpf
import
matplotlib
as
mpl
import
matplotlib.pyplot
as
plt
def
plot_kline
(
ka
,
bs
=
None
,
file_html
=
"kline.html"
,
width
=
"1400px"
,
height
=
"680px"
):
"""
:param ka: KlineAnalyze
:param bs: pd.DataFrame
买卖点,包含三个字段 ["操作提示", "交易时间", "交易价格"]
:param file_html: str
:param width: str
:param height: str
:return: None
"""
df
=
ka
.
to_df
(
use_macd
=
True
,
ma_params
=
(
5
,
20
,))
x
=
df
.
dt
.
to_list
()
title
=
"%s | %s 至 %s"
%
(
ka
.
symbol
,
ka
.
start_dt
,
ka
.
end_dt
)
kline
=
(
Kline
()
.
add_xaxis
(
xaxis_data
=
x
)
.
add_yaxis
(
series_name
=
""
,
y_axis
=
df
[[
'open'
,
'close'
,
'low'
,
'high'
]].
values
.
tolist
(),
itemstyle_opts
=
opts
.
ItemStyleOpts
(
color
=
"#ef232a"
,
color0
=
"#14b143"
,
border_color
=
"#ef232a"
,
border_color0
=
"#14b143"
,
),
)
.
set_series_opts
(
markarea_opts
=
opts
.
MarkAreaOpts
(
is_silent
=
True
)
)
.
set_global_opts
(
title_opts
=
opts
.
TitleOpts
(
title
=
title
,
pos_left
=
"0"
),
xaxis_opts
=
opts
.
AxisOpts
(
type_
=
"category"
,
is_scale
=
True
,
boundary_gap
=
False
,
axisline_opts
=
opts
.
AxisLineOpts
(
is_on_zero
=
False
),
splitline_opts
=
opts
.
SplitLineOpts
(
is_show
=
False
),
split_number
=
20
,
min_
=
"dataMin"
,
max_
=
"dataMax"
,
),
yaxis_opts
=
opts
.
AxisOpts
(
is_scale
=
True
,
splitline_opts
=
opts
.
SplitLineOpts
(
is_show
=
True
),
axislabel_opts
=
opts
.
LabelOpts
(
is_show
=
True
,
position
=
"inside"
)
),
tooltip_opts
=
opts
.
TooltipOpts
(
trigger
=
"axis"
,
axis_pointer_type
=
"line"
),
datazoom_opts
=
[
opts
.
DataZoomOpts
(
is_show
=
False
,
type_
=
"inside"
,
xaxis_index
=
[
0
,
0
],
range_end
=
100
),
opts
.
DataZoomOpts
(
is_show
=
True
,
xaxis_index
=
[
0
,
1
],
pos_top
=
"96%"
,
range_end
=
100
),
opts
.
DataZoomOpts
(
is_show
=
False
,
xaxis_index
=
[
0
,
2
],
range_end
=
100
),
],
# 三个图的 axis 连在一块
axispointer_opts
=
opts
.
AxisPointerOpts
(
is_show
=
True
,
link
=
[{
"xAxisIndex"
:
"all"
}],
label
=
opts
.
LabelOpts
(
background_color
=
"#777"
),
),
)
)
kline_line
=
(
Line
()
.
add_xaxis
(
xaxis_data
=
x
)
.
add_yaxis
(
series_name
=
"笔"
,
y_axis
=
df
.
bi
.
tolist
(),
is_smooth
=
False
,
is_connect_nones
=
True
,
symbol
=
'diamond'
,
symbol_size
=
8
,
linestyle_opts
=
opts
.
LineStyleOpts
(
opacity
=
1
,
type_
=
'dotted'
,
width
=
2
),
label_opts
=
opts
.
LabelOpts
(
is_show
=
False
),
)
.
add_yaxis
(
series_name
=
"线段"
,
y_axis
=
df
.
xd
.
tolist
(),
is_smooth
=
False
,
is_connect_nones
=
True
,
symbol
=
'triangle'
,
symbol_size
=
12
,
linestyle_opts
=
opts
.
LineStyleOpts
(
opacity
=
1
,
type_
=
'solid'
,
width
=
2
),
label_opts
=
opts
.
LabelOpts
(
is_show
=
True
,
position
=
'right'
),
)
.
set_global_opts
(
xaxis_opts
=
opts
.
AxisOpts
(
type_
=
"category"
,
grid_index
=
1
,
axislabel_opts
=
opts
.
LabelOpts
(
is_show
=
False
),
),
yaxis_opts
=
opts
.
AxisOpts
(
grid_index
=
1
,
split_number
=
3
,
axisline_opts
=
opts
.
AxisLineOpts
(
is_on_zero
=
False
),
axistick_opts
=
opts
.
AxisTickOpts
(
is_show
=
False
),
splitline_opts
=
opts
.
SplitLineOpts
(
is_show
=
False
),
axislabel_opts
=
opts
.
LabelOpts
(
is_show
=
True
,
position
=
"inside"
),
),
)
)
# Overlap Kline + Line
overlap_kline_line
=
kline
.
overlap
(
kline_line
)
if
isinstance
(
bs
,
pd
.
DataFrame
)
and
len
(
bs
)
>
0
:
c
=
(
Scatter
()
.
add_xaxis
(
bs
[
'交易时间'
].
to_list
())
.
add_yaxis
(
"买卖点"
,
bs
[
'交易价格'
].
to_list
(),
label_opts
=
opts
.
LabelOpts
(
is_show
=
True
,
position
=
"left"
,
formatter
=
JsCode
(
"function(params){return bsName[params.dataIndex][0];}"
)
),
))
overlap_kline_line
=
overlap_kline_line
.
overlap
(
c
)
# draw volume
bar_1
=
(
Bar
()
.
add_xaxis
(
xaxis_data
=
x
)
.
add_yaxis
(
series_name
=
"Volumn"
,
yaxis_data
=
df
.
vol
.
tolist
(),
xaxis_index
=
1
,
yaxis_index
=
1
,
label_opts
=
opts
.
LabelOpts
(
is_show
=
False
),
itemstyle_opts
=
opts
.
ItemStyleOpts
(
color
=
JsCode
(
"""
function(params) {
var colorList;
if (barData[params.dataIndex][1] > barData[params.dataIndex][0]) {
colorList = '#ef232a';
} else {
colorList = '#14b143';
}
return colorList;
}
"""
)
),
)
.
set_global_opts
(
xaxis_opts
=
opts
.
AxisOpts
(
type_
=
"category"
,
grid_index
=
1
,
axislabel_opts
=
opts
.
LabelOpts
(
is_show
=
False
),
),
yaxis_opts
=
opts
.
AxisOpts
(
axislabel_opts
=
opts
.
LabelOpts
(
is_show
=
True
,
position
=
'inside'
)
),
legend_opts
=
opts
.
LegendOpts
(
is_show
=
False
),
)
)
# Bar-2 (Overlap Bar + Line)
bar_2
=
(
Bar
()
.
add_xaxis
(
xaxis_data
=
x
)
.
add_yaxis
(
series_name
=
"MACD"
,
yaxis_data
=
df
.
macd
.
tolist
(),
xaxis_index
=
2
,
yaxis_index
=
2
,
label_opts
=
opts
.
LabelOpts
(
is_show
=
False
),
itemstyle_opts
=
opts
.
ItemStyleOpts
(
color
=
JsCode
(
"""
function(params) {
var colorList;
if (params.data >= 0) {
colorList = '#ef232a';
} else {
colorList = '#14b143';
}
return colorList;
}
"""
)
),
)
.
set_global_opts
(
xaxis_opts
=
opts
.
AxisOpts
(
type_
=
"category"
,
grid_index
=
2
,
axislabel_opts
=
opts
.
LabelOpts
(
is_show
=
False
),
),
yaxis_opts
=
opts
.
AxisOpts
(
grid_index
=
2
,
split_number
=
4
,
axisline_opts
=
opts
.
AxisLineOpts
(
is_on_zero
=
False
),
axistick_opts
=
opts
.
AxisTickOpts
(
is_show
=
False
),
splitline_opts
=
opts
.
SplitLineOpts
(
is_show
=
False
),
axislabel_opts
=
opts
.
LabelOpts
(
is_show
=
True
,
position
=
"inside"
),
),
legend_opts
=
opts
.
LegendOpts
(
is_show
=
False
),
)
)
line_2
=
(
Line
()
.
add_xaxis
(
xaxis_data
=
x
)
.
add_yaxis
(
series_name
=
"DIF"
,
y_axis
=
df
[
'diff'
].
tolist
(),
xaxis_index
=
2
,
yaxis_index
=
2
,
label_opts
=
opts
.
LabelOpts
(
is_show
=
False
),
)
.
add_yaxis
(
series_name
=
"DEA"
,
y_axis
=
df
[
'dea'
].
tolist
(),
xaxis_index
=
2
,
yaxis_index
=
2
,
label_opts
=
opts
.
LabelOpts
(
is_show
=
False
),
)
.
set_global_opts
(
legend_opts
=
opts
.
LegendOpts
(
is_show
=
False
))
)
# draw MACD
overlap_bar_line
=
bar_2
.
overlap
(
line_2
)
# 最后的 Grid
grid_chart
=
Grid
(
init_opts
=
opts
.
InitOpts
(
width
=
width
,
height
=
height
,
page_title
=
title
))
grid_chart
.
add_js_funcs
(
"var barData = {}"
.
format
(
df
[[
'open'
,
'close'
,
'low'
,
'high'
]].
values
.
tolist
()))
if
isinstance
(
bs
,
pd
.
DataFrame
)
and
len
(
bs
)
>
0
:
grid_chart
.
add_js_funcs
(
"var bsName = {}"
.
format
(
bs
[[
"操作提示"
,
"交易价格"
]].
values
.
tolist
()))
grid_chart
.
add
(
overlap_kline_line
,
grid_opts
=
opts
.
GridOpts
(
pos_left
=
"3%"
,
pos_right
=
"1%"
,
height
=
"60%"
),
)
grid_chart
.
add
(
bar_1
,
grid_opts
=
opts
.
GridOpts
(
pos_left
=
"3%"
,
pos_right
=
"1%"
,
pos_top
=
"71%"
,
height
=
"10%"
),
)
grid_chart
.
add
(
overlap_bar_line
,
grid_opts
=
opts
.
GridOpts
(
pos_left
=
"3%"
,
pos_right
=
"1%"
,
pos_top
=
"82%"
,
height
=
"14%"
),
)
grid_chart
.
render
(
path
=
file_html
)
def
plot_ka
(
ka
,
file_image
,
mav
=
(
5
,
20
,
120
,
250
),
max_k_count
=
1000
,
dpi
=
50
):
"""绘制 ka,保存到 file_image"""
df
=
ka
.
to_df
(
use_macd
=
True
,
ma_params
=
(
5
,
20
,))
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录