Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
zengbin93
czsc
提交
2650f6e1
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,发现更多精彩内容 >>
提交
2650f6e1
编写于
7月 26, 2019
作者:
Z
zengbin93
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add k线预处理,分型查找
上级
0c4dd6ad
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
114 addition
and
0 deletion
+114
-0
chan/utils.py
chan/utils.py
+114
-0
未找到文件。
chan/utils.py
0 → 100644
浏览文件 @
2650f6e1
# coding: utf-8
from
collections
import
OrderedDict
class
OrderedAttrDict
(
OrderedDict
):
"""OrderedDict that can get attribute by dot"""
def
__init__
(
self
,
*
args
,
**
kwargs
):
super
(
OrderedAttrDict
,
self
).
__init__
(
*
args
,
**
kwargs
)
self
.
__dict__
=
self
def
preprocess
(
kline
):
"""去除包含关系"""
kline
[
'high_m'
]
=
None
kline
[
'low_m'
]
=
None
# 首行处理
last_h
=
kline
.
loc
[
0
,
'high'
]
last_l
=
kline
.
loc
[
0
,
'low'
]
if
last_h
>=
kline
.
loc
[
1
,
'high'
]:
direction
=
0
# 下跌
else
:
direction
=
1
# 上涨
for
i
,
row
in
kline
.
iterrows
():
cur_h
,
cur_l
=
row
[
'high'
],
row
[
'low'
]
# 左包含 or 右包含
if
(
cur_h
<=
last_h
and
cur_l
>=
last_l
)
or
(
cur_h
>=
last_h
and
cur_l
<=
last_l
):
if
direction
==
0
:
last_h
=
min
(
last_h
,
cur_h
)
last_l
=
min
(
last_l
,
cur_l
)
elif
direction
==
1
:
last_h
=
max
(
last_h
,
cur_h
)
last_l
=
max
(
last_l
,
cur_l
)
else
:
raise
ValueError
# 尾行更新 high low
if
i
==
len
(
kline
)
-
1
:
kline
.
loc
[
i
,
'high_m'
]
=
last_h
kline
.
loc
[
i
,
'low_m'
]
=
last_l
continue
# 当期行与上一行之间无包含关系,更新上一行 high low
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
:
direction
=
0
# 下跌
else
:
direction
=
1
# 上涨
last_h
=
cur_h
last_l
=
cur_l
# 根据包含关系的检查结果,生成新的 K 线数据
kline_new
=
kline
[[
'symbol'
,
'dt'
,
'open'
,
'close'
,
'high_m'
,
'low_m'
]]
kline_new
=
kline_new
.
rename
({
"high_m"
:
"high"
,
"low_m"
:
"low"
},
axis
=
'columns'
)
kline_new
=
kline_new
.
dropna
(
subset
=
[
'high'
,
'low'
])
kline_new
=
kline_new
.
reset_index
(
drop
=
True
)
return
kline_new
def
find_fx
(
kline
):
"""找出全部分型,并验证有效性
0 - 顶分型
1 - 底分型
2 - 无效分型
:param kline: pd.DataFrame
经过预处理,去除了包含关系的 K 线
:return: kline: pd.DataFrame
"""
kline
[
'fx'
]
=
None
for
i
in
range
(
1
,
len
(
kline
)
-
1
):
data
=
kline
.
iloc
[
i
-
1
:
i
+
2
]
row
=
kline
.
iloc
[
i
]
if
max
(
data
[
'high'
])
==
row
[
'high'
]:
kline
.
loc
[
row
.
name
,
'fx'
]
=
0
elif
min
(
data
[
'low'
])
==
row
[
'low'
]:
kline
.
loc
[
row
.
name
,
'fx'
]
=
1
else
:
continue
# 确定分型的有效性:满足结合律;实现方式:从后往前,不满足结合律就处理成无效分型
last_index
=
None
for
i
in
kline
.
index
[::
-
1
]:
if
kline
.
loc
[
i
,
'fx'
]
not
in
[
0
,
1
]:
continue
if
kline
.
loc
[
i
,
'fx'
]
in
[
0
,
1
]:
if
last_index
is
None
:
last_index
=
i
else
:
curr_index
=
i
if
last_index
-
curr_index
<
3
:
kline
.
loc
[
last_index
,
'fx'
]
=
2
kline
.
loc
[
curr_index
,
'fx'
]
=
2
last_index
=
None
else
:
last_index
=
curr_index
return
kline
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录