Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
full order doesn't get put in on the same day?

I'm trying to do a very simple example algo where I make a portfolio where I have 1M in the account and use all of it to buy a stock on the first day, then hold it.
https://www.quantopian.com/algorithms/5366fabc9a6412ac1500008b

I noticed that I when i call order(SID, amount), it doesn't actually place the full order. In my example, the system is supposed to buy 14463 shares, but it buys 10051 on the first day, then fills up to 14405 on the next day, then 14463 finally on the next day.

what is going on?

'''

def handle_data(context, data):
price = data[context.vanguardConsumerStaples].price
notional = context.portfolio.positions[context.vanguardConsumerStaples].amount * price
numshares_tobuy = math.floor(context.max_notional / price)
numshares_vanguardConsumerStaples = context.portfolio.positions[context.vanguardConsumerStaples].amount

debug statments

print "Notional today: " + str(notional)  
print "Num shares today: " + str(numshares_vanguardConsumerStaples)  
print "Price today: " + str(price)  
print "Num shares to buy today =floor(context.max_notional/price) = " + str(numshares_tobuy)  
if numshares_vanguardConsumerStaples == 0:  
    print "BUY " + str(numshares_tobuy) + " shares " + str(context.vanguardConsumerStaples)  
    order(context.vanguardConsumerStaples, int(numshares_tobuy))  
print "Portfolio value: " + str(context.portfolio.portfolio_value)  
print "Positions value: " + str(context.portfolio.positions_value)  
print "Num shares now, after IF statement: " + str(context.portfolio.positions[context.vanguardConsumerStaples].amount)  
print "-------------------------------------"  

'''

2008-05-05PRINTNotional today: 0.0
2008-05-05PRINTNum shares today: 0
2008-05-05PRINTPrice today: 69.14
2008-05-05PRINTNum shares to buy today =floor(context.max_notional/price) = 14463.0
2008-05-05PRINTBUY 14463.0 shares Security(25903 [VDC])
2008-05-05PRINTPortfolio value: 1000000.0
2008-05-05PRINTPositions value: 0.0
2008-05-05PRINTNum shares now, after IF statement: 0
2008-05-05PRINT-------------------------------------
2008-05-06PRINTNotional today: 696132.26
2008-05-06PRINTNum shares today: 10051
2008-05-06PRINTPrice today: 69.26
2008-05-06PRINTNum shares to buy today =floor(context.max_notional/price) = 14438.0
2008-05-06PRINTPortfolio value: 995348.292615
2008-05-06PRINTPositions value: 696132.26
2008-05-06PRINTNum shares now, after IF statement: 10051
2008-05-06PRINT-------------------------------------
2008-05-07PRINTNotional today: 988327.05
2008-05-07PRINTNum shares today: 14405
2008-05-07PRINTPrice today: 68.61
2008-05-07PRINTNum shares to buy today =floor(context.max_notional/price) = 14575.0
2008-05-07PRINTPortfolio value: 986818.116042
2008-05-07PRINTPositions value: 988327.05
2008-05-07PRINTNum shares now, after IF statement: 14405
2008-05-07PRINT-------------------------------------
2008-05-08PRINTNotional today: 994186.62
2008-05-08PRINTNum shares today: 14463
2008-05-08PRINTPrice today: 68.74
2008-05-08undefined:undefinedWARNLogging limit exceeded; some messages discarded
2008-05-23PRINTNotional today: 999393.3
2008-05-23PRINTNum shares today: 14463
2008-05-23PRINTPrice today: 69.1
2008-05-23PRINTNum shares to buy today =floor(context.max_notional/price) = 14471.0
2008-05-23PRINTPortfolio value: 993895.704438
2008-05-23PRINTPositions value: 999393.3
2008-05-23PRINTNum shares now, after IF statement: 14463
2008-05-23PRINT-------------------------------------
2008-05-27PRINTNotional today: 1002864.42
2008-05-27PRINTNum shares today: 14463
2008-05-27PRINTPrice today: 69.34
2008-05-27PRINTNum shares to buy today =floor(context.max_notional/price) = 14421.0
2008-05-27PRINTPortfolio value: 997366.824438
2008-05-27PRINTPositions value: 1002864.42
2008-05-27PRINTNum shares now, after IF statement: 14463
2008-05-27PRINT-------------------------------------
2008-05-28PRINTNotional today: 1000405.71
2008-05-28PRINTNum shares today: 14463
2008-05-28PRINTPrice today: 69.17
2008-05-28PRINTNum shares to buy today =floor(context.max_notional/price) = 14457.0
2008-05-28PRINTPortfolio value: 994908.114438
2008-05-28PRINTPositions value: 1000405.71
2008-05-28PRINTNum shares now, after IF statement: 14463
2008-05-28PRINT-------------------------------------

2 responses

I believe this is due to the data frequency being daily and the slippage model being used. By default, a volume share slippage model is used (see the API docs) which limits the amount of shares you can buy or sell on a single bar.

The daily data frequency means that the next available bar is always the next days close price. I think this would explain what is going on, I'd like to hear from others on this too though.

In an algorithm, orders are submitted in one bar, and filled in the next consecutive bar according to the slippage and commission models. David was correct, and by default, you can trade up to 25% of the security's traded volume in a single bar. The rest of your order is filled in subsequent bars. You can change the slippage to any percentage you'd like or create your own slippage model. We created this to simulate real-world conditions on limitations of placing large order sizes.

If you want to place the entire order in the first bar, then you can set the slippage to 0.

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.