Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Runtime exception: TypeError: 'str' object is not callable

Hi,

I'm getting this error and don't understand why! Any ideas anyone?

My code is here:

import math  
import talib as ta  
import pandas as pd  
import numpy as np

def initalise_context(sid, context):  
    historical_close = history(bar_count=365, frequency='1d', field='close_price')

    daily_close = historical_close[sid]


def process_order(sid, order, context, close):  
    cash = context.portfolio.cash  
    context.notional = context.notional + context.portfolio.positions[sid].amount * close  
    datetime = pd.Timestamp(get_datetime()).tz_convert('US/Eastern')  
    if ( order.lower()[0][0] == 'b' ):  
        if ( context.notional < context.max_notional ):  
            number_of_shares = int(cash/close)  
            message = '{s}: {o} {n} @ {d}'  
            log.info( message.format( s=sid, o=order, n=number_of_shares, d=datetime ))  
            #order_percent(sid, 0.33, style=MarketOrder(exchange=IBExchange.SMART))  
            order(sid, number_of_shares)  
            context.notional = context.notional + close * number_of_shares  
    else:  
        if ( context.notional > context.min_notional ):  
            number_of_shares = context.portfolio.positions[sid].amount  
            message = '{s}: {o} {n} @ {d}'  
            log.info( message.format( s=sid, o=order, n=number_of_shares, d=datetime ))  
            #order_percent(sid, -0.33, style=MarketOrder(exchange=IBExchange.SMART))  
            order(sid, -number_of_shares)  
            context.notional = context.notional - close * number_of_shares

def initialize(context):  
    context.stocks = [symbol('AAPL'), symbol('MSFT')]  
    context.max_notional = 1000000.1  
    context.min_notional = 20000.0  
    context.notional = 0  
    context.day = None  

def handle_data(context, data):  
    for sid in context.stocks:  
        close = data[sid].close_price  
        if context.portfolio.positions[sid].amount > 0:  
            process_order(sid, 'Sell', context, close)  
        if get_datetime().day == context.day:  
            return  
        context.day = get_datetime().day  
        initalise_context(sid, context)  
        process_order(sid, 'Buy', context, close)  
2 responses

Hey Andrew,

Essentially, you were passing in 'order' as a parameter into your process_order method which essentially 'overwrites' Quantopian's 'order' method. In order to fix this, you just had to change the 'order' parameter in process_order to a different name:

#: You were passing in order, as a string, which 'overwrites' the 'order' method  
#: so when you were calling order(sid, -number_of_shares), you were essentially trying to do  
#: 'Sell'(sid, -number_of_shares)  
#: I've changed the name around here:  
def process_order(sid, order_name, context, close):  
    cash = context.portfolio.cash  
    context.notional = context.notional + context.portfolio.positions[sid].amount * close  
    datetime = pd.Timestamp(get_datetime()).tz_convert('US/Eastern')  
    if ( order_name.lower()[0][0] == 'b' ):  
        if ( context.notional < context.max_notional ):  
            number_of_shares = int(cash/close)  
            message = '{s}: {o} {n} @ {d}'  
            log.info( message.format( s=sid, o=order, n=number_of_shares, d=datetime ))  
            #order_percent(sid, 0.33, style=MarketOrder(exchange=IBExchange.SMART))  
            order(sid, number_of_shares)  
            context.notional = context.notional + close * number_of_shares  
    else:  
        if ( context.notional > context.min_notional ):  
            number_of_shares = context.portfolio.positions[sid].amount  
            message = '{s}: {o} {n} @ {d}'  
            log.info( message.format( s=sid, o=order, n=number_of_shares, d=datetime ))  
            #order_percent(sid, -0.33, style=MarketOrder(exchange=IBExchange.SMART))  
            order(sid, -number_of_shares)  
            context.notional = context.notional - close * number_of_shares  

Seong

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.

Derrrrh!! Quite obvious now you mention it. Thanks Seong!!