Ta-Lib源码解析(三):蜡烛图指标 (Candlestick Indicator) #(附Python重构代码和示意图)(补充中)

3.(蜡烛图指标)Candlestick Indicator

蜡烛图指标为识别K线形态的指标,笔者认为重点在于定义方式所提供的思路,背后的投资逻辑可以借鉴但不建议直接参考

文档的翻译内容和源码逻辑存在不一致的现象,笔者分别列出中文百科中的定义和源码实际逻辑,供大家参考

一. 前言

技术分析理论中的一些模糊词,ta-lib中给出了精确的定义

源码
ta_global.c

TA_RetCode TA_RestoreCandleDefaultSettings( TA_CandleSettingType settingType )
{
    const TA_CandleSetting TA_CandleDefaultSettings[] = {
        /* real body is long when it's longer than the average of the 10 previous candles' real body */
        { TA_BodyLong, TA_RangeType_RealBody, 10, 1.0 },
        /* real body is very long when it's longer than 3 times the average of the 10 previous candles' real body */
        { TA_BodyVeryLong, TA_RangeType_RealBody, 10, 3.0 },
        /* real body is short when it's shorter than the average of the 10 previous candles' real bodies */
        { TA_BodyShort, TA_RangeType_RealBody, 10, 1.0 },
        /* real body is like doji's body when it's shorter than 10% the average of the 10 previous candles' high-low range */
        { TA_BodyDoji, TA_RangeType_HighLow, 10, 0.1 },
        /* shadow is long when it's longer than the real body */
        { TA_ShadowLong, TA_RangeType_RealBody, 0, 1.0 },
        /* shadow is very long when it's longer than 2 times the real body */
        { TA_ShadowVeryLong, TA_RangeType_RealBody, 0, 2.0 },
        /* shadow is short when it's shorter than half the average of the 10 previous candles' sum of shadows */
        { TA_ShadowShort, TA_RangeType_Shadows, 10, 1.0 },
        /* shadow is very short when it's shorter than 10% the average of the 10 previous candles' high-low range */
        { TA_ShadowVeryShort, TA_RangeType_HighLow, 10, 0.1 },
        /* when measuring distance between parts of candles or width of gaps */
        /* "near" means "<= 20% of the average of the 5 previous candles' high-low range" */
        { TA_Near, TA_RangeType_HighLow, 5, 0.2 },
        /* when measuring distance between parts of candles or width of gaps */
        /* "far" means ">= 60% of the average of the 5 previous candles' high-low range" */
        { TA_Far, TA_RangeType_HighLow, 5, 0.6 },
        /* when measuring distance between parts of candles or width of gaps */
        /* "equal" means "<= 5% of the average of the 5 previous candles' high-low range" */
        { TA_Equal, TA_RangeType_HighLow, 5, 0.05 }
    };

    int i;
    if( settingType > TA_AllCandleSettings )
        return TA_BAD_PARAM;
    if( settingType == TA_AllCandleSettings )
        for( i = 0; i < TA_AllCandleSettings; ++i )
            TA_Globals->candleSettings[i] = TA_CandleDefaultSettings[i];
    else
        TA_Globals->candleSettings[settingType] = TA_CandleDefaultSettings[settingType];
    return TA_SUCCESS;
}

翻译

TA_BodyLong: 当期蜡烛体长度超过过去十日蜡烛体长度的平均值
TA_ShadowVeryLong: 当期蜡烛体长度超过过去十日蜡烛体长度的平均值的三倍
TA_BodyShort: 当期蜡烛体长度低于过去十日蜡烛体长度的平均值
TA_BodyDoji: 蜡烛体类似于十字线,短于过去十日蜡烛体长度的平均值的10%
TA_ShadowLong: 当期影线长度长于蜡烛体长度
TA_ShadowVeryLong: 当期影线长度长于蜡烛体长度两倍
TA_ShadowShort: 当期影线短于前十期影线长度平均值的一半
TA_ShadowVeryShort: 当期影线短于前十期high-low长度平均值的10%
TA_Near: 小于前五期20%的high-low长度平均值
TA_Far: 大于前五期60%的high-low长度平均值
TA_Equal: 小于前五期5%的high-low长度平均值

二. 单K线模型

CDLBELTHOLD(捉腰带线)

python函数原型:

cdlbelthold= CDL2BELTHOLD(open, high, low, close)

解释:

看涨为例,其开市价位于当日的最低点,然后市场一路上扬。

在这里插入图片描述
重构代码(来自ta_CDLBELTHOLD.c, 有修改, 只保留逻辑):

def CDLBELTHOLD_(open, high, low, close):
    _len = len(open)
    open, high, low, close = pd.Series(open), pd.Series(high), pd.Series(low), pd.Series(close)
    if _len >= 11:
        high_low_range = high - low
        shadow_very_short = high_low_range.rolling(10).mean().shift(1) * 0.1
        real_body = abs(open - close)
        body_long = real_body.rolling(10).mean().shift(1)
        shadow_upper = high - np.maximum(close, open)
        shadow_lower = np.minimum(close, open) - low
        condition_1 = real_body > body_long
        condition_2_1 = (close > open) & (shadow_lower < shadow_very_short)
        condition_2_2 = (close < open) & (shadow_upper < shadow_very_short)
        cdlbelthold = np.where(condition_1 & condition_2_1, 100, 0)
        cdlbelthold = np.where(condition_1 & condition_2_2, -100, cdlbelthold)
    else:
        cdlbelthold = np.zeros(_len).astype(int)
    return cdlbelthold

CDLCLOSINGMARUBOZU (收盘缺影线)

python函数原型:

cdlclosingmarubozu= CDLCLOSINGMARUBOZU(open, high, low, close)

解释:

中文百科:一日K线模式,以阳线为例,最低价低于开盘价,收盘价等于最高价, 预示着趋势持续。
源码与抓腰带线几乎相同

在这里插入图片描述
重构代码(来自ta_CDLBELTHOLD.c, 与BELTHOLD极其相似应该直接复制粘贴该条件):

def CDLCLOSINGMARUBOZU_(open, high, low, close):
    _len = len(open)
    open, high, low, close = pd.Series(open), pd.Series(high), pd.Series(low), pd.Series(close)
    if _len >= 11:
        high_low_range = high - low
        shadow_very_short = high_low_range.rolling(10).mean().shift(1) * 0.1
        real_body = abs(open - close)
        body_long = real_body.rolling(10).mean().shift(1)
        shadow_upper = high - np.maximum(close, open)
        shadow_lower = np.minimum(close, open) - low
        condition_1 = real_body > body_long
        condition_2_1 = (close > open) & (shadow_upper < shadow_very_short)
        condition_2_2 = (close < open) & (shadow_lower < shadow_very_short)
        cdlclosingmarubozu = np.where(condition_1 & condition_2_1, 100, 0)
        cdlclosingmarubozu = np.where(condition_1 & condition_2_2, -100, cdlclosingmarubozu)
    else:
        cdlclosingmarubozu = np.zeros(_len).astype(int)
    return cdlclosingmarubozu

CDLDOJI(doji十字)

python函数原型:

cdldoji = CDLDOJI(open, high, low, close)

解释:

单k线模型,十字线

在这里插入图片描述
重构代码(来自ta_CDLDOJI.c, 有修改, 只保留逻辑):

def CDLDOJI_(open, high, low, close):
    _len = len(open)
    open, high, low, close = pd.Series(open), pd.Series(high), pd.Series(low), pd.Series(close)
    if _len >= 11:
        real_body = abs(open - close)
        high_low_range = abs(high - low)
        doji = high_low_range.rolling(10).mean().shift(1) * 0.1
        condition = real_body < doji
        cdldoji = np.where(condition, 100, 0)
    else:
        cdldoji = np.zeros(_len).astype(int)
    return cdldoji

CDLDRAGONFLYDOJI (蜻蜓十字)

python函数原型:

cdldragonflydoji= CDLDRAGONFLYDOJI (open, high, low, close)

解释:

单k线模型,T字线

在这里插入图片描述

重构代码(来自ta_CDLDRAGONFLYDOJI.c, 有修改, 只保留逻辑):

def CDLDRAGONFLYDOJI_(open, high, low, close):
    _len = len(open)
    open, high, low, close = pd.Series(open), pd.Series(high), pd.Series(low), pd.Series(close)
    if _len >= 11:
        real_body = abs(open - close)
        high_low_range = abs(high - low)
        shadow_upper = high - np.maximum(close, open)
        shadow_lower = np.minimum(close, open) - low
        shadow_very_short = high_low_range.rolling(10).mean().shift(1) * 0.1
        doji = high_low_range.rolling(10).mean().shift(1) * 0.1
        condition = (real_body < doji) & (shadow_upper < shadow_very_short) & (shadow_lower > shadow_very_short)
        cdldragonflydoji = np.where(condition, 100, 0)
    else:
        cdldragonflydoji = np.zeros(_len).astype(int)
    return cdldragonflydoji

CDLGRAVESTONEDOJI (墓碑十字)

python函数原型:

cdlgravestonedoji = CDLGRAVESTONEDOJI(open, high, low, close)

解释:

单k线模型,倒T字线

在这里插入图片描述
重构代码(来自ta_CDLGRAVESTONEDOJI.c, 有修改, 只保留逻辑):

def CDLGRAVESTONEDOJI_(open, high, low, close):
    _len = len(open)
    open, high, low, close = pd.Series(open), pd.Series(high), pd.Series(low), pd.Series(close)
    if _len >= 11:
        real_body = abs(open - close)
        high_low_range = abs(high - low)
        shadow_upper = high - np.maximum(close, open)
        shadow_lower = np.minimum(close, open) - low
        shadow_very_short = high_low_range.rolling(10).mean().shift(1) * 0.1
        doji = high_low_range.rolling(10).mean().shift(1) * 0.1
        condition = (real_body < doji) & (shadow_upper > shadow_very_short) & (shadow_lower < shadow_very_short)
        cdlgravestonedoji = np.where(condition, 100, 0)
    else:
        cdlgravestonedoji = np.zeros(_len).astype(int)
    return cdlgravestonedoji

三. 双K线模型

CDLCOUNTERATTACK (反击线)

python函数原型:

cdlcounterattack = CDLCOUNTERATTACK(open, high, low, close)

解释:

双k线模型,一大阴一大阳,且收盘价接近,
延续第二根K线地行情

在这里插入图片描述
重构代码(来自ta_CDLCOUNTERATTACK.c, 有修改, 只保留逻辑):

def CDLCOUNTERATTACK_(open, high, low, close):
    _len = len(open)
    open, high, low, close = pd.Series(open), pd.Series(high), pd.Series(low), pd.Series(close)
    if _len >= 12:
        real_body = abs(open - close)
        high_low_range = abs(high - low)
        equal = high_low_range.rolling(5).mean().shift(1) * 0.05
        body_long = real_body.rolling(10).mean().shift(1)
        condition_1_1 = (close.shift(1) > open.shift(1)) & \
                        (close < open)
        condition_1_2 = (close.shift(1) < open.shift(1)) & \
                        (close > open)
        condition_2 = (real_body.shift(1) > body_long.shift(1)) & \
                      (real_body > body_long) & \
                      (close <= close.shift(1) + equal.shift(1)) & \
                      (close >= close.shift(1) - equal.shift(1))

        cdlcounterattack = np.where(condition_1_1 & condition_2, -100, 0)
        cdlcounterattack = np.where(condition_1_2 & condition_2, 100, cdlcounterattack)
    else:
        cdlcounterattack = np.zeros(_len).astype(int)
    return cdlcounterattack

CDLDARKCLOUDCOVER(乌云压顶)

python函数原型:

cdldarkcloudcover = CDLDARKCLOUDCOVER(open, high, low, close, penetration=0.5)

解释:

双k线模型,第一根大阳
第二根阴线开盘高于前一日最高
收盘再前一日实体下半部分
见顶信号

在这里插入图片描述

重构代码(来自ta_CDLDARKCLOUDCOVER.c, 有修改, 只保留逻辑):

def CDLDARKCLOUDCOVER_(open, high, low, close, penetration=0.5):
    _len = len(open)
    open, high, low, close = pd.Series(open), pd.Series(high), pd.Series(low), pd.Series(close)
    if _len >= 12:
        real_body = abs(open - close)
        high_low_range = abs(high - low)
        equal = high_low_range.rolling(5).mean().shift(1) * 0.05
        body_long = real_body.rolling(10).mean().shift(1)
        condition_1 = (close.shift(1) > open.shift(1)) & \
                      (real_body.shift(1) > body_long.shift(1))
        condition_2 = (close < open) & \
                      (open > high.shift(1)) & \
                      (close > open.shift(1)) & \
                      (close < close.shift(1) - real_body.shift(1) * penetration)
        cdldarkcloudcover = np.where(condition_1 & condition_2, -100, 0)
    else:
        cdldarkcloudcover = np.zeros(_len).astype(int)
    return cdldarkcloudcover

CDLDOJISTAR(十字星)

python函数原型:

cdldojistar = CDLDOJISTAR(open, high, low, close)

解释:

双k线模型,第一根大阴或者大阳
第二根高开or低开,十字星
反转信号

在这里插入图片描述

重构代码(来自ta_CDLDOJISTAR.c, 有修改, 只保留逻辑):

def CDLDOJISTAR_(open, high, low, close):
    _len = len(open)
    open, high, low, close = pd.Series(open), pd.Series(high), pd.Series(low), pd.Series(close)
    if _len >= 12:
        real_body = abs(open - close)
        high_low_range = abs(high - low)
        doji = high_low_range.rolling(10).mean().shift(1) * 0.1
        body_long = real_body.rolling(10).mean().shift(1)
        condition_1_1 = (close.shift(1) > open.shift(1)) & \
                        (np.maximum(close, open) > close.shift(1))
        condition_1_2 = (close.shift(1) < open.shift(1)) & \
                        (np.minimum(close, open) < close.shift(1))
        condition_2 = (real_body.shift(1) > body_long.shift(1)) & \
                      (real_body <= doji)
        cdldojistar = np.where(condition_1_1 & condition_2, -100, 0)
        cdldojistar = np.where(condition_1_2 & condition_2, 100, cdldojistar)
    else:
        cdldojistar = np.zeros(_len).astype(int)
    return cdldojistar

CDLENGULFING (吞噬模式)

python函数原型:

cdlengulfing= CDLENGULFING(open, high, low, close)

解释:

两日K线模式,分多头吞噬和空头吞噬,以多头吞噬为例,第一日为阴线, 第二日阳线,第一日的开盘价和收盘价在第二日开盘价收盘价之内,但不能完全相同。

在这里插入图片描述

重构代码(来自ta_CDLENGULFING.c, 有修改, 只保留逻辑):

def CDLENGULFING_(open, high, low, close):
    _len = len(open)
    open, high, low, close = pd.Series(open), pd.Series(high), pd.Series(low), pd.Series(close)
    if _len >= 12:
        condition_1 = (close.shift(1) < open.shift(1)) & (close > open) & \
                      (close > open.shift(1)) & (open < close.shift(1))
        condition_2 = (close.shift(1) > open.shift(1)) & (close < open) & \
                      (close < open.shift(1)) & (open > close.shift(1))
        cdlengulfing = np.where(condition_1, 100, 0)
        cdlengulfing = np.where(condition_2, -100, cdlengulfing)
    else:
        cdlengulfing = np.zeros(_len).astype(int)
    return cdlengulfing

CDLHAMMER(锤头)

python函数原型:

cdlhammer = CDLHAMMER(open, high, low, close)

解释:

两日K线模式,第二日为长下影短上影的短实体,且实体下沿低于前一日最低价(Near)

重构代码(来自ta_CDLHAMMER.c, 有修改, 只保留逻辑):

def CDLHAMMER_(open, high, low, close):
    _len = len(open)
    open, high, low, close = pd.Series(open), pd.Series(high), pd.Series(low), pd.Series(close)
    if _len >= 12:
        condition_1 = (close.shift(1) < open.shift(1)) & (close > open) & \
                      (close > open.shift(1)) & (open < close.shift(1))
        condition_2 = (close.shift(1) > open.shift(1)) & (close < open) & \
                      (close < open.shift(1)) & (open > close.shift(1))
        cdlengulfing = np.where(condition_1, 100, 0)
        cdlengulfing = np.where(condition_2, -100, cdlengulfing)
    else:
        cdlengulfing = np.zeros(_len).astype(int)
    return cdlengulfing

四. 三K线模型

CDL2CROW(双乌鸦)

python函数原型:

cdl2crows= CDL2CROWS(open, high, low, close)

解释:

对于双乌鸦形状的定义,中文百科和ta-lib中定义的方式不一样,

中文百科中第一天长阳线后,第二天高开收阴, 形成缺口,第三天的大阴线包裹住第二天的阴线

ta-lib中第一根和第二跟定义相同,第三跟阴线开盘价再第二根阴线开收之间,收盘价再第一根阳线开收之间

在这里插入图片描述

投资逻辑(仅供参考,不宜借鉴):按照主散博弈模型,主力推高出大阳线吸引散户关注,高开吸引散户进场,主力出货一部分,再次高开吸引散户进场,主力在次出货

第一根长阳线:
O P E N − 2 − C L O S E − 2 > A V G ( ∣ O P E N − C L O S E ∣ ) OPEN_{-2}-CLOSE_{-2} > AVG(|OPEN-CLOSE|) OPEN2CLOSE2>AVG(OPENCLOSE)
第二根阴线:
O P E N − 1 > C L O S E − 1 > C L O S E − 2 OPEN_{-1} > CLOSE_{-1} > CLOSE_{-2} OPEN1>CLOSE1>CLOSE2
第三根阴线:
O P E N − 1 < O P E N 0 < C L O S E − 2 OPEN_{-1} < OPEN_{0} < CLOSE_{-2} OPEN1<OPEN0<CLOSE2

重构代码(来自ta_CDL2CROW.c, 有修改, 只保留逻辑):

def CDL2CROWS_(open, high, low, close):
    _len = len(open)
    open, high, low, close = pd.Series(open), pd.Series(high), pd.Series(low), pd.Series(close)
    if _len >= 13:
        real_body = abs(open - close)
        body_long = real_body.rolling(10).mean().shift(1)
        # 第一根大阳线
        condition1 = (close.shift(2) - open.shift(2)) > body_long.shift(2)
        # 第二根阴线
        condition2 = (open.shift(1) > close.shift(1)) & (close.shift(1) > close.shift(2))
        # 第三根阴线
        condition3 = (open.shift(1) > open) & (open > close.shift(1)) & \
                     (close.shift(2) > close) & (close > open.shift(2))
        cdl2crow = np.where(condition1 & condition2 & condition3, -100, 0)
        cdl2crow[:12] = 0
    else:
        cdl2crow = np.zeros(_len).astype(int)
    return cdl2crow

CDL3BLACKCROW(三乌鸦)

python函数原型:

cdl3blackcrows= CDL3BLACKCROWS(open, high, low, close)

解释:

(1)连续三天长阴线。(ta-lib无此要求
(2)每天的收盘出现新低。
(3)每天的开盘在前一天的实体内。
(4)每天的收盘等于或接近当天的最低。
(5)第一根阴线跌破前阳线最高点(ta-lib特有要求

在这里插入图片描述

第一根阳线:
O P E N − 3 − C L O S E − 3 > 0 OPEN_{-3}-CLOSE_{-3} > 0 OPEN3CLOSE3>0
第一根阴线:
O P E N − 2 > C L O S E − 2 C L O S E − 2 < H I G H − 3 C L O S E − 2 − L O W − 2 < A V G ( C L O S E − L O W ) − 2 OPEN_{-2} > CLOSE_{-2} \\ CLOSE_{-2} < HIGH_{-3} \\ CLOSE_{-2} - LOW_{-2} < AVG(CLOSE-LOW)_{-2} OPEN2>CLOSE2CLOSE2<HIGH3CLOSE2LOW2<AVG(CLOSELOW)2
第二根阴线:
O P E N − 1 > C L O S E − 1 C L O S E − 1 < C L O S E − 2 C L O S E − 1 − L O W − 1 < A V G ( C L O S E − L O W ) − 1 OPEN_{-1} > CLOSE_{-1} \\ CLOSE_{-1} < CLOSE_{-2} \\ CLOSE_{-1} - LOW_{-1} < AVG(CLOSE-LOW)_{-1} OPEN1>CLOSE1CLOSE1<CLOSE2CLOSE1LOW1<AVG(CLOSELOW)1
第三根阴线:
O P E N 0 > C L O S E 0 C L O S E 0 < C L O S E − 1 C L O S E 0 − L O W 0 < A V G ( C L O S E − L O W ) 0 OPEN_{0} > CLOSE_{0} \\ CLOSE_{0} < CLOSE_{-1} \\ CLOSE_{0} - LOW_{0} < AVG(CLOSE-LOW)_{0} OPEN0>CLOSE0CLOSE0<CLOSE1CLOSE0LOW0<AVG(CLOSELOW)0

重构代码(来自ta_CDL3BLACKCROWS.c, 有修改, 只保留逻辑):

def CDL3BLACKCROWS_(open, high, low, close):
    _len = len(open)
    open, high, low, close = pd.Series(open), pd.Series(high), pd.Series(low), pd.Series(close)
    if _len >= 14:
        shadow_lower = np.minimum(close, open) - low
        high_low_range = abs(high - low)
        shadow_very_short = high_low_range.rolling(10).mean().shift(1) * 0.1
        # 第一根阳线
        condition1 = (close.shift(3) - open.shift(3)) > 0
        # 第一根阴线
        condition2 = (open.shift(2) > close.shift(2)) & \
                     (close.shift(2) < high.shift(3)) & \
                     (shadow_lower.shift(2) < shadow_very_short.shift(2))
        # 第二根阴线
        condition3 = (open.shift(1) > close.shift(1)) & \
                     (close.shift(1) < close.shift(2)) & \
                     (shadow_lower.shift(1) < shadow_very_short.shift(1))
        # 第三根阴线
        condition4 = (open > close) & \
                     (close < close.shift(1)) & \
                     (shadow_lower < shadow_very_short)
        cdl3blackcrows = np.where(condition1 & condition2 & condition3 & condition4, -100, 0)
        cdl3blackcrows[:13] = 0
    else:
        cdl3blackcrows = int(np.zeros(_len))
    return cdl3blackcrows

CDL3INSIDE(三内部上涨和下跌)

python函数原型:

cdl2inside = CDL3INSIDE(open, high, low, close)

解释:

  1. 母子信号是该信号的主要组成部分。第一天包裹第二天
  2. 母蜡烛的颜色应当与长蜡烛日的颜色相反。
  3. 如果趋势表明为上涨,则第三天的收盘价高于第一天的开盘价;否则,第三天的收盘价会低于第一天的开盘价。

以下四种情况皆会产生信号(四种情况,一二涨,三四跌)
三内部下跌

三内部下跌
在这里插入图片描述
在这里插入图片描述

三内部上涨

第一根阴线:
O P E N − 2 − C L O S E − 2 > A V G ( ∣ O P E N − C L O S E ∣ ) OPEN_{-2}-CLOSE_{-2} > AVG(|OPEN-CLOSE|) OPEN2CLOSE2>AVG(OPENCLOSE)
第二根线:
∣ O P E N − 1 − C L O S E − 1 ∣ < A V G ( ∣ O P E N − C L O S E ∣ ) m a x ( O P E N − 1 , C L O S E − 1 ) < O P E N − 2 m i n ( O P E N − 1 , C L O S E − 1 ) > C L O S E − 2 |OPEN_{-1} - CLOSE_{-1}| < AVG(|OPEN-CLOSE|) \\ max(OPEN_{-1}, CLOSE_{-1}) < OPEN_{-2} \\ min(OPEN_{-1}, CLOSE_{-1}) > CLOSE_{-2} \\ OPEN1CLOSE1<AVG(OPENCLOSE)max(OPEN1,CLOSE1)<OPEN2min(OPEN1,CLOSE1)>CLOSE2
第三根阳线:
O P E N < C L O S E C L O S E > O P E N − 2 OPEN < CLOSE \\ CLOSE > OPEN_{-2} \\ OPEN<CLOSECLOSE>OPEN2

三内部下跌

第一根阳线:
C L O S E − 2 − O P E N − 2 > A V G ( ∣ O P E N − C L O S E ∣ ) CLOSE_{-2}-OPEN_{-2} > AVG(|OPEN-CLOSE|) CLOSE2OPEN2>AVG(OPENCLOSE)
第二根线:
∣ O P E N − 1 − C L O S E − 1 ∣ < A V G ( ∣ O P E N − C L O S E ∣ ) m a x ( O P E N − 1 , C L O S E − 1 ) < C L O S E − 2 m i n ( O P E N − 1 , C L O S E − 1 ) > O P E N − 2 |OPEN_{-1} - CLOSE_{-1}| < AVG(|OPEN-CLOSE|) \\ max(OPEN_{-1}, CLOSE_{-1}) < CLOSE_{-2} \\ min(OPEN_{-1}, CLOSE_{-1}) > OPEN_{-2} \\ OPEN1CLOSE1<AVG(OPENCLOSE)max(OPEN1,CLOSE1)<CLOSE2min(OPEN1,CLOSE1)>OPEN2
第三根阳线:
O P E N > C L O S E C L O S E < O P E N − 2 OPEN > CLOSE \\ CLOSE < OPEN_{-2} \\ OPEN>CLOSECLOSE<OPEN2

重构代码(来自ta_CDL3INSIDE.c, 有修改, 只保留逻辑):

def CDL3INSIDE_(open, high, low, close):
    _len = len(open)
    open, high, low, close = pd.Series(open), pd.Series(high), pd.Series(low), pd.Series(close)
    if _len >= 13:
        body_long = abs(open - close)
        avg_body_long = body_long.rolling(10).mean().shift(1)
        # 第一根母线
        condition1 = abs(close.shift(2) - open.shift(2)) > avg_body_long.shift(2)
        # 第二根子线
        condition2 = (abs(close.shift(1) - open.shift(1)) <= avg_body_long.shift(1)) & \
                     (np.maximum(close.shift(1), open.shift(1)) < np.maximum(close.shift(2), open.shift(2))) & \
                     (np.minimum(close.shift(1), open.shift(1)) > np.minimum(close.shift(2), open.shift(2)))
        # 第三根长阴线
        condition3_1 = (close.shift(2) > open.shift(2)) & (close < open) & (close < open.shift(2))
        # 第三根长阳线
        condition3_2 = (close.shift(2) < open.shift(2)) & (close > open) & (close > open.shift(2))

        cdl3inside = np.where(condition1 & condition2 & condition3_1, -100, 0)
        cdl3inside = np.where(condition1 & condition2 & condition3_2, 100, cdl3inside)
        cdl3inside[:12] = 0
    else:
        cdl3inside = np.zeros(_len).astype(int)
    return cdl3inside

CDL3OUTSIDE(三外部上涨和下跌)

python函数原型:

cdl2outside = CDL3OUTSIDE(open, high, low, close)

解释:

  1. 母子信号是该信号的主要组成部分,第一天包裹第二天
  2. 第一日和第二日的颜色相反
  3. 如果趋势表明为上涨,则第三天的收盘价高于第一天的开盘价;否则,第三天的收盘价会低于第一天的开盘价。

在这里插入图片描述

三外部上涨(下跌同理)

第一根阴线:
O P E N − 2 > C L O S E − 2 OPEN_{-2}>CLOSE_{-2} OPEN2>CLOSE2
第二根阳线:
O P E N − 1 < C L O S E − 1 C L O S E − 1 > O P E N − 2 O P E N − 1 < C L O S E − 2 OPEN_{-1}<CLOSE_{-1} \\ CLOSE_{-1}>OPEN_{-2} \\ OPEN_{-1}<CLOSE_{-2} \\ OPEN1<CLOSE1CLOSE1>OPEN2OPEN1<CLOSE2
第三根阳线:
C L O S E < C L O S E − 1 CLOSE < CLOSE_{-1} CLOSE<CLOSE1
重构代码(来自ta_CDL3OUTSIDE.c, 有修改, 只保留逻辑):

def CDL3OUTSIDE_(open, high, low, close):
    _len = len(open)
    open, high, low, close = pd.Series(open), pd.Series(high), pd.Series(low), pd.Series(close)
    if _len >= 4:
        # 第一根子线阳
        condition_1_1 = close.shift(2) > open.shift(2)
        # 第二根母线阴
        condition_1_2 = (open.shift(1) > close.shift(1)) & \
                        (close.shift(1) < open.shift(2)) & \
                        (open.shift(1) > close.shift(2))
        # 第三根阴线
        condition_1_3 = (close < close.shift(1))
        cdl3outside = np.where(condition_1_1 & condition_1_2
                               & condition_1_3, -100, 0)

        # 第一根子线阳
        condition_2_1 = close.shift(2) < open.shift(2)
        # 第二根母线阴
        condition_2_2 = (open.shift(1) < close.shift(1)) & \
                       (close.shift(1) > open.shift(2)) & \
                       (open.shift(1) < close.shift(2))
        # 第三根阴线
        condition_2_3 = (close > close.shift(1))
        cdl3outside = np.where(condition_2_1 & condition_2_2
                               & condition_2_3, 100, cdl3outside)
        cdl3outside[:3] = 0
    else:
        cdl3outside = np.zeros(_len).astype(int)
    return cdl3outside

CDL3STARSINSOUTH (南方三星)

python函数原型:

cdl3starsinsouth = CDL3STARSINSOUTH(open, high, low, close)

解释:

(1)三天连续阴线
(2)第一天大阴线且有较长的大影线
(3)第二天开盘位于前一天实体内,收盘位于前一天下影线内,且实体短于前一天
(4)第三天上下影线特别短(veryshort),实体短(bodyshort)且位于前一天的high,low之间
(5)见底信号

在这里插入图片描述
第一根阴线:
C L O S E − 2 < O P E N − 2 O P E N − 2 − C L O S E − 2 > A V G ( ∣ C L O S E − O P E N ∣ ) C L O S E − 2 − L O W − 2 > B O D Y _ L O N G − 2 CLOSE_{-2} < OPEN_{-2} \\ OPEN_{-2}-CLOSE{-2} > AVG(|CLOSE-OPEN|) \\ CLOSE_{-2}-LOW_{-2} > BODY\_LONG_{-2} CLOSE2<OPEN2OPEN2CLOSE2>AVG(CLOSEOPEN)CLOSE2LOW2>BODY_LONG2

第二根阴线:
C L O S E − 1 < O P E N − 1 O P E N − 1 − C L O S E − 1 < C L O S E − 1 − O P E N − 1 C L O S E − 2 < O P E N − 1 < = H I G H − 2 L O W − 2 < = L O W − 1 < C L O S E − 2 C L O S E − 1 − L O W − 1 > S H A D O W _ V E R Y _ S H O R T − 1 CLOSE_{-1} < OPEN_{-1} \\ OPEN_{-1}-CLOSE{-1} < CLOSE_{-1} - OPEN_{-1} \\ CLOSE_{-2} < OPEN_{-1} <= HIGH_{-2} \\ LOW_{-2} <= LOW_{-1} < CLOSE_{-2} \\ CLOSE_{-1}-LOW_{-1} >SHADOW\_VERY\_SHORT_{-1} CLOSE1<OPEN1OPEN1CLOSE1<CLOSE1OPEN1CLOSE2<OPEN1<=HIGH2LOW2<=LOW1<CLOSE2CLOSE1LOW1>SHADOW_VERY_SHORT1

第三根阴线:
C L O S E < O P E N O P E N − C L O S E > B O D Y _ S H O R T H I G H − O P E N < S H A D O W _ V E R Y _ S H O R T C L O S E − L O W < S H A D O W _ V E R Y _ S H O R T L O W > L O W − 1 H I G H < H I G H − 1 CLOSE<OPEN \\ OPEN-CLOSE > BODY\_SHORT \\ HIGH-OPEN < SHADOW\_VERY\_SHORT \\ CLOSE-LOW < SHADOW\_VERY\_SHORT \\ LOW > LOW_{-1} \\ HIGH < HIGH_{-1} CLOSE<OPENOPENCLOSE>BODY_SHORTHIGHOPEN<SHADOW_VERY_SHORTCLOSELOW<SHADOW_VERY_SHORTLOW>LOW1HIGH<HIGH1

重构代码(来自ta_CDL3STARSINSOUTH.c, 有修改, 只保留逻辑):

def CDL3STARSINSOUTH_(open, high, low, close):
    _len = len(open)
    open, high, low, close = pd.Series(open), pd.Series(high), pd.Series(low), pd.Series(close)
    if _len >= 13:
        real_body = abs(open - close)
        avg_body_long = real_body.rolling(10).mean().shift(1)
        body_short = real_body.rolling(10).mean().shift(1) * 0.5
        shadow_lower = np.minimum(close, open) - low
        high_low_range_10per = (high - low) * 0.1
        shadow_very_short = high_low_range_10per.rolling(10).mean().shift(1)
        shadow_upper = high - np.maximum(close, open)
        # 第一根阴线
        condition_1 = (close.shift(2) < open.shift(2)) & (real_body.shift(2) > avg_body_long.shift(2)) & \
                      (shadow_lower.shift(2) > 2 * real_body.shift(2))
        # 第二根阴线
        condition_2 = (close.shift(1) < open.shift(1)) & (real_body.shift(1) < real_body.shift(2)) & \
                      (open.shift(1) > close.shift(2)) & (open.shift(1) <= high.shift(2)) & \
                      (low.shift(1) < close.shift(2)) & (low.shift(1) >= low.shift(2)) & \
                      (shadow_lower.shift(1) > shadow_very_short.shift(1))
        # 第三根阴线
        condition_3 = (close < open) & (real_body < body_short) & \
                      (shadow_lower < shadow_very_short) & (shadow_upper < shadow_very_short) & \
                      (low > low.shift(1)) & (high < high.shift(1))

        cdl3starsinsouth = np.where(condition_1 & condition_2 & condition_3, 100, 0)
    else:
        cdl3starsinsouth = np.zeros(_len).astype(int)
    return cdl3starsinsouth

CDL3WHITESOLDIERS (三白兵)

python函数原型:

cdl3whitesoldiers = CDL3WHITESOLDIERS(open, high, low, close)

解释:

(1)三天连续阳线
(2)三天上影线非常短
(3)开盘价接近(near)前一日实体之内
(4)后一日实体不比前一日小很多(far)
(5)最后一日实体不短(bodyshort)

示意图:
在这里插入图片描述
第一日阳线:
C L O S E − 2 > O P E N − 2 S H A D O W _ U P P E R − 2 > S H A D O W _ V E R Y _ S H O R T − 2 CLOSE_{-2} > OPEN_{-2} \\ SHADOW\_UPPER_{-2} > SHADOW\_VERY\_SHORT_{-2} CLOSE2>OPEN2SHADOW_UPPER2>SHADOW_VERY_SHORT2

第二日阳线:
C L O S E − 1 > O P E N − 1 S H A D O W _ U P P E R − 1 > S H A D O W _ V E R Y _ S H O R T − 1 C L O S E − 1 > C L O S E − 2 O P E N − 1 > O P E N − 2 O P E N − 1 < = C L O S E − 2 + N E A R R E A L _ B O D Y − 1 > R E A L _ B O D Y − 2 − F A R CLOSE_{-1} > OPEN_{-1} \\ SHADOW\_UPPER_{-1} > SHADOW\_VERY\_SHORT_{-1} \\ CLOSE_{-1} > CLOSE_{-2} \\ OPEN_{-1} > OPEN_{-2} \\ OPEN_{-1} <= CLOSE_{-2} + NEAR \\ REAL\_BODY_{-1} > REAL\_BODY_{-2} - FAR CLOSE1>OPEN1SHADOW_UPPER1>SHADOW_VERY_SHORT1CLOSE1>CLOSE2OPEN1>OPEN2OPEN1<=CLOSE2+NEARREAL_BODY1>REAL_BODY2FAR

第三日阳线:
C L O S E > O P E N S H A D O W _ U P P E R > S H A D O W _ V E R Y _ S H O R T C L O S E > C L O S E − 1 O P E N > O P E N − 1 O P E N < = C L O S E − 1 + N E A R R E A L _ B O D Y > R E A L _ B O D Y − 1 − F A R R E A L _ B O D Y > B O D Y _ S H O R T CLOSE > OPEN \\ SHADOW\_UPPER > SHADOW\_VERY\_SHORT \\ CLOSE > CLOSE_{-1} \\ OPEN > OPEN_{-1} \\ OPEN <= CLOSE_{-1} + NEAR \\ REAL\_BODY > REAL\_BODY_{-1} - FAR \\ REAL\_BODY > BODY\_SHORT CLOSE>OPENSHADOW_UPPER>SHADOW_VERY_SHORTCLOSE>CLOSE1OPEN>OPEN1OPEN<=CLOSE1+NEARREAL_BODY>REAL_BODY1FARREAL_BODY>BODY_SHORT

重构代码(来自ta_CDL3LINESTRKE.c, 有修改, 只保留逻辑):

def CDL3WHITESOLDIERS_(open, high, low, close):
    _len = len(open)
    open, high, low, close = pd.Series(open), pd.Series(high), pd.Series(low), pd.Series(close)
    if _len >= 13:
        real_body = abs(open - close)
        body_short = real_body.rolling(10).mean().shift(1) * 0.5
        high_low_range_10per = (high - low) * 0.1
        shadow_very_short = high_low_range_10per.rolling(10).mean().shift(1)
        shadow_upper = high - np.maximum(close, open)
        high_low_range_20per = (high - low) * 0.2
        high_low_range_60per = (high - low) * 0.6
        near = high_low_range_20per.rolling(5).mean().shift(1)
        far = high_low_range_60per.rolling(5).mean().shift(1)

        # 第一根阳线
        condition_1 = (close.shift(2) > open.shift(2)) & (shadow_upper.shift(2) < shadow_very_short.shift(2))

        # 第二根阳线
        condition_2 = (close.shift(1) > open.shift(1)) & (shadow_upper.shift(1) < shadow_very_short.shift(1)) & \
                      (close.shift(1) > close.shift(2)) & (open.shift(1) > open.shift(2)) & \
                      (open.shift(1) <= close.shift(2) + near.shift(2)) & \
                      (real_body.shift(1) > real_body.shift(2) - far.shift(2))

        # 第三根阳线
        condition_3 = (close > open) & (shadow_upper < shadow_very_short) & (close > close.shift(1)) & \
                      (open > open.shift(1)) & (open <= close.shift(1) + near.shift(1)) & \
                      (real_body > real_body.shift(1) - far.shift(1)) & \
                      (real_body > body_short)

        cdl3whitesoldiers = np.where(condition_1 & condition_2 & condition_3, 100, 0)
    else:
        cdl3whitesoldiers = np.zeros(_len).astype(int)
    return cdl3whitesoldiers

CDLABANDONEDBABY(弃婴)

python函数原型:

cdlabandonedbaby = CDLABANDONEDBABY(open, high, low, close, penetration=0.3)

解释:

(1)三天K线,若第一日涨
(2)第二日跳空高开且收十字星
(3)第三日低开收阴
(4)预示反转

在这里插入图片描述
顶部为例:
第一根阳线:
C L O S E − 2 > O P E N − 2 R E A L _ B O D Y − 2 > B O D Y _ L O N G − 2 CLOSE_{-2} > OPEN_{-2} \\ REAL\_BODY_{-2} > BODY\_LONG_{-2} CLOSE2>OPEN2REAL_BODY2>BODY_LONG2

第二根上缺口:
R E A L _ B O D Y − 1 < = B O D Y _ D O J I − 1 L O W − 1 > H I G H − 2 REAL\_BODY_{-1} <= BODY\_DOJI_{-1} \\ LOW_{-1} > HIGH_{-2} REAL_BODY1<=BODY_DOJI1LOW1>HIGH2

第三跟阴线:
C L O S E > O P E N R E A L _ B O D Y > B O D Y _ L O N G C L O S E < C L O S E − 2 − R E A L _ B O D Y − 2 × P E N E T R A T I O N L O W − 1 > H I G H CLOSE > OPEN \\ REAL\_BODY > BODY\_LONG \\ CLOSE < CLOSE_{-2} - REAL\_BODY_{-2} \times PENETRATION \\ LOW_{-1} > HIGH CLOSE>OPENREAL_BODY>BODY_LONGCLOSE<CLOSE2REAL_BODY2×PENETRATIONLOW1>HIGH

重构代码(来自ta_CDLABANDONEDBABY.c, 有修改, 只保留逻辑):

def CDLABANDONEDBABY_(open, high, low, close, penetration=0.3):
    _len = len(open)
    open, high, low, close = pd.Series(open), pd.Series(high), pd.Series(low), pd.Series(close)
    if _len >= 13:
        real_body = abs(open - close)
        body_short = real_body.rolling(10).mean().shift(1) * 0.5
        body_long = real_body.rolling(10).mean().shift(1)
        body_doji = real_body.rolling(10).mean().shift(1) * 0.1
        # 第一根阳线
        condition_1_1 = (close.shift(2) > open.shift(2)) & (real_body.shift(2) > body_long.shift(2))
        # 第二根上缺口
        condition_1_2 = (real_body.shift(1) <= body_doji.shift(1)) & \
                        (low.shift(1) > high.shift(2))

        # 第三根阴线
        condition_1_3 = (close < open) & (real_body > body_short) & \
                        (close < close.shift(2) - real_body.shift(2) * penetration) & \
                        (low.shift(1) > high)
        cdl3abandonbaby = np.where(condition_1_1 & condition_1_2 & condition_1_3, -100, 0)

        # 第一根阳线
        condition_2_1 = (close.shift(2) < open.shift(2)) & (real_body.shift(2) > body_long.shift(2))
        # 第二根上缺口
        condition_2_2 = (real_body.shift(1) <= body_doji.shift(1)) & \
                        (high.shift(1) < low.shift(2))

        # 第三根阴线
        condition_2_3 = (close > open) & (real_body > body_short) & \
                        (close > close.shift(2) + real_body.shift(2) * penetration) & \
                        (high.shift(1) < low)
        cdl3abandonbaby = np.where(condition_2_1 & condition_2_2 & condition_2_3, 100, cdl3abandonbaby)
    else:
        cdl3abandonbaby = np.zeros(_len).astype(int)
    return cdl3abandonbaby

CDLADVANCEBLOCK(大敌当前)

python函数原型:

cdladvanceblock = = CDLADVANCEBLOCK(open, high, low, close)

百科解释:三日K线模式,三日都收阳,每日收盘价都比前一日高, 开盘价都在前一日实体以内,实体变短,上影线变长。

talib中定义了四种满足advanceblock的条件
公共条件:

  1. 三练阳,收盘价逐日往上
  2. 三日开盘价高于前一日开盘价,不超过(near)前一日收盘价
  3. 首日长实体(body_long),短上影线(shadow_short)
情况1:上升阻碍由于第二根bar的短实体
  1. 第二日实体远短于(far)第一日实体
  2. 第三日实体不长于(near)第二日实体

在这里插入图片描述

R E A L _ B O D Y − 1 < R E A L _ B O D Y − 2 − F a r − 2 R E A L _ B O D Y 0 < R E A L _ B O D Y − 1 + N e a r − 1 REAL\_BODY_{-1} < REAL\_BODY_{-2} - Far_{-2} \\ REAL\_BODY_{0} < REAL\_BODY_{-1} + Near_{-1} REAL_BODY1<REAL_BODY2Far2REAL_BODY0<REAL_BODY1+Near1

情况2:上升阻碍由于第三根bar的短实体
  1. 第三日实体远短于(far)第二日实体

在这里插入图片描述

R E A L _ B O D Y 0 < R E A L _ B O D Y − 1 − F a r − 1 REAL\_BODY_{0} < REAL\_BODY_{-1} - Far_{-1} REAL_BODY0<REAL_BODY1Far1

情况3:上升阻碍由于逐渐变短的实体和不短的上影线
  1. 第三根实体小于第二根,第二根实体小于第一根
  2. 第三根或第二根上影线不短(shadow_short)

在这里插入图片描述

R E A L _ B O D Y − 1 < R E A L _ B O D Y − 2 R E A L _ B O D Y 0 < R E A L _ B O D Y − 1 ( S H A D O W _ U P P E R 0 > S H A D O W _ S H O R T 0 )   o r ( S H A D O W _ U P P E R − 1 > S H A D O W _ S H O R T − 1 ) REAL\_BODY_{-1} < REAL\_BODY_{-2} \\ REAL\_BODY_{0} < REAL\_BODY_{-1} \\ (SHADOW\_UPPER_{0} > SHADOW\_SHORT_{0})\ or \\ (SHADOW\_UPPER_{-1} > SHADOW\_SHORT_{-1}) REAL_BODY1<REAL_BODY2REAL_BODY0<REAL_BODY1(SHADOW_UPPER0>SHADOW_SHORT0) or(SHADOW_UPPER1>SHADOW_SHORT1)

情况4:上升阻碍由于第三根较短的实体和长上影线
  1. 第三根实体小于第二根
  2. 第三根上影线长(shadow_long)

在这里插入图片描述

R E A L _ B O D Y 0 < R E A L _ B O D Y − 1 S H A D O W _ U P P E R 0 > S H A D O W _ L O N G 0 REAL\_BODY_{0} < REAL\_BODY_{-1} \\ SHADOW\_UPPER_{0} > SHADOW\_LONG_{0} REAL_BODY0<REAL_BODY1SHADOW_UPPER0>SHADOW_LONG0

重构代码(来自ta_CDL3LINESTRKE.c, 有修改, 只保留逻辑):

def CDLADVANCEBLOCK_(open, high, low, close):
    _len = len(open)
    open, high, low, close = pd.Series(open), pd.Series(high), pd.Series(low), pd.Series(close)
    if _len >= 13:
        real_body = abs(open - close)
        body_long = real_body.rolling(10).mean().shift(1)
        high_low_range = high - low
        near = high_low_range.rolling(5).mean().shift(1) * 0.2
        far = high_low_range.rolling(5).mean().shift(1) * 0.6
        shadow_upper = high - np.maximum(close, open)
        shadow_lower = np.minimum(close, open) - low
        shadow_short = (shadow_upper + shadow_lower).rolling(10).mean().shift(1) * 0.5
        common_condition = (close.shift(2) > open.shift(2)) & (close.shift(1) > open.shift(1)) & \
                           (close > open) & (close > close.shift(1)) & (close.shift(1) > close.shift(2)) & \
                           (open.shift(1) > open.shift(2)) & (open.shift(1) <= close.shift(2) + near.shift(2)) & \
                           (open > open.shift(1)) & (open < close.shift(1) + near.shift(1)) & \
                           (real_body.shift(2) > body_long.shift(2)) & (shadow_upper.shift(2) < shadow_short.shift(2))

        # 第二根远小于第一根, 第三根不长于第二根
        # 上升阻碍在第二根bar,第三根不得阻碍
        condition_1 = (real_body.shift(1) < real_body.shift(2) - far.shift(2)) & \
                      (real_body < real_body.shift(1) + near.shift(1))

        # 第三更远小于第二根
        # 上升阻碍在第三根bar
        condition_2 = (real_body < real_body.shift(1) - far.shift(1))

        # 第三根小于第二根,第二根小于第一根, 第三和第二根不具有长上影线
        # 上升阻碍在逐渐变小地上影线和实体
        condition_3 = (real_body.shift(1) < real_body.shift(2)) & (real_body < real_body.shift(1)) & \
                      (shadow_upper > shadow_short) & (shadow_upper.shift(1) > shadow_short.shift(1))

        # 第三根小于第二根, 第三根有很长地上影线
        # 上升阻碍在第三根长上影线和短实体
        condition_4 = (real_body < real_body.shift(1)) & (shadow_upper > real_body)

        cdladvanceblock = np.where(common_condition & (condition_1 | condition_2 | condition_3 | condition_4), -100, 0)
    else:
        cdladvanceblock = np.zeros(_len).astype(int)
    return cdladvanceblock

CDLEVENINGDOJISTAR (十字暮星)

python函数原型:

cdleveningdojistar = CDLEVENINGDOJISTAR (open, high, low, close, penetration=0.3)

解释:

与CDLABANDONEDBABY(弃婴)非常相似,缺口放宽至实体缺口

  1. 第一日大阳线
  2. 第二日高开十字线
  3. 第三日大阴线,收盘小于第一根实体的30%分位数

在这里插入图片描述
重构代码(来自ta_CDLEVENINGDOJISTAR.c, 有修改, 只保留逻辑):

def CDLEVENINGDOJISTAR_(open, high, low, close, penetration=0.3):
    _len = len(open)
    open, high, low, close = pd.Series(open), pd.Series(high), pd.Series(low), pd.Series(close)
    if _len >= 13:
        real_body = abs(open - close)
        body_long = real_body.rolling(10).mean().shift(1)
        high_low_range = high - low
        doji = high_low_range.rolling(10).mean().shift(1) * 0.1
        condition_1 = (real_body.shift(2) > body_long.shift(2)) & \
                      (close.shift(2) > open.shift(2))
        condition_2 = (real_body.shift(1) < doji.shift(1)) & \
                      (np.minimum(close.shift(1), open.shift(1)) > close.shift(2))
        condition_3 = (real_body > body_long) & \
                      (close < open) & \
                      (close < close.shift(2) - real_body.shift(2) * penetration)
        cdleveningdojistar = np.where(condition_1 & condition_2 & condition_3, -100, 0)
    else:
        cdleveningdojistar = np.zeros(_len).astype(int)
    return cdleveningdojistar

CDLEVENINGSTAR (暮星)

python函数原型:

cdleveningstar= CDLEVENINGSTAR (open, high, low, close, penetration=0.3)

与CDLEVENINGDOJISTAR (十字暮星)非常相似,十字线放宽至短线

  1. 第一日大阳线
  2. 第二日高开短线
  3. 第三日大阴线,收盘小于第一根实体的30%分位数

在这里插入图片描述

重构代码(来自ta_CDLEVENINGSTAR.c, 有修改, 只保留逻辑):

def CDLEVENINGSTAR_(open, high, low, close, penetration=0.3):
    _len = len(open)
    open, high, low, close = pd.Series(open), pd.Series(high), pd.Series(low), pd.Series(close)
    if _len >= 13:
        real_body = abs(open - close)
        body_long = real_body.rolling(10).mean().shift(1)
        condition_1 = (real_body.shift(2) > body_long.shift(2)) & \
                      (close.shift(2) > open.shift(2))
        condition_2 = (real_body.shift(1) <= body_long.shift(1)) & \
                      (np.minimum(close.shift(1), open.shift(1)) > close.shift(2))
        condition_3 = (real_body > body_long) & \
                      (close < open) & \
                      (close < close.shift(2) - real_body.shift(2) * penetration)
        cdleveningstar = np.where(condition_1 & condition_2 & condition_3, -100, 0)
    else:
        cdleveningstar = np.zeros(_len).astype(int)
    return cdleveningstar

CDLGAPSIDESIDEWHITE (向上/下跳空并列阳线)

python函数原型:

cdlgapsidesidewhite = CDLGAPSIDESIDEWHITE(open, high, low, close)

解释:

向上或向下跳空后,两根并列(equal)几乎等长(Near)的阳线
趋势方向为跳空方向
在这里插入图片描述
三连阴情况类似

重构代码(来自ta_CDLGAPSIDESIDEWHITE.c, 有修改, 只保留逻辑):

def CDLGAPSIDESIDEWHITE_(open, high, low, close):
    _len = len(open)
    open, high, low, close = pd.Series(open), pd.Series(high), pd.Series(low), pd.Series(close)
    if _len >= 13:
        real_body = abs(open - close)
        high_low_range = high - low
        near = high_low_range.rolling(5).mean().shift(1) * 0.2
        equal = high_low_range.rolling(5).mean().shift(1) * 0.05
        # 向上跳空
        condition_1_1 = np.maximum(close.shift(2), open.shift(2)) < np.minimum(open.shift(1), open)
        # 向下跳空
        condition_1_2 = np.minimum(close.shift(2), open.shift(2)) > np.minimum(close.shift(1), close)

        condition_2 = (close.shift(1) > open.shift(1)) & \
                      (close > open)

        condition_3 = (real_body <= real_body.shift(1) + near.shift(1)) & \
                      (real_body >= real_body.shift(1) - near.shift(1)) & \
                      (open <= open.shift(1) + equal.shift(1)) & \
                      (open >= open.shift(1) - equal.shift(1))
        cdlgapsidesidewhite = np.where(condition_1_1 & condition_2 & condition_3, 100, 0)
        cdlgapsidesidewhite = np.where(condition_1_2 & condition_2 & condition_3, -100, cdlgapsidesidewhite)
    else:
        cdlgapsidesidewhite = np.zeros(_len).astype(int)
    return cdlgapsidesidewhite

五. 四K线模型

CDL3LINESTRIKE(三线打击)

python函数原型:

cdl3linestrike = CDL3LINESTRIKE(open, high, low, close)

解释:

(1)三天连续阳线
(2)每天阳线收盘价大于前一日收盘价
(3)开盘价接近(near)前一日实体之内
(4)第四日高开,跌破第一日开盘价,被认为是上涨信号
(5)阴线情况相反

在这里插入图片描述

三连阳情况

第一根阳线:
O P E N − 3 < C L O S E − 3 OPEN_{-3} < CLOSE_{-3} OPEN3<CLOSE3
第二根阳线:
O P E N − 2 < C L O S E − 2 O P E N − 3 − N e a r < = O P E N − 2 < = C L O S E − 3 + N e a r C L O S E − 2 > C L O S E − 3 OPEN_{-2} < CLOSE_{-2} \\ OPEN_{-3}-Near<= OPEN_{-2} <= CLOSE_{-3}+Near \\ CLOSE_{-2} > CLOSE_{-3} OPEN2<CLOSE2OPEN3Near<=OPEN2<=CLOSE3+NearCLOSE2>CLOSE3
第三根阳线:
O P E N − 1 < C L O S E − 1 O P E N − 2 − N e a r < = O P E N − 1 < = C L O S E − 2 + N e a r C L O S E − 1 > C L O S E − 2 OPEN_{-1} < CLOSE_{-1} \\ OPEN_{-2}-Near<= OPEN_{-1} <= CLOSE_{-2}+Near \\ CLOSE_{-1} > CLOSE_{-2} OPEN1<CLOSE1OPEN2Near<=OPEN1<=CLOSE2+NearCLOSE1>CLOSE2
第四根阴线:
O P E N 0 > C L O S E − 1 C L O S E 0 < O P E N − 3 OPEN_{0} > CLOSE_{-1} \\ CLOSE_{0} < OPEN_{-3} OPEN0>CLOSE1CLOSE0<OPEN3

三连阴情况类似

重构代码(来自ta_CDL3LINESTRKE.c, 有修改, 只保留逻辑):

def CDL3LINESTRIKE_(open, high, low, close):
    _len = len(open)
    open, high, low, close = pd.Series(open), pd.Series(high), pd.Series(low), pd.Series(close)
    if _len >= 9:
        high_low_range_20per = (high - low) * 0.2
        near = high_low_range_20per.rolling(5).mean().shift(1)
        # 三阳线
        # 第一根阳线
        condition_1_1 = (close.shift(3) > open.shift(3))
        # 第二根阳线
        condition_1_2 = (close.shift(2) > open.shift(2)) & \
                        (close.shift(2) > close.shift(3)) & \
                        (open.shift(2) >= open.shift(3) - near.shift(3)) & \
                        (open.shift(2) <= close.shift(3) + near.shift(3))
        # 第三根阳线
        condition_1_3 = (close.shift(1) > open.shift(1)) & \
                        (close.shift(1) > close.shift(2)) & \
                        (open.shift(1) >= open.shift(2) - near.shift(2)) & \
                        (open.shift(1) <= close.shift(2) + near.shift(2))
        # 第四根阴线
        condition_1_4 = (close < open) & \
                        (open > close.shift(1)) & \
                        (close < open.shift(3))

        cdl3linestrike = np.where(condition_1_1 & condition_1_2
                                  & condition_1_3 & condition_1_4, 100, 0)
        # 三阴线
        # 第一根阴线
        condition_2_1 = (close.shift(3) < open.shift(3))
        # 第二根阴线
        condition_2_2 = (close.shift(2) < open.shift(2)) & \
                        (close.shift(2) < close.shift(3)) & \
                        (open.shift(2) >= close.shift(3) - near.shift(3)) & \
                        (open.shift(2) <= open.shift(3) + near.shift(3))
        # 第三根阴线
        condition_2_3 = (close.shift(1) < open.shift(1)) & \
                        (close.shift(1) < close.shift(2)) & \
                        (open.shift(1) >= close.shift(2) - near.shift(2)) & \
                        (open.shift(1) <= open.shift(2) + near.shift(2))
        # 第四根阳线
        condition_2_4 = (close > open) & \
                        (open < close.shift(1)) & \
                        (close > open.shift(3))

        cdl3linestrike = np.where(condition_2_1 & condition_2_2
                                  & condition_2_3 & condition_2_4, -100, cdl3linestrike)
        cdl3linestrike[:8] = 0
    else:
        cdl3linestrike = np.zeros(_len).astype(int)
    return cdl3linestrike

CDLCONCEALBABYSWALL(藏婴吞没)

python函数原型:

cdlconcealbabyswall = CDLCONCEALBABYSWALL(open, high, low, close)

四日连续阴线,前两日无影线
第三日跌出实体缺口,但有不短的上影线,且最高价超过第二日收盘
第四日最高和最低价包裹第三日
底部信号
在这里插入图片描述

重构代码(来自ta_CDLCONCEALBABYSWALL.c, 有修改, 只保留逻辑):


def CDLCONCEALBABYSWALL_(open, high, low, close):
    _len = len(open)
    open, high, low, close = pd.Series(open), pd.Series(high), pd.Series(low), pd.Series(close)
    if _len >= 14:
        high_low_range = high - low
        shadow_very_short = high_low_range.rolling(10).mean().shift(1) * 0.1
        shadow_upper = high - np.maximum(close, open)
        shadow_lower = np.minimum(close, open) - low
        condition_1 = (close.shift(3) < open.shift(3)) & \
                      (shadow_upper.shift(3) < shadow_very_short.shift(3)) & \
                      (shadow_lower.shift(3) < shadow_very_short.shift(3))

        condition_2 = (close.shift(2) < open.shift(2)) & \
                      (shadow_upper.shift(2) < shadow_very_short.shift(2)) & \
                      (shadow_lower.shift(2) < shadow_very_short.shift(2))

        condition_3 = (close.shift(1) < open.shift(1)) & \
                      (shadow_upper.shift(1) > shadow_very_short.shift(1)) & \
                      (open.shift(1) < close.shift(2)) & \
                      (high.shift(1) > close.shift(2))

        condition_4 = (close < open) & \
                      (high > high.shift(1)) & \
                      (low < low.shift(1))

        cdlconcealbabyswall = np.where(condition_1 & condition_2 & condition_3 & condition_4, 100, 0)
    else:
        cdlconcealbabyswall = np.zeros(_len).astype(int)
    return cdlconcealbabyswall

六. 五K线模型

CDLBREAKAWAY(脱离)

python函数原型:

cdlbreakaway = CDLBREAKAWAY(open, high, low, close)

以看涨脱离为例,大阴线后低开形成实体缺口,继续走低
第三日和第四日阴线的high和low持续走低
第五日反弹收盘在一二日缺口内(中文百科中无此要求

在这里插入图片描述

重构代码(来自ta_CDLBREAKAWAY.c, 有修改, 只保留逻辑):

def CDLBREAKAWAY_(open, high, low, close):
    _len = len(open)
    open, high, low, close = pd.Series(open), pd.Series(high), pd.Series(low), pd.Series(close)
    if _len >= 15:
        real_body = abs(open - close)
        body_long = real_body.rolling(10).mean().shift(1)
        # 看涨脱离
        condition_1_1 = (close.shift(4) < open.shift(4)) & (real_body.shift(4) > body_long.shift(4))
        condition_1_2 = (close.shift(3) < open.shift(3)) & (open.shift(3) < close.shift(4))
        condition_1_3 = (close.shift(2) < open.shift(2)) & \
                        (low.shift(2) < low.shift(3)) & \
                        (high.shift(2) < high.shift(3))
        condition_1_4 = (close.shift(1) < open.shift(1)) & \
                        (low.shift(1) < low.shift(2)) & \
                        (high.shift(1) < high.shift(2))
        condition_1_5 = (close > open) & (close > open.shift(3)) & (close < close.shift(4))

        # 看跌脱离
        condition_2_1 = (close.shift(4) > open.shift(4)) & (real_body.shift(4) > body_long.shift(4))
        condition_2_2 = (close.shift(3) > open.shift(3)) & (open.shift(3) > close.shift(4))
        condition_2_3 = (close.shift(2) > open.shift(2)) & \
                        (low.shift(2) > low.shift(3)) & \
                        (high.shift(2) > high.shift(3))
        condition_2_4 = (close.shift(1) > open.shift(1)) & \
                        (low.shift(1) > low.shift(2)) & \
                        (high.shift(1) > high.shift(2))
        condition_2_5 = (close < open) & (close < open.shift(3)) & (close > close.shift(4))

        cdlbreakaway = np.where(condition_1_1 & condition_1_2 & condition_1_3 & condition_1_4 & condition_1_5, 100, 0)
        cdlbreakaway = np.where(condition_2_1 & condition_2_2 & condition_2_3 & condition_2_4 & condition_2_5,
                               -100, cdlbreakaway)
    else:
        cdlbreakaway = np.zeros(_len).astype(int)
    return cdlbreakaway

免责声明

本文仅为技术解析文档,内容全部开放,不对任何个人或机构提供任何投资建议

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

量化就是探索生活

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值