[R-SIG-Finance] Fw: Custom Indicator Problem

Atakan Okan atakanokan at outlook.com
Sat Feb 25 18:57:25 CET 2017


Thanks for the suggestion for a more concise version of the indicator Brian, however when I run the backtest it gives an error like the following:

Error in if (inherits(sret$indicators, "xts") & nrow(mktdata) == nrow(sret$indicators)) { : 
  argument is of length zero
In addition: Warning message:
In to.period(x, "days", name = name, ...) :
  missing values removed from data




From: Atakan Okan <atakanokan at outlook.com>
Sent: Saturday, February 25, 2017 5:22 PM
To: John Kumar
Subject: Re: [R-SIG-Finance] Custom Indicator Problem
  


Thanks Mr. Kumar,

Fixed it with a simple:

if (anyNA(y[,1])){} and works fine now.

Best regards,

Atakan Okan
  

From: John Kumar <rjkft at yahoo.com>
Sent: Saturday, February 25, 2017 1:50:27 PM
To: 'Atakan Okan'
Subject: RE: [R-SIG-Finance] Custom Indicator Problem
    
The problem is with this line:

y <- y[-which(is.na(y[,1])),]

It appears to attempt to remove any rows that have an NA in the open column
(i.e no price data). However, with the data used in your example this is
always FALSE so the code returns an integer of 0, which ends up assigning y
to nothing and the rest of the operations in the function are on nothing. If
you comment out this line the example runs and your indicator is added, but
you lose the functionality. You'll have to rewrite this line to correctly
accommodate the case in which all entries have data if you want to keep the
functionality. Cheers

John

-----Original Message-----
From: R-SIG-Finance [mailto:r-sig-finance-bounces at r-project.org] On Behalf
Of Atakan Okan
Sent: Saturday, 25 February 2017 10:34 AM
To: r-sig-finance at r-project.org
Subject: Re: [R-SIG-Finance] Custom Indicator Problem

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



     
_______________________________________________
R-SIG-Finance at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only. If you want to post, subscribe first.
-- Also note that this is not the r-help list where general R questions
should go.

    


More information about the R-SIG-Finance mailing list