Exemplo n.º 1
0
def exclude_analyze(basic, kdata, pick_index, take_index, policy_args):
    """ 根据日线做排除性校验
    """
    max_rise     = __parse_policy_args(policy_args, MAX_RISE)
    max_pclose   = __parse_policy_args(policy_args, MAX_PCLOSE)
    outstanding  = __parse_policy_args(policy_args, OUTSTANDING)

    ### 净资产为负数的
    if basic[dogen.BVPS] < 0:
        logger.debug("Invalid bvps")
        return True

    ### 特征三
    if kdata.iloc[take_index][dogen.P_CLOSE] > max_pclose:
        logger.debug("Too high close price at %s" % kdata.index[take_index])
        return True
    if kdata.iloc[take_index][dogen.P_CLOSE] * basic[dogen.OUTSTANDING] > outstanding:
        logger.debug("Too large outstanding at %s" % kdata.index[take_index])
        return True
    
    ### 特征四
    rise_range = dogen.get_last_rise_range(kdata, max_rise, max_fall=max_rise/2, eIdx=22)
    if rise_range is not None:
        [min_index, max_index, inc_close, get_lhigh, tmp_index] = rise_range
        if max_rise < dogen.caculate_incr_percentage(kdata.iloc[take_index][dogen.P_CLOSE], kdata.iloc[min_index][dogen.P_CLOSE]):
            logger.debug("Too large rise-range")
            return True
        pass

    ### 特征五
    if kdata.iloc[take_index][dogen.MA5] < kdata.iloc[take_index+1][dogen.MA5] and kdata.iloc[take_index][dogen.MA20] < kdata.iloc[take_index+1][dogen.MA20]:
        logger.debug("Invalid MA5&MA20 at %s" % kdata.index[take_index])
        return True

    return False
Exemplo n.º 2
0
def exclude_analyze(basic, kdata, pick_index, take_index, policy_args):
    max_rclose  = __parse_policy_args(policy_args, MAX_RCLOSE)
    min_ramp    = __parse_policy_args(policy_args, MIN_RAMP)
    max_rise    = __parse_policy_args(policy_args, MAX_RISE)
    max_pclose  = __parse_policy_args(policy_args, MAX_PCLOSE)
    outstanding = __parse_policy_args(policy_args, OUTSTANDING)

    ### 净资产为负数的
    if basic[dogen.BVPS] < 0.1:
        logger.debug("Invalid bvps")
        return True

    ### 特征三
    if kdata.iloc[take_index][dogen.P_CLOSE] > max_pclose:
        logger.debug("Too high close price at %s" % kdata.index[take_index])
        return True
    if kdata.iloc[take_index][dogen.P_CLOSE] * basic[dogen.OUTSTANDING] > outstanding:
        logger.debug("Too large outstanding at %s" % kdata.index[take_index])
        return True

    ### 特征四
    rise_range = dogen.get_last_rise_range(kdata, max_rise, max_fall=max_rise/2, eIdx=22)
    if rise_range is not None:
        [min_index, max_index, inc_close, get_lhigh, tmp_index] = rise_range
        if max_rise < dogen.caculate_incr_percentage(kdata.iloc[take_index][dogen.P_CLOSE], kdata.iloc[min_index][dogen.P_CLOSE]):
            logger.debug("Too large rise-range")
            return True
        pass

    ### 特征五
    tdata = kdata[0:5]
    if tdata[tdata[dogen.R_CLOSE]>0].index.size <= tdata.index.size/2:
        logger.debug("Don't contain enough up trades in last week")
        return True
    if dogen.get_highlimit_trades(tdata).size > 0:
        logger.debug("There is hl trade in last week")
        return True

    ### 特征六
    for temp_index in range(pick_index, -1, -1):
        ### 下跌
        if kdata.iloc[temp_index][dogen.R_CLOSE] >= 0 or kdata.iloc[temp_index+1][dogen.R_CLOSE] <= 0:
            continue
        if kdata.iloc[temp_index][dogen.VOLUME] <= kdata.iloc[temp_index+1][dogen.VOLUME]:
            continue
        ### 放量下跌之后未被上涨突破
        maxi_index = dogen.get_last_column_max(kdata, dogen.P_CLOSE, eIdx=temp_index)
        if maxi_index is None or kdata.iloc[temp_index][dogen.P_OPEN] > kdata.iloc[maxi_index][dogen.P_CLOSE]:
            logger.debug("Invalid fall-trade at %s" % kdata.index[temp_index])
            return True
        pass

    return False
Exemplo n.º 3
0
def score_analyze(basic, kdata, high_index, pick_index, take_index, fall_range,
                  policy_args):
    """ 根据股票股价、市值、成交量等方面给股票打分:
            * 股价估分,总计40分;
            * 市值估分,总计40分;
            * 涨停估分,总分20分,两个月内一个涨停板10分;
    """
    max_pclose = __parse_policy_args(policy_args, MAX_PCLOSE)
    outstanding = __parse_policy_args(policy_args, OUTSTANDING)
    pick_start = __parse_policy_args(policy_args, PICK_START)
    [high_index, pick_index, dec_close, get_llow, tmpId] = fall_range

    score = dogen.score_by_pclose(30, kdata.iloc[take_index][dogen.P_CLOSE],
                                  max_pclose)
    score += dogen.score_by_outstanding(
        30, kdata.iloc[take_index][dogen.P_CLOSE] * basic[dogen.OUTSTANDING],
        outstanding)

    temp_score = 20
    temp_slice = 20
    tdata = kdata[0:high_index + 1]
    count = tdata[tdata[dogen.P_CLOSE] >= tdata[dogen.L_HIGH]].index.size
    if (count > temp_score / temp_slice):
        count = temp_score / temp_slice
    if (count > 0):
        score += temp_slice * count

    temp_score = 20
    temp_slice = 10
    temp_index = pick_index
    for i in range(0, (int)(temp_score / temp_slice)):
        rise_range = dogen.get_last_rise_range(kdata, 10, sIdx=temp_index)
        if rise_range is None:
            break
        else:
            [min_index, max_index, inc_close, get_hl, tmpId] = rise_range
        score += temp_slice

    return (int)(score)
Exemplo n.º 4
0
def stock_analyze(basic, kdata, policy_args):
    ### 基本条件选取
    get_index = include_analyze(basic, kdata, policy_args)
    if get_index is None:
        logger.debug("include_analyze() return None")
        return None
    else:
        [pick_index, take_index] = get_index

    ### 排它条件过滤
    if exclude_analyze(basic, kdata, pick_index, take_index, policy_args):
        logger.debug("exclude_analyze() return True")
        return None

    rise_range = dogen.get_last_rise_range(kdata, 0, sIdx=pick_index)
    if rise_range is not None:
        [pick_index, max_index, inc_close, get_lhigh, tmp_index] = rise_range

    ### 构造结果
    result = {}
    result[dogen.RST_COL_CODE] = basic.name  # 股票代码
    result[dogen.RST_COL_NAME] = basic[dogen.NAME]  #  证券简写
    result[dogen.RST_COL_INDUSTRY] = basic[dogen.INDUSTRY]
    result[dogen.RST_COL_START] = kdata.index[pick_index]
    result[dogen.RST_COL_TAKE_TRADE] = kdata.index[take_index]  # 命中交易日
    result[dogen.RST_COL_LAST_CLOSE] = kdata.iloc[0][dogen.P_CLOSE]  # 最后一日收盘价
    result[dogen.RST_COL_OUTSTANDING] = round(
        kdata.iloc[0][dogen.P_CLOSE] * basic[dogen.OUTSTANDING], 2)  # 流通市值
    result[dogen.RST_COL_INC_HL] = dogen.get_highlimit_trades(kdata,
                                                              eIdx=pick_index +
                                                              1).size
    result[dogen.RST_COL_SCORE] = score_analyze(basic, kdata, pick_index,
                                                take_index, policy_args)
    result[dogen.RST_COL_MATCH_TIME] = dogen.datetime_now()  # 选中时间
    result[dogen.RST_COL_INDEX] = '%s_%s' % (
        basic.name, kdata.index[take_index])  # 唯一标识,用于持久化去重

    return result
Exemplo n.º 5
0
def exclude_analyze(basic, kdata, pick_index, take_index, policy_args):
    """ 根据日线做排除性校验
    """
    max_rise = __parse_policy_args(policy_args, MAX_RISE)
    max_take2low = __parse_policy_args(policy_args, MAX_TAKE2LOW)
    min_falls = __parse_policy_args(policy_args, MIN_FALLS)
    max_rclose = __parse_policy_args(policy_args, MAX_RCLOSE)
    min_ramp = __parse_policy_args(policy_args, MIN_RAMP)
    min_polyf2 = __parse_policy_args(policy_args, MIN_POLYF2)
    max_pclose = __parse_policy_args(policy_args, MAX_PCLOSE)
    outstanding = __parse_policy_args(policy_args, OUTSTANDING)

    ### 取回调最低价
    mini_index = dogen.get_last_column_min(kdata,
                                           dogen.P_CLOSE,
                                           sIdx=take_index,
                                           eIdx=pick_index)

    ### 净资产为负数的
    if basic[dogen.BVPS] < 0:
        logger.debug("Invalid bvps")
        return True

    ### 特征三
    if kdata.iloc[take_index][dogen.P_CLOSE] > max_pclose:
        logger.debug("Too high close price at %s" % kdata.index[take_index])
        return True
    if kdata.iloc[take_index][dogen.P_CLOSE] * basic[
            dogen.OUTSTANDING] > outstanding:
        logger.debug("Too large outstanding at %s" % kdata.index[take_index])
        return True

    ### 特征四
    rise_range = dogen.get_last_rise_range(kdata,
                                           max_rise,
                                           max_fall=max_rise / 2,
                                           eIdx=22)
    if rise_range is not None:
        [min_index, max_index, inc_close, get_lhigh, tmp_index] = rise_range
        if max_rise < dogen.caculate_incr_percentage(
                kdata.iloc[take_index][dogen.P_CLOSE],
                kdata.iloc[min_index][dogen.P_CLOSE]):
            logger.debug("Too large rise-range")
            return True
        pass

    ### 特征五
    if pick_index >= 5:
        high_index = pick_index
        if kdata.iloc[pick_index - 1][dogen.R_CLOSE] > 0:
            high_index = pick_index - 1
        if dogen.caculate_incr_percentage(
                kdata.iloc[take_index][dogen.P_CLOSE],
                kdata.iloc[high_index][dogen.P_CLOSE]) < -min_falls:
            logger.debug("Too low P-CLOSE at take-trade %s" %
                         kdata.index[take_index])
            return True
        if kdata.iloc[take_index][dogen.MA5] < kdata.iloc[take_index +
                                                          1][dogen.MA5]:
            logger.debug("Invalid MA20 at %s" % kdata.index[take_index])
            return True
        if kdata.iloc[take_index][dogen.MA20] < kdata.iloc[take_index +
                                                           1][dogen.MA20]:
            logger.debug("Invalid MA20 at %s" % kdata.index[take_index])
            return True
        if kdata.iloc[take_index][
                dogen.VOLUME] > kdata.iloc[take_index + 1][dogen.VOLUME] * 1.1:
            h2l = dogen.caculate_incr_percentage(
                kdata.iloc[take_index][dogen.P_HIGH],
                kdata.iloc[take_index][dogen.P_LOW])
            c2l = dogen.caculate_incr_percentage(
                kdata.iloc[take_index][dogen.P_CLOSE],
                kdata.iloc[take_index][dogen.P_LOW])
            if c2l * 2 < h2l:
                logger.debug("Get up shadow at %s" % kdata.index[take_index])
                return True
            pass
        pass

    ### 特征七
    if pick_index >= 5:
        for temp_index in range(mini_index - 1, -1, -1):
            ### 下跌
            if kdata.iloc[temp_index][dogen.R_CLOSE] >= 0 or kdata.iloc[
                    temp_index + 1][dogen.R_CLOSE] <= 0:
                continue
            if kdata.iloc[temp_index][dogen.VOLUME] <= kdata.iloc[
                    temp_index + 1][dogen.VOLUME]:
                continue
            ### 放量下跌之后未被上涨突破
            maxi_index = dogen.get_last_column_max(kdata,
                                                   dogen.P_CLOSE,
                                                   eIdx=temp_index)
            if maxi_index is None or kdata.iloc[temp_index][
                    dogen.P_OPEN] > kdata.iloc[maxi_index][dogen.P_CLOSE]:
                logger.debug("Invalid fall-trade at %s" %
                             kdata.index[temp_index])
                return True
            pass
        pass

    ### 特征八
    if pick_index >= 5:
        tdata = kdata[0:mini_index]
        if tdata[tdata[dogen.R_AMP] >= min_ramp].index.size <= 0:
            logger.debug(
                "Don't include trade with 5 percentage R-AMP since %s" %
                kdata.index[mini_index])
            return True
        for temp_index in range(mini_index, 0, -1):
            hl_price = dogen.caculate_l_high(
                kdata.iloc[temp_index][dogen.P_CLOSE])
            tdata = kdata[temp_index - 4:temp_index - 1]
            if tdata[tdata[dogen.P_CLOSE] >= hl_price].index.size > 0:
                logger.debug("Too large heap-close from %s to %s" %
                             (tdata.index[-1], tdata.index[0]))
                return True
            pass
        pass

    return False
Exemplo n.º 6
0
def __statistics_analyze(db, basic, kdata, save_result, args):
    """ 统计单只股票上涨区间

        参数说明:
            basic - 股票基本信息
            kdata - 股票交易数据
            mini_rise - 区间最小涨幅
            mini_hl - 区间最少涨停板数
    """
    ### 参数处理
    mini_rise = __parse_policy_args(args, MINI_RISE)
    mini_hl = __parse_policy_args(args, MINI_HL)

    ### 循环检查
    tmpId = 0
    match = []
    while True:
        rise_range = dogen.get_last_rise_range(kdata, mini_rise, max_fall=round(mini_rise/2, 2), sIdx=tmpId)
        if rise_range is None:
            break
        else:
            [min_index, max_index, inc_close, get_hl, tmpId] = rise_range

        ### 忽略不符合连板要求的区间
        outstanding   = None
        max_hl_serial = 0
        tmp_hl_serial = 0
        for temp_index in range(min_index, max_index-1, -1):
            if kdata.iloc[temp_index][dogen.P_CLOSE] < kdata.iloc[temp_index][dogen.L_HIGH]:
                tmp_hl_serial = 0
                continue
            tmp_hl_serial += 1
            if tmp_hl_serial > max_hl_serial:
                max_hl_serial = tmp_hl_serial
            if tmp_hl_serial > 2 and outstanding is None:
                outstanding = round(kdata.iloc[temp_index][dogen.P_CLOSE] * basic[dogen.OUTSTANDING], 2)
            pass
        if max_hl_serial < mini_hl:
            continue
            
        ### 保存区间结果
        result = {}
        result[dogen.RST_COL_CODE]        = basic.name
        result[dogen.RST_COL_NAME]        = basic[dogen.NAME] #  证券简写
        result[dogen.RST_COL_INDUSTRY]    = basic[dogen.INDUSTRY]
        result[dogen.RST_COL_START]       = kdata.index[min_index]
        result[dogen.RST_COL_END]         = kdata.index[max_index]
        result[dogen.RST_COL_START_CLOSE] = kdata.iloc[min_index][dogen.P_CLOSE] # 起始收盘价
        result[dogen.RST_COL_LAST_CLOSE]  = kdata.iloc[max_index][dogen.P_CLOSE] # 最高收盘价
        result[dogen.RST_COL_RISE_RATE]   = dogen.caculate_incr_percentage(result[dogen.RST_COL_LAST_CLOSE], result[dogen.RST_COL_START_CLOSE])
        result[dogen.RST_COL_INC_HL]      = get_hl
        result[dogen.RST_COL_OUTSTANDING] = outstanding # 流通市值
        result[dogen.RST_COL_MATCH_TIME]  = dogen.datetime_now() # 选中时间
        result[dogen.RST_COL_INDEX]       = '%s_%s' % (basic.name, kdata.index[min_index])

        match.append(result)

        ### 保存结果
        if save_result:
            rdata = db.lookup_statistics_largerise_range(cond={dogen.RST_COL_CODE:basic.name}, descending_by=dogen.RST_COL_START)
            if rdata is None or len(rdata)<=0:
                db.insert_statistics_largerise_range([result], key_name=dogen.RST_COL_INDEX)
                continue
            rlast = rdata[0]
            if rlast[dogen.RST_COL_END] < result[dogen.RST_COL_START]:
                db.insert_statistics_largerise_range([result], key_name=dogen.RST_COL_INDEX)
                continue
            ### 重叠合并
            tdata = kdata[(kdata.index > rlast[dogen.RST_COL_END])&(kdata.index <= result[dogen.RST_COL_END])]

            rlast[dogen.RST_COL_INC_HL] = rlast[dogen.RST_COL_INC_HL] + tdata[tdata[dogen.P_CLOSE]>=tdata[dogen.L_HIGH]].index.size
            rlast[dogen.RST_COL_END] = result[dogen.RST_COL_END]
            rlast[dogen.RST_COL_LAST_CLOSE] = result[dogen.RST_COL_LAST_CLOSE]
            rlast[dogen.RST_COL_RISE_RATE] = dogen.caculate_incr_percentage(rlast[dogen.RST_COL_LAST_CLOSE], rlast[dogen.RST_COL_START_CLOSE])
            rlast[dogen.RST_COL_MATCH_TIME] = result[dogen.RST_COL_MATCH_TIME]
            db.insert_statistics_largerise_range([rlast], key_name=dogen.RST_COL_INDEX)

    return match
Exemplo n.º 7
0
def include_analyze(basic, kdata, policy_args):
    """ 
    """
    ### 参数解析
    take_valid = __parse_policy_args(policy_args, TAKE_VALID)
    pick_valid = __parse_policy_args(policy_args, PICK_VALID)
    min_rise   = __parse_policy_args(policy_args, MIN_RISE)
    max_fallen = __parse_policy_args(policy_args, MAX_FALLEN)

    ### 特征一
    rise_range = dogen.get_last_rise_range(kdata, min_rise, max_fall=max_fallen, eIdx=22)
    if rise_range is None:
        logger.debug("Don't get valid rise-range")
        return None
    else:
        [pick_index, high_index, inc_close, get_lhigh, tmpId] = rise_range
        if pick_index < pick_valid:
            logger.debug("Invalid rise-range from %s" % kdata.index[pick_index])
            return None
        pass

    ### 特征二
    heap_rises = 0
    take_index = None
    for temp_index in range(pick_index, -1, -1):
        temp_close = kdata.iloc[temp_index][dogen.R_CLOSE]
        if temp_close < 0:
            heap_rises = 0
        else:
            heap_rises += temp_close
        if kdata.iloc[temp_index][dogen.MA5] < kdata.iloc[temp_index][dogen.MA20]:
            continue
        if kdata.iloc[temp_index][dogen.MA5] < kdata.iloc[temp_index+1][dogen.MA5]:
            continue
        if kdata.iloc[temp_index][dogen.VOLUME] < kdata.iloc[temp_index+1][dogen.VOLUME]:
            continue
        if heap_rises >= 5:
            if take_index is None or take_index > temp_index:
                take_index = temp_index
            pass
        if temp_close >= 3:
            if take_index is None or take_index > temp_index:
                take_index = temp_index
            pass
        pass
    if take_index is not None:
        high_index = dogen.get_last_column_max(kdata, dogen.P_CLOSE, sIdx=take_index, eIdx=pick_index)
        if high_index != take_index:
            logger.debug("Invalid pclose of take-trade at %s" % kdata.index[take_index])
            return None
        ### take_index之后缩量下跌(限一个交易日),也符合策略
        if take_index == 1\
        and kdata.iloc[take_index-1][dogen.R_CLOSE] < 0\
        and kdata.iloc[take_index-1][dogen.VOLUME]  < kdata.iloc[take_index][dogen.VOLUME]:
            take_index-= 1
        ### 最近收盘价比take_index(不能取更新后值)高更新
        elif take_index <= 3\
        and kdata.iloc[0][dogen.R_CLOSE] > 0\
        and kdata.iloc[0][dogen.P_CLOSE] > kdata.iloc[0][dogen.P_OPEN]\
        and kdata.iloc[0][dogen.P_CLOSE] >= kdata.iloc[take_index][dogen.P_CLOSE]:
            take_index = 0
        pass
    if take_index is None or take_index > take_valid:
        logger.debug("Don't get valid take-trade since %s" % kdata.index[pick_index])
        return None

    return [pick_index, take_index]
Exemplo n.º 8
0
def include_analyze(basic, kdata, policy_args):
    """ 
    """
    ### 参数解析
    take_valid = __parse_policy_args(policy_args, TAKE_VALID)
    pick_valid = __parse_policy_args(policy_args, PICK_VALID)
    min_rise = __parse_policy_args(policy_args, MIN_RISE)
    min_fallen = __parse_policy_args(policy_args, MIN_FALLEN)

    ### 预处理
    if kdata.iloc[0][dogen.MA5] > kdata.iloc[0][dogen.MA20]:
        logger.debug("Invalid MA5&MA20 at %s" % kdata.index[0])
        return None

    ### 特征一
    fall_range = dogen.get_last_fall_range(kdata,
                                           min_fallen,
                                           max_rise=min_rise)
    if fall_range is None:
        logger.debug("Don't get valid fall-range")
        return None
    else:
        [high_index, pick_index, dec_close, get_llow, tmpId] = fall_range
        for temp_index in range(high_index, -1, -1):
            if kdata.iloc[temp_index][dogen.MA5] > kdata.iloc[temp_index][
                    dogen.MA20]:
                continue
            break
    rise_range = dogen.get_last_rise_range(kdata,
                                           min_rise,
                                           max_fall=min_fallen,
                                           sIdx=high_index)
    if rise_range is None:
        logger.debug("Don't get valid rise-range")
        return None
    else:
        [from_index, max_index, inc_close, get_lhigh, tmpId] = rise_range
        if max_index != high_index:
            logger.debug("Invalid rise-range from %s to %s" %
                         (kdata.index[from_index], kdata.index[max_index]))
            return None
        if kdata.iloc[pick_index][dogen.P_CLOSE] < kdata.iloc[from_index][
                dogen.P_CLOSE]:
            logger.debug("Invalid pick-trade at %s" % kdata.index[pick_index])
            return None
        pass

    ### 特征二
    heap_rises = 0
    take_index = None
    if pick_index + 1 < pick_valid:
        for temp_index in range(pick_index - 1, -1, -1):
            if kdata.iloc[temp_index][dogen.P_CLOSE] >= dogen.caculate_l_high(
                    kdata.iloc[pick_index][dogen.P_CLOSE]):
                take_index = temp_index
            pass
        pass
    else:
        if dogen.caculate_incr_percentage(kdata.iloc[0][dogen.P_CLOSE],
                                          kdata.iloc[0][dogen.MA5]) < 3:
            for temp_index in range(pick_index, -1, -1):
                if kdata.iloc[temp_index][dogen.R_CLOSE] > 0 and kdata.iloc[
                        temp_index][dogen.R_AMP] >= 5:
                    if take_index is None or take_index > temp_index:
                        take_index = temp_index
                    pass
                pass
            pass
        for temp_index in range(pick_index, -1, -1):
            temp_close = kdata.iloc[temp_index][dogen.R_CLOSE]
            if temp_close < 0:
                heap_rises = 0
            else:
                heap_rises += temp_close
            if kdata.iloc[temp_index][dogen.P_CLOSE] < kdata.iloc[temp_index][
                    dogen.P_OPEN]:
                continue
            if heap_rises >= 5:
                if take_index is None or take_index > temp_index:
                    take_index = temp_index
                pass
            if temp_close >= 3 and kdata.iloc[temp_index][
                    dogen.P_CLOSE] > kdata.iloc[temp_index][dogen.P_OPEN]:
                if take_index is None or take_index > temp_index:
                    take_index = temp_index
                pass
            pass
        ### MACD点校验
        temp_index = 0
        if (kdata.iloc[temp_index][dogen.MACD] >= 0.01) and (
                dogen.forecast_macd(kdata[temp_index:-1][dogen.MACD]) <= 0.01):
            if take_index is None or take_index > temp_index:
                take_index = temp_index
            pass
        if take_index is not None:
            ### take_index之后缩量下跌(限一个交易日),也符合策略
            if take_index == 1\
            and kdata.iloc[take_index-1][dogen.R_CLOSE] < 0\
            and kdata.iloc[take_index-1][dogen.VOLUME]  < kdata.iloc[take_index][dogen.VOLUME]:
                take_index -= 1
            ### 最近收盘价比take_index(不能取更新后值)高更新
            elif take_index <= 3\
            and kdata.iloc[0][dogen.R_CLOSE] > 0\
            and kdata.iloc[0][dogen.P_CLOSE] > kdata.iloc[0][dogen.P_OPEN]\
            and kdata.iloc[0][dogen.P_CLOSE] >= kdata.iloc[take_index][dogen.P_CLOSE]:
                take_index = 0
            pass
        pass
    if take_index is None or take_index > take_valid:
        logger.debug("Don't get valid take-trade since %s" %
                     kdata.index[pick_index])
        return None

    return [pick_index, take_index, rise_range]
Exemplo n.º 9
0
def include_analyze(basic, kdata, policy_args):
    """ 
    """
    ### 参数解析
    take_valid = __parse_policy_args(policy_args, TAKE_VALID)
    pick_start = __parse_policy_args(policy_args, PICK_START)
    pick_end = __parse_policy_args(policy_args, PICK_END)
    min_fallen = __parse_policy_args(policy_args, MIN_FALLEN)

    ### 预处理
    if kdata.iloc[0][dogen.MA5] > kdata.iloc[0][dogen.MA20]:
        logger.debug("Invalid MA5&MA20 at %s" % kdata.index[0])
        return None
    if dogen.get_last_column_min(kdata, dogen.P_CLOSE, eIdx=30) > pick_end:
        logger.debug("Invalid pick-trade")
        return None

    ### 特征一
    fall_range = dogen.get_last_fall_range(kdata,
                                           min_fallen,
                                           max_rise=min_fallen)
    if fall_range is None:
        logger.debug("Don't get valid fall-range")
        return None
    else:
        [high_index, pick_index, dec_close, get_llow, tmpId] = fall_range
        if pick_index >= pick_end:
            logger.debug("Too early pick-trade at %s" %
                         kdata.index[pick_index])
            return None
        tdata = kdata[0:pick_index]
        if tdata[tdata[dogen.MA5] > tdata[dogen.MA20]].index.size > 0:
            logger.debug("Invalid pick-trade at %s" % kdata.index[pick_index])
            return None
        pass

    ### 特征二
    heap_rises = 0
    take_index = None
    if pick_index + 1 < pick_start:
        if pick_index == 1 and kdata.iloc[pick_index][
                dogen.R_CLOSE] < -3 and kdata.iloc[pick_index -
                                                   1][dogen.R_CLOSE] > 0:
            take_index = pick_index
        for temp_index in range(pick_index - 1, -1, -1):
            if kdata.iloc[temp_index][dogen.P_CLOSE] >= dogen.caculate_l_high(
                    kdata.iloc[pick_index][dogen.P_CLOSE]):
                take_index = temp_index
            pass
        rise_range = dogen.get_last_rise_range(kdata, 5, eIdx=pick_index + 1)
        if (rise_range
                is not None) and (dogen.forecast_macd(kdata[dogen.MACD]) >= 0):
            [min_index, max_index, inc_close, get_lhigh, tmpIdx] = rise_range
            if take_index is None or take_index > max_index:
                take_index = max_index
            pass
        pass
    else:
        rise_range = dogen.get_last_rise_range(kdata, 5, eIdx=pick_index + 1)
        if rise_range is not None:
            [min_index, max_index, inc_close, get_lhigh, tmpIdx] = rise_range
            if take_index is None or take_index > max_index:
                take_index = max_index
            pass
        for temp_index in range(pick_index, -1, -1):
            if kdata.iloc[temp_index][dogen.R_CLOSE] >= 3:
                if take_index is None or take_index > temp_index:
                    take_index = temp_index
                pass
            if kdata.iloc[temp_index][dogen.R_AMP] >= 5 and kdata.iloc[
                    temp_index][dogen.R_CLOSE] >= 0:
                if take_index is None or take_index > temp_index:
                    take_index = temp_index
                pass
            pass
        ### MACD点校验
        temp_index = 0
        if (kdata.iloc[temp_index][dogen.MACD] <=
                -0.01) and (dogen.forecast_macd(
                    kdata[temp_index:-1][dogen.MACD]) >= -0.01):
            if take_index is None or take_index > temp_index:
                take_index = temp_index
            pass
        if (kdata.iloc[temp_index][dogen.MACD] >= 0.01) and (
                dogen.forecast_macd(kdata[temp_index:-1][dogen.MACD]) <= 0.01):
            if take_index is None or take_index > temp_index:
                take_index = temp_index
            pass
    if take_index is not None:
        ### take_index之后缩量下跌(限一个交易日),也符合策略
        if take_index == 1\
        and kdata.iloc[take_index-1][dogen.R_CLOSE] < 0\
        and kdata.iloc[take_index-1][dogen.VOLUME]  < kdata.iloc[take_index][dogen.VOLUME]:
            take_index -= 1
        ### 最近收盘价比take_index(不能取更新后值)高更新
        elif kdata.iloc[0][dogen.R_CLOSE] >= 0\
        and kdata.iloc[0][dogen.P_CLOSE] >= kdata.iloc[0][dogen.P_OPEN]\
        and kdata.iloc[0][dogen.P_CLOSE] >= kdata.iloc[take_index][dogen.P_CLOSE]:
            take_index = 0
        ### MACD点校验
        if (kdata.iloc[0][dogen.MACD] <= -0.01) and (dogen.forecast_macd(
                kdata[dogen.MACD]) >= -0.01):
            take_index = 0
        pass
    if take_index is None or take_index > take_valid:
        logger.debug("Don't get valid take-trade since %s" %
                     kdata.index[pick_index])
        return None

    return [pick_index, take_index, fall_range]