[R-SIG-Finance] Custom Indicator Problem
Atakan Okan
atakanokan at outlook.com
Sat Feb 25 10:33:43 CET 2017
Just remembered that Outlook sent emails in HTML format, so here is the problem in plain text format in case you couldn't see it before. Still couldn't find a way to make this work.
From: Atakan Okan <atakanokan at outlook.com>
Sent: Wednesday, February 15, 2017 11:54 AM
To: r-sig-finance at r-project.org
Subject: Custom Indicator Problem
Hi everyone,
I am facing a problem regarding a higher-time-frame custom indicator. I have daily yahoo finance data but would like to use the technical indicator MACD on weekly close data. I have written a custom function as I have seen in a previous post some years ago but couldn't manage to add the weekly indicator output to mktdata. Any help is appreciated, thank you
Reproducible example:
#Custom Indicator Problem
library(quantmod)
library(quantstrat)
library(TTR)
Sys.setenv(TZ = "UTC")
.strategy <- new.env()
.blotter <- new.env()
getSymbols("GARAN.IS")
#Stock
symbol.name = "GARAN.IS"
tick.size = 0.01
currency('TRY')
stock(symbol.name, currency="TRY", multiplier=1,tick_size= tick.size)
initialEquity = 1000
port.acct.currency <- "TRY"
strategy.st <- 'Custom_Prob'
rm.strat(strategy.st)
initDate = as.character(as.Date(index(GARAN.IS[1])-1))
initPortf(strategy.st, symbol.name, initDate=initDate, currency = port.acct.currency)
initAcct(strategy.st, portfolios=strategy.st, initDate=initDate,
initEq=initialEquity, currency = port.acct.currency)
initOrders(portfolio=strategy.st,initDate=initDate)
strategy(strategy.st,store=TRUE)
summary(getStrategy(strategy.st))
#MACD W1 indicator
MACD_W1 <- function(mktdata=quote(mktdata),
nFast = 12,
nSlow = 26,
nSig = 9){
y <- GARAN.IS
is.xts(y)
y <- to.weekly(y)
y <- Cl(y)
y <- MACD(y,
nFast = nFast,
nSlow = nSlow,
nSig = nSig,
maType = "EMA")
y <- cbind(mktdata, y[paste(first(index(mktdata)),
last(index(mktdata)),
sep = "/")])
y <- y[-which(is.na(y[,1])),]
y <- na.locf(y)
y <- y[,c((ncol(y)-1),ncol(y))]
y
}
add.indicator(strategy.st,
name = "MACD",
arguments = list(x=Cl(GARAN.IS)),
label='macd')
add.indicator(strategy.st,
name = "MACD_W1",
arguments = list(mktdata=quote(mktdata)))
apply.indicators.df <- applyIndicators(strategy.st, mktdata=GARAN.IS) #testing indicator calculations
-Atakan Okan
More information about the R-SIG-Finance
mailing list