Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Help with my simple algorithm

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

Welcome to Quantopian, Jaime.

In general, you'll get better feedback if you "share" your algorithm rather than copy and paste the code. To do that, click on "Full Backtest" and once the full backtest is finished, then click on "Share Results". You get something easy to clone, like I did below.

The algo does everything you were looking for. It's a great start. You'll need to use more flexible code to do live trading - in minutely mode, this algo will order 390 times, once each minute! And if you set it for a specific time, you have to worry about Daylight Savings, things like that. But those are all things to learn in the future.

I added one line to your code so that it will record() and graph your holdings.

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.