Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Buy at open, sell at close

Hello everybody. I am just getting started with Quantopian.
I have a simple code where I buy at open and sell at close. But when I log it seems that I don't sell. I assume I would get zeros for share.
I can't figure where I went wrong.
I appreciate any help.

def initialize(context):  
    context.data = symbol('AAPL')  
    schedule_function(buy,  
                      date_rules.every_day(),  
                      time_rules.market_open())  
    schedule_function(sell,  
                      date_rules.every_day(),  
                      time_rules.market_open())

def handle_data(context, data):  
    pass  
def buy(context,data):  
    order_percent(symbol('AAPL'), 0.50)  
    order_share = context.portfolio.positions[symbol('AAPL')].amount  
    #print("check order_share=",security.symbol," share=",order_share)  
def sell(context,data):  
    order_target_value(symbol('AAPL'),0)  
    order_share = context.portfolio.positions[symbol('AAPL')].amount  
    print("check order_share=",security.symbol," share=",order_share)  

Log:

2015-01-05PRINT('check order_share=', u'AAPL', ' share=', 0)
2015-01-06PRINT('check order_share=', u'AAPL', ' share=', 4705)
2015-01-07PRINT('check order_share=', u'AAPL', ' share=', 4706)
2015-01-08PRINT('check order_share=', u'AAPL', ' share=', 4671)
2015-01-09PRINT('check order_share=', u'AAPL', ' share=', 4584)
2015-01-12PRINT('check order_share=', u'AAPL', ' share=', 4581)
2015-01-13PRINT('check order_share=', u'AAPL', ' share=', 4637)
2015-01-14PRINT('check order_share=', u'AAPL', ' share=', 4615)
2015-01-15PRINT('check order_share=', u'AAPL', ' share=', 4623)

10 responses

It looks like sell is scheduled also at open, you probably wanted something like time_rules.market_close(hours=0, minutes=1 as in documentation examples.

My mistake, I was actually running it with time_rules.market_open(hours=6) But I get exactly the same results with (time_rules.market_close(hours=0, minutes=1)

If i understand correctly, the orders are not executed immediately but at the next bar (minute), so i think checking position right away won't really work. So your log in "sell" function really shows the position before the sell order is executed. If you uncomment the logging inside of buy (and update the text to be different so you can distinguish log lines) then you should see zeroes logged in buy functions since that your position from the previous day, after sell is executed. If you want to see the result of sell on the same day you will need to schedule another function to be called 1 min after sell. You may need to shift the sell function 1 min earlier as i am not sure Q allows scheduling a function for the last minite (which means after last 1-minute bar of the day, i.e. after market close).

Thank you for your help.
I still get the same log, but every line is repeated 3 times. But I am sure that sell function is working because when I get rid of it I get a very different backtest graph. My only problem is that I can't seem to figure out at what point I have actually sold all the shares.

I am worried about it, because I am writing a different strategy where I have sell at close, and it wouldn't work properly if I hold the position till next trading day.

After reading this thread

I believe I should be able to see zeros in my log after I sell everything

Hi Artem,

Would you be willing to share a backtest of your algorithm so I can take a look? If not, you can also send an email to [email protected] and I can take a look there!

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.

Hello Jamie,
I decided to buy at open and sell 6 hours later. According to backtest, I am selling the next day. So if I buy at open on 01/06/16, I sell at close on 01/07/16, but I also buy on 01/07/17

Same thing happens if I sell at close, or 10 minutes before close.

schedule_function(sell,  
date_rules.every_day(),  
time_rules.market_close())  
# or time_rules.market_close(minutes=10)  

You need to run the backtest in minutely mode.

Hello Jack.
That fixed it. Thank you very much.
I was under impression that if time_rules.market_close()) minutely mode vs daily mode wouldn't matter. I was wrong.