Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How to improve the return of gold and silver pairs trading strategy?

Hello, I am very new to Quantopian and Algorithm Trading. So, I want to share what I have done and learn from all of you.

The Strategy
It is a mean reversion strategy that tries to capitalize the mean-reverting feature of the spread between gold and silver.

Run every day
First of all, I extracted 1 year (around, I don't know how to make it exactly a year) of the price of gold and silver. Then using 20 days moving windows, I calculate the slope and intercept from the linear regression between the two assets. I stored the calculated spreads (should have 365 -20 number of spread, approximately). Finally, I calculate the MEAN and SD of all the spreads.

Entry Run every day
I checked the entry after market started by using the schedule function. If the current spread is higher than 1.5SD + MEAN, I will sell. If the current spread is lower than MEAN - 1.5SD, I will buy. The max. open order is 5. That means the program will continue entering the position if conditions are met. The reason for doing this is that I cannot pick the exact top but I am sure that it will revert to the mean. So, I keep adding positions.

Exit Run every minute
If the current spread reverted to the mean, I will close all positions. However, it is difficult to catch the exactly equal. So, I put a range of 10% error.

I have attached the backtest for your reference.

One problem I got is that I don't know why sometimes, even the 5 opened positions reverted to the mean, I still have negative return. You can try that in the backtest started from 01/01/2017

3 responses

Max Lvrg: 2.15 2016-02-11 is one thing to address. Also take a look at some others that trade GLD:
https://www.google.com/search?q=gld+"def+initialize"+site:quantopian.com

Thanks for your reply!
Sorry. What do you mean by Max Lvrg: 2.15 2016-02-11?

On 2016-02-11, leverage hit a new high of 2.15. It's fine if intended, if accidental can ruin one's day. This might help. Also logging context.portfolio.cash can be useful.