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

Joshua Ulrich josh.m.ulrich at gmail.com
Wed May 24 19:45:40 CEST 2017


This is a bug that was introduced here:
https://github.com/braverock/blotter/commit/24b578628415111885d29ef97d5ab0eed1c7bb75

Here's a *minimal* reproducible example:
require(blotter)
currency("USD")
initPortf("test", symbols="AAPL", currency="USD")

addTxn("test", "AAPL", as.Date("2017-01-01"),  1, 10)
addTxn("test", "AAPL", as.Date("2017-01-02"), -1, 10)

Thanks for the report!  I'll work on a fix.

Best,
Josh


On Wed, May 24, 2017 at 10:25 AM, John Kumar via R-SIG-Finance
<r-sig-finance at r-project.org> wrote:
> 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)
>
> _______________________________________________
> 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.



-- 
Joshua Ulrich  |  about.me/joshuaulrich
FOSS Trading  |  www.fosstrading.com
R/Finance 2017 | www.rinfinance.com



More information about the R-SIG-Finance mailing list