Back to Community
How to use the talib.SMA() on a DataFrame with multiple stocks?

Normally I use the talib on a single stock. I wonder how to use it on a DataFrame with multiple stocks as described in notebook.

Loading notebook preview...
8 responses

Try this:

mySMA = data.rolling(window=10).mean()  

For SMA one can use the mean(). But if I want to calculate the EMA, MACD, etc? And if I do want to use the talib? Seems not possible? Assumed I have the 'open', 'low' etc.

I've tried using the pd. apply(), but it seems I can't overhand the paerameter such the preiod length.

Here the updated notebook.

Loading notebook preview...

how to overhand the period 30 as argument/parameter?

Try this:

ma1_list = data.apply(myMA1).dropna()  

Bit in this form the function myMA1 is not reusebal. I mean if I want to calculate the EMA with period of 50, 100? I can't overhand this parameter to the myMA1().

Try this:

Loading notebook preview...

Arguments can be passed to an 'applied' function by using the args parameter. See the pandas 0.18 docs for the apply method .

So, the following will generate the 50 day and 100 day SMA for 'SSO' and 'SDS' using the talib library.

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', fields='close_price')

sma_50 = data.apply(talib.SMA, args=(50,))  
sma_100 = data.apply(talib.SMA, args=(100,))

A few things to notice. The comma after the final arg is required (ie 50, ). Don't forget that. The apply method in ver 0.18 will create a new dataframe. One would need to manually merge it with the original dataframe if one wants all the data in one place. If one just wants the latest SMA as a number then use the tail or the ilocmethods. These will return the same values just formatted differently. tail returns a dataframe with securities as columns. iloc returns a series with the securities as the index.

# The following returns a dataframe of values with the date as the index  

# The following returns a series of values with securities as the index  


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.

Many thanks!