Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Paper Trading Tech Stock Momentum

When we launched paper trading, we needed an algo to demonstrate the feature with. Jean whipped this one up in a few minutes, and it's been surprisingly interesting to watch.

It's a long-only algorithm that enters the market when prices are moving up quickly and exits when they are moving down quickly. As a long-only, it never goes short. It's also cash constrained in that it can never own more than 300 shares (as it's currently written anyway).

If you don't have an idea for your paper trading algorithm, you can give this one a chance. It's currently out of the market (it sat out the slaughter last week) so you can give it a clean start on Monday.

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.

14 responses

This algorithm is interesting just for the advanced usage of record() for custom variable plotting!

I used the record function to experiment with visualizing the trades that this algorithm performs. For each stock, each line is the duration of a single trade.

If nothing else, it shows how powerful the record function is!

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 Dan & Jean,

I understand the algorithm was just whipped up, but any guidance on how to make this algorithm realistic? The initial capital is $150K...a lot for prudent common folk to put into 3 tech stocks. And would this algorithm be subject to the SEC $25K limit for day-trading? Without a "Pattern Day Trading" account, my understanding is that there is a 3-day settling period. Would the algorithm still work? Do you think that the default Quantopian commissions model is representative of the costs one would incur with Quantopian/Interactive Brokers (IB)? What if odd lots were allowed? Would the algorithm still work?

Jean, perhaps you could elaborate on your syntax for the record function...how does it work?

Grant

@Grant, the algorithm is an intraday momentum strategy. So it will trade multiple times per day when the conditions are right. Definitely needs a margin account with >$25k.

Added the following line so the algorithm can be tested for days when not all 3 securities were traded.

 if not sid in data:  
            continue  

More to the point, the algorithm does not outperform the benchmark in longer backdated test. Such non-universal performance gain observed for the last few months but not for the last few years leads to following questions:

  1. When should this strategy be deployed? In up market? Down market? During high volatility?
  2. How should the stocks be picked?

Long backtest should have favored the algorithm over S&P 500 given the fact we picker AAPL and GOOG in the mix, two of the best outperforming stocks. That did not help. Had we been in buy and hold position we would have done much better given out initial picks. Momentum trade is great but the million dollar question is when.

Grant - I think the answer to your question is as much semantic as technical

In a technical sense, yes it's realistic. It's trading in highly liquid stocks that you can reasonably expect to get in and out of (though you can never be sure). One way to ask the question is "is this backtest a faithful representation of what would have happened if this algorithm was connected to my broker?" I think it is. Would you be able to get this performance if you invested $25,000 and had $20 transaction fees per trade? Definitely not. But you could model that out super fast.

I think the next sense, the sense that Vlatko is talking about, is that the algo probably isn't realistic. This algo probably looks good because it's fooling around with stocks that far outperfomed the market in the last few years. I think his point is well taken - how do you know in advance that the environment for this algo is right? So the big question on this one is "Would I put my money into it?" And for me personally, the answer is no.

How does the intra-day backtesting take bid-ask spreads into account? Is there some slippage penalty? For a $400 or $800 stock, you can lose a lot by paying to enter and exit positions... ?

Slippage model is described in API, you can also add your custom one.

https://www.quantopian.com/help#ide-slippage

Thanks Dan,

Good points. Is there a way to identify momentumish (momentumy?) stocks? Perhaps a screen could be written with set_universe?

Grant

Grant,
Good idea, I think what you're talking about can easily be done. Here's an example, although you can also test volume and other stuff. Note there may be some discrepancy because stocks can come and go due to set_universe, and mavg can only calculate the average from days the stock has been in our portfolio.

def initialize(context):  
    set_universe(universe.DollarVolumeUniverse(floor_percentile=97, ceiling_percentile=99))

def handle_data(context, data):  
    # For each stock from universe  
    for sid in data:  
        # Test the stock  
        # As an example, test if the 3 day moving average is higher than the 20 day moving average*1.2  
        if data[sid].mavg(3) > data[sid].mavg(20)*1.2:  
            # Just log the sid, but we could also buy or whatever  
            log.info(sid)  
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.

Hello Dan, Jean, and all,

Is there any evidence (e.g. academic/industry studies) that supports the viability of momentum-based trading algorithms? Where does this type of strategy fit within the spectrum of risk-reward? It would be helpful to put your example into a context.

Grant

One academic study that I know of is http://dspace.mit.edu/handle/1721.1/44439 . The author considers a number of strategies, including momentum and contrarian versions. His conclusion is that their might be some working strategies, but it that strategy selection is really hard and requires some human insight. Tbh, I would find it hard to believe that any strategy in the public domain significantly and consistently outperforms the market, unless it requires specific ressources (capital, computation power, etc.) that protect it from being arbitraged away. Hence his 'maybe' result is not too surprising.

Judging from indirect evidence - e.g. the existence of momentum-based funds - there is clearly something to momentum-based strategies, though probably not in their most simple versions.

Yeah, I think you are mostly right. As soon as something like that gets released into public domain, its value is usually reduced and it loses effectiveness. That said, there are many, many papers and theories out there and there are definitely some that work. It would be super interesting to see some meta-ish study about the performance of theories before and after the paper's (or whatever's) release.

Can anyone change the backtest.. as it is not working in Quantopian 2