[R-SIG-Finance] ROC

Stephen Choularton stephen at organicfoodmarkets.com.au
Tue Dec 21 03:42:17 CET 2010


Hi

I am having a bit of a problem with ROC.  I am doing some quick 
backtesting and at one point I use this:

ret <- ROC(price)
ret[1] <- 0

having established the spread between two stocks earlier like this:

price <- spread$Close - spread$Close.1

when I have been working before this has always been positive as the 
first share has always been greater in value than the second.  But just 
now I have done this with two stocks which are approximately the same 
value and whose values have crossed each other and I get a lot of NAN's

I have put the info into a table below

                        Close    Mean Upper.Boundary Lower.Boundary 
Signal  Daily Return
2007-01-12 -3.87 -3.9565  -3.6844106184    -4.22858938      0           NaN
2007-01-15 -3.95 -3.9595  -3.6854677314    -4.23353227      0           NaN
2007-01-16 -3.99 -3.9810  -3.6598900828    -4.30210992      0           NaN
2007-01-17 -3.87 -3.9790  -3.6599949679    -4.29800503      0           NaN
2007-01-18 -4.03 -3.9695  -3.6794927406    -4.25950726      0           NaN
2007-01-19 -4.02 -3.9600  -3.6900292413    -4.22997076      0           NaN.
.
.
.
.
2007-05-15 -2.20 -3.0000  -2.1068211707    -3.89317883      0           NaN
2007-05-16 -1.97 -2.9755  -2.0843156904    -3.86668431      0           NaN
2007-05-17 -2.87 -2.9375  -2.0836326369    -3.79136736      0           NaN
2007-05-18 -2.87 -2.9045  -2.0836538384    -3.72534616     -1           NaN
2007-05-21 -2.96 -2.8900  -2.0855125856    -3.69448741     -1           NaN
2007-05-22 -2.92 -2.8785  -2.0941725979    -3.66282740      0           NaN
2007-05-23 -3.22 -2.8650  -2.1195577081    -3.61044229      0           NaN
2007-05-24 -2.98 -2.8675  -2.1119764799    -3.62302352      0           NaN
2007-05-25 -2.97 -2.8785  -2.0990190661    -3.65798093      0           NaN
.
.
.
.
.2007-10-25 -1.46 -0.9005   1.5666694841    -3.36766948      0           NaN
2007-10-26 -0.60 -0.7765   1.7468501123    -3.29985011      0           NaN
2007-10-29 -0.07 -0.6650   1.8963442195    -3.22634422      0           NaN
2007-10-30  0.26 -0.5735   1.9383688786    -3.08536888      0           NaN
2007-10-31  0.44 -0.4585   1.9300540660    -2.84705407      0  5.260931e-01
2007-11-01  0.64 -0.3545   1.9303284515    -2.63932845      0  3.746934e-01
2007-11-02  0.50 -0.2285   1.8929421609    -2.34994216      0 -2.468601e-01
2007-11-05  0.36 -0.1080   1.7407959097    -1.95679591      0 -3.285041e-01
2007-11-06  0.51 -0.0230   1.5140799383    -1.56007994      0  3.483067e-01
2007-11-07 -0.02  0.0725   1.1553592755    -1.01035928      0           NaN
2007-11-08  0.49  0.1305   0.9630856997    -0.70208570      0           NaN
2007-11-09  0.67  0.1380   0.9451939700    -0.66919397      0  3.128723e-01
2007-11-12  0.60  0.1230   0.9571677226    -0.71116772      0 -1.103481e-01
2007-11-13 -0.03  0.1190   0.9511664813    -0.71316648      0           NaN
2007-11-14  0.01  0.1025   0.9208391396    -0.71583914      0           NaN
2007-11-15  0.08  0.0680   0.8482185929    -0.71221859      0  2.079442e+00
2007-11-16  0.29  0.0515   0.8068117168    -0.70381172      0  1.287854e+00
2007-11-19 -0.03  0.0510   0.8054646063    -0.70346461      0           NaN
2007-11-20 -0.69  0.0420   0.7774654810    -0.69346548      0           NaN
2007-11-21 -0.50  0.0750   0.8565369473    -0.70653695      0           NaN
2007-11-22 -0.30  0.0595   0.8183488512    -0.69934885      0           NaN
2007-11-23 -0.45  0.0485   0.7758101127    -0.67881011      0           NaN
2007-11-26 -0.37  0.0515   0.7888144726    -0.68581447      0           NaN
2007-11-27  0.18  0.1115   1.0005746224    -0.77757462      0           NaN
2007-11-28  0.11  0.1865   1.2710959713    -0.89809597      0 -4.924765e-01
2007-11-29 -0.05  0.2605   1.5047114391    -0.98371144      0           NaN
2007-11-30  0.17  0.3160   1.6607191059    -1.02871911      0           NaN
2007-12-03  0.35  0.3850   1.7950018664    -1.02500187      0  7.221347e-01
2007-12-04  0.33  0.4975   1.9054006394    -0.91040064      0 -5.884050e-02
2007-12-05  0.64  0.5695   1.9081949655    -0.76919497      0  6.623755e-01
2007-12-06  0.18  0.6070   1.8837244273    -0.66972443      0 -1.268511e+00
etc

As you can see when the spread goes negative ROC returns 0.

Is there some way of getting it to produce the right result?

It should work out if there is as later on I go to do this:

# Calculate equity curves
eq_up <- cumprod(1+ret*sigup)
eq_dn <- cumprod(1+ret*sigdn*-1)
eq_all <- cumprod(1+ret*sig)

so that should work appropriately on negative and positive returns 
(shouldn't it)


-- 


Stephen Choularton Ph.D., FIoD



More information about the R-SIG-Finance mailing list