TA_Lib指标目录
- 3.(蜡烛图指标)Candlestick Indicator
- 免责声明
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|) OPEN−2−CLOSE−2>AVG(∣OPEN−CLOSE∣)
第二根阴线:
O P E N − 1 > C L O S E − 1 > C L O S E − 2 OPEN_{-1} > CLOSE_{-1} > CLOSE_{-2} OPEN−1>CLOSE−1>CLOSE−2
第三根阴线:
O P E N − 1 < O P E N 0 < C L O S E − 2 OPEN_{-1} < OPEN_{0} < CLOSE_{-2} OPEN−1<OPEN0<CLOSE−2
重构代码(来自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 OPEN−3−CLOSE−3>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