0

I am trying to use the applySignals function on my stock data and additional filter rule that I created since the TTR package does not provide a simple filter rule.

When I use the applyIndicators function I can see that an additional column appears with the filter (daily return) so I think this works. However when I add my long and short signal based on this filter and I use the applySignals function I get the following error:

test <- applySignals(strategy = strategy.st, mktdata = test_init) Error in dimnames<-.xts(*tmp*, value = dn) : length of 'dimnames' [2] not equal to array extent In addition: Warning message: In match.names(column, colnames(data)) : all columns not located in filter for OTRK.Open OTRK.High OTRK.Low OTRK.Close FILTER.filter longfilter

I used the same example and code from a professional who provided the code but it doesn't seem to work for me. (chapter 9.3 Simple filter rule: https://bookdown.org/kochiuyu/technical-analysis-with-r-second-edition/simple-filter-rule.html)

I am new to R so I don't really understand the error message. Thanks in advance!

This is my code as far as the error:



install.packages("quantstrat")
install.packages("TTR")

## Library
library("quantstrat")
library("TTR")

## Quantsrat not directly available: solution
install.packages("devtools")
require(devtools)
install_github("braverock/blotter") # dependency
install_github("braverock/quantstrat")

###############################################################################################################################
###############################################################################################################################

initdate <- "2003-12-15"
from <- "2018-12-15"
to <- "2021-04-07"

Sys.setenv(TZ = "UTC")
currency("USD")
getSymbols("OTRK", from = from, to = to, src= "yahoo", adjust = TRUE)
stock("OTRK", currency = "USD", multiplier=1)

tradesize <- 1000
initeq <- 1000

#txnfees <- -10
#orderqty <- 100
#nsamples <- 5


#Parameters indicators#

filterthreshold <- 0.05

#Initialise#
strategy.st <- portfolio.st <- account.st <- "FilterRule"
rm.strat(strategy.st)

initPortf(portfolio.st, symbols = "OTRK", initDate = initdate, currency = "USD")
initAcct(account.st, portfolios = portfolio.st, initDate = initdate, currency = "USD", initEq = initeq)
initOrders(portfolio.st, symbols = "OTRK", initDate = initdate)
strategy(strategy.st, store = TRUE)

#Adding indicators#

FILTER <- function(price) {
  lagprice <- lag(price,1)
  temp<- price/lagprice - 1
  colnames(temp) <- "FILTER"
  return(temp)
}


add.indicator(
  strategy=strategy.st,
  name = "FILTER", 
  arguments = list(price = quote(Cl(mktdata))), 
  label= "filter")




#Check test indicators work#
test_init <- applyIndicators(strategy = strategy.st, mktdata = OHLC(OTRK))
head(test_init, n=7)
tail(test_init, n=7)

#Adding signals#

add.signal(strategy.st, 
           name="sigThreshold",
           arguments = list(threshold= filterthreshold,   
                            column="filter",
                            relationship="gt",   
                            cross=TRUE),
           label="longfilter")

add.signal(strategy.st, 
           name="sigThreshold",
           arguments = list(threshold= - filterthreshold, 
                            column="filter",
                            relationship="lt",
                            cross=TRUE),
           label="shortfilter")




#Test signals on data
test <- applySignals(strategy = strategy.st, mktdata = test_init)
Peter
  • 1
  • 2
  • 1
    welcome to StackOverflow! Could you try to create a [Minimal Reproducible Example](https://stackoverflow.com/a/5963610/13513328)? Installing so many packages to understand your problem might discourage helpers – Waldi May 01 '21 at 17:18
  • I think for this code you only need the quantstrat package and possibly the TTR package ## Install packages install.packages("quantstrat") install.packages("TTR") ## Library library("quantstrat") library("TTR") ## Quantsrat not directly available: solution install.packages("devtools") require(devtools) install_github("braverock/blotter") # dependency install_github("braverock/quantstrat") – Peter May 01 '21 at 17:36
  • @Waldi I deleted the install packages which I think are not needed – Peter May 01 '21 at 17:43
  • I fixed the problem by editing the column name in add.signal to "FILTER.filter" instead of just "filter". For some reason, you have to use the full column name instead of just the label name you gave the indicator. Some standard functions don't always need this I think but this one did. – Peter May 05 '21 at 15:44

0 Answers0