Hello,
I would appreciate if someone could tell me if I am selling the 1000 shares I bought of SPY correctly? I am trying to learn Python and use the API correctly. Hopefully soon I can write more interesting algorithms. Thanks for the help.
I would also appreciate advice on how to do the following in Python:
I would like to make buy 1000 shares of SPY on specific dates such as the ex-dividend date for the past two years. Then, I would like to sell it all a couple of days after the pay date in the same year it was bought.
So, if I bought SPY on year 2012, I would like to sell it on the last pay date of 2012. I hope what I said made sense.
Thanks
def initialize(context):
context.SPY = sid(8554)
#context.orderID=order(context.SPY, 1000)
context.portfolio.positions[context.SPY]
def handle_data(context, data):
day = 20
month = 9
year = 2013
day2 = 31
month2 = 10
year2 = 2013
current_day = get_datetime()
if year == current_day.year and month == current_day.month and day == current_day.day: #want to buy SPY on ex-dividend date for now.
order(context.SPY, 1000)
print str(current_day)
buyPrice = data[context.SPY].price
print 'The buy price for SPY was %f' % buyPrice
print 'Order submitted, SPY, 1000 shares'
if year2 == current_day.year and month2 == current_day.month and day2 == current_day.day: #am I selling it correctly?
order(context.SPY, -1000)
print str(current_day)
sellPrice = data[context.SPY].price
print 'Sell price %f' % sellPrice