Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Access to Transaction details and other discrepancies

Hi everyone,

First of all, i'd like to say that Quantopian is looking great and seems a very interesting platform.

I've been playing around with Quantopian. And i've noticed there are some strange discrepancies.

  1. I've yet to find a way to access single Transaction details. As I understand it is impossible to get properties on open positions from the same stock. I.e. If I buy Apple once at 100 and then later at 110, there is no way of retrieving the return on each transaction separately, as well as closing the separately. (Yes, I realise for many things it doesn't change much)
  2. It seems the Cost Basis isn't working properly. If I buy a stock and
    log the cost basis, its not the same as the price paid, even though
    no further stocks have been purchased. I assume i'm doing something
    wrong...

cost_basis
Float: The volume-weighted average price paid (price and commission) per share in this position.

Output:
2014-05-05startportfolio:84INFOSetting up basic portfolio...
2014-05-05startportfolio:91INFOBuying ADBE : [email protected]**61.43** : 491.44
2014-05-05handle_data:52INFOCost Basis: ADBE : 0.0
2014-05-06handle_data:52INFOCost Basis: ADBE : 59.48

  1. Returns method
    When using the returns method I keep getting an error. While any other method for data[sid] does seem to work in my code.
    ---error----
    There was a runtime error.
    RuntimeInvalidTransform: 0064 You accessed data in some way other than data[sid].returns. We do not support calling returns in other ways.

Code:

def handle_data(context, data):  
    # Implement your algorithm logic here.

    # data[sid(X)] holds the trade event data for that stocks.  
    # context.portfolio holds the current portfolio state.  
    #General Vars  
    cash = context.portfolio.cash  
    #margin = int(cash/10)  
    #free_cash = int(cash - margin)  
    free_cash = cash  
    handle_data.target = int(free_cash * context.weight)  
    #log.info("Current Cash: %s" % (cash))  
    #log.info("Margin: %s" % (margin))  
    #log.info("Free Cash: %s" % (free_cash))  
    if context.portfolio.positions_value <= 0:  
        startportfolio(context, data)  

    #for stock in context.stocks:  
    returns = data[114].returns  
    log.info('Return %s' % (returns))

def startportfolio(context, data):  
    log.info("Setting up basic portfolio...")  
    for stock in context.stocks:  
        p_current_price = float(data[stock].price)  
        p_amount_order = int(0.5 * handle_data.target / p_current_price)  
        p_costs = p_amount_order * p_current_price  
        order(stock, +p_amount_order)  
        log.info('Buying %s' % (stock.symbol) + " - " + str(p_amount_order) + "@" + str(p_current_price) + " - %s" %p_costs )  
3 responses

Nicolas,
the data[sid].returns method is a function, you need to call it via data[sid].returns(). If you run a full backtest you will be able to look at a breakdown of the individual transactions in the results window. Also, when you place an order from within an algorithm, the function returns a hash id for that order, you can access the info via get_order(order_id).

Check out this backtest, hopefully it will help you figure out what's going on.

Hi Nicholas,

You can also take a look at this thread about tracking and calculating the cost basis: https://www.quantopian.com/posts/fill-price-of-market-orders

And to follow-up on David's suggestion about checking the order ID, you can query each transaction by its ID and check its status (open, filled), along with other information. Here's more information about the function in the API documentation:

https://www.quantopian.com/help#api-get-open-orders
https://www.quantopian.com/help#api-get-order

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.

Dear David and Alisa,

Thank you very much for your detailled replies! I will have a look at it as soon as possible. It seems the transaction ID is indeed what I have been looking for, and will check out its functionalities as suggested.

I'll post the results here, and if I find a working solution I'll add it here for future reference.