Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Simple Classifier Trading for Predicting Long/Short Positions.

I'm sharing this as an example of using a Classifier to place orders. It predicts whether future values are greater than present ones for many stocks.

6 responses

That's pretty fascinating. Thanks for sharing this.

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 Jacob,

Would you provide an outline of how the code works? References?

Thanks,

Grant

Hi Jacob,

I agree with Grant -- this looks really interesting (great use of BatchTransform too ;) ) but some more context/description would be helpful!

Thomas

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.

It's a simple classification of $P(Y_t > 0| (Y_t-1, Y_t-2, .. , Y_t-n)$ where $Y$ is given by the $\delta W$, where $W$ is the signal and $\delta$ is the difference operator. The probabilities of the classifier are sorted into long and short positions. The classifier is gaussian naive bayes, which assumes normality of the features. The joint pdf of the features with the two classes is computed, then the probability is assigned according to Bayes rule and assumptions of independence(thus naive bayes).

Oddly the classifier is trained on all of the data within that percentile.

Thanks Mick,

A bit over my head at this point, but I did clone your algorithm and noticed a few things:

  1. Your use of margin seems o.k. Basically, you start out with $100K and then maintain two short positions of ~$100K each. Referring to http://www.investopedia.com/ask/answers/05/shortmarginrequirements.asp, you'd need to ante up $100K for every $200K short, so you are using all of your available margin.
  2. I noticed in the "Position Values" table of the backtest output that the securities your algorithm shorts tend to be adjacent in the list of securities (e.g on 2012-03-16, you are short NLY and WAG, which listed in adjacent columns). Perhaps this is a result of how the backtester structures the "Position Values" table and is unrelated to how your algorithm selects securities?
  3. Under "Transaction Details" you transact as little as 1 share. I don't have experience, but my sense is that in real-world trading such odd lots might cause problems (see http://ibkb.interactivebrokers.com/article/1062). Also, I wonder if the small transactions would just generate commissions without any substantial benefit to the returns.

Grant

Thanks for the overview. One would hope that the independence assumption of Naive Bayes is violated or you wouldn't be able to predict anything ;). I am thus wondering if a classifier that does not have the assumption could do any better. E.g. have you tried Logistic Regression or SVM that can also assign probabilities but should be able to capture covariance structure?