Error in addTxn - Quantstrat

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
8 messages Options
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Error in addTxn - Quantstrat

Rmetrics mailing list
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)

_______________________________________________
[hidden email] 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.
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Error in addTxn - Quantstrat

Joshua Ulrich
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
<[hidden email]> 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)
>
> _______________________________________________
> [hidden email] 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

_______________________________________________
[hidden email] 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.
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Error in addTxn - Quantstrat

Daniel Cegiełka
2017-05-24 19:45 GMT+02:00 Joshua Ulrich <[hidden email]>:
>
> This is a bug that was introduced here:
> https://github.com/braverock/blotter/commit/24b578628415111885d29ef97d5ab0eed1c7bb75


https://github.com/braverock/blotter/commit/24b578628415111885d29ef97d5ab0eed1c7bb75#diff-7347fe5a0f184f79ef064e92e3beb297R69

https://github.com/braverock/blotter/blob/master/R/addTxn.R#L85

why end() from stats?

https://github.com/braverock/blotter/blob/master/R/addTxn.R#L154

Should be end.xts(), eg:

end.xts <- function (x, ...)
{
    if (!length(x)) {
        index(x[length(.index(x)), ])
    } else
        index(x[NROW(x), ])
}

If I understood the problem correctly...

Best,
Daniel


>
>
>
> 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
>

_______________________________________________
[hidden email] 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.
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Fwd: Error in addTxn - Quantstrat

Daniel Cegiełka
In reply to this post by Joshua Ulrich
---------- Forwarded message ----------



ok, I found a bug.

https://github.com/braverock/blotter/blob/master/R/addTxn.R#L81

as.Date("2017-01-01") return time-based object:

if(!is.timeBased(TxnDate) ){
TxnDate<-as.POSIXct(TxnDate)
}

and TxnDate != POSIXct

Best,
Daniel

_______________________________________________
[hidden email] 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.

blotter_addTxn.diff (738 bytes) Download Attachment
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Error in addTxn - Quantstrat

Joshua Ulrich
In reply to this post by Daniel Cegiełka
On Wed, May 24, 2017 at 1:10 PM, Daniel Cegiełka
<[hidden email]> wrote: Because stats::end is the generic.

> https://github.com/braverock/blotter/blob/master/R/addTxn.R#L154
>
> Should be end.xts(), eg:
>
end.xts() is not exported from the xts namespace.  And it's generally
a bad idea to call methods directly, because you do not know if that
method will be able to handle objects of different classes.

> end.xts <- function (x, ...)
> {
>     if (!length(x)) {
>         index(x[length(.index(x)), ])
>     } else
>         index(x[NROW(x), ])
> }
>
> If I understood the problem correctly...
>
> Best,
> Daniel
>
>
>>
>>
>>
>> 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
>>



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

_______________________________________________
[hidden email] 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.
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Fwd: Error in addTxn - Quantstrat

Joshua Ulrich
In reply to this post by Daniel Cegiełka
On Wed, May 24, 2017 at 2:37 PM, Daniel Cegiełka
<[hidden email]> wrote:

>
> ok, I found a bug.
>
> https://github.com/braverock/blotter/blob/master/R/addTxn.R#L81
>
> as.Date("2017-01-01") return time-based object:
>
> if(!is.timeBased(TxnDate) ){
> TxnDate<-as.POSIXct(TxnDate)
> }
>
> and TxnDate != POSIXct
>
Yes, I arrived at the same conclusion before I sent my initial email.
I mentioned it in the bug report I created:
https://github.com/braverock/blotter/issues/51

Apologies for not mentioning it in the email... there was no need for
you to spend time on it.

> Best,
> Daniel
>
> _______________________________________________
> [hidden email] 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

_______________________________________________
[hidden email] 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.
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Error in addTxn - Quantstrat

Rmetrics mailing list
In reply to this post by Joshua Ulrich
Thanks for taking the time to look at this guys. Sorry for the verbose example but minimal means different things to different people depending on their skills, or lack thereof in my case. Cheers
John

-----Original Message-----
From: Joshua Ulrich [mailto:[hidden email]]
Sent: Wednesday, 24 May 2017 10:31 PM
To: Daniel Cegiełka <[hidden email]>
Cc: John Kumar <[hidden email]>; r-sig-finance <[hidden email]>
Subject: Re: [R-SIG-Finance] Error in addTxn - Quantstrat

On Wed, May 24, 2017 at 1:10 PM, Daniel Cegiełka <[hidden email]> wrote:

> 2017-05-24 19:45 GMT+02:00 Joshua Ulrich <[hidden email]>:
>>
>> This is a bug that was introduced here:
>> https://github.com/braverock/blotter/commit/24b578628415111885d29ef97
>> d5ab0eed1c7bb75
>
>
> https://github.com/braverock/blotter/commit/24b578628415111885d29ef97d
> 5ab0eed1c7bb75#diff-7347fe5a0f184f79ef064e92e3beb297R69
>
> https://github.com/braverock/blotter/blob/master/R/addTxn.R#L85
>
> why end() from stats?
>
Because stats::end is the generic.

> https://github.com/braverock/blotter/blob/master/R/addTxn.R#L154
>
> Should be end.xts(), eg:
>
end.xts() is not exported from the xts namespace.  And it's generally a bad idea to call methods directly, because you do not know if that method will be able to handle objects of different classes.

> end.xts <- function (x, ...)
> {
>     if (!length(x)) {
>         index(x[length(.index(x)), ])
>     } else
>         index(x[NROW(x), ])
> }
>
> If I understood the problem correctly...
>
> Best,
> Daniel
>
>
>>
>>
>>
>> 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
>>



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

_______________________________________________
[hidden email] 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.
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Error in addTxn - Quantstrat

Joshua Ulrich
This is now fixed in:
https://github.com/braverock/blotter/commit/537f7870665859346a1157b2bc786c6d7325f6fb

Thanks for the report!

On Wed, May 24, 2017 at 3:46 PM, John Kumar <[hidden email]> wrote:

> Thanks for taking the time to look at this guys. Sorry for the verbose
> example but minimal means different things to different people depending on
> their skills, or lack thereof in my case. Cheers
> John
>
> -----Original Message-----
> From: Joshua Ulrich [mailto:[hidden email]]
> Sent: Wednesday, 24 May 2017 10:31 PM
> To: Daniel Cegiełka <[hidden email]>
> Cc: John Kumar <[hidden email]>; r-sig-finance <
> [hidden email]>
> Subject: Re: [R-SIG-Finance] Error in addTxn - Quantstrat
>
> On Wed, May 24, 2017 at 1:10 PM, Daniel Cegiełka <
> [hidden email]> wrote:
> > 2017-05-24 19:45 GMT+02:00 Joshua Ulrich <[hidden email]>:
> >>
> >> This is a bug that was introduced here:
> >> https://github.com/braverock/blotter/commit/24b578628415111885d29ef97
> >> d5ab0eed1c7bb75
> >
> >
> > https://github.com/braverock/blotter/commit/24b578628415111885d29ef97d
> > 5ab0eed1c7bb75#diff-7347fe5a0f184f79ef064e92e3beb297R69
> >
> > https://github.com/braverock/blotter/blob/master/R/addTxn.R#L85
> >
> > why end() from stats?
> >
> Because stats::end is the generic.
>
> > https://github.com/braverock/blotter/blob/master/R/addTxn.R#L154
> >
> > Should be end.xts(), eg:
> >
> end.xts() is not exported from the xts namespace.  And it's generally a
> bad idea to call methods directly, because you do not know if that method
> will be able to handle objects of different classes.
>
> > end.xts <- function (x, ...)
> > {
> >     if (!length(x)) {
> >         index(x[length(.index(x)), ])
> >     } else
> >         index(x[NROW(x), ])
> > }
> >
> > If I understood the problem correctly...
> >
> > Best,
> > Daniel
> >
> >
> >>
> >>
> >>
> >> 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
> >>
>
>
>
> --
> Joshua Ulrich  |  about.me/joshuaulrich
> FOSS Trading  |  www.fosstrading.com
> R/Finance 2017 | www.rinfinance.com
>
>


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

        [[alternative HTML version deleted]]

_______________________________________________
[hidden email] 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.
Loading...