Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Pattern matching strategy

This is something I've been playing around with for a while and I think that the idea is interesting. Essentially this looks over past market behavior and compares it to current market behavior. If a sequence of price percentile changes in the past looks like the current sequence of price percentile changes, and the price went up after the sequence of events in the past, we get a buy signal. If the sequence in the past looks similar but the price went down after, we get a sell signal. The signals are weighted such that more recent sequences are more significant. The signals are then added together and this addition is used to take action. I went into quite a bit of detail in the code comments about how exactly this works.

I couldn't find great results when using the S&P 500 as my security, but there are probably stocks out there and parameters you can enter into this that will perform well.

If you're interested, clone this and mess around with it. I would be excited to see if any good modifications could be made or if this performs better under certain conditions.

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.

9 responses

Instead of using simple prices, how about using technical indicators with normalized value like RSI or so? I've read one or two articles on it (though I cannot remember exact strategies they used).

That would be pretty easy to do here. On line 29, I just append the fraction price change to the main list this matches from. So you can change that to something else like RSI, it might be worth a try.

Here's a different example where there's an additional loop. Instead of going through just a set amount of days for each base point it goes through a range of days for each one.

Hello Gus,

You might be interested in

Ming Li; Xin Chen; Xin Li; Bin Ma; Vitanyi, P.M.B.; , "The similarity metric,"
Information Theory, IEEE Transactions on , vol.50, no.12, pp. 3250- 3264, Dec. 2004
http://homepages.cwi.nl/~paulv/papers/similarity.pdf

Lin, Jessica, et al. "A symbolic representation of time series, with implications for
streaming algorithms." Proceedings of the 8th ACM SIGMOD workshop on Research issues
in data mining and knowledge discovery. ACM, 2003.
www.cs.ucr.edu/~stelo/papers/DMKD03.pdf

I wrote a Quantopian algorithm based on zlib for pattern matching...if I get the chance, I'll see if I can come up with a trading algorithm and share it.

Grant

Hello Gus,

Here's the pattern matching algorithm that I developed. I ran it on SPY & IVV (both S&P 500 ETFs) as an illustration. It is interesting that there is an oscillation in price similarity, with a quarterly frequency. Note, however, that the oscillations are damped during the 2008-2009 financial crisis (and also possibly over other periods, e.g. July 2010).

It still requires some work, but I think that the approach has merit. Presently, the similarity metric is pretty noisy. As the second paper suggests, a "Piecewise Aggregate Approximation" can be used for the time series (i.e. average a trailing window of prices). Also, as S.J.C. suggests above, alternate indicators could be tried (e.g. dollar-volume, money flow index, etc.).

I posted the code to https://github.com/gkiehne/quantopian/blob/master/pattern_recognition.py.

Grant

Hey Gus,

Love the concept, had a similar idea, but was having trouble coding it so this has been very helpful. Had a couple thoughts and questions though.

One subtlety I think in your method is that confidence naturally increases over time, mostly artificially. Because the code weighs more recent sections of data higher, and is constantly adding to it, confidence can get quite huge over time. This isn't a problem except that the confidence level set by the user becomes somewhat useless. What I find is that the code rarely trades early on and then eventually trades almost every day.

The other point is more of a question, and I must be having a brain fart, but in the weighting why do you subtract tomorrows fractional change from todays? Because they are already returns, can't you just use tmow's return as the first part of that weighting?

Grant, your results are interesting. The damping seems to happen on my implementation too. Even though this approach doesn't seem to work in its current state, that says something in itself, but I wonder how close the approach is to something viable.

Yes Kyle, that is a consequence of the algo having more data to work off of later. In some ways it makes sense (we know more as more time passes) but in some ways it definitely doesn't. I think this could be changed pretty easily though, as I attempted to do in the visualization of the confidence to keep the record graphs looking decent. Not sure of the exact function to use, I just quickly guess and checked until I was happy.

And, aha, you are correct I think. My mistake. I think this is correct now. The results still seem to be similar, though.

Hello Gus,

Any literature on the topic of pattern-based trading algorithms? Also, as a Quantopian employee, do you have free-ranging access to the data set? For example, can you run zipline on the data in a more flexible way than ordinary users can online? Or are you subject to the same access/download restrictions? It's really difficult trying to develop fundamentally new algorithms with the online backtester (which is basically a kind of verification tool).

Grant

Hey Grant, I use the same standard online Quantopian IDE everyone else uses. As for developing new algorithms, I don't have a difficult time doing so, but I can see where you're coming from. If you have any ideas, etc., I know we are happy to listen. There is a lot being worked on already, though.