示例#1
0
def show_result():
    if len(count_dict) > 0:
        console.write_blank()
        for index in count_dict:
            console.write_number(index, str(count_dict[index]))
    reset()
    return
示例#2
0
def share_detail(code_list, reset_flag=False):
    """
    将股票数据推送至influxdb
    """
    f = h5py.File(conf.HDF5_FILE_SHARE, 'a')
    for code in code_list:
        code_prefix = code[0:3]
        code_group_path = '/' + code_prefix + '/' + code
        if f.get(code_group_path) is None:
            console.write_msg(code + "目录不存在")
            continue

        console.write_head(conf.HDF5_OPERATE_PUSH, conf.HDF5_RESOURCE_TUSHARE,
                           code)
        # 推送原始kline
        measurement = conf.MEASUREMENT_SHARE
        _raw_kline(f[code_prefix][code], measurement, code, reset_flag)

        # 推送缠论kline
        measurement = conf.MEASUREMENT_SHARE_WRAP
        _wrap_kline(f[code_prefix][code], measurement, code, reset_flag)

        # 推送基本面数据
        measurement = conf.MEASUREMENT_SHARE_BASIC
        _basic_info(f[code_prefix][code], measurement, code, reset_flag)

        console.write_blank()
        console.write_tail()
    f.close()
    return
示例#3
0
 def output(self):
     """
     打印结果
     """
     trend_now = self.small.iloc[-1]
     phase_now = self.phase.iloc[-1]
     # phase_pre = self.phase.iloc[-2]
     side = self._get_side(phase_now[action.INDEX_PHASE_STATUS])
     console.write_msg(
         "【%s, %s, %s】" %
         (self.code, trend_now[conf.HDF5_SHARE_DATE_INDEX], side))
     if len(self.phase) > 1:
         phase_pre = self.phase.iloc[-2]
         msg = "上阶段,macd差值%f,price差值%d,连续%d次,dif位置%f"
         console.write_msg(
             msg % (phase_pre[phase.MACD_DIFF], phase_pre[phase.PRICE_END] -
                    phase_pre[phase.PRICE_START], phase_pre[phase.COUNT],
                    phase_pre[phase.DIF_END]))
     msg = "建议价格%d - %d"
     console.write_msg(msg %
                       (phase_now[phase.PRICE_START], trend_now["close"]))
     msg = "medium级别背离情况:%s"
     console.write_msg(msg % (self.medium_trend_reverse))
     msg = "big级别背离情况:%s"
     console.write_msg(msg % (self.big_trend_reverse))
     console.write_blank()
     return
示例#4
0
def code_detail(code_list, start_date):
    """
    将code的basic内容,整理至share文件下
    """
    # 获取basic所有日期的detail,并遍历读取详细信息
    f = h5py.File(conf.HDF5_FILE_BASIC, 'a')
    f_share = h5py.File(conf.HDF5_FILE_SHARE, 'a')
    console.write_head(conf.HDF5_OPERATE_ARRANGE, conf.HDF5_RESOURCE_TUSHARE,
                       conf.HDF5_BASIC_DETAIL)
    path = '/' + conf.HDF5_BASIC_DETAIL
    if f.get(path) is None:
        return

    code_basic_dict = dict()
    for date in f[path]:
        if start_date is not None and date < start_date:
            console.write_msg(start_date + "起始日期大于基本数据的最大日期")
            continue
        df = tool.df_from_dataset(f[path], date, None)
        df["code"] = df["code"].str.decode("utf-8")
        df = df.set_index("code")
        for code in df.index:
            if code not in code_list:
                continue

            if code not in code_basic_dict:
                code_basic_dict[code] = tool.init_empty_df(df.columns)
            code_basic_dict[code].loc[date] = df.loc[code, :]

    for code, code_df in code_basic_dict.items():
        code_df.index.name = conf.HDF5_SHARE_DATE_INDEX
        code_df = code_df.reset_index().sort_values(
            by=[conf.HDF5_SHARE_DATE_INDEX])

        code_prefix = code[0:3]
        code_group_path = '/' + code_prefix + '/' + code
        if f_share.get(code_group_path) is None:
            console.write_msg(code + "的detail文件不存在")
            continue

        if start_date is None:
            tool.delete_dataset(f_share[code_group_path],
                                conf.HDF5_BASIC_DETAIL)
        tool.merge_df_dataset(f_share[code_group_path], conf.HDF5_BASIC_DETAIL,
                              code_df)
        console.write_exec()
    console.write_blank()
    console.write_tail()
    f_share.close()
    f.close()
    return
示例#5
0
def code_exec(f, code):
    """
    个股筛选
    """
    MACD_DIVERSE_LIMIT = 5
    code_prefix = code[0:3]
    code_dict = dict()
    code_dict["code"] = code
    omit_flag = False
    for ktype in ["D", "W", "M", "30", "5"]:
        if f[code_prefix][code].get(ktype) is None:
            console.write_blank()
            console.write_msg(code + "阶段" + ktype + "的detail数据不存在")
            return None
        share_df = tool.df_from_dataset(f[code_prefix][code], ktype, None)

        index_ds_name = conf.HDF5_INDEX_DETAIL + "_" + ktype
        if f[code_prefix][code].get(index_ds_name) is None:
            console.write_blank()
            console.write_msg(code + "阶段" + ktype + "的macd与均线数据不存在")
            return None
        index_df = tool.df_from_dataset(f[code_prefix][code], index_ds_name,
                                        None)
        share_df = share_df.merge(index_df,
                                  left_on=conf.HDF5_SHARE_DATE_INDEX,
                                  right_on=conf.HDF5_SHARE_DATE_INDEX,
                                  how='outer')
        share_df[conf.HDF5_SHARE_DATE_INDEX] = share_df[
            conf.HDF5_SHARE_DATE_INDEX].str.decode("utf-8")
        # 检查macd的趋势
        if ktype in ["D", "M", "W"]:
            code_dict = _filter_trend(share_df.tail(50), code_dict, ktype)
            if code_dict is None:
                omit_flag = True
                break
        # 检查macd的背离
        if ktype in ["D", "30"]:
            code_dict = _filter_diverse(share_df.tail(100), code_dict, ktype)
    # 如果日线不存在macd的上涨趋势,则忽略该股票
    if omit_flag is True:
        return None
    # 如果日线、30min一个阶段内都不存在背离,忽略
    if code_dict["30" +
                 INDEX_MACD_DIVERSE_COUNT] <= MACD_DIVERSE_LIMIT and code_dict[
                     "d" + INDEX_MACD_DIVERSE_COUNT] <= MACD_DIVERSE_LIMIT:
        return None
    return code_dict
示例#6
0
    def output(self):
        now = self.trade.iloc[-1]
        console.write_msg(
            "【%s, %s, %s】" %
            (self.code, now[conf.HDF5_SHARE_DATE_INDEX], now["type"]))
        msg = "个股5min,趋势%s,连续%d次,macd趋势%f, macd差值%d%%"
        console.write_msg(
            msg %
            (now[DF_FIVE + TRADE_STATUS], now[DF_FIVE + TRADE_TREND_COUNT],
             now[DF_FIVE + TRADE_MACD_PHASE], now[DF_FIVE + TRADE_MACD_DIFF]))
        dtype_dict = {
            DF_THIRTY: "个股30min",
            DF_BIG: "个股大趋势",
            DF_INDEX: "指数30min",
        }
        for dtype in dtype_dict:
            msg = "%s,趋势%s,连续%d次,macd趋势%f, macd差值%d%%"
            console.write_msg(
                msg %
                (dtype_dict[dtype], now[dtype + TRADE_STATUS],
                 now[dtype + TRADE_TREND_COUNT], now[dtype + TRADE_MACD_PHASE],
                 now[dtype + TRADE_MACD_DIFF]))

        # TODO 背离情况要计算两次买卖点trend_count的差值
        trend_count = now[DF_FIVE + TRADE_TREND_COUNT]
        upper_estimate = (trend_count + 1) * 5
        lower_estimate = (trend_count - 1) * 5
        if self.stype == conf.STYPE_ASHARE:
            remain_seconds = tradetime.get_ashare_remain_second(
                now[conf.HDF5_SHARE_DATE_INDEX])
            remain_minutes = round(remain_seconds / 60, 0)
            msg = "剩余时间%d分钟,下个交易点预估需要%d-%d分钟,模式%s"
            if (upper_estimate * 60) <= remain_seconds:
                trade_opportunity = "T+0"
            else:
                trade_opportunity = "T+1"
            console.write_msg(msg % (remain_minutes, lower_estimate,
                                     upper_estimate, trade_opportunity))
        elif self.stype == conf.STYPE_BITMEX:
            msg = "下个交易点预估需要%d-%d分钟"
            console.write_msg(msg % (lower_estimate, upper_estimate))

        console.write_msg("建议仓位:%d/3" % (now[TRADE_POSITIONS]))
        console.write_blank()
        return
示例#7
0
def index_detail(reset_flag=False):
    """
    将指数数据推送至influxdb
    """
    f = h5py.File(conf.HDF5_FILE_INDEX, 'a')
    for code in f:
        console.write_head(conf.HDF5_OPERATE_PUSH, conf.HDF5_RESOURCE_TUSHARE,
                           code)
        # 推送原始kline
        measurement = conf.MEASUREMENT_INDEX
        _raw_kline(f[code], measurement, code, reset_flag)

        # 推送缠论kline
        measurement = conf.MEASUREMENT_INDEX_WRAP
        _wrap_kline(f[code], measurement, code, reset_flag)

        console.write_blank()
        console.write_tail()
    f.close()
    return
示例#8
0
def all_share(omit_list, init_flag=True):
    """
    获取所有股票的macd与所处均线等指标
    """
    f = h5py.File(conf.HDF5_FILE_SHARE, 'a')
    for code_prefix in f:
        if code_prefix in omit_list:
            continue
        console.write_head(conf.HDF5_OPERATE_INDEX, conf.HDF5_RESOURCE_TUSHARE,
                           code_prefix)
        for code in f[code_prefix]:
            # 忽略停牌、退市、无法获取的情况
            if f[code_prefix][code].attrs.get(
                    conf.HDF5_BASIC_QUIT) is not None:
                continue
            if f[code_prefix][code].attrs.get(conf.HDF5_BASIC_ST) is not None:
                continue

            code_group_path = '/' + code_prefix + '/' + code
            for ktype in conf.HDF5_SHARE_KTYPE:
                try:
                    if f.get(code_group_path) is None or f[code_prefix][
                            code].get(ktype) is None:
                        console.write_msg(code + "-" + ktype + "的detail不存在")
                        continue
                    df = tool.df_from_dataset(f[code_prefix][code], ktype,
                                              None)
                    index_df = one_df(df, init_flag)
                    ds_name = conf.HDF5_INDEX_DETAIL + "_" + ktype
                    if init_flag is True:
                        tool.delete_dataset(f[code_prefix][code], ds_name)
                    tool.merge_df_dataset(f[code_prefix][code], ds_name,
                                          index_df.reset_index())
                except Exception as er:
                    print(str(er))
            console.write_exec()
        console.write_blank()
        console.write_tail()
    f.close()
    return
示例#9
0
def filter_share(code_list, start_date):
    """
    整理筛选的股票缠论k线
    """
    f = h5py.File(conf.HDF5_FILE_SHARE, 'a')
    console.write_head(conf.HDF5_OPERATE_WRAP, conf.HDF5_RESOURCE_TUSHARE,
                       conf.HDF5_INDEX_WRAP)
    for code in code_list:
        code_prefix = code[0:3]
        code_group_path = '/' + code_prefix + '/' + code
        if f.get(code_group_path) is None:
            continue
        # 忽略停牌、退市、无法获取的情况
        if f[code_prefix][code].attrs.get(
                conf.HDF5_BASIC_QUIT
        ) is not None or f[code_prefix][code].attrs.get(
                conf.HDF5_BASIC_ST) is not None:
            continue

        for ktype in conf.HDF5_SHARE_WRAP_KTYPE:
            ds_name = ktype
            if f[code_prefix][code].get(ds_name) is None:
                continue
            share_df = tool.df_from_dataset(f[code_prefix][code], ds_name,
                                            None)
            wrap_df = one_df(share_df)
            if wrap_df is not None:
                ds_name = conf.HDF5_INDEX_WRAP + "_" + ktype
                if f[code_prefix][code].get(ds_name) is not None:
                    tool.delete_dataset(f[code_prefix][code], ds_name)
                tool.create_df_dataset(f[code_prefix][code], ds_name, wrap_df)
                console.write_exec()
            else:
                console.write_pass()
    console.write_blank()
    console.write_tail()
    f.close()
    return
示例#10
0
def classify_detail(classify_list, reset_flag=False):
    """
    将分类推送至influxdb
    """
    f = h5py.File(conf.HDF5_FILE_CLASSIFY, 'a')
    # 获取classify列表
    for ctype in classify_list:
        console.write_head(conf.HDF5_OPERATE_PUSH, conf.HDF5_RESOURCE_TUSHARE,
                           ctype)
        for classify_name in f[ctype]:
            # 推送原始kline
            measurement = conf.MEASUREMENT_CLASSIFY + "_" + ctype
            _raw_kline(f[ctype][classify_name], measurement, classify_name,
                       reset_flag)

            # 推送缠论kline
            measurement = conf.MEASUREMENT_CLASSIFY_WRAP + "_" + ctype
            _wrap_kline(f[ctype][classify_name], measurement, classify_name,
                        reset_flag)
        console.write_blank()
        console.write_tail()
    f.close()
    return
示例#11
0
def all_exec(omit_list):
    """
    筛选出存在背离的股票
    """
    # 筛选记录内容如下:
    # 1. 月线macd趋势
    # 2. 周线macd趋势
    # 3. 日线macd趋势,是否背离,数值差距
    # 4. 30min的macd趋势,是否背离,数值差距,震荡中枢数量
    # 5. 5min的macd趋势,是否背离,数值差距,震荡中枢数量
    console.write_head(conf.HDF5_OPERATE_SCREEN, conf.HDF5_RESOURCE_TUSHARE,
                       conf.STRATEGY_TREND_AND_REVERSE)
    f = h5py.File(conf.HDF5_FILE_SHARE, 'a')
    filter_df = tool.init_empty_df(_ini_filter_columns())
    for code_prefix in f:
        if code_prefix in omit_list:
            continue
        for code in f[code_prefix]:
            code_group_path = '/' + code_prefix + '/' + code
            if f.get(code_group_path) is None:
                console.write_blank()
                console.write_msg(code + "的tushare数据不存在")
                continue

            # 忽略停牌、退市、无法获取的情况
            if f[code_prefix][code].attrs.get(
                    conf.HDF5_BASIC_QUIT
            ) is not None or f[code_prefix][code].attrs.get(
                    conf.HDF5_BASIC_ST) is not None:
                console.write_blank()
                console.write_msg(code + "已退市或停牌")
                continue

            try:
                code_dict = code_exec(f, code)
                if code_dict is None:
                    console.write_pass()
                    continue
                else:
                    console.write_exec()
                    filter_df = filter_df.append(code_dict, ignore_index=True)
            except Exception as er:
                console.write_msg("[" + code + "]" + str(er))
    f.close()
    f_screen = h5py.File(conf.HDF5_FILE_SCREEN, 'a')
    if f_screen.get(conf.STRATEGY_TREND_AND_REVERSE) is None:
        f_screen.create_group(conf.STRATEGY_TREND_AND_REVERSE)
    if f_screen[conf.STRATEGY_TREND_AND_REVERSE].get(
            conf.SCREEN_SHARE_FILTER) is None:
        f_screen[conf.STRATEGY_TREND_AND_REVERSE].create_group(
            conf.SCREEN_SHARE_FILTER)
    today_str = tradetime.get_today()
    tool.delete_dataset(
        f_screen[conf.STRATEGY_TREND_AND_REVERSE][conf.SCREEN_SHARE_FILTER],
        today_str)
    tool.merge_df_dataset(
        f_screen[conf.STRATEGY_TREND_AND_REVERSE][conf.SCREEN_SHARE_FILTER],
        today_str, filter_df)
    f_screen.close()
    console.write_blank()
    console.write_tail()
    return