Back to Community
Is it possible to do live trading with a daily trading frequency?

I would like to know if it is possible to do live trading with a daily trading frequency for the algorithm data?

9 responses

There's a similar question here: https://www.quantopian.com/posts/how-to-create-different-time-intervals

If you only want once a day, I think the best way is to use get_datetime() and check if it's, say, 12:00 noon and only execute the remainder of the script if it is. You can basically want to limit the algorithm to execute once every day. Does that help?

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,

Thanks for you response, but I should elaborate more on what I am attempting to do with technical indicator's evaluation time period.

ta.ATR(timeperiod=3)  

From API doc on TA-lib:

Daily vs minute data

Quantopian's TA-Lib methods respect the data frequency in your
backtest or live algorithm. If you're backtesting with daily data,
then all the time periods are calculated in days. If you're
backtesting or live trading with minute data, all the time periods are
in minutes.

If I am understanding quantopian's logic, timeperiod = 3 for daily backtest will look at 3 days and for minutely backtest or live trading will look at 3 minute bars. Is that correct?

What I want is even with live trading selection, is there a parameter which can be used to specify the time period to be 3 days for the evaluation of technical indicators from TA-lib.

Wow, I am listening to this thread for sure.

What I did previously, was to introduce a stuff called context.counter. Before doing anything I made it +=1. Then I use a modulo to control the frequency of updating values, positions etc. This is a problematic and dirty hack.
Should have read the API doc before. And I am looking forward to seeing how you guys will solve this problem.

Hmm, there might be a solution. Doing some experimenting with the code below, moving the first line of handle_data into the if statement produces different results than the same line in its current location out of the if statement. So you may be able to control when the ATR frame is updated.

atr = ta.ATR(timeperiod=4)

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

def handle_data(context, data):  
    N = atr(data)[context.security]  
    if get_datetime().hour == 20 and get_datetime().minute == 59:  
        log.info(N)  

But, this isn't bulletproof, because sometimes the market closes early. So see this thread for some discussion about that: https://www.quantopian.com/posts/market-on-close-moc-orders.

If that fails, you can also try calculating ATR manually.

Hopefully that points you in the right direction?

Gus, Thanks for your reply!

Now, I tried using the batch_transform feature and it was great experience learning this API feature on batch_transform and pandas dataframe concepts:

def get_ATR_today(context, data):  
    close_prices = get_all_closes(data)  
    if close_prices is None:  
        return  
    prev_close_price = close_prices[context.security][0]  
    high_price = data[context.security].high  
    low_price = data[context.security].low  
    atr = max(high_price, prev_close_price) - min(low_price, prev_close_price)  
    log.info("Today's ATR: %.3f C: %.2f H: %.2f L %.2f" % (atr, prev_close_price, high_price, low_price))  
@batch_transform(refresh_period=1, window_length=2)    # compute_only_full=True (default)  
def get_all_closes(datapanel):  
    closes_df = datapanel['close_price']  
    if closes_df is not None:    # window is full now  
        print closes_df  
        return closes_df  
    else:  
        return None  

Here is my log output:

2013-08-06PRINT 8554 2013-08-05 00:00:00+00:00 170.73 2013-08-06 00:00:00+00:00 169.75
2013-08-06get_ATR_today:80INFOToday's ATR: 1.380 C: 170.73 H: 170.52 L 169.35

2013-08-07PRINT 8554 2013-08-06 00:00:00+00:00 169.75 2013-08-07 00:00:00+00:00 169.20
2013-08-07get_ATR_today:80INFOToday's ATR: 1.200 C: 169.75 H: 169.43 L 168.55

2013-08-08PRINT 8554 2013-08-07 00:00:00+00:00 169.20 2013-08-08 00:00:00+00:00 169.79
2013-08-08get_ATR_today:80INFOToday's ATR: 1.250 C: 169.20 H: 170.18 L 168.93

Below is data from the Yahoo Finance for SPY:

Date Open High Low Close Volume Adj Close*
Aug 8, 2013 169.98 170.18 168.93 169.80 101,927,800 169.80
Aug 7, 2013 169.19 169.43 168.55 169.18 84,582,400 169.18
Aug 6, 2013 170.37 170.74 169.35 169.73 87,369,700 169.73

First thing, I noticed the difference on the close prices from your data source and Yahoo Finance! Can we know why this discrepancy, (albeit, slight!), so that we are aware of it, but it may or may not make a difference. I have not done extensive data comparison on the historical data yet for O, H, L, C, V.

Second question, when the batch transform in above algo is used for minutely mode or live trading, does it still go by the daily frequency? Or in the above example, will it record the previous minute's data bar (not the previous day's data bar like I am expecting for daily data)

From API doc:

Minute Data in batch_transform, Rolling Updates, and refresh_period

The same code above could also be run in minutely mode. In that case,
the window of data would be 10 trading days, but because updates are
at minute granularity, the first and last day would be partial. For
example a 10 day window would stretch from 10:32am on 3/1/2013 to
10:31am on 3/14/2013. This is in contrast to daily mode, where the
trading days are always complete.

As you can see from this example, batch_transform is by default
rolling - each new event passed to the function will update the window
of data. We chose rolling as the default behavior because it is the
most commonly needed, but sometimes it doesn't make sense to update
the window on every bar: a batch transform may depend on comparing
whole trading days, or complete weeks and months.

For these cases, the batch_transform provides an optional parameter
refresh_period. For example, if you wanted your minutely simulation to
only advance the window in whole day increments, so that the end of
the window would always be through yesterday's close, you could
specify the batch_transform like this:


@batch_transform(window_length=10, refresh_period=1)  
def get_average(datapanel): 

My concern with this: why is refresh_period=1 and why not refresh_period=6.5*60 (the number of trading minutes on a normal close day - 9:30a-4p)

Hello Hardik,

This decorator will cause the batch transform to update every minute, with a trailing window of a day's worth of data (I think it is fixed at 390 tics):

@batch_transform(refresh_period=0, window_length=1)

Grant

Hardik, answering your question about differences in close prices between Quantopian and Yahoo. One of the things that I have found in finance is even the simple things end up being more complex than you'd ever think. . . .

Yahoo is an 'end-of-day' (EOD) datasource. Yahoo and other EOD data providers get their price and volume data from the official exchange record. Quantopian's data is generated by the actual trades, regardless of what exchange the trade was made on. The EOD sources rarely exactly match data derived from intraday data. For instance, the official close for a NYSE stock is the last trade of the day for the stock on NYSE. But if the stock also trades on Chicago, Pacific or another regional exchange, the last trade on one of those exchanges could be our close.

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.

@Grant, thank you for your suggestion on the decorator. I will try experimenting that more with live trading/minute mode.

@Dan, Thanks for the explanation on the data source difference between Quantopian and the EOD data sources.