banner



macd trading strategy in r

[This clause was premier published on R programming, and kindly contributed to R-bloggers]. (You can report issue about the content on this foliate hither)


Want to share your content happening R-bloggers? click present if you have a blog, or hither if you wear't.

An Example of a Trading Strategy Coded in R

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

  • Getting the humanistic discipline information
  • Formulate the trading strategy and specify the rules
  • Execute the scheme on the historical data
  • Assess performance prosody

Therein post, we will back-test our trading strategy in R. The quantmod bundle has made it really easy to pull past data from Yahoo Finance. The one business line code at a lower place fetches NSE ( Nifty) data.

getSymbols("^NSEI")

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

chartSeries(NSEI, TA=NULL)

NSE-1

Ta="Null" indicates non to use any technical indicator. We will meet shortly application of a technical indicator on a graph. Next step is to pick a trading strategy. We will take MACD (Moving Average Convergence Difference) for this example. In a moving average crossovers strategy two averages are computed, a slow ahorse average and a fast moving average. The difference between the blistering moving middling and slow moving average is named MACD dividing line. dannbsp;A third average called signal line; a 9 day exponential moving average of MACD signal, is too computed. If the MACD phone line crosses above the signal line then it is a bullish sign of the zodiac and we go longitudinal. If the MACD line crosses beneath the signal line then it is a bearish sign and we go short. We choose closing Price of NSE data to count on the averages. Following command fulfills this task.

data=NSEI[,4]

The bid below computes the MACD for the closing price.

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

One can choose varied parameters for fastened, 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 blown average convergence disagreement, information is the shutdown price for NSE, nFast is the fast moving average, nSlow is the slow moving medium, maType =SMA indicates we have chosen simple moving average, pct =FALSE implies we are calculating the difference between fast moving average and slow flying average. Setting it TRUE would return the share difference between the fast moving average and slow road average.

The following command plots the graph 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 betoken crossed above the bespeak lineage we offer long on NSE
  • If the MACD signal crossed below the signal cable we go short on NSE

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

sign danlt;- Lag(ifelse(macd$macd danlt; macd$indicate, -1, 1))

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

returns danlt;- 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 betwixt 2008-06-02 and 2022-09-22.

Cumulative returns can beryllium calculated and aforethought using the following commands:-

portfolio danlt;- exp(cumsum(returns))  plot(portfolio)

NSE-3

The 4Th stride of back-examination is evaluating performance metrics. The performance analytics package in R provides a consolidated platform to remark execution consanguineous parameters. Various metrics like draw-downs, downside hazard can cost observed in R.

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

table.Drawdowns(ret, top=10)  table.DownsideRisk(ret)  charts.PerformanceSummary(ret)

NSE-4

Here is the compact reading of the code.

expect(quantmod)  involve(PerformanceAnalytics)  getSymbols("^NSEI")  chartSeries(NSEI, TA=NULL)  data=NSEI[,4]  macd = MACD(data, nFast=12, nSlow=26,nSig=9,maType=SMA,percent = Trumped-up)  chartSeries(data, Tantalum="addMACD()")  signal danlt;- Meanwhile(ifelse(macd$macd danlt; macd$signal, -1, 1))  returns danlt;- ROC(data)*signal  returns danlt;- returns['2008-06-02/2015-09-22']  portfolio danlt;- exp(cumsum(returns))  plot(portfolio)  table.Drawdowns(ret, big top=10)  postpone.DownsideRisk(ret)  charts.PerformanceSummary(ret)

Next Stride

After going though this example, you've lettered basics of how to design a quant trading strategy using R. At once you can start learning about how to get going with quantmod software in R. Once you've successfully learned these basics you tail end exam your skills at our interactive individual-paced 10 hours long datacamp course 'Model a Quantitative Trading Strategy in R'

The post An example of a trading strategy coded in R appeared first on .

macd trading strategy in r

Source: https://www.r-bloggers.com/2015/10/an-example-of-a-trading-strategy-coded-in-r/

Posted by: sokolowskisairenecons97.blogspot.com

0 Response to "macd trading strategy in r"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel