Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How to use ta.EMA?

Greetings,

Its my first day on Q, so please excuse my noobishness.

I am trying to calculate EMA from 15d window history, but no luck.

# Put any initialization logic here.  The context object will be passed to  
# the other methods in your algorithm.  
def initialize(context):  
    # init  
    set_symbol_lookup_date('2015-01-01')  
    # get data  
    context.stocks = symbols('AAPL', 'TSLA', 'FB', 'NFLX')

# Will be called on every trade event for the securities you specify.  
def handle_data(context, data):  
    for stock in context.stocks:  
        last_15 = history(bar_count = 15, frequency='1d', field='close_price')  
        print last_15[stock]

        ema_15 = ta.EMA(timeperiod=15)  
        print ema_15(last_15[stock])  

        #print ta.EMA(last_15[stock], timeperiod=15)  

This code returns an error:
AttributeError: 'SingleBlockManager' object has no attribute 'itervalues' There was a runtime error on line 18.

While

# Put any initialization logic here.  The context object will be passed to  
# the other methods in your algorithm.  
def initialize(context):  
    # init  
    set_symbol_lookup_date('2015-01-01')  
    # get data  
    context.stocks = symbols('AAPL', 'TSLA', 'FB', 'NFLX')

# Will be called on every trade event for the securities you specify.  
def handle_data(context, data):  
    for stock in context.stocks:  
        last_15 = history(bar_count = 15, frequency='1d', field='close_price')  
        print last_15[stock]

        #ema_15 = ta.EMA(timeperiod=15)  
        #print ema_15(last_15[stock])  

        print ta.EMA(last_15[stock], timeperiod=15)[-1]  

Returns:

TypeError: __init__() got multiple values for keyword argument 'close'  
There was a runtime error on line 21.

Does anyone know what I'm missing here?

2 responses

I noticed that talib.EMA seems to behave a bit better, albeit I'm not sure if results are accurate.

print talib.EMA(last_15[stock].values, timeperiod=15)[-1]  
2015-09-01PRINT111.781326667  
2015-09-01PRINT240.708266667  
2015-09-01PRINT90.18634  
2015-09-01PRINT114.691333333  
2015-09-02PRINT111.585993333  
2015-09-02PRINT241.352266667  
2015-09-02PRINT89.8970066667  
2015-09-02PRINT113.687333333  
2015-09-03PRINT111.263993333  
2015-09-03PRINT241.5596  
2015-09-03PRINT89.545  
2015-09-03PRINT112.175326667  
2015-09-04PRINT110.811326667  
2015-09-04PRINT241.4776  
2015-09-04PRINT89.1303333333  
2015-09-04PRINT110.539326667  

It looks like you found an old, unsupported Quantopian functionality. In the beginning we created a ta wrapper around the talib library. This made it easier to use technical indicators within your algorithm. As we grew, the wrapper began showing some holes and we no longer support it. To use these indicators in your algo, import the external talib library and access it via talib. notation. Here are some examples: https://www.quantopian.com/help#api-talib

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.