Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Quick question regarding a Runtime Error

Hi guys. I am trying to build a simple algo that goes short a stock when the SMAs cross. I am getting a runtime error on line 30(error pasted below), which is 'mavg_short = price_hist.mean(5)'. I know I am missing something simple, but some help would be much appreciated! Thank you!

ValueError: No axis named 5 for object type
There was a runtime error on line 30.

# To run an algorithm in Quantopian, you need two functions: initialize and  
# handle_data.


def initialize(context):  
    # This initialize function sets any data or variables that you'll use in  
    # your algorithm.  For instance, you'll want to define the security (or  
    # securities) you want to backtest.  You'll also want to define any  
    # parameters or values you're going to use.

    # In our example, we're looking at Apple.  If you re-type this line  
    # yourself, you'll see the auto-complete that is available for the  
    # security ID.  
    context.stock = sid(24)


def handle_data(context, data):  
    # This handle_data function is where the real work is done.  Our data is  
    # minute-level tick data, and each minute is called a frame.  This function  
    # runs on each frame of the data.  
    # We will be working with Apple only  
    stock = context.stock  
    # And we will need Apple current and historic data  
   # stock_data = data.current(stock  
    # We will need average price data  
    price_hist = data.history(stock, 'price', 30, '1d')  
    mavg_short = price_hist.mean(5)  
    mavg_long = price_hist.mean(30)  
    # And the current price of the stock  
    current_price = stock_data.price

    # In order to make market decisions we need to know how much cash we have  
    cash = context.portfolio.cash  


    # This is the meat of the algorithm, placed in this if statement.  
    if mavg_short < mavg_long and cash > current_price and context.portfolio.positions[stock.sid].amount < 1:  
        # When shorting we need to know how many shares we can short  
        number_of_shares = int(cash/current_price)  
        # int() helps us get a integer value, we can't buy half a share you know  
        # Finally, we place the short  
        order(stock, -number_of_shares)  
    elif mavg_short > mavg_long and context.portfolio.positions[stock.sid].amount > 1:  
        # Before we cover we need to know how many shares we have  
        number_of_shares = context.portfolio.positions[stock.sid].amount  
        # Now we can cover  
        order(stock, number_of_shares) # When selling we need to pass a negative number of shares

2 responses

Try this:

def initialize(context):  
    context.stock = symbol('AAPL')  
    schedule_function(trade, date_rules.every_day(), time_rules.market_close(minutes=60))

def trade(context, data):  
    stock = context.stock  
    price_hist = data.history(stock, 'price', 30, '1d')  
    mavg_short = price_hist.tail(5).mean()  
    mavg_long = price_hist.mean()  

    if mavg_short < mavg_long :  
        order_target_percent(stock, -0.5)  
    elif mavg_short > mavg_long :  
         order_target_percent(stock, 1.0)  

Thanks Vladimir. I will try this out.