Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
SMA Trouble (Minute)

For some reason I can not get my algo to execute using the SMA historical data, gives me an error message and says runtime error.

def initialize(context):

context.index = symbol('VXX')  
context.price_history = 0  
context.price_list = 0

def handle_data(context, data):

context.price_history = history(1950,'1m','price')  
context.price_list = history(5850,'1m','price')  

print context.price_list  

current_price_risk = data[context.index].price  

current_positions = context.portfolio.positions[symbol('VXX')].amount  

cash = context.portfolio.cash  

if (context.price_history > context.price_list) and current_positions == 0:  
    number_of_shares = int(cash/current_price_risk)  
    order(context.index, number_of_shares)  
    log.info('Buying Shares')  

elif (context.price_history < context.price_list) and current_positions != 0:  
    order_target(context.index, 0)  
    log.info('Selling Shares')  

record(Price = current_price_risk)  
2 responses

Hi Matt, interesting problem!

After reviewing your code, I think I found a solution. In your initialize method, you set context.price_history and context.price_list to be integers, but in your handle_data method, you set them to store the return values of the history() function. The history function actually returns a pandas DataFrame object with the minutely price history for each stock in your universe. Because of this, when you try to check if context.price_history > context.price_list, you are actually comparing two DataFrames, which is what gives you the runtime error. Looking at your code, this doesn't seem like the functionality you were looking for.

What you might want to do is try using the mean of your price_history and your price_list with the following lines:

#mean price over the last 1950 minutes  
context.price_history = history(1950, '1m', 'price').mean().values[0]  
#mean price over the last 5850 minutes  
context.price_list = history(5850, '1m', 'price').mean().values[0]  

This change should make your code run.

Let me know if this helps or if you have any further questions!

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.

Great! Thank you so much for your help Jamie, I'll try this and let you know.