Learning Quantitative Finance with R by Dr. Param Jeet & Prashant Vats

Learning Quantitative Finance with R by Dr. Param Jeet & Prashant Vats

Author:Dr. Param Jeet & Prashant Vats [Jeet, Dr. Param]
Language: eng
Format: azw3
Publisher: Packt Publishing
Published: 2017-03-23T04:00:00+00:00


Figure 5.4: Normalized price series, difference, and trading signals

The upper (ub) and lower bounds (lb) of the difference series can be calculated by adding and subtracting n times standard deviation from the mean:

>ub<- me + n * std >lb<- me - n*std

Finding optimal parameter values for n is not straightforward. We either have to use the hit and trial method to come up with the optimal value of n or use a grid-based parameter optimizer to find the optimal value.

As an example, here I used n = 1 just for the sake of demonstration. A buy signal (1) is generated when the difference value is lower than the lower band, and a short signal (-1) is generated when the difference is above the upper band; otherwise the signal is hold (0).

When the difference value, that is, the spread, is above the upper band, we speculate it will return to its mean value as, historically, it has been there most of the time. Similarly, when the spread is below the lower band, even in this case, we speculate it will return to its mean value:

> n <- 1 > signal <- ifelse(diff > ub,1,ifelse(diff < lb,-1,0))

Here I used the full time series of difference values to calculate mean and standard deviation, as can be seen previously where I calculated me and std. You can also calculate mean and standard deviation dynamically on a rolling window. This dynamic mean and standard deviation will change signal generation, and entry and exit will be even more frequent.

Dynamic mean and standard deviations can be calculated using rollapply(). You should define dataset, length, and function in rollapply():

>me_dynamic<- rollapply(diff,10,mean) >std_dynamic<- rollapply(diff,10,sd)

The plot() function plots the signal as presented in Figure 5.4. A non-zero signal value shows our participation in the market and zero means we are not participating in the market:

>plot(signal, type="l")

Difference in normalized prices is also called spread. As we are generating signals using spread, we will be trading spread instead of individual stock. So, we have to clearly understand what is meant by spread trading. When I say buy, it means I am buying spread, which implies long position on XOM and short position on CVX or short on XOM and long on CVX when we have short signal. The following two lines calculate the spread and trade return:

>spread_return<- ret_xom - ret_cvx >trade_return<- spread_return*lag(signal) - cost

The variable named spread_return is the return spread, trade_return is the return of trade, and cost is the expenses to carry out trading activities; it includes transaction cost, brokerage cost, and slippage.

The purpose of this book is only to teach you R coding, not to generate profitable trading ideas. So, I considered the cost as 0, but you have to incorporate the appropriate cost while backtesting your ideas and putting money into a real account.

Now we apply performance measure commands to extract a performance summary:

> summary(trade_return) Min. 1st Qu. Median Mean 3rd Qu. Max. -0.0330000 0.0000000 0.0000000 0.0002135 0.0000000 0.0373400

All key performance indicators can be calculated using the following commands. All these commands have already been used in the Momentum trading section:

>cumm_ret<- Return.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.