Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Help Needed: TypeError: 'instancemethod' object has no attribute '__getitem__'

This is the error message that I am getting

TypeError: 'instancemethod' object has no attribute '__getitem__'
... USER ALGORITHM:12, in handle_dataGo to IDE
current_price = data.current[context.security,'price']

Here is my code

def initialize(context):  
    context.security = symbol('SPY')

#Will be called upon every trade event for the securities you specified  
def handle_data(context,data):  
    print(data)  
    price_hist1 = data.history(context.security, 'price', 50, '1d')  
    MA1 = price_hist1.mean()  
    price_hist2 = data.history(context.security, 'price', 200, '1d')  
    MA2 = price_hist2.mean()

    current_price = data.current[context.security,'price']  
    current_position = context.portfolio.positions[symbol('SPY')].amount  
    cash = context.portfolio.cash  
    if (MA1 > MA2) and current_positions == 0:  
        numbe_of_shares = int(cash/current_price)  
        order(context.security, number_of_shares)  
        log.info('Buysing Shares')  
    elif (MA1 < MA2) and current_price != 0:  
        order_target(context.security, 0) #second para = # of shares you keep  
        log.info('Selling Shares')  
    record(MA1 = MA1, MA2 = MA2, Price = current_price) # Name = actual value  

2 responses

Hi Andrew,

data.current should be called as a function, using parentheses instead of brackets. So your line 12 should look like this:

current_price = data.current(context.security, 'price')  

Let me know if you need any more help.

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.

Thanks Nathan!!