Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
zengbin93
czsc
提交
32edeb88
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 搜索 >>
提交
32edeb88
编写于
8月 02, 2019
作者:
Z
zengbin93
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add macd, boll
上级
eeaf93ad
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
117 addition
and
19 deletion
+117
-19
chan/utils.py
chan/utils.py
+85
-5
test/test_analyze.py
test/test_analyze.py
+0
-14
test/test_utils.py
test/test_utils.py
+32
-0
未找到文件。
chan/utils.py
浏览文件 @
32edeb88
...
...
@@ -21,9 +21,9 @@ def preprocess(kline):
last_l
=
kline
.
loc
[
0
,
'low'
]
if
last_h
>=
kline
.
loc
[
1
,
'high'
]:
direction
=
0
# 下跌
direction
=
0
# 下跌
else
:
direction
=
1
# 上涨
direction
=
1
# 上涨
for
i
,
row
in
kline
.
iterrows
():
cur_h
,
cur_l
=
row
[
'high'
],
row
[
'low'
]
...
...
@@ -46,8 +46,8 @@ def preprocess(kline):
continue
# 当期行与上一行之间无包含关系,更新上一行 high low
kline
.
loc
[
i
-
1
,
'high_m'
]
=
last_h
kline
.
loc
[
i
-
1
,
'low_m'
]
=
last_l
kline
.
loc
[
i
-
1
,
'high_m'
]
=
last_h
kline
.
loc
[
i
-
1
,
'low_m'
]
=
last_l
# 更新 direction, last_h, last_l
if
last_h
>=
cur_h
:
...
...
@@ -67,7 +67,7 @@ def preprocess(kline):
return
kline_new
def
find_
fx
(
kline
):
def
find_
bi
(
kline
):
"""找出全部分型,并验证有效性
0 - 顶分型
...
...
@@ -126,3 +126,83 @@ def find_fx(kline):
return
kline
def
find_xd
(
kline
):
"""线段查找。输入:确定了分型的 K 线;输出:加入线段查找结果的 K 线"""
# 找出所有可能的线段终点
gd1
=
kline
[
kline
[
'bi_mark'
]
==
0
].
iloc
[
0
]
gd2
=
kline
[
kline
[
'bi_mark'
]
==
2
].
iloc
[
0
]
if
gd1
[
'high'
]
<
gd2
[
'high'
]:
direction
=
"向上"
else
:
direction
=
"向下"
i
=
4
mark
=
0
kline
[
'xd_mark'
]
=
None
while
i
<=
kline
[
'bi_mark'
].
max
():
gd1
=
kline
[
kline
[
'bi_mark'
]
==
i
-
3
].
iloc
[
0
]
dd1
=
kline
[
kline
[
'bi_mark'
]
==
i
-
2
].
iloc
[
0
]
gd2
=
kline
[
kline
[
'bi_mark'
]
==
i
-
1
].
iloc
[
0
]
dd2
=
kline
[
kline
[
'bi_mark'
]
==
i
].
iloc
[
0
]
# 第二个顶分型的最高价小于或等于第一个顶分型的最高价,向上过程有可能结束
if
direction
==
"向上"
and
gd2
[
'high'
]
<=
gd1
[
'high'
]:
kline
.
loc
[
gd1
.
name
,
'xd_mark'
]
=
mark
mark
+=
1
direction
=
"向下"
# 第二个底分型的最低价大于或等于第一个底分型的最低价,向下过程有可能结束
elif
direction
==
"向下"
and
dd2
[
'low'
]
>=
dd1
[
'low'
]:
kline
.
loc
[
dd1
.
name
,
'xd_mark'
]
=
mark
mark
+=
1
direction
=
"向上"
i
+=
2
# 线段有效的基础: 标准特征序列中至少含一笔
# TODO(zengbin): 检查线段的有效性
return
kline
def
macd
(
kline
):
"""计算 MACD 指标
:param kline: pd.DataFrame
K线,确保含有 close 列
:return: pd.DataFrame
在原始数据中新增 diff,dea,macd 三列
"""
short_
,
long_
,
m
=
12
,
26
,
9
kline
[
'diff'
]
=
kline
[
'close'
].
ewm
(
adjust
=
False
,
alpha
=
2
/
(
short_
+
1
),
ignore_na
=
True
).
mean
()
-
\
kline
[
'close'
].
ewm
(
adjust
=
False
,
alpha
=
2
/
(
long_
+
1
),
ignore_na
=
True
).
mean
()
kline
[
'dea'
]
=
kline
[
'diff'
].
ewm
(
adjust
=
False
,
alpha
=
2
/
(
m
+
1
),
ignore_na
=
True
).
mean
()
kline
[
'macd'
]
=
2
*
(
kline
[
'diff'
]
-
kline
[
'dea'
])
kline
[
'diff'
]
=
kline
[
'diff'
].
apply
(
round
,
args
=
(
4
,))
kline
[
'dea'
]
=
kline
[
'dea'
].
apply
(
round
,
args
=
(
4
,))
kline
[
'macd'
]
=
kline
[
'macd'
].
apply
(
round
,
args
=
(
4
,))
return
kline
def
boll
(
kline
):
"""计算 BOLL 指标
:param kline: pd.DataFrame
K线,确保含有 close 列
:return: pd.DataFrame
在原始数据中新增 BOLL 指标结果
"""
kline
[
'boll-mid'
]
=
kline
[
'close'
].
rolling
(
26
).
mean
()
kline
[
'boll-tmp2'
]
=
kline
[
'close'
].
rolling
(
20
).
std
()
kline
[
'boll-top'
]
=
kline
[
'boll-mid'
]
+
2
*
kline
[
'boll-tmp2'
]
kline
[
'boll-bottom'
]
=
kline
[
'boll-mid'
]
-
2
*
kline
[
'boll-tmp2'
]
kline
[
'boll-mid'
]
=
kline
[
'boll-mid'
].
apply
(
round
,
args
=
(
4
,))
kline
[
'boll-tmp2'
]
=
kline
[
'boll-tmp2'
].
apply
(
round
,
args
=
(
4
,))
kline
[
'boll-top'
]
=
kline
[
'boll-top'
].
apply
(
round
,
args
=
(
4
,))
kline
[
'boll-bottom'
]
=
kline
[
'boll-bottom'
].
apply
(
round
,
args
=
(
4
,))
return
kline
test/test_analyze.py
已删除
100644 → 0
浏览文件 @
eeaf93ad
# coding: utf-8
import
unittest
from
chan.a
import
get_kline
from
chan.utils
import
preprocess
,
find_fx
class
TestAnalyze
(
unittest
.
TestCase
):
def
test_analyze
(
self
):
kline
=
get_kline
(
ts_code
=
'600977.SH'
,
start_date
=
'20190501'
,
end_date
=
'20190725'
,
freq
=
'5min'
)
kline_new
=
preprocess
(
kline
)
kline_fx
=
find_fx
(
kline_new
)
test/test_utils.py
0 → 100644
浏览文件 @
32edeb88
# coding: utf-8
import
unittest
from
chan.a
import
get_kline
import
chan.utils
as
u
class
TestAnalyze
(
unittest
.
TestCase
):
def
setUp
(
self
):
self
.
kline
=
get_kline
(
ts_code
=
'600977.SH'
,
start_date
=
'20190501'
,
end_date
=
'20190725'
,
freq
=
'5min'
)
def
test_utils
(
self
):
kline_new
=
u
.
preprocess
(
self
.
kline
)
self
.
assertTrue
(
len
(
kline_new
)
<
len
(
self
.
kline
))
kline_bi
=
u
.
find_bi
(
kline_new
)
self
.
assertTrue
(
"bi_mark"
in
kline_bi
.
columns
)
kline_xd
=
u
.
find_xd
(
kline_bi
)
self
.
assertTrue
(
"xd_mark"
in
kline_xd
.
columns
)
kline_macd
=
u
.
macd
(
kline_xd
)
self
.
assertTrue
(
"macd"
in
kline_macd
.
columns
)
kline_boll
=
u
.
boll
(
kline_macd
)
self
.
assertTrue
(
"boll-top"
in
kline_boll
.
columns
)
if
__name__
==
"__main__"
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录