Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How to use the apply() on talib.CDLENGULFING() for multiple securities?

I want to calculate the Engulfing of some stocks as follow:

import talib

start = '2018-08-01'
end = '2019-08-30'

assets = ['SSO', 'SDS']

data = get_pricing(assets, start_date=start, end_date=end, frequency='daily')

engulfing_List = data.apply(talib.CDLENGULFING,args=())

But I got error as follow:
TypeError: CDLENGULFING() takes exactly 4 positional arguments (1 given)

2 responses

One can apply a TALIB function to multiple securities by first grouping by security (using the groupby method ) and then applying a function to each group (using the apply method).

First, one needs to change the results of the get_pricing method into a dataframe. The default is a pandas panel. The to_frame method works nicely. Once we have a dataframe then group by security and apply our talib function. The one 'gotcha' is the apply method passes a dataframe to the function. The TALIB functions expect series. A straightforward workaround is to use a helper function which takes the dataframe as an input then calls the TALIB function with the appropriate columns. Like this

def my_CDLENGULFING(df):  
    """  
    Map the appropriate columns from the get_pricing dataframe to the TALIB function  
    Return the latest value of CDLENGULFING  
    """  
    return talib.CDLENGULFING(  
        open=df.open_price,  
        high=df.high,  
        low=df.low,  
        close=df.close_price,  
        )[-1]

Putting it all together, something like this will work to get the last (most recent) value of the 'talib.CDLENGULFING' function for multiple securities. The result is a series indexed by security. The values of the series are the latest CDLENGULFING values.

# Import the talib library  
import talib

# Set the start and end dates  
start = '2018-08-01'  
end = '2019-08-30'

# Set the securites (assets) we want to check  
assets = ['SSO', 'SDS']

# Fetch the OHLCV data for the securities  
data_panel = get_pricing(assets, start_date=start, end_date=end, frequency='daily')  
data_df = data_panel.to_frame()

# Now apply our talib helper function to each security  
# Level 1 are the securities  
cdlengulfing_latest = data_df.groupby(level=1).apply(my_CDLENGULFING)

See the attached notebook. It goes into more detail first passing a single security to a TALIB function and then finally multiple securities as above. Good question!

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

Wonderful and many thanks!