Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Need help to under API --- order(stock_symbol, amount)

I'm trying to understand API --- order(stock_symbol, amount)
I have initial capital_base=10000 and I set context.set_commission(PerTrade(cost = 7))
at 05/21/2015, I purchased stock symbol 'VGT' about 89 shares, the close price is 111.04, what would be my purchase price?
(from my trace log, it shows capital_used: 9900.95, cash: 99.04 , 111.04 x 89=9882.56
the next day 05/22/2015 close price is: 111.08 111.08 x 89 = 9886.12
)

3 responses

The price at which your order was filled does not seem to be available via the API, even though it is available to the IDE.

At https://www.quantopian.com/help, scroll the table of contents on the left to "Order object" and click on it. You will see a description of the structure of the order objects returned by the order function. Fields of this object are updated later, as the order is filled. One field is amount and holds the number of shares ordered in this order (positive to buy, negative to sell). Another is filled and holds the number of shares filled (bought or sold, respectively) for this order so far. There is Commission (why capitalized?). There is nothing that would tell you, or let you calculate, how much money, net of commissions, you have been charged for the shares that have been filled. (Have in mind that, in general, you will be charged a different price than data[sid].price, the most recent price available to you at the time you placed your order, and that the order, especially if large, may have been filled by different sellers/buyers at different prices.)

In my opinion, the amount field should be renamed number, nShares, or number_of_shares, since in general the word "amount" refers to uncountables, like water and money, and "number" to countables, like stock shares or people. Then another field, called amount, should be added, and updated to hold the money amount charged for the filled shares, net of commissions. With that, amount/filled would yield the volume-weighted average price, net of commissions, at which the order has been filled. After the order has been filled in full, this would equal amount/number, and amount+commission the total cost of the order.

Andre:

Thank you for replying. I don't quite understand you comment (below)

Have in mind that, in general, you will be charged a different price than data[sid].price, the most recent price available to you at the time you placed your order, and that the order, especially if large, may have been filled by different sellers/buyers at different prices.)

as I trying to understand how order price is decided on my back test. ( the order price should be either recent price I put my order or next day close price? but it looks it's not)

The price you're "charged" in a backtest simulates the price you would be charged on the market. In real life, most orders are "market orders" - you just send an order to buy (or sell) a certain number of shares of a certain security (stock or ETF), and then the exchange matches your order with market-makers' offers to sell (or buy). So you never know exactly what price you're going to get. The difference between the most recent price for your security (which, in the Quantopian API, you can refer to as data[sid].price, if you refer to the security ID as sid) is known as slippage. Quantopian has two built-in slippage models that try to calculate an estimate of the actual slippage, and you can code and use your own - read more about it in the Quantopian help page if you're interested.

See