Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
zengbin93
czsc
提交
0c4dd6ad
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 搜索 >>
提交
0c4dd6ad
编写于
7月 19, 2019
作者:
Z
zengbin93
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
modify - 20190718
上级
9d9e886b
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
151 addition
and
14 deletion
+151
-14
README.md
README.md
+5
-1
chan/__init__.py
chan/__init__.py
+4
-1
chan/a/__init__.py
chan/a/__init__.py
+1
-1
chan/a/calendar.py
chan/a/calendar.py
+104
-0
chan/a/daily_classfier.py
chan/a/daily_classfier.py
+5
-0
chan/a/utils.py
chan/a/utils.py
+30
-5
chan/analyze.py
chan/analyze.py
+0
-6
requirements.txt
requirements.txt
+2
-0
未找到文件。
README.md
浏览文件 @
0c4dd6ad
...
...
@@ -22,4 +22,8 @@ pip install git+git://github.com/zengbin93/chan.git
```
*
vr 表示 量比
*
dt 表示 该周期的交易结束时间
\ No newline at end of file
*
dt 表示 该周期的交易结束时间
### K线绘图
chan/__init__.py
浏览文件 @
0c4dd6ad
# coding: utf-8
import
os
from
.
import
a
...
...
@@ -8,3 +8,6 @@ version = "0.0.1"
email
=
"zeng_bin8888@163.com"
cache_path
=
os
.
path
.
join
(
os
.
path
.
expanduser
(
'~'
),
".chan"
)
chan/a/__init__.py
浏览文件 @
0c4dd6ad
...
...
@@ -6,4 +6,4 @@
"""
from
.daily_classfier
import
daily_classifier
from
.utils
import
get_kline
from
.utils
import
get_kline
,
get_realtime_kline
chan/a/calendar.py
0 → 100644
浏览文件 @
0c4dd6ad
# coding: utf-8
import
os
import
time
import
functools
import
tushare
as
ts
import
pandas
as
pd
import
warnings
from
datetime
import
datetime
from
chan
import
cache_path
# A股交易日历
# --------------------------------------------------------------------
FILE_CALENDAR
=
os
.
path
.
join
(
cache_path
,
'calendar.csv'
)
if
os
.
path
.
exists
(
FILE_CALENDAR
)
and
\
time
.
time
()
-
os
.
path
.
getmtime
(
FILE_CALENDAR
)
<
3600
*
24
*
300
:
trade_calendar
=
pd
.
read_csv
(
FILE_CALENDAR
,
encoding
=
'utf-8'
)
else
:
trade_calendar
=
ts
.
trade_cal
()
# tushare提供的交易日历
trade_calendar
.
to_csv
(
FILE_CALENDAR
,
index
=
False
,
encoding
=
'utf-8'
)
def
is_trade_day
(
date
):
"""判断date日期是不是交易日
:param date: str or datetime.date, 如 2018-03-15
:return: Bool
"""
trade_day
=
trade_calendar
[
trade_calendar
[
"isOpen"
]
==
1
]
trade_day_list
=
list
(
trade_day
[
'calendarDate'
])
if
isinstance
(
date
,
datetime
):
date
=
str
(
date
.
date
())
if
date
in
trade_day_list
:
return
True
else
:
return
False
def
in_trade_time
():
"""判断当前是否是交易时间"""
today
=
str
(
datetime
.
now
().
date
())
if
not
is_trade_day
(
today
):
# 如果今天不是交易日,返回False
return
False
t1
=
today
+
" "
+
"09:30:00"
t1
=
datetime
.
strptime
(
t1
,
"%Y-%m-%d %H:%M:%S"
)
t2
=
today
+
" "
+
"11:30:00"
t2
=
datetime
.
strptime
(
t2
,
"%Y-%m-%d %H:%M:%S"
)
t3
=
today
+
" "
+
"13:00:00"
t3
=
datetime
.
strptime
(
t3
,
"%Y-%m-%d %H:%M:%S"
)
t4
=
today
+
" "
+
"15:00:00"
t4
=
datetime
.
strptime
(
t4
,
"%Y-%m-%d %H:%M:%S"
)
if
t1
<=
datetime
.
now
()
<=
t2
or
t3
<=
datetime
.
now
()
<=
t4
:
return
True
else
:
return
False
def
recent_trade_days
(
date
,
n
=
10
):
"""返回任一日期date前后的n个交易日日期
:param n: int
返回交易日的数量n。
如果 n > 0,则返回date日期未来的n个交易日日期;
如果 n < 0,则返回date日期过去的n个交易日日期。
:param date: str 默认值 today
:return: list
n个交易日日期列表
"""
cal_date
=
list
(
trade_calendar
[
'calendarDate'
])
date_index
=
cal_date
.
index
(
date
)
if
n
>=
0
:
recent_days
=
cal_date
[
date_index
:
date_index
+
n
+
20
]
else
:
recent_days
=
cal_date
[
date_index
+
n
-
20
:
date_index
+
1
]
recent_days
=
recent_days
[::
-
1
]
res
=
[]
for
d
in
recent_days
:
if
is_trade_day
(
d
):
res
.
append
(
d
)
if
len
(
res
)
==
abs
(
n
):
break
return
res
def
run_at_trade_time
(
func
):
"""控制函数func仅在交易时间段运行"""
@
functools
.
wraps
(
func
)
def
wrapper
(
*
args
,
**
kw
):
if
in_trade_time
():
res
=
func
(
*
args
,
**
kw
)
else
:
msg
=
"%s 不是交易时间,函数 %s 仅在交易时间执行"
%
(
datetime
.
now
().
__str__
(),
func
.
__name__
)
warnings
.
warn
(
msg
)
res
=
None
return
res
return
wrapper
chan/a/daily_classfier.py
浏览文件 @
0c4dd6ad
...
...
@@ -21,6 +21,7 @@ def daily_classifier(ts_code, trade_date, return_central=False):
>>> kind, central = daily_classifier('600122.SH', "20190613", return_central=True)
>>> print(kind, central)
"""
start_date
=
datetime
.
strptime
(
trade_date
,
'%Y%m%d'
)
end_date
=
start_date
+
timedelta
(
days
=
1
)
end_date
=
end_date
.
date
().
__str__
().
replace
(
"-"
,
""
)
...
...
@@ -62,12 +63,15 @@ def daily_classifier(ts_code, trade_date, return_central=False):
elif
(
first_central
is
None
and
last_central
)
or
(
first_central
and
last_central
is
None
):
max_p
=
max
(
data
.
iloc
[:
3
,
:][
'close'
])
min_p
=
min
(
data
.
iloc
[:
3
,
:][
'close'
])
# 1、在前三根30分钟K线出现当天高点
if
max
(
data
[
'close'
])
==
max_p
:
kind
=
"弱平衡市"
# 2、在前三根30分钟K线出现当天低点
elif
min
(
data
[
'close'
])
==
min_p
:
kind
=
"强平衡市"
# 3、在前三根30分钟K线不出现当天高低点
else
:
kind
=
"转折平衡市"
...
...
@@ -80,6 +84,7 @@ def daily_classifier(ts_code, trade_date, return_central=False):
kind
=
"向上两中枢走势"
else
:
raise
ValueError
(
"两中枢的最低价不可以相等"
)
else
:
raise
ValueError
(
'中枢计算错误'
)
...
...
chan/a/utils.py
浏览文件 @
0c4dd6ad
...
...
@@ -15,7 +15,7 @@ def get_kline(ts_code, start_date, end_date, freq='30min'):
:param end_date: str
日期,如 20190610
:return: pd.DataFrame
columns = ["symbol", "dt", "open", "close", "high", "low", "v
r
"]
columns = ["symbol", "dt", "open", "close", "high", "low", "v
ol
"]
example:
>>> get_kline(ts_code='600122.SH', start_date='20190601', end_date='20190610', freq='30min')
...
...
@@ -33,10 +33,35 @@ def get_kline(ts_code, start_date, end_date, freq='30min'):
df
.
sort_values
(
'dt'
,
inplace
=
True
)
df
.
reset_index
(
drop
=
True
,
inplace
=
True
)
df
[
'vr'
]
=
0
for
i
in
range
(
5
,
len
(
df
)):
df
.
loc
[
i
,
'vr'
]
=
round
(
df
.
loc
[
i
,
'vol'
]
/
df
.
loc
[
i
-
5
:
i
-
1
,
'vol'
].
mean
(),
4
)
return
df
[[
'symbol'
,
'dt'
,
'open'
,
'close'
,
'high'
,
'low'
,
'vr'
]]
# 计算量比
# df['vr'] = 0
# for i in range(5, len(df)):
# df.loc[i, 'vr'] = round(df.loc[i, 'vol'] / df.loc[i-5:i-1, 'vol'].mean(), 4)
return
df
[[
'symbol'
,
'dt'
,
'open'
,
'close'
,
'high'
,
'low'
,
'vol'
]]
def
get_realtime_kline
(
ts_code
,
freq
=
"5min"
):
"""实时获取分钟K线
:param ts_code: str
tushare 股票代码,如 600122.SH
:param freq: str
K线周期,分钟级别,可选值 5min 15min 30min 60min
:return: pd.DataFrame
columns = ["symbol", "dt", "open", "close", "high", "low", "vol"]
"""
code
=
ts_code
[:
6
]
df
=
ts
.
get_k_data
(
code
=
code
,
ktype
=
freq
.
replace
(
"min"
,
""
))
df
[
'symbol'
]
=
ts_code
df
.
rename
(
columns
=
{
'date'
:
'dt'
,
'volume'
:
'vol'
},
inplace
=
True
)
df
.
sort_values
(
'dt'
,
inplace
=
True
)
df
.
reset_index
(
drop
=
True
,
inplace
=
True
)
return
df
[[
"symbol"
,
"dt"
,
"open"
,
"close"
,
"high"
,
"low"
,
"vol"
]]
chan/analyze.py
浏览文件 @
0c4dd6ad
...
...
@@ -80,9 +80,3 @@ def find_fx(kline):
return
kline
def
find_bi
(
kline
):
kline1
=
kline
.
dropna
(
axis
=
0
,
how
=
'any'
,
subset
=
[
'fx'
])
kline2
=
kline
.
dropna
(
axis
=
0
,
how
=
'any'
,
subset
=
[
'high_m'
,
'low_m'
])
pass
requirements.txt
浏览文件 @
0c4dd6ad
tqdm
pandas
tushare
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录