[R-SIG-Finance] Error in addTxn - Quantstrat

John Kumar rjkft at yahoo.com
Wed May 24 17:25:30 CEST 2017


Hi All,
If I take the basic luxor strategy (which trades) and change the instrument
to AAPL or another stock, getSymbols method and to date it gives me the
following error:

Error in addTxn(Portfolio = portfolio, Symbol = symbol, TxnDate = txntime,
: 
  Transactions must be added in order. TxnDate (2003-01-22) is before last
transaction in portfolio (2003-01-17) for AAPL
In addition: Warning messages:
1: In addTxn(Portfolio = portfolio, Symbol = symbol, TxnDate = txntime,  :
  Incompatible methods ("Ops.Date", "Ops.POSIXt") for "<"
2: In addTxn(Portfolio = portfolio, Symbol = symbol, TxnDate = txntime,  :
  Incompatible methods ("Ops.Date", "Ops.POSIXt") for "<"

I have also experienced this error with other quantstrat scripts that worked
prior to upgrading to R 3.4 and blotter 0.11.3. I cannot find any other
mentions of this error and am wondering what I am missing here? Something to
do with date format? 
Minimal reproducible example below(Note: R 3.4, blotter 0.11.3,quantstrat
0.10.0,quantmod 0.4-8 produced error WORKS with R 3.3, blotter
0.9.1741,quantstrat 0.9.1739, quantmod 0.4-8) .
Cheers John

require(quantstrat)

Sys.setenv(TZ="UTC")

###

startDate = '2002-10-21'

.from=startDate

.to='2012-10-31'

strategy.st = 'luxor'
portfolio.st = 'forex'
account.st = 'IB'

.orderqty = 100000
.threshold = 0.0005
.txnfees = -6		# round-trip fee
.fast = 10
.slow = 30

currency(c('GBP', 'USD'))

stock('AAPL', currency = "USD", multiplier = 1)

### quantmod

getSymbols("AAPL", from = .from, to=.to)

### blotter

initPortf(portfolio.st, symbols='AAPL', currency='USD')
initAcct(account.st, portfolios=portfolio.st, currency='USD')

### quantstrat

initOrders(portfolio.st)

### define strategy

strategy(strategy.st, store=TRUE)

### indicators

add.indicator(strategy.st, name = "SMA",
              arguments = list(
                x = quote(Cl(mktdata)[,1]),
                n = .fast
              ),
              label="nFast"
)

add.indicator(strategy.st, name="SMA",
              arguments = list(
                x = quote(Cl(mktdata)[,1]),
                n = .slow
              ),
              label="nSlow"
)

### signals

add.signal(strategy.st, name='sigCrossover',
           arguments = list(
             columns=c("nFast","nSlow"),
             relationship="gte"
           ),
           label='long'
)

add.signal(strategy.st, name='sigCrossover',
           arguments = list(
             columns=c("nFast","nSlow"),
             relationship="lt"
           ),
           label='short'
)

### rules

add.rule(strategy.st, name='ruleSignal',
         arguments=list(sigcol='long' , sigval=TRUE,
                        orderside='short',
                        ordertype='market',
                        orderqty='all',
                        TxnFees=.txnfees,
                        replace=TRUE
         ),
         type='exit',
         label='Exit2LONG'
)

add.rule(strategy.st, name='ruleSignal',
         arguments=list(sigcol='short', sigval=TRUE,
                        orderside='long' ,
                        ordertype='market',
                        orderqty='all',
                        TxnFees=.txnfees,
                        replace=TRUE
         ),
         type='exit',
         label='Exit2SHORT'
)

add.rule(strategy.st, name='ruleSignal',
         arguments=list(sigcol='long' , sigval=TRUE,
                        orderside='long' ,
                        ordertype='stoplimit', prefer='High',
threshold=.threshold,
                        orderqty=+.orderqty,
                        replace=FALSE
         ),
         type='enter',
         label='EnterLONG'
)

add.rule(strategy.st, name='ruleSignal',
         arguments=list(sigcol='short', sigval=TRUE,
                        orderside='short',
                        ordertype='stoplimit', prefer='Low',
threshold=-.threshold,
                        orderqty=-.orderqty,
                        replace=FALSE
         ),
         type='enter',
         label='EnterSHORT'
)

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

applyStrategy(strategy.st, portfolio.st)



More information about the R-SIG-Finance mailing list