Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
[Newbie question] Interpretation of transaction

I'm newbie in quantopian and tried backtesting with very simple code

def initialize(context):  
    # Reference to AAPL  
    context.aapl = sid(24)

def handle_data(context, data):  
    # Position 100% of our portfolio to be long in AAPL  
    order_target_percent(context.aapl, 1.00)  
  1. Backtest setting
    From 2008-03-03 to 2008-03-05 with $1,000,000 initial capital

  2. Transaction Detail
    2008-03-03 11:32 PM : BUY $125.35 x 4127 (=$517,311.20)
    2008-03-03 11:33 PM : BUY $125.78 x 3886 (=$488,792.74)
    2008-03-03 11:33 PM : BUY $125.79 x 2137 (=$268,808,96)
    2008-03-03 11:34 PM : BUY $125.31 x 1713 (=$214,659.46)
    2008-03-03 11:34 PM : SELL $125.31 x -2186 (= -$273,918.92)
    2008-03-03 11:35 PM : SELL $125.65 x -1721 (= -$216,247.09)
    2008-03-03 11:36 PM : BUY $125.84 x 3 (=$377.52)

This is how I understand "Transaction Detail":

  • 11:32PM : There are only 4127 available shares (at $125.35) in market so bought them all. Cash I hold : 1,000,000 - 517,311.20 = $482,688.8
  • 11:33PM : There are only 3886 available shares (at $125.78) in market so bought them all. Cash I hold : 482,688.8 - 488,792.74 = ($-6,103.94)

Q1. How can I buy shares more than I could afford? I mean, buying 3886 shares make my cash minus value... How could it happen?
Q2. How could transaction occur two times in 11:33pm? Why doesn't it occur only once?
Q3. As you can see above, agent start to sell its share on 11:34pm and 11:35pm. How did it have to sell its share?
Q4. Why does transaction not exist after 11:36pm?

I think that these questions are all caused by my misunderstanding of order_target_percent(context.aapl, 1.00). Isn't it mean "Buy all available shares in every time handle_data() is executed?"

Thanks you in advance for your explanation.

1 response

First off, welcome aboard! Maybe someone else can explain better than I, but here is my take...

Q1. How can I buy shares more than I could afford? I mean, buying 3886 shares make my cash minus value... How could it happen?
The platform assumes you are Warren Buffet and will lend you all the money you need to buy any shares you put in an order for. Negative cash simply means you've borrowed on margin. It's up to you to track your margin and leverage to stay within whatever bounds you set.

Q2. How could transaction occur two times in 11:33pm? Why doesn't it occur only once?
Even though you are buying the same stock, you are buying from two separate orders (hence two entries. one for each order). The 'handle_data(context, data)' function is executed EVERY minute. So, at 11:31 you placed an order for 100% of your portfolio value of AAPL. Then, AGAIN at 11:32 you placed another order for 100% of your portfolio value of AAPL. The transactions are by order so two transactions.

Q3. As you can see above, agent start to sell its share on 11:34pm and 11:35pm. How did it have to sell its share?
Your code 'overbought' in the first few minutes so now it's selling some shares to get to 100% (not over 100%) invested in AAPL. See this post for an explanation which may help. https://www.quantopian.com/posts/order-target-percent-ordering-too-much

Q4. Why does transaction not exist after 11:36pm?
The 'order_target_percent(context.aapl, 1.00)' instruction is saying 'buy or sell enough shares of AAPL so that the value of the resulting position will be equal to 100% of the portfolio value. It stops trading because you now have the requested number of shares in the portfolio.

order_target_percent(context.aapl, 1.00). Isn't it mean "Buy all available shares in every time handle_data() is executed?"
No, it means 'place an order for x number of shares of AAPL to adjust the value of the AAPL shares in my portfolio to be equal to 100% of the portfolio value'. If one starts with a portfolio value of $1000 and no shares of AAPL, it simply takes the last close price (lets assume $126) and divides that into $1000. That equals 7.93. Since one can't order fractions of a share, it places an order for 7 shares. See https://www.quantopian.com/help#api-order-target-percent

One other misunderstanding may be the default 'slippage model'. You don't (by default) ever buy all the available shares. One can only trade 2.5% of the minute volume at a time (though this can be changed by setting a different slippage model).
Maybe look at this post https://www.quantopian.com/posts/orders-not-filling-for-even-small-amounts .