Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Another system for someone else to try
11 responses

What's the easy way to "sell at the close"? I didn't find it...

The only way that I know of is to add a test for 15:58 and then only run on minute wise back tests...

Well, I guess the strategy isn't that great, then -- my backtest sells at 15:00, but if there's a lot of return in the last hour of a down-day's trading, I'd be surprised. If we run that strategy on a universe instead of just the S&P, it does better, but I haven't run it for a long time -- see attached backtest.

I'm going to be making a bigger announcement later, but I wanted to give you guys a heads up about a bug we're about to fix.

Right now when you trade, the first minute bar is marked 14:31 UTC, every day. Unfortunately, that's not correct - during daylight savings time, the market open is 13:31 UTC. We're going to ship a change soon that will fix that bug. The behavior of "sell at close" and similar strategies will need to be updated after we fix the bug.

Sorry for the inconvenience.

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.

Oh yeah, definitely need to have a timezone in any time-of-day comparisons.

@Dan thanks -- I guess this is not the thread for it but many markets will have a "market state" that might be relevant to the bar creation. Otherwise we're going to have to track the first bar of the day and (worse but more realistic) anticipate the last bar of the day for open and close strategies. This is to some extent unavoidable but perhaps having a "daily bar count" to the data frame, or a well-known recipe / FAQ on how to track that, would be useful (perhaps there is one already and I've missed it :)).

Pallas, the situation is far worse than you might imagine.

US markets typically close early at 1pm (Eastern) the day before Christmas or Independence Day and the day after Thanksgiving.

However since Christmas and Independence Day could fall on a weekend there are numerous exceptions. The market may close the full day Friday for a Saturday holiday or Monday for a Sunday holiday. You would expect this would move the day of the 1pm early close to either the Thursday or Friday beforehand. Except sometimes they close at 1pm the day after Independence Day (if that would be a Friday). Or don't close early at all if the holiday was on a Sunday or Monday. You get the idea.

And all the markets (NYSE, NASDAQ, etc) don't always close at the same time. This is especially true for presidential funerals and whatnot.

So while it would be nice to have a "market state" object it would actually need to be "expected time of close of trading" per security.

And that doesn't even count the days the markets were closed by weather or interrupted by glitches or brief memorial services.

Perhaps less importantly the markets will occasionally stay open after the expected close to process orders for customers affected by earlier glitches.

I think the most realistic option is to just start wrapping things up after 3pm (Eastern) and treat all the other stuff as unavoidable exceptions. If you want you can try to avoid the holiday chaos by not trading at all a day or two before or after a holiday.

@Dennis definitely, the market state will be per-security per-market.

Hello Dennis,

The QSTK package used on the Coursera 'Computational Investing, Part I' course (https://www.coursera.org/course/compinvesting1) has a getNYSEdays function that reads a text file of the days the NYSE was open. It covers 1962 to 2030 in a little over 17000 lines. And it doesn't help with early closes!

Regards,

Peter

It's a very tedious problem, but the good news it's also an area where quantopian could add a lot of value by relieving developers of that tedium. It would also offer them a measure of platform lock-in, in that once algos come to rely on the nice environment that wraps up trading hours, afterhours, holidays, and so forth, people will be disinclined to switch if it means having to code up all that themselves!

I'm following this thread with interest. I think all the points here are great. Pallas wants to be able to trade in a certain way, the reality of the markets make it very hard to trade that way, and Quantopian should provide the tools to make that possible. We're excited to remove all of that friction and make it easy to turn ideas into algos.