joshuaulrich / quantmod

Quantitative Financial Modelling Framework
http://www.quantmod.com/
GNU General Public License v3.0
794 stars 219 forks source link

chart_Series gives error running the basic example in quantstrat under R 4.3 #403

Closed comintel closed 10 months ago

comintel commented 10 months ago

Description

chart_Series() gives error running the basic example in quantstrat under R 4.3. Works ok in 4.2.

Expected behavior

Run error free in R 4.3

Minimal, reproducible example

#From the example in https://github.com/braverock/quantstrat#readme
library(FinancialInstrument)
library(PerformanceAnalytics)
library(quantstrat)

stock.str='AAPL' # what are we trying it on
currency('USD')
stock(stock.str,currency='USD',multiplier=1)
#
startDate="1999-12-31"
initEq=1000000
portfolio.st='macross'
account.st='macross'
initPortf(portfolio.st,symbols=stock.str)

initAcct(account.st,portfolios=portfolio.st, initEq=initEq)

initOrders(portfolio=portfolio.st)
stratMACROSS<- strategy(portfolio.st)
stratMACROSS <- add.indicator(strategy = stratMACROSS, name = "SMA", arguments = list(x=quote(Cl(mktdata)), n=50),label= "ma50" )
stratMACROSS <- add.indicator(strategy = stratMACROSS, name = "SMA", arguments = list(x=quote(Cl(mktdata)[,1]), n=200),label= "ma200")

stratMACROSS <- add.signal(strategy = stratMACROSS,name="sigCrossover",arguments = list(columns=c("ma50","ma200"), relationship="gte"),label="ma50.gt.ma200")
stratMACROSS <- add.signal(strategy = stratMACROSS,name="sigCrossover",arguments = list(column=c("ma50","ma200"),relationship="lt"),label="ma50.lt.ma200")

stratMACROSS <- add.rule(strategy = stratMACROSS,name='ruleSignal', arguments = list(sigcol="ma50.gt.ma200",sigval=TRUE, orderqty=100, ordertype='market', orderside='long'),type='enter')
stratMACROSS <- add.rule(strategy = stratMACROSS,name='ruleSignal', arguments = list(sigcol="ma50.lt.ma200",sigval=TRUE, orderqty='all', ordertype='market', orderside='long'),type='exit')

getSymbols(stock.str,from=startDate)
for(i in stock.str)
    assign(i, adjustOHLC(get(i),use.Adjusted=TRUE))
start_t<-Sys.time()
out<-applyStrategy(strategy=stratMACROSS , portfolios=portfolio.st)

end_t<-Sys.time()
print(end_t-start_t)

start_t<-Sys.time()
updatePortf(Portfolio='macross',Dates=paste('::',as.Date(Sys.time()),sep=''))

end_t<-Sys.time()
print("trade blotter portfolio update:")

print(end_t-start_t)
chart.Posn(Portfolio='macross',Symbol=stock.str, TA=c("add_SMA(n=50,col='red')","add_SMA(n=200,col='blue')"))

The line that gives the error is

chart.Posn(Portfolio='macross',Symbol=stock.str, TA = c("add_SMA(n=50,col='red')", "add_SMA(n=200,col='blue')"))
## "Error in !is.null(TA) && nchar(TA) > 0 :   'length = 2' in coercion to 'logical(1)' "
traceback()
## 2: chart_Series(Prices, name = Symbol, TA = TA, ...) at chart.Posn.R#82
## 1: chart.Posn(Portfolio = "macross", Symbol = stock.str, TA = c("add_SMA(n=50,col='red')", 
##        "add_SMA(n=200,col='blue')"))

It occurs because TA has 2 elements

joshuaulrich commented 10 months ago

Thanks for the report and reproducible example!