Introduction

The Following is a Project to improve profits when Mean Reversion is applied in Markets.

Use In Finance

The study of mean reversion in asset returns has received considerable attention. Of the two theories generally offered to explain mean reversion, one claims that equity risk premiums are time varying rather than constant. In that case, mean reversion in asset returns reflects a rational response by investors. The alternate theory concerning mean reversion is that investors have overreacted irrationally to prior events. There is a large and growing literature that investigates evidence for mean reversion in stock prices. Though empirically, there is no consensus as to whether stock prices are mean reverting or random walk processes at best, the results are mixed. Finance professional usage mean reversion particularly in analyzing the relative movement of one stock in relation to another stock.

Correlation in Data sets

It must be well emphasized that the datasets (Stocks in our case) should be correlated with a Mathematical Correlation of more than 0.8 . This is kept such so that their relative indices (their ratio in our case) should regress towards a mean value.


Algorithm For Trading Based on Mean Reversion

Mean and standard devation are rolling

1.Take the ratio of the **paired correlated** stocks over a period of time(say stock1/stock2).
2.Take thier Rolling mean and Rolling Standard Deviation of ratio the past 20 days.
3.If the ratio passes above **mean+2sigma** value then a signal is generated ( * suggesting that drifted to a point from where mean reversion would take place *) . **Short stock 1 Buy stock 2**.
4.If the ratio passes below **mean-2sigma** value then a signal is generated(*similar to step 3*).**Buy stock 1 Short stock 2**.
5.If The ratio passes above or below **mean**.**Both the trades are squared off**.

Synopsis

The analysis of Mean Reversion Techniques Reveals that it has a quantifiable profit . The overall subject of profitability depends on the number of datasets on which mean reversion is considered.
This study provides new evidence of mean reversion of stock market returns. It shows that applying mean reversion to stock prices leads to a significant profit. This study examines daily returns of stock prices of various commercial banks traded on National Stock Exchange, India during 16 June 2008 to 14 July 2013 period.
The study reveals an economically and statistically significant negative relationship between the profit and duration of the trade. Study also suggests using concept of ‘time-stops’ to improve profit profile. Findings are also robust across various sub time periods and securities.


Getting Data

The data for stocks have been taken NSE India . Similar data points could be GOOGLE Finance and Yahoo Finance .

library(RcppRoll)
## Loading required package: Rcpp
## Loading required package: RcppArmadillo
file<-function(directory,id){
  read.csv(paste(directory, "/", sprintf("%03d", id),".csv", sep=''))
}

The following data is obtained is made ambigous for reasons of proper unbiased analysis. Stock 1 and Stock 2 are the two stocks and the ratio is carried out between Stock 1 and 2.


Generating Rolling Mean and Standard Deviation and Creating Signal

##          DATE STOCK1 STOCK2     RATIO RATIO_MEAN   RATIO_SD SWITCH
## 1   11/7/2008  53.90  50.50 1.0673267  1.1177454 0.05700270      0
## 2  11/10/2008  54.95  51.90 1.0587669  1.1141022 0.05838035      0
## 3  11/11/2008  55.45  51.10 1.0851272  1.1101626 0.05749349      0
## 4  11/12/2008  53.85  51.00 1.0558824  1.1038402 0.05607475      0
## 5  11/14/2008  53.60  51.50 1.0407767  1.0961337 0.05342935      0
## 6  11/17/2008  51.95  50.90 1.0206287  1.0865769 0.04852728      0
## 7  11/18/2008  49.80  48.80 1.0204918  1.0806570 0.04902891      0
## 8  11/19/2008  46.90  47.95 0.9781022  1.0769085 0.05387530      0
## 9  11/20/2008  45.45  47.45 0.9578504  1.0729319 0.05957955      0
## 10 11/21/2008  47.00  48.65 0.9660843  1.0701471 0.06328280      0
## 11 11/24/2008  46.85  49.90 0.9388778  1.0651848 0.06951076      0
## 12 11/25/2008  46.90  52.10 0.9001919  1.0543981 0.07750185      0
## 13 11/26/2008  48.10  51.80 0.9285714  1.0423961 0.07747011      0
## 14 11/28/2008  46.25  55.00 0.8409091  1.0282140 0.08701353      1
## 15  12/1/2008  47.10  55.50 0.8486486  1.0115667 0.08796860      1
## 16  12/2/2008  47.25  55.90 0.8452594  0.9963326 0.08911854      1
## 17  12/3/2008  47.40  57.90 0.8186528  0.9815293 0.09292676      1
## 18  12/4/2008  47.00  58.80 0.7993197  0.9663370 0.09675277      1
## 19  12/5/2008  46.95  57.90 0.8108808  0.9526852 0.09853539      1
## 20  12/8/2008  46.35  56.30 0.8232682  0.9402809 0.09842548      1
## 21 12/10/2008  46.95  56.10 0.8368984  0.9287594 0.09623336      1
## 22 12/11/2008  47.35  58.70 0.8066440  0.9161533 0.09480959      1
## 23 12/12/2008  47.55  58.00 0.8198276  0.9028883 0.08825667      1
## 24 12/15/2008  48.70  58.80 0.8282313  0.8915058 0.08194055      1
## 25 12/16/2008  49.85  58.35 0.8543273  0.8821833 0.07431549      1
## 26 12/17/2008  47.85  56.85 0.8416887  0.8732363 0.06720149      1
## 27 12/18/2008  49.60  58.25 0.8515021  0.8647868 0.05765829      1
## 28 12/19/2008  50.05  58.00 0.8629310  0.8590282 0.05112674      0
## 29 12/22/2008  50.30  57.90 0.8687392  0.8545727 0.04565107      0
## 30 12/23/2008  50.00  56.55 0.8841733  0.8504771 0.03818395      0
## 31 12/24/2008  49.00  56.00 0.8750000  0.8472833 0.03267460      0
## 32 12/26/2008  50.00  55.60 0.8992806  0.8472377 0.03259747      0
## 33 12/29/2008  50.05  54.95 0.9108280  0.8463505 0.03043727      1
## 34 12/30/2008  50.55  55.15 0.9165911  0.8501346 0.03419746      1
## 35 12/31/2008  52.05  55.10 0.9446461  0.8549345 0.04018988      1
## 36   1/1/2009  53.05  57.90 0.9162349  0.8584833 0.04236530      1
## 37   1/2/2009  54.65  59.40 0.9200337  0.8635523 0.04340122      1
## 38   1/5/2009  56.00  62.20 0.9003215  0.8686024 0.04136216      1
## 39   1/6/2009  55.45  60.10 0.9226290  0.8741898 0.04069685      1
## 40   1/7/2009  52.15  56.50 0.9230088  0.8791768 0.04023701      1
## 41   1/9/2009  52.30  54.95 0.9517743  0.8849206 0.04204285      1
## 42  1/12/2009  52.05  55.70 0.9344704  0.8913119 0.03913229      1
## 43  1/13/2009  51.70  55.50 0.9315315  0.8968971 0.03625865      1
## 44  1/14/2009  52.15  55.80 0.9345878  0.9022150 0.03333964      1
## 45  1/15/2009  50.65  55.00 0.9209091  0.9055441 0.03158421      1
## 46  1/16/2009  51.85  55.85 0.9283796  0.9098786 0.02811805      1
## 47  1/19/2009  54.30  57.30 0.9476440  0.9146857 0.02572948      1
## 48  1/20/2009  53.65  57.60 0.9314236  0.9181103 0.02287859      1
## 49  1/21/2009  54.05  56.60 0.9549470  0.9224207 0.02114239      1
## 50  1/22/2009  52.60  55.40 0.9494585  0.9256850 0.01993157      1
## 51  1/23/2009  50.10  54.40 0.9209559  0.9279828 0.01605235      0

The following is a small fragment of over a 4 year long datasheet which is derived from the provided Algorithm . The signal being generated as 1 when the ratio hits either of the two rolling extremum and 0 when the ratio leaps over mean .


RESULTS

Trade Summary

##        DATE_O STOCK1_O STOCK2_O   RATIO_O     DATE_C STOCK1_C STOCK2_C
## 1    9/8/2008    65.40    61.55 1.0625508  9/23/2008    64.10    58.95
## 2   9/26/2008    64.50    57.05 1.1305872 10/20/2008    56.55    53.70
## 3  10/21/2008    55.50    53.50 1.0373832 10/27/2008    45.05    38.55
## 4  11/28/2008    46.25    55.00 0.8409091 12/19/2008    50.05    58.00
## 5  12/29/2008    50.05    54.95 0.9108280  1/23/2009    50.10    54.40
## 6   1/29/2009    49.50    55.05 0.8991826  2/16/2009    50.00    53.95
## 7   3/19/2009    37.70    42.45 0.8881037   4/2/2009    43.85    48.40
## 8    5/4/2009    56.95    56.55 1.0070734   5/8/2009    55.35    62.10
## 9    7/3/2009    79.85    80.35 0.9937772   7/7/2009    73.85    80.00
## 10  8/20/2009    90.60    89.10 1.0168350  10/1/2009   114.05   104.80
## 11 10/29/2009   117.65   112.85 1.0425343  11/5/2009   123.10   111.70
## 12 12/30/2009   127.20   104.00 1.2230769  2/15/2010   130.10   100.35
## 13   3/4/2010   140.40   102.05 1.3757962  3/30/2010   144.60   105.45
## 14  3/31/2010   142.50   108.10 1.3182239   4/7/2010   167.65   119.05
## 15  4/30/2010   164.65   132.30 1.2445200  5/21/2010   156.10   126.60
## 16  6/30/2010   162.20   130.05 1.2472126  7/12/2010   164.75   136.35
## 17  7/23/2010   185.15   145.55 1.2720715  8/18/2010   214.70   164.95
## 18  10/8/2010   237.25   163.90 1.4475290 10/13/2010   242.85   172.30
## 19  11/8/2010   264.70   180.45 1.4668883  12/7/2010   219.30   150.10
## 20  12/8/2010   203.95   147.00 1.3874150 12/14/2010   224.10   152.10
## 21   1/3/2011   230.15   150.00 1.5343333  1/28/2011   201.80   137.85
## 22   3/7/2011   204.00   135.45 1.5060908   4/1/2011   225.85   151.25
## 23  4/25/2011   224.05   145.05 1.5446398  4/29/2011   204.90   139.00
## 24   5/2/2011   198.40   135.50 1.4642066   6/9/2011   201.35   144.90
## 25  6/15/2011   197.65   138.45 1.4275912  7/28/2011   201.55   136.90
## 26  8/25/2011   173.05   123.10 1.4057677 10/24/2011   146.85   114.45
## 27  11/2/2011   161.05   118.30 1.3613694 12/19/2011   131.40    84.85
## 28 12/22/2011   124.70    86.25 1.4457971   1/5/2012   129.05    84.55
## 29   2/1/2012   163.85   101.95 1.6071604  2/14/2012   190.95   128.35
## 30  2/27/2012   173.75   118.15 1.4705882  2/28/2012   189.00   120.90
## 31  3/27/2012   189.10   119.45 1.5830892  4/13/2012   185.30   122.50
## 32  4/26/2012   164.90   115.35 1.4295622   5/4/2012   169.95   114.05
## 33   5/7/2012   162.45   118.30 1.3732037  6/20/2012   143.40   115.50
## 34  6/27/2012   148.60   115.15 1.2904907  7/13/2012   142.90   114.95
## 35  7/25/2012   137.60   106.10 1.2968897  8/17/2012   127.40    98.00
## 36  9/14/2012   124.90    92.80 1.3459052  9/24/2012   143.40   111.65
## 37 10/10/2012   136.90   107.40 1.2746741 10/31/2012   136.05   103.45
## 38  11/7/2012   142.45   106.50 1.3375587 11/21/2012   133.00   101.80
## 39 12/18/2012   168.00   117.45 1.4303959  1/10/2013   178.90   126.55
## 40  1/15/2013   186.75   125.00 1.4940000  1/28/2013   171.05   116.60
## 41  2/26/2013   143.10    94.80 1.5094937   3/7/2013   142.95    99.15
## 42  3/21/2013   123.95    92.85 1.3349488  4/11/2013   124.05    90.65
## 43  4/15/2013   129.45    91.15 1.4201865   5/7/2013   126.50    90.90
## 44  6/10/2013   113.60    86.90 1.3072497       <NA>       NA       NA
##      RATIO_C DATE_DIFF    TYPE TRADE_PROFIT
## 1  1.0873622   15 days -2Sigma    2.2364404
## 2  1.0530726   24 days +2Sigma    6.4535393
## 3  1.1686122    6 days -2Sigma    9.1150964
## 4  0.8629310   21 days -2Sigma    2.7616708
## 5  0.9209559   24 days +2Sigma   -1.1008100
## 6  0.9267841   18 days -2Sigma    3.0082845
## 7  0.9059917   14 days -2Sigma    2.2965074
## 8  0.8913043    4 days +2Sigma   12.6238056
## 9  0.9231250    4 days +2Sigma    7.0784946
## 10 1.0882634   42 days +2Sigma   -8.2623513
## 11 1.1020591    7 days -2Sigma    5.6514360
## 12 1.2964624   46 days +2Sigma   -5.7894896
## 13 1.3712660   26 days +2Sigma    0.3402472
## 14 1.4082318    7 days -2Sigma    7.5196131
## 15 1.2330174   21 days -2Sigma   -0.8844433
## 16 1.2082875   12 days +2Sigma    3.2721575
## 17 1.3016065   26 days +2Sigma   -2.6312794
## 18 1.4094602    5 days +2Sigma    2.7646969
## 19 1.4610260   29 days +2Sigma    0.3324288
## 20 1.4733728    6 days -2Sigma    6.4104848
## 21 1.4639100   25 days +2Sigma    4.2180534
## 22 1.4932231   25 days +2Sigma    0.9540367
## 23 1.4741007    4 days +2Sigma    4.3762238
## 24 1.3895790   38 days -2Sigma   -5.4503742
## 25 1.4722425   43 days +2Sigma   -3.0927227
## 26 1.2830931   60 days -2Sigma   -8.1133254
## 27 1.5486152   47 days +2Sigma   -9.8651390
## 28 1.5263158   13 days -2Sigma    5.4593866
## 29 1.4877289   13 days +2Sigma    9.3555287
## 30 1.5632754    1 days -2Sigma    6.4494287
## 31 1.5126531   17 days +2Sigma    4.5628884
## 32 1.4901359    8 days -2Sigma    4.1894669
## 33 1.2415584   44 days -2Sigma   -9.3598212
## 34 1.2431492   16 days +2Sigma    3.6621143
## 35 1.3000000   23 days +2Sigma   -0.2215166
## 36 1.2843708   10 days +2Sigma    5.5006505
## 37 1.3151281   21 days -2Sigma    3.0569487
## 38 1.3064833   14 days +2Sigma    2.2207611
## 39 1.4136705   22 days +2Sigma    1.2598826
## 40 1.4669811   13 days +2Sigma    1.6869612
## 41 1.4417549   10 days +2Sigma    4.6934294
## 42 1.3684501   21 days -2Sigma    2.4500907
## 43 1.3916392   22 days +2Sigma    2.0045990
## 44        NA   NA days -2Sigma           NA

The following datasheet is the entire trade summary provided at the interval of every generated signal . All such signals are marked as with id’s for easier computation .Clearly the trade profit provided can be used the seek the credibility of the algorithm . The overall profit at the end of 4 years from this single datasheet is

## [1] 83.19408

So the average profit returned at the end of each year is about

## [1] 20.09519

Graphs

The following is the graph of values vs number of observations .
The lines here are (in order of legend):

  • Mean

  • Mean +2Sigma

  • Mean -2Sigma

  • Ratio lines

    In the following graph it can be easily drawn that the Ratio lines when crosses Extremum positions it tries to revert back to the mean proving the Mean Reversion formulation .

Profit vs Time duration graph

Another Important Aspect of The Mean Reversion Algorithm Created can be visualized by plotting a graph of Profit vs Time Duration (Number of Days for which a trade is active).

Clearly There is a downward trend between Profit and Duration of trade which can be modelled as :

## 
## Call:
## lm(formula = y$diff ~ y$trade_profit)
## 
## Coefficients:
##    (Intercept)  y$trade_profit  
##         24.800          -2.397

This happens because when finding the correlation between the two stocks , We are not finding the Running Correlation but rather a Static Correlation . The given are all such points where the running correlation between the two stocks have gone considerably below its average . In Other Words the two stocks are not in sink at these points of anomaly .

Halting Trade At 15 Days

When the following trend graph of Trade Duration vs Trade Profit was made for other Stocks as well , The same trend was seen . So One way to make a more safer trading would be to Square up all the trades if the duration of trade crosses 15 days .
Although The overall Profit Made by The Trade after putting a 15 Day Halting Constrarints is a bit less ::

## [1] 75.42325

But the average trade profit over a time duration is much higher than it was previously.


Conclusion

It’s clearly established here that applying mean reversion to stock prices leads to a significant profit. So, in spite of theoretical debate over efficient and non-efficient markets, there is ample scope for a finance practitioner to extract profit under risk neutral environment. The study also reveals an economically and statistically significant negative relationship between the profit and duration of the trade. And a robust concept of ‘time-stops’ can use this statistically significant relationship to improve profit profile. Improvements could be assigned to the facts that mean reversion experiences break-downs at certain time interval. Further studies are suggested to model these time intervals. Further studies is also suggested to use this ‘time stops’ concept while modeling error term (which is example of a perfect mean reversion) in linear regression as that may help identify break down of linear regression model in data analysis.