An example of a trading strategy coded using Quantmod Package in R

40 篇文章 7 订阅

Back-testing of a trading strategy can be implemented in four stages.

  • Getting the historical data
  • Formulate the trading strategy and specify the rules
  • Execute the strategy on the historical data
  • Evaluate performance metrics

In this post, we will back-test our trading strategy in R. The quantmod package has made it really easy to pull historical data from Yahoo Finance. The one line code below fetches NSE ( Nifty) data.

getSymbols("^NSEI")

Quantmod provides various features to visualize data. The command below creates chart for the NSE data.

chartSeries(NSEI, TA=NULL)

NSE-1

TA=”Null” indicates not to use any technical indicator. We will see shortly application of a technical indicator on a chart. Next step is to pick a trading strategy. We will choose MACD (Moving Average Convergence Divergence) for this example. In a moving average crossovers strategy two averages are computed, a slow moving average and a fast moving average. The difference between the fast moving average and slow moving average is called MACD line.  A third average called signal line; a 9 day exponential moving average of MACD signal, is also computed. If the MACD line crosses above the signal line then it is a bullish sign and we go long. If the MACD line crosses below the signal line then it is a bearish sign and we go short. We choose closing price of NSE data to calculate the averages. Following command fulfills this task.

data=NSEI[,4]

The command below computes the MACD for the closing price.

macd = MACD(data, nFast=12, nSlow=26,nSig=9,maType=SMA, percent = FALSE)

One can choose varying parameters for fast, slow and signal averages depending upon the trading requirements. Here we stick to the standard parameters. MACD is the function in quantmod that calculates the moving average convergence divergence, data is the closing price for NSE, nFast is the fast moving average, nSlow is the slow moving average, maType =SMA indicates we have chosen simple moving average, percent =FALSE implies we are calculating the difference between fast moving average and slow moving average. Setting it TRUE would return the percentage difference between the fast moving average and slow moving average.

The following command plots the chart for the closing price of NSE along with the MACD parameters.

chartSeries(NSEI, TA="addMACD()")

NSE-2

As discussed before we define our trading signal as follows:-

  • If the MACD signal crossed above the signal line we go long on NSE
  • If the MACD signal crossed below the signal line we go short on NSE

Following command generates the trading signal accordingly. We use the lag operator to eliminate look ahead bias.

signal = Lag(ifelse(macd$macd < macd$signal, -1, 1))

We will apply this strategy on the historical data of NSE from 2007-09-17 to 2015-09-22. The trading signal is applied to the closing price to obtain the returns of our strategy.

returns = ROC(data)*signal

The ROC function provides the percentage difference between the two closing prices. We can choose the duration for which we want to see the returns. The following command chooses the returns between 2008-06-02 and 2015-09-22.

Cumulative returns can be calculated and plotted using the following commands:-

portfolio = exp(cumsum(returns))

plot(portfolio)

NSE-3

The 4th step of back-testing is evaluating performance metrics. The performance analytics package in R provides a consolidated platform to observe performance related parameters. Various metrics like draw-downs, downside risk can be observed in R.

Following command provides a summary of above mentioned parameters and much more!

table.Drawdowns(ret, top=10)

table.DownsideRisk(ret)

charts.PerformanceSummary(ret)

NSE-4

Here is the succinct version of the code.
require(quantmod)
require(PerformanceAnalytics)
getSymbols(‘^NSEI’)
chartSeries(NSEI, TA=NULL)
data=NSEI[,4]
macd = MACD(data, nFast=12, nSlow=26,nSig=9,maType=SMA,percent = FALSE)
chartSeries(data, TA=’addMACD()’)
signal = Lag(ifelse(macd$macd < macd$signal, -1, 1))
returns = ROC(data)*signal
returns = returns[‘2008-06-02/2015-09-22’]
portfolio = exp(cumsum(returns))
plot(portfolio)
table.Drawdowns(ret, top=10)
table.DownsideRisk(ret)
charts.PerformanceSummary(ret)

Next Step

After going though this example, you’ve learned basics of how to design a quant trading strategy using R. Now you can start learning about how to get started with quantmod package in R. Once you’ve successfully learned these basics you can test your skills at our interactive self-paced 10 hours long datacamp course ‘Model a Quantitative Trading Strategy in R

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值