Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
In Minute time frame backtesting, how to buy/sell on close price?

I am new to Quantopian, anyone experienced can help me? Thanks.

4 responses

From trial and error, I've found that the only way to consistently trade a security with good volume is to give it 1 minute to transact. So, on a typical trading day the code would look something like:

if now == close_t-timedelta(minutes=1): # buy wasn't going through without -1 minute  
    order_target_percent(context.buy_me, 1.0)  

where "now" is the current time (returned by get_date_time() and some timezone fussing). There will be another frame after this one, but orders from within that frame are likely to execute the next day - at least in backtesting.

I've never tried this with live trading, and I've also never tried this with low-volume securities.

Hi Jun,

When you run a backtest, the algorithm will buy/sell using the close_price of the previous bar. Orders are submitted in one bar and filled in the next consecutive bar according to the slippage model. This means for backtests in minute mode, orders submitted at 10:00AM are filled at 10:01AM, assuming the stock traded in the next minute. In daily mode, an order submitted on Tuesday will get filled at the end of the day on Wednesday, given the same assumption.

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 custom slippage model. We created this to simulate real-world conditions on limitations of placing large order sizes.

In the attached algo, buys and sells are driven by the close_price of the previous bar. It's a simple algo that will buy stock if the current price is above the 5 day moving average and close the position if the current price is below the moving average. Feel free to clone the algo and play around with it.

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.

Thank you, Ray and Alisa, I will look into these codes later. Have a good weekend.

Imagine, what if it were pretty easy to change back end code for daily mode when an order is made to go ahead and send the next minute bar thru handle_data to fill the order that following minute, and then back to daily again.