Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Set Timeframe (in minute mode)

Hi all, new here so take it easy - Could not find a clear answer to this one:

Q: How to set the timeframe when in minute mode
ie. run algo in 240min bar mode

Thank you

5 responses

Hello Darell,

To my knowledge, there is no '240min bar mode' available. Do you actually need OHLCV bars that are 240 minutes long? Or something else?

With a little cleverness, you should be able to construct custom bars, but handle_data will still be called every minute (typically).

You might have a look 'history' on the help page. In particular, when frequency='1d', the data returned are:

Returned Data

The returned data for daily history, for each day, is:

close_price: the close of the last minute bar for that day. For the current day, the most recent close is returned.  
price: same as close_price.  
open_price: the open of the first minute bar of the given day.  
volume: the sum of all minute volumes. For the current day, the sum of volume thus far.  
high: the max of the minute highs for the given day.  
low: the min of the minute bar lows for the given day.

So, when you are 240 minutes into a given day, history can give you an OHLCV bar. Or do you need the 240 minute bar on a rolling basis?

Grant

Thanks for getting back. What i meant was how to run the moving averages, entries, exits and everything else in 240min mode (on a rolling basis)
so if i have 50period moving average in the formula, it would be 50ma of last 240min price bars.

Hope its clear now, its pretty basic stuff so should be simple
thanks

Is this what you need? --Grant

import pandas as pd

def initialize(context):  
    context.stock = sid(8554) 

def handle_data(context, data):  
    price = history(240,'1m','price')  
    price_ma = pd.rolling_mean(price,50)  
    print price_ma.tail(1)  

Are you looking to make 240 minute bars, then from the last 50 of those bars, work with them to get things like the moving average? If so, I don't think history is the best candidate, because it is slow storing 12,000 bars and we just need 1/240th of that. I used a deque to store the close prices we need. But a solution more like Grant's does have the advantage that it doesn't need to warm up, while this takes a week or two of backtest time.

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.

Gus and Grant - Thanks, i can now tinker on and will ask more as needed