Backtesting XAG/USD (Silver) price prediction using Facebook Prophet

In our previous blog post we explored the use of Facebook Prophet in Python to predict the future 2 months of Silver prices in USD.

As promised in this one we will backtest its prediction for the whole of 2017 prices by training its model with data from 2015 and 2016.

Trading Strategy

The trading strategy is made be as real as possible, even if it's not. We have not taken spreads into account, only stop loss is time based and we take a position at the start of every month and close our position at the end of the month(regardless of loss) if our take profit has not been triggered. We didn't even use correct decimal places! (just noticing)

We have an account of 2500 USD, a margin of 1:1. We are taking positions of value which is over 1700 USD. In your real account please never take a position of that proportion to our account size. We are also putting a ridiculous stop loss if our account reaches below 2000 USD at any point after a trade.

The project can be accessed at https://github.com/Lougarou/backtestfbprophet

We are using daily data from investing.com and minute data from histdata.com

How The Backtesting Works

We are looping through every month starting from January 2017 to October because we don't have December data to predict from November. We fit a model using all the months before that month. 

We take the difference in Prediction(Last-First) and add it to our current price to get our target. If the target is higher than the current price it's a long position if not it's a short position.

For example for January:
Note: You can access all the images in the results folder.

target = currentPrice + (dfResult['Prediction'].iloc[-1] - dfResult['Prediction'].iloc[0])
In the first month we have made a loss because we shorted with a take profit at 15.73 which didn't happen for this month:
------ Month 1 ------
Current Price: 15.98 at 2017-01-02 00:00:00
Short position: 15.729300232
Closing position: loss: -25.0699768043 at 2017-01-31 23:58:00

All Output

------ Month 1 ------
Current Price: 15.98 at 2017-01-02 00:00:00
Short position: 15.729300232
Closing position: loss: -25.0699768043 at 2017-01-31 23:58:00
------ Month 2 ------
Current Price: 17.44 at 2017-02-02 00:00:00
Short position: 17.3030496063
Short take profit at 2017-02-02 22:32:00, 17.301
------ Month 3 ------
Current Price: 17.82 at 2017-03-02 00:00:00
Long position: 18.3536200225
Long take profit at 2017-03-01 03:35:00, 18.36
------ Month 4 ------
Current Price: 18.28 at 2017-04-03 00:00:00
Short position: 17.9630014026
Short take profit at 2017-04-07 13:26:00, 17.883
------ Month 5 ------
Current Price: 16.864 at 2017-05-02 00:00:00
Short position: 16.5116999357
Short take profit at 2017-05-03 15:50:00, 16.51
------ Month 6 ------
Current Price: 17.548 at 2017-06-02 00:00:00
Short position: 17.2965927275
Short take profit at 2017-06-01 01:23:00, 17.296
------ Month 7 ------
Current Price: 16.193 at 2017-07-03 00:00:00
Short position: 15.6622601726
Short take profit at 2017-07-06 19:07:00, 14.666
------ Month 8 ------
Current Price: 16.543 at 2017-08-02 00:00:00
Short position: 16.4962176113
Short take profit at 2017-08-02 08:51:00, 16.485
------ Month 9 ------
Current Price: 17.836 at 2017-09-04 00:00:00
Short position: 17.5198237189
Short take profit at 2017-09-01 01:05:00, 17.516
------ Month 10 ------
Current Price: 16.593 at 2017-10-02 00:00:00
Short position: 16.5223874529
Short take profit at 2017-10-06 09:35:00, 16.485

[-25.069976804331517, 13.695039368216655, 53.36200224959882, 31.699859740758285, 35.23000642923151, 25.140727250849082, 53.0739827436868, 4.67823886720744, 31.61762811057436, 7.0612547128860115]
Total profit: 230.48876266867745
Final account: 2730.4887626686777
% Growth: 9.21955050674711

Final Words

We hope that this was as interesting for you to read as it was to experiment for me. In the end, the experiment gave a profit of 9.22% for 10 months of prediction. But more surprisingly 9 out 10 correct predictions! If you have any question please comment below!

Disclaimer

There are potential risks relating to investing and trading. You must be aware of such risks and familiarize yourself in regard to such risks and seek independent advice relating thereto. You should not trade with money that you cannot afford to lose. This website and its content should not be construed as a solicitation to invest and/or trade. You should seek independent advice in this regard. Past performance is not indicative of future performance. No representation is being made that any results discussed within the service and its related media content will be achieved. All opinions, news, research, analyses, prices or other information is provided as general market commentary and not as investment advice. This website and its creators do not warrant the completeness, accuracy or timeliness of the information supplied, and they shall not be liable for any loss or damages, consequential or otherwise, which may arise from the use or reliance of this website and its content.

Comments

Popular Posts