Back to Community
Gold Silver Pairs Trading.

I am very new to Quantopian and Python. And I am worried that I did something wrong in the code. Can anyone help check it?

Strategy Description
The words/ numbers in < > can be changed by users in the program
Step 1: Calculate linear regression between Gold ETF (GLV) and Silver ETF (SLV)
• Look back day prices • Gold – asilver = constant (a is some constant) [linear regression]

Step 2: Use co-integration test to test mean reversion for the formula obtained from linear regression
• Test mean reversion for Gold – a*silver = constant (a is some constant) • If p-value <= , then the pair is tradable.

Step 2: Construct Bollinger band.
• Bollinger Band settings: days SMA; Standard Deviation • If pairs value (i.e. the constant in the formula) >= upper band, short (Gold – a * silver) o Sell 1 share Gold and Buy "a" share Silver  “a” should be an integer (round the number if needed) • If pairs value <= lower band, buy (Gold –a *silver) o Buy 1 share Gold and Sell "a" share Silver o “a” should be an integer (round the number if needed)

Step 3: Exit
• Take Profit: % • Stop loss: %

Clone Algorithm
13
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 58ef81040bec0564c5da33d6
There was a runtime error.
1 response

Hello Kai,

This is a great start. A few things you can do here:

  1. Research the pairs in a notebook to see if they actually satisfy requirements. It could be that there's just no signal in the stocks. You can use this notebook to get started.
    https://www.quantopian.com/lectures#Introduction-to-Pairs-Trading

  2. Look at your target position weights, they could be very low. You can check this using the debugger feature. Click on a line number in the algorithm editor and the next time you build algorithm you'll be popped into a debugging environment.

  3. Look at an existing template. https://www.quantopian.com/lectures#Example:-Pairs-Trading-Algorithm

If these don't work then post back here and I'll try to dig a bit more.

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.