Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
TaLib CDL, Candle Indicators

Edit:
There are a lot of talib indicators that start with "CDL", stands for Candle, or Candlesticks, where each one (in my current understanding) gives you a non-zero number when a candlestick matches the particular pattern for the name indicated in the function (like MORNINGDOJISTAR), for example plain Doji is open and close same price. Some pretty informative general reading on some of them here and there and categorized or lots of reading, and info at youtube candlestick patterns maybe.
Evidently for many of the CDL talib indicators the instances where each type of candlestick shows up can be extremely rare, accounting for the mostly zero outputs.

To test them further I'm wondering if there is a way in Python code to start with a list of their function names as strings and call each function turning the string into its function name in a loop (the input parameters can always be the same).

So, starting with this list, running for example talib.CDL2CROWS([inputs]):
function_names = ['CDL2CROWS', 'CDL3BLACKCROWS', 'CDL3INSIDE', 'CDL3LINESTRIKE', 'CDL3OUTSIDE', 'CDL3STARSINSOUTH', 'CDL3WHITESOLDIERS', 'CDLABANDONEDBABY', 'CDLADVANCEBLOCK', 'CDLBELTHOLD', 'CDLBREAKAWAY', 'CDLCLOSINGMARUBOZU', 'CDLCONCEALBABYSWALL', 'CDLCOUNTERATTACK', 'CDLDARKCLOUDCOVER', 'CDLDOJI', 'CDLDOJISTAR', 'CDLDRAGONFLYDOJI', 'CDLENGULFING', 'CDLEVENINGDOJISTAR', 'CDLEVENINGSTAR', 'CDLGAPSIDESIDEWHITE', 'CDLGRAVESTONEDOJI', 'CDLHAMMER', 'CDLHANGINGMAN', 'CDLHARAMI', 'CDLHARAMICROSS', 'CDLHIGHWAVE', 'CDLHIKKAKE', 'CDLHIKKAKEMOD', 'CDLHOMINGPIGEON', 'CDLIDENTICAL3CROWS', 'CDLINNECK', 'CDLINVERTEDHAMMER', 'CDLKICKING', 'CDLKICKINGBYLENGTH', 'CDLLADDERBOTTOM', 'CDLLONGLEGGEDDOJI', 'CDLLONGLINE', 'CDLMARUBOZU', 'CDLMATCHINGLOW', 'CDLMATHOLD', 'CDLMORNINGDOJISTAR', 'CDLMORNINGSTAR', 'CDLONNECK', 'CDLPIERCING', 'CDLRICKSHAWMAN', 'CDLRISEFALL3METHODS', 'CDLSEPARATINGLINES', 'CDLSHOOTINGSTAR', 'CDLSHORTLINE', 'CDLSPINNINGTOP', 'CDLSTALLEDPATTERN', 'CDLSTICKSANDWICH', 'CDLTAKURI', 'CDLTASUKIGAP', 'CDLTHRUSTING', 'CDLTRISTAR', 'CDLUNIQUE3RIVER', 'CDLUPSIDEGAP2CROWS', 'CDLXSIDEGAP3METHODS']

Original message (edited some):

This page has some Python code for calling various talib indicators (handful of different ways they have to be called).

In case anyone is curious, here are a few of the top percent returns when added to my algo in testing a few days ago (tested 75 of them one at a time):

729    CDLLADDERBOTTOM  
613    ADX  
555    CDLSTALLEDPATTERN  
534    MFI  
525    CDLUPSIDEGAP2CROWS  
518    CDLMORNINGDOJISTAR  
518    ULTOSC  
517    AVGPRICE  
513    CDL3STARSINSOUTH  
509    ATR  

Some of the more common talib indicators (SMA, RSI etc) return values on every bar, CDL's often zero.

The attached code returns mostly zero values [edit: explained above].

2 responses

This will run all of the talib functions that start with CDL in a loop, while the online IDE won't allow getattr() this could be do-able in Zipline or Python in a console. Answer via stackoverflow.

import talib, re  
import numpy as np

opens_hst  = history(10, '1d', 'open_price')  
highs_hst  = history(10, '1d', 'high')  
lows_hst   = history(10, '1d', 'low')  
closes_hst = history(10, '1d', 'close_price')  
opens_hst  = opens_hst .ffill() # forward fill w/ prev val if NaN latest  
highs_hst  = highs_hst .ffill()  
lows_hst   = lows_hst  .ffill()  
closes_hst = closes_hst.ffill()  
opens_hst  = opens_hst .bfill() # back fill any NaN with current  
highs_hst  = highs_hst .bfill() #    (think IPO's coming onboard)  
lows_hst   = lows_hst  .bfill()  
closes_hst = closes_hst.bfill()  
O          = np.array( opens_hst[sec])  
H          = np.array( highs_hst[sec])  
L          = np.array(  lows_hst[sec])  
C          = np.array(closes_hst[sec])

# List of CDL function names as strings  
cdls = re.findall('(CDL\w*)', ' '.join(dir(talib)))

for cdl in cdls:  
    toExec = getattr(talib, cdl)  
    out    = toExec(np.array(O), np.array(H), np.array(L), np.array(C))  
    print str(out) + ' ' + cdl

Out, excerpt...  
[...]
[0 0 0 0 0 0 0 0 0 0] CDLSTICKSANDWICH
[0 0 0 0 0 0 0 0 0 0] CDLTAKURI
[0 0 0 0 0 0 0 0 0 0] CDLTASUKIGAP
[0 0 0 0 0 0 0 0 0 0] CDLTHRUSTING
[   0    0    0    0 -100  100    0    0    0    0] CDLTRISTAR
[0 0 0 0 0 0 0 0 0 0] CDLUNIQUE3RIVER
[0 0 0 0 0 0 0 0 0 0] CDLUPSIDEGAP2CROWS
[0 0 0 0 0 0 0 0 0 0] CDLXSIDEGAP3METHODS
[...]

Gary,
I'm not positive how you could do this in the online IDE, but this routine will give you a dict of the CDL function results for AAPL. It should be fine to do this as a static analysis outside of a backtest if you just want to see when they all have triggered.

import talib  
import pandas as pd  
from zipline.utils.factory import load_bars_from_yahoo

def get_candle_funcs():  
    funcs = {}  
    for name in talib.abstract.__FUNCTION_NAMES:  
        if name.startswith('CDL'):  
            funcs[name] = getattr(talib, name)  
    return funcs


lookback = 500 * pd.tseries.offsets.BDay()

end = pd.Timestamp.utcnow()  
start = end - lookback

data = load_bars_from_yahoo(stocks=['AAPL'],  
                            start=start, end=end)

aapl = data['AAPL']

O = aapl.open.values  
H = aapl.high.values  
L = aapl.low.values  
C = aapl.close.values

funcs = get_candle_funcs()

results = {}  
for f in funcs:  
    results[f] = funcs[f](O, H, L, C)

print results