提交 a6528953 编写于 作者: Z zengbin93

add macd view

上级 64443321
......@@ -7,7 +7,7 @@ Pypi上已经存在一个名为chan的库,以致于这个库没法上传到Pyp
执行以下代码直接从github安装:
```
pip install git+git://github.com/zengbin93/chan.git
pip install git+git://github.com/zengbin93/chan.git -U
```
## K线数据样例
......
......@@ -15,186 +15,6 @@ def clean_cache():
os.mkdir(cache_path)
def preprocess(kline):
"""去除包含关系
:param kline: pd.DataFrame
K线,columns = ["symbol", "dt", "open", "close", "high", "low", "vol"]
:return: pd.DataFrame
"""
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_bi(kline):
"""找出全部分型,并验证有效性
0 - 顶分型
1 - 底分型
2 - 无效分型
一个分型能否成为笔的一部分,至少需要看到后面两个分型的情况才能确定。
:param kline: pd.DataFrame
经过预处理,去除了包含关系的 K 线,
columns = ["symbol", "dt", "open", "close", "high", "low", "vol"]
:return: kline: pd.DataFrame
"""
kline['fx'] = None
kline['bi'] = 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]:
for i in kline.index:
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 curr_index - last_index < 4:
kline.loc[last_index, 'fx'] = 2
kline.loc[curr_index, 'fx'] = 2
kline_part = kline.iloc[:i]
last_index = kline_part[kline_part['fx'].isin([0, 1])].iloc[-1].name
last_index = int(last_index)
else:
last_index = curr_index
# 添加 笔标记 - 从第一个有效顶分型开始标记
kline['bi_mark'] = None
mark = 0
for i, row in kline.iterrows():
if mark == 0 and row['fx'] == 0:
kline.loc[i, 'bi_mark'] = mark
kline.loc[i, 'bi'] = kline.loc[i, 'high']
mark += 1
continue
if mark > 0 and row['fx'] in [0, 1]:
kline.loc[i, 'bi_mark'] = mark
mark += 1
if row['fx'] == 0:
kline.loc[i, 'bi'] = kline.loc[i, 'high']
elif row['fx'] == 1:
kline.loc[i, 'bi'] = kline.loc[i, 'low']
else:
raise ValueError("fx value error, valid is 0 or 1,"
" current is %i" % row['fx'])
return kline
def find_xd(kline):
"""线段查找。输入:确定了分型的 K 线;输出:加入线段查找结果的 K 线
:param kline: pd.DataFrame
K线,columns = ["symbol", "dt", "open", "close", "high", "low", "vol"]
:return:
"""
# 找出所有可能的线段终点
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
# 线段有效的基础: 标准特征序列中至少含一笔
# 添加 线段标记 - 从第一个有效顶分型开始标记
return kline
def ma(kline, params=(5, 10, 20, 60, 120, 250)):
"""计算指定周期的若干 MA 均线
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册