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
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.
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(7850)
Learning SQL by Alan Beaulieu(5408)
Weapons of Math Destruction by Cathy O'Neil(5034)
Big Data Analysis with Python by Ivan Marin(2998)
Blockchain Basics by Daniel Drescher(2889)
Hands-On Machine Learning for Algorithmic Trading by Stefan Jansen(2512)
Pandas Cookbook by Theodore Petrou(2499)
Mastering Python for Finance by Unknown(2472)
Building Statistical Models in Python by Huy Hoang Nguyen & Paul N Adams & Stuart J Miller(2453)
Azure Data and AI Architect Handbook by Olivier Mertens & Breght Van Baelen(2420)
Serverless Machine Learning with Amazon Redshift ML by Debu Panda & Phil Bates & Bhanu Pittampally & Sumeet Joshi(2353)
How The Mind Works by Steven Pinker(2211)
Data Wrangling on AWS by Navnit Shukla | Sankar M | Sam Palani(2137)
Building Machine Learning Systems with Python by Richert Willi Coelho Luis Pedro(2057)
Data Engineering with dbt by Roberto Zagni(2041)
Driving Data Quality with Data Contracts by Andrew Jones(2007)
Network Science with Python and NetworkX Quick Start Guide by Edward L. Platt(1969)
Python Natural Language Processing by Jalaj Thanaki(1891)
Machine Learning Model Serving Patterns and Best Practices by Md Johirul Islam(1803)