Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Why the calculated SMA is quite different by using Built-in Factor and by talib.SMA()?

Since Q will shutdown the living trading and I have to look for another alternative. Since the wonderful Built-in Factor not available elsewhere, I have to change my code using the Built-in-Factor for SMA calculation to use the talib.SMA. But I found the result are quite different.

Attached is an example. As I calculate the SSO's SMA(150) with talib.SMA() from Jul 01, 2006 to Jul 31, 2006, the values are NAN. I can understand because the SSO's inception day is on Jun 19, 2006.

But as I use the Built-in Factor, the values I get are not NAN. Why?

4 responses

If you will use .bfill() you will get same results.

# SMA indicator

import talib

def initialize(context):  
    schedule_function(record_SMA, date_rules.every_day(), time_rules.market_close())

def record_SMA(context, data):  
    etf, period  = symbol('SSO'), 150

    price = data.current(etf, 'price')  
    prices = data.history(etf, 'price', period, '1d').bfill()  
    SMA_ta = talib.SMA(prices, period)  
    mavg = prices.mean()

    print(SMA_ta[-1],mavg)  
    record(price = price, mavg_ta = SMA_ta[-1], mavg = mavg)  

2006-07-03 09:59 PRINT (71.516933333333327, 71.51693333333333)
2006-07-05 12:59 PRINT (71.529820000000001, 71.52982)
2006-07-06 12:59 PRINT (71.545133333333325, 71.54513333333333)
2006-07-07 12:59 PRINT (71.55353333333332, 71.55353333333332)
2006-07-10 12:59 PRINT (71.563333333333318, 71.56333333333332)
2006-07-11 12:59 PRINT (71.577286666666666, 71.57728666666667)
2006-07-12 12:59 PRINT (71.579553333333322, 71.57955333333332)

Hi,
Thanks for the reply first.

But:
You use the talib.SAM and mean(). They will have the same values. But have you tried using the Built-in-Factor 'SimpleMovingAverage'? They are somewhat different.

Is it a correct way to use the bfill() here? The inception day of SSO was on Jun 19, 2006. There was no price 150 days before this date. The SMA should be NAN. If not, this means it is not really the SMA(150), right?

Hi @Thomas,

The built-in SimpleMovingAverage factor ignores NaN values when computing the mean. On the other hand, NaN values will cause talib.SMA to return an array filled with NaN values. As you noticed, your algorithm is passing NaN values to talib.SMA through stock_history. To solve this, you can drop NaN values from stock_history by using its .dropna() attribute method.

The above solution will introduce another problem. Talib's SMA function will return an array of NaN values if the timeperiod used is larger than the size of the input array. Since we are dropping NaN values from stock_history, we will need to set timeperiod to be the length of stock_history.

The attached backtest applies both fixes. You will notice in the log output that the values returned by both talib.SMA and SimpleMovingAverage are equal.

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.

@Ernesto
Many thanks for the reply.

But this means the SMA at the beginning is not of SMA(150) but SMA(50) or even SMA(10), SMA(5) etc?

I prefer to do as follow:

    if len(stock_history) < context.sma_period:  
        stock_sma = talib.SMA(stock_history, timeperiod=len(stock_history))[-1]  
    else:  
        stock_sma = talib.SMA(stock_history, timeperiod=context.sma_period)[-1]