Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Backtest transactions

New to Quantopian,

Can someone please explain to me the results of this back test, it is a sample algo which buys AAPL stock for 100% of the cash available.

As I understand, algo should buy 1million$ worth of AAPL stock and hold it. In the transaction details there are two trades on the first day where the algo bought 2087.51$ worth of stock more then it should have, thus the total position in AAPL was 1.002.087.51$, the second day ran the rebalance function and sold the excess amount of stock, so now we have a bit less of 100% of cash invested in AAPL stock.

And now the part where it gets fuzzy for me, you can see in the transaction detail, the algo had 7 more trades during the back-test where it bought approx 4000$ worth of AAPL stock. I don't understand why it did that as it was already invested 100% in the AAPL?

2 responses

There are two things going on which explain your results.

First, the "order_target_percent" method is a convenience method that by default places a market order. Behind the scenes the method takes the current account buying power and divides by the last known price for the security (and includes commissions) to come up with a quantity of shares to order. That quantity is placed as a market order with the expectation it will get filled at the last price. However, being a market order it will fill at whatever the current price is. (see https://www.quantopian.com/help#api-order-target-percent for details).

Look at the attached backtest and check the log. I added logging to show the current account cash, the number of shares being ordered, the last price (at the time the order was placed), and the total expected cost of those shares (shares x price). The first order was placed as a market order for 7869 shares. The last price was 127.08 so the expected cost was 999992.52. However, the shares were subsequently actually bought at a higher price of 127.38 and 127.30. This resulted in a higher than expected cost and the account balance went negative. Therefore, the following day an order was placed to sell shares to get to a positive balance.

Second, the backtester accounts for dividends. The reason you see 7 more trades is that there were 7 quarterly dividend payouts of roughly .4% or $4000 each. That shows up magically as cash in your account. The algorithm sees the extra cash and immediately buys more shares to be 100% invested.

Thank you very much for your reply, I have been busting my head over this, couldn't figure it out. Completely neglected the dividend payout, Thank you again :)