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-------------------------------------