Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How to create different time intervals?

Hello,

I am extremely new, but wanted to try Quantopian out with a strategy that has intrigued me for a while now.

Are there any current algorithms that "watch" the 1 minute charts for a speficied amount of time and then once a trade is executed, switch to a longer time interval like 5, 10 or 15 minutes?

I know that only minute and daily data is available, was hoping someone has already created code to handle creating a different time series.

Thanks,
Chuck

6 responses

Welcome to Quantopian!

In your own code, you can turn 1-minute bars into any larger size that you'd like. Depending on your algorithm it can be really easy. For instance, if you only care about the close price at the end of the hour, you can run your algo in minutely mode and use an "if" statement to only operate your logic every hour. There's a sample of looking at by-minute code here: https://www.quantopian.com/posts/trading-in-the-minute

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.

Welcome Chuck,

I'm not sure that I understand your request. Are you interested in thinly traded securities (ones that don't trade every market 'tic')? Or are you looking to buy/sell and then wait for 5/10/15 minutes?

Quantopian can handle both.

Grant

Thanks Dan, I think I understand that code and believe I can do something with it. It has been years since I have done coding in Python, so that is likely my real problem!

@Grant, My algorithm will be watching the start of the trading day at the minute level, but after 15 minutes I would like to switch my time interval to making decisions on a 5 minute chart for the remainder of the trading day.

Basically I want to start the day watching a minute chart to ensure I do not miss an entry, while after the flurry of the market open has subsided, we can look at a longer interval for entry and exit points.

Thanks,

Chuck

Hello Chuck,

I've attached some example code that will submit an order for 100 shares of SPY every day, upon market open. If you haven't already sorted it out, it is important to understand how Quantopian deals with time. In the background, for the code I posted, the backtester is effectively looking for the first trade (tic) of the day for SPY in the database. As soon as it sees an SPY trade in the new day, it submits an order (and the order will be filled in the next SPY tic).

The backtester is event-driven, not time-based (although each event in the database has an associated datetime stamp). Also, for a given security, there is no flag in the database that signals the market to be open for that security.

Grant

@Chuck, here is a template you can use to build a strategy. At the moment it doesn't have any trading logic. But it does split up the day into three parts: opening rally, 5-min periods, closing rally.

The idea is you would put your trading logic in handle_open_rally(), handle_5min() and handle_close_rally(). These are called once for each stock you are tracking.

The functions for the opening and closing rallies are called every minute. Please note that some days the market closes before 3:45pm. This template doesn't have any code to handle those exceptions.

The 5-min function is called every 5 minutes and provides a list of the 5 prices for each minute in that time period.

Enjoy!

Thanks Dennis,

One potential "gotcha" is handling of thinly traded securities. Also, in a similar vein, I'm not sure it is best to base the opening rally on a fixed time frame. It seems that the first trade of the day should be detected. For example, what if trading in a given security is delayed, or the whole market opens late? This is similar to the early closing problem you flagged, but upon opening.

I need to think about it, but it seems that the approach would be more general by counting events for a given security, rather than using the datetime stamps.

Chuck...regardless, I would just forge ahead and start tinkering with the code Dennis posted (and see if you can find the "corner cases"--then you'll really understand what's going on).

Grant