01/05/2017

Technical Analysis in R (addMACD)

*Due to html problem, some codes are broken,
 so download a complete code in my google drive please
Click and download the code


required packages : quantmod / PerformanceAnalytics


1) collect data, S&P 500 index
      snp <- auto.assign="FALSE)</p" getsymbols="" src="yahoo">
2) take only adjust price
      adjustprice_snp<-snp -snp="" p="">
3) Draw MACD chart
      chartSeries(adjustprice_snp, TA="addMACD()")




4) adjust variable in macd analysis
      macd =
 MACD(adjustprice_snp, nFast=15, nSlow=30,nSig=9,maType=SMA, percent = FALSE)


5) create trading signal
    signal = Lag(ifelse(macd$macd>=macd$signal, 1,0))

-get rid of lookahead bias by lagging
-trade based on signal

6) calculate return
     ret = ROC(adjustprice_snp)*signal

7) set time
    ret = ret['2009-01-01/2017-01-10']

8) calculate cumulative return
    portCumRet = exp(cumsum(ret))
    plot(portCumRet)

9) evaluate risk
   #check worst 10 period and downside risk
     table.Drawdowns(ret,top=10)
     table.DownsideRisk(ret)
     
     charts.PerformanceSummary(ret)



#other functions for technical analysis including MACD
addADX
add Welles Wilder's Directional Movement Indicator*
addATR
add Average True Range *
addBBands:
add Bollinger Bands *
addCCI
add Commodity Channel Index *
addCMF
add Chaiken Money Flow *
addCMO
add Chande Momentum Oscillator *
addDEMA
add Double Exponential Moving Average *
addDPO
add Detrended Price Oscillator *
addEMA
add Exponential Moving Average *
addEnvelope
add Moving Average Envelope
addEVWMA
add Exponential Volume Weighted Moving Average *
addExpiry
add options or futures expiration lines
addLines
add line(s)
addMACD:
add Moving Average Convergence Divergence *
addMomentum
add Momentum *
addPoints
add point(s) 
addROC:
add Rate of Change *
addRSI
add Relative Strength Indicator *
addSAR
add Parabolic SAR *
addSMA
add Simple Moving Average *
addSMI
add Stochastic Momentum Index *
addTRIX
add Triple Smoothed Exponential Oscillator *
addVo:
add Volume if available
addWMA
add Weighted Moving Average *
addWPR
add Williams Percent R *
addZLEMA


-reference-
http://blog.naver.com/htk1019/220908871667


Share this

Tag :

0 Comment to "Technical Analysis in R (addMACD)"

Post a Comment