Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
New to backtesting in python here, quick question

Hello,

I've done a lot of back testing through Excel and VBA but just recently decided to give python a shot. I don't really know much about python so my code is based off other forums. Anyways, I get this error when I try to run it in minute mode: "Runtime exception: ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()."

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

def handle_data(context, data):
H = history(30,'1m','price')
A2 = H.tail(10).mean()
A3 = H.tail(20).mean()
A4 = H.tail(30).mean()

if A2>A3 and A3>A4:  
    order(context.security, + 1000 )  
    log.info("Buying %s" % (context.security.symbol))  
elif A2<A3 or A3<A4:  
    order_target(context.security, 0)  
    log.info("Selling %s" % (context.security.symbol))  
record(stock_price=data[context.security].price)

Any feedback is appreciated!

6 responses

Graham,

Just a little basis, Pandas is a data analysis package for Python. It is our preferred package due to its versatility and power. Pandas has three main DataStructures: Series, DataFrame, and Panel. A Series is one-dimensional data, DataFrame 2D, and Panel 3D. So due to these variations on the dimensions of the various data structures in Pandas things can get kind of confusing. Check out this Intro to Pandas page and it should give you a pretty good run down on Pandas usage in general. Play around with that and comeback and visit your code. If you still have issues post here and I'll help you out.

J

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! I'll take a look at that!

After reading the intro you linked, I realized I don't need pandas so I took that out of my algo. I still get an error when I try to back test. I think I'm just messing up the history function.

You are calling the history function correctly, (make sure that you are building/backtesting in minute mode). As I'm sure you are aware the .tail() function grabs the last n values from the DataFrame, if you had other securities in the history DataFrame it would get the last n values for those securities as well, so then you call .mean(), and now you have a the average value over the last n days for each security in the history DataFrame. So in the end you end up with a collection of values for each security, these are stored in a Pandas Series. What I'm trying to get at is that even with only one security it still returns a collection, but that collection only contains one object. So you have to remove that value from the collection to use it.

Pandas does have some support for boolean-esque comparing of Series objects but I don't think that is what you are trying to do. If you want the value from each of those averages that you just tag on [context.security] to the end of the .mean() method and you should be good to go!

A2 = H.tail(10).mean()[context.security]  

Thanks so much! That's exactly what I was missing.

Check this out too, its the same content as what I linked to earlier but its interactive and in our Research environment

https://www.quantopian.com/research/notebooks/Tutorials%20and%20Documentation/Tutorial%20-%20Manipulating%20Data%20with%20Pandas.ipynb