daily_classfier.py 3.3 KB
Newer Older
Z
zengbin93 已提交
1 2 3 4 5 6
# coding: utf-8

from datetime import datetime, timedelta
import tushare as ts


Z
zengbin93 已提交
7
def daily_classifier(ts_code, trade_date, asset='E', return_central=False):
Z
zengbin93 已提交
8 9
    """ A 股每日走势的分类

Z
zengbin93 已提交
10 11
    asset 交易资产类型,可选值 E股票 I沪深指数

Z
zengbin93 已提交
12 13 14 15 16 17 18 19 20 21 22
    使用该方法前,请仔细阅读:http://blog.sina.com.cn/s/blog_486e105c010009uy.html

    每个交易日产生 8 根 30 分钟 K 线,分别在前三根和最后三根中计算中枢,其结果分以下情况:
    1)没有中枢;
    2)仅有一个中枢,或计算得到的两个中枢区间有重叠;
    3)有两个中枢,且没有重叠。

    example:
    >>> kind, central = daily_classifier('600122.SH', "20190613", return_central=True)
    >>> print(kind, central)
    """
Z
zengbin93 已提交
23

Z
zengbin93 已提交
24 25 26 27
    start_date = datetime.strptime(trade_date, '%Y%m%d')
    end_date = start_date + timedelta(days=1)
    end_date = end_date.date().__str__().replace("-", "")

Z
zengbin93 已提交
28 29
    df = ts.pro_bar(ts_code=ts_code, freq='30min', asset=asset,
                    start_date=trade_date, end_date=end_date)
Z
zengbin93 已提交
30
    df.sort_values('trade_time', inplace=True)
Z
zengbin93 已提交
31
    data = df[['ts_code', 'trade_time', 'high', 'low', 'close']].iloc[1:, :]
Z
zengbin93 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
    data = data.reset_index(drop=True)
    assert len(data) == 8, "每个交易日,A股有且只有8跟30分钟K线"

    def _central(tri):
        c_low = max(tri['low'])
        c_high = min(tri['high'])
        if c_low >= c_high:
            # None means no central found
            central = None
        else:
            central = {
                "time_span": "%s - %s" % (tri.iloc[0, 1], tri.iloc[2, 1]),
                "price_span": (c_low, c_high)
            }
        return central

    first_central = _central(data.iloc[:3, :])
    last_central = _central(data.iloc[-3:, :])

Z
zengbin93 已提交
51 52 53 54 55 56 57
    # 若两个中枢之间存在价格重叠部分,则第二个中枢不存在
    if first_central and last_central:
        fp = first_central['price_span']
        lp = last_central["price_span"]
        if fp[1] >= lp[0] >= fp[0] or fp[1] >= lp[1] >= fp[0]:
            last_central = None

Z
zengbin93 已提交
58 59 60 61 62 63
    # 没有中枢的情况
    if first_central is None and last_central is None:
        kind = "最强单边走势"

    # 一个中枢的情况(平衡市)
    elif (first_central is None and last_central) or (first_central and last_central is None):
Z
fix  
zengbin93 已提交
64 65
        max_p = max(data.iloc[:3, :]['high'])
        min_p = min(data.iloc[:3, :]['low'])
Z
zengbin93 已提交
66

Z
zengbin93 已提交
67
        # 1、在前三根30分钟K线出现当天高点
Z
fix  
zengbin93 已提交
68
        if max(data['high']) == max_p:
Z
zengbin93 已提交
69
            kind = "弱平衡市"
Z
zengbin93 已提交
70

Z
zengbin93 已提交
71
        # 2、在前三根30分钟K线出现当天低点
Z
fix  
zengbin93 已提交
72
        elif min(data['low']) == min_p:
Z
zengbin93 已提交
73
            kind = "强平衡市"
Z
zengbin93 已提交
74

Z
zengbin93 已提交
75 76 77 78 79 80 81 82 83 84 85 86
        # 3、在前三根30分钟K线不出现当天高低点
        else:
            kind = "转折平衡市"

    # 两个中枢的情况
    elif first_central and last_central:
        if first_central['price_span'][0] > last_central['price_span'][0]:
            kind = "向下两中枢走势"
        elif first_central['price_span'][0] < last_central['price_span'][0]:
            kind = "向上两中枢走势"
        else:
            raise ValueError("两中枢的最低价不可以相等")
Z
zengbin93 已提交
87

Z
zengbin93 已提交
88 89 90 91 92 93 94
    else:
        raise ValueError('中枢计算错误')

    if return_central:
        return kind, (first_central, last_central)
    else:
        return kind