The error I'm getting (error 3 is what I believe is the main crux of it):
> applyStrategy(strategy = strategy.st, portfolios = portfolio.st)
Error in `dimnames<-.xts`(`*tmp*`, value = dn) :
length of 'dimnames' [2] not equal to array extent
In addition: Warning messages:
1: In read.table(file = file, header = header, sep = sep, quote = quote, :
incomplete final line found by readTableHeader on 'https://query1.finance.yahoo.com/v7/finance/download/SPY?period1=-2208988800&period2=1679097600&interval=1d&events=split'
2: In read.table(file = file, header = header, sep = sep, quote = quote, :
incomplete final line found by readTableHeader on 'https://query2.finance.yahoo.com/v7/finance/download/SPY?period1=-2208988800&period2=1679097600&interval=1d&events=split'
3: In match.names(column, colnames(data)) :
all columns not located in Hammer for SPY.Open SPY.High SPY.Low SPY.Close SPY.Volume SPY.Adjusted Hammer InvertedHammer
So here's my boilerplate code:
require(quantstrat)
require(quantmod)
require(FinancialInstrument)
require(candlesticks)
symbols = c("SPY")
initDate <- "1999-01-01"
from <- "2003-01-01"
to <- "2015-12-31"
options(width=70)
options("getSymbols.warning4.0"=FALSE)
#set account currency and system timezone
currency('USD')
stock("SPY",currency="USD",multiplier=1)
Sys.setenv(TZ="UTC")
tradeSize <- 1e6
initEq <- tradeSize*length(symbols)
getSymbols(symbols, from=from, to=to, src="yahoo", adjust=TRUE)
# Define the names of your strategy, portfolio and account
strategy.st <- "firststrat"
portfolio.st <- "firststrat"
account.st <- "firststrat"
if (!exists('.blotter')) .blotter <- new.env()
if (!exists('.strategy')) .strategy <- new.env()
rm.strat(strategy.st)
initPortf(portfolio.st, symbols=symbols, initDate=initDate, currency='USD')
initAcct(account.st, portfolios=portfolio.st, initDate=initDate,
currency='USD',initEq=initEq)
initOrders(portfolio.st, initDate=initDate)
strategy(strategy.st, store=TRUE)
My custom indicators:
HammerCandlestickIndicator <- function(
mktdata
) {
out <- candlesticks::CSPHammer(
mktdata,
minlowershadowCL=3/4,
maxuppershadowCL=.1,
minbodyCL=.1
)
colnames(out) <- 'Hammer'
# lets create an xts object
# the index are dates and the other column is hammer indicator
dates <- index(mktdata)
out <- xts(x = out, order.by = dates)
return(out)
}
add.indicator(
strategy = strategy.st,
name = "HammerCandlestickIndicator",
arguments =
list(mktdata = quote(mktdata)),
label = 'Hammer'
)
HangingManInvertedHammerCandleStickIndicator <- function(
mktdata
) {
out <- candlesticks::CSPInvertedHammer(
mktdata,
minuppershadowCL=3/4,
maxlowershadowCL=.1,
minbodyCL=.1
)
colnames(out) <- 'InvertedHammer'
# lets create an xts object
# the index are dates and the other column is hammer indicator
dates <- index(mktdata)
out <- xts(x = out, order.by = dates)
return(out)
}
add.indicator(
strategy = strategy.st,
name = "HangingManInvertedHammerCandleStickIndicator",
arguments =
list(mktdata = quote(mktdata)),
label = 'InvertedHammer'
)
add.signal(
strategy.st,
name = "sigThreshold",
arguments = list(
column = "Hammer",
threshold = FALSE,
relationship = "gt",
cross = TRUE
),
label = "HammerSigEnter"
)
applyStrategy(strategy = strategy.st, portfolios = portfolio.st)