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