Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Need help with coding stat.arb [basci]

Hi all ,

Hope you are doing good . I wanted to know if someone could help me code a stat.arb strategy in python . I have it in R , but i want to shift it to python . I'm putting down the steps , its a very basic and simple stat.arb strategy . I haven't attempted to code it in python , cause i dont know much about it . If it is any help , i can post the R codes .

  1. Find the correlation Coefficient of 2 stocks , and plot thier prices on a graph .
  2. then calculate its price ratio (using EOD/end of day data) and then calculate a 100day weighted moving average of the price ratio and plot the 100day WMA on the same chart as the 2 stock prices .
  3. Calculate 2.5 standard deviation from the 100day WMA above and below (i,e. 2.5+ SD and 2.5- SD)
  4. generate trade signal when one of the prices hit the 2.5+ SD band and one hit the 2.5- SD .
  5. Stoploss at 3.5SD and get out at 1.5SD .

Could really use help regarding it badly .

Regards,

Lost

5 responses

Hello,

For an example stat.arb strategy, take a look at this community forum thread: https://www.quantopian.com/posts/fixed-version-of-ernie-chans-gold-vs-gold-miners-stat-arb. Make sure to scroll down to the fixed version posted by Thomas Wiecki!

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.

Hey Jamie ,

I just checked out the fixed version by Thomas . the trades seem to be generated based on spread difference/changes . mine on the other hand trades are generated based on the SD .
secondly i dont want python/quantopina to do the trading for me , it would be good enough if it could just show the statistics such as pair % winning , log returns of each stock in the pair of a trade , trade open , closed .

The logic behind his trade is slightly different than mine . Would it be helpful for u guys if i post the R codes ?

I'm not sure that I follow what you're asking 100%, but backtesting in Quantopian does not actually place trades, it is a simulation run over historical data. While you can definitely log results and get information from creating an algorithm and running it in the backtester, I suggest you check out our Research service. In Research, you might have an easier time gathering the information you are looking for without having to code your whole algorithm.

Hi Jamie ,

It's just that i dont knw coding and i am looking for some coding help regarding that strategy .

Regards

Ah, well let me point you toward our tutorial series then!

Lesson 1: Learn the Basics of the IDE: https://www.quantopian.com/posts/quantopian-webinar-lesson-1-the-basics-of-the-ide
Lesson 2: Universe, Fetcher, Schedule_Function: https://www.quantopian.com/posts/quantopian-tutorial-with-portfolio-rebalance-algorithm-lesson-2-universe-fetcher-and-schedule-function
Lesson 3: Working with Fundamental Data: https://www.quantopian.com/posts/quantopian-tutorial-lesson-3-basic-fundamentals-with-piotroski-score-growth-stocks-and-uptrending-volatile-small-cap-algorithms

When you're ready for the advanced series, take a look at these lectures: https://www.quantopian.com/lectures.