Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Get ready to live trade with Quantopian: sample algo for daily rebalance

This algorithm is perfect for people interested in live trading. It is very simple. It creates a portfolio and then rebalances on a regular basis - daily, or weekly, or however you specify. It is long-only, with no shorting and no leverage.

This particular version invests in an equal-weighted portfolio of 9 sector ETFs, and rebalances every day to keep the portfolio weights constant.

We have been adding quants to our pilot live trading program every day (if you are on the waitlist already don't worry we are coming for you soon!) - and one thing that I've learned so far is that we need to be sharing more sample code that is designed with live trading in mind.

Thanks Seong for your help coding this up.

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.

8 responses

Thanks Jessica,

In looking over the code, I don't see a check for open orders. Additionally, in one of my algorithms, I check that all securities have real trade data for the current tic. A few lines of code handles both cases:

# skip tic if any orders are open or any stocks did not trade  
    for stock in context.stocks:  
        if bool(get_open_orders(stock)) or data[stock].datetime < get_datetime():  
            return  

For a discussion of the filling issue, see https://www.quantopian.com/posts/thinly-traded-stocks-why-no-gaps.

The first check ensures that no orders are pending (re-balancing with open orders could result in an imbalance). The second check ensures that all of the trade data are real and not interpolated by Quantopian.

Also, you might consider what happens if the market opens late (after 9:30). Will the code still execute?

Grant

Grant,

Thanks for that, I've added a check for open orders and thinly traded securities into the code. However, I'm not too familiar with late market open orders. Would these occur in cases of natural disasters and etc.?

-Seong

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.

Hi Seong,

I'll have to look over your code a bit more to understand if there may be a problem with your assumption that the market will always open at 9:30.

Grant

A list of NYSE closings:

http://www.nyse.com/pdfs/closings.pdf

Hi Grant,

Timing rebalance is something I'd expect each algo author to want to modify on their own since trade execution can be a proprietary part of the algo. We put the 9:30am trigger here really just as a placeholder - and you are absolutely right that it is a brittle part of the code.

Probably more robust is to replace that with a wider range trigger that looks to rebalance anytime between say 10am and 3pm - in that case you just need to make sure to also add a condition that will keep your algo from rebalancing more than once in the trading day (unless that's what you intend).

So in this example you might modify the method openofday_check to be intradingwindow_check and do something like this:

def intradingwindow_check(context):
# Converts all time-zones into US EST to avoid confusion
loc_dt = get_datetime().astimezone(timezone('US/Eastern'))
if loc_dt.hour > 10 and loc_dt.hour < 15:
return True
else:
return False

Best regards,
Jess

Thanks Jess,

With the code you and Seong posted, is there a way to go to 100% cash (completely out of the market)? I don't have time to try it now, but it seems that by setting all of the weights to zero, the rebalance function should go to cash right? Does it work?

Grant

Grant,

Yes, setting the weights of all securities to 0 will essentially exit all of your positions, leaving you in cash.

An example would be

context.weights = 0  
rebalance(context, data, 0.0, context.weights, 0.0)  

Hello Jesse and Seong,

I added variable weighting in the attached example (with random weights applied to test the code). Perhaps of interest to you or others.

By the way, any discussion at Quantopian to integrate github? It sure would be nice to track changes and collaborate on code.

Grant