This blog covers in brief the concept of strategy back-testing using R. Before dwelling into the trading jargons using R let us spend some time understanding what R is. R is an open source. There are more than 4000 add-on packages,18000 plus members of LinkedIn’s group and close to 80 R Meetup groups currently in existence. It is a perfect tool, for statistical analysis, especially for data analysis. The concise setup of Comprehensive R Archive Network knows as CRAN provides a list of packages along with the base installation required. There are a lot of packages available depending upon the analysis needs to be done. To implement the trading strategy, we will use the package called quantstrat.
Four Step Process of Any Basic Trading Strategy
- Hypothesis formation
We plot the Bollinger band for the closing price.
We set a threshold level to compare the fluctuations in the price. If the price increases/decreases, we update the threshold column. The closing price is compared with the upper band and with the lower band. When the upper band is crossed, it is a signal for sell. Similarly, when the lower band is crossed, it is a buy signal. The coding section can be summarized as follows:-
- Adding indicators
- Adding signals
- Adding rules
Thus our hypothesis that market is mean reverting is supported. Since this is back-testing we have room for refining the trading parameters that would improve our average returns and the profits realized. This can be done by setting different threshold levels, more strict entry rules, stop loss etc. One could choose more data for back-testing, use Bayseian approach for the threshold set up, take volatility into account.
Once you are confident about the trading strategy backed by the back-testing results you could step into live trading. The Production environment is a big topic in itself and it’s out of scope in the article’s context. To explain in brief this would involve writing the strategy on a trading platform.