Error in addTxn - Quantstrat

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

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.
Reply | Threaded
Open this post in threaded view
|

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.
Reply | Threaded
Open this post in threaded view
|

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.
Reply | Threaded
Open this post in threaded view
|

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
Reply | Threaded
Open this post in threaded view
|

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.
Reply | Threaded
Open this post in threaded view
|

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.
Reply | Threaded
Open this post in threaded view
|

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.
Reply | Threaded
Open this post in threaded view
|

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.
Reply | Threaded
Open this post in threaded view
|

Quantstrat - Multi-symbol (cross-section) analysis implimentation question..

Michael Chen

Hi,


I am trying to impliment a seemingly simple analysis in Quanstrat, but I am stuck.  Let's say that I have 50 futures in my data frame.  Each future is a time series of OHLC data.  I want to at each time stamp, compare the value of an indicator for each future and rank the futures accordingly.   For example, I want to rank the futures based on each's 5-day return.  How do I impliment this in Quanstrat?


So far, the only way I came up with go get around this is to pre-process each future's data then store the ranking data for each future in its own data set.  But this is not flexible and cumbersome.


Thank your for any suggestions, obviously I don't have a reproducible code yet.


Michael


        [[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.
Reply | Threaded
Open this post in threaded view
|

Re: Quantstrat - Multi-symbol (cross-section) analysis implimentation question..

Frank-2
Hi,

Well, reproducible code would get us all closer to making helpful
suggestions. Would you show us the code to read in two futures price streams
and make your calculation? Then we can see what the ranking problem is.

Thanks,

Frank
Chicago

-----Original Message-----
From: R-SIG-Finance [mailto:[hidden email]] On Behalf
Of Michael Chen
Sent: Sunday, July 30, 2017 12:11 PM
To: r-sig-finance <[hidden email]>
Subject: [R-SIG-Finance] Quantstrat - Multi-symbol (cross-section) analysis
implimentation question..


Hi,


I am trying to impliment a seemingly simple analysis in Quanstrat, but I am
stuck.  Let's say that I have 50 futures in my data frame.  Each future is a
time series of OHLC data.  I want to at each time stamp, compare the value
of an indicator for each future and rank the futures accordingly.   For
example, I want to rank the futures based on each's 5-day return.  How do I
impliment this in Quanstrat?


So far, the only way I came up with go get around this is to pre-process
each future's data then store the ranking data for each future in its own
data set.  But this is not flexible and cumbersome.


Thank your for any suggestions, obviously I don't have a reproducible code
yet.


Michael


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

_______________________________________________
[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.
Reply | Threaded
Open this post in threaded view
|

Re: Quantstrat - Multi-symbol (cross-section) analysis implimentation question..

Michael Chen
Hi Frank,


Thanks.  Yes,  reproducible code would be good, but unfortunately I have been struggling on that end, but I will try to put something together.  At the mean time, please let me explain the problem conceptually again, it may help.


Using Quantstrat, once you load the symbols and initialize the parameters,  I would define the indicators.  For example:


add.indicator(strategy.st, name = "runPercentRank",
              arguments = list(x = quote(Vo(mktdata)), n=90),
              label = "VolPctRnkX")


This indicator is just the running percent rank on Volume data for the past 90 days for each symbol.  Then I add signals and strategy rules, simple enough.    However,  as far as I can know,  strategy rules (signals and indicators) in quanstrat are applied to each symbol in the dataset sequentially and in chronological order.   Let's say, I have three futures in my dataframe:  AA, BB, and CC (as an example), the "VolPctRnkX" will be calculated for each future: AA, BB, CC.  If I examine the mktdata for each symbol (AA, BB, or CC), each will have a column data named "VolPctRankX"


My problem is that I want to compare this indictor "VolPctRnkX" for each symbol at each timestamp to that of other symbols.  In otherwords, ranks the sybmols according to its own "VolPctRnkX" value.  So, in this example,  at a particular date: "2010-10-01, the ranking could be  BB, CC, AA, based on each's "VolPctRankX" value on that day.

Symbol   VolPctRankX

BB            0.95

CC            0.85

AA            0.50


Then, I can make another indicator or rule using this ranking to size my order.  For this to work, there has to be cross-section (across symbols) comparasion at each time stamp.  How do I impliment this in the Quanstrat framework?


Is this a little clearer??


thanks again,


Michael


________________________________
From: Frank <[hidden email]>
Sent: Sunday, July 30, 2017 12:53 PM
To: 'Michael Chen'; 'r-sig-finance'
Subject: RE: [R-SIG-Finance] Quantstrat - Multi-symbol (cross-section) analysis implimentation question..

Hi,

Well, reproducible code would get us all closer to making helpful
suggestions. Would you show us the code to read in two futures price streams
and make your calculation? Then we can see what the ranking problem is.

Thanks,

Frank
Chicago

-----Original Message-----
From: R-SIG-Finance [mailto:[hidden email]] On Behalf
Of Michael Chen
Sent: Sunday, July 30, 2017 12:11 PM
To: r-sig-finance <[hidden email]>
Subject: [R-SIG-Finance] Quantstrat - Multi-symbol (cross-section) analysis
implimentation question..


Hi,


I am trying to impliment a seemingly simple analysis in Quanstrat, but I am
stuck.  Let's say that I have 50 futures in my data frame.  Each future is a
time series of OHLC data.  I want to at each time stamp, compare the value
of an indicator for each future and rank the futures accordingly.   For
example, I want to rank the futures based on each's 5-day return.  How do I
impliment this in Quanstrat?


So far, the only way I came up with go get around this is to pre-process
each future's data then store the ranking data for each future in its own
data set.  But this is not flexible and cumbersome.


Thank your for any suggestions, obviously I don't have a reproducible code
yet.


Michael


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


        [[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.
Reply | Threaded
Open this post in threaded view
|

Re: Quantstrat - Multi-symbol (cross-section) analysis implimentation question..

Frank-2
Hi Michael,

“but unfortunately I have been struggling on that end.” Show us your
struggles:

1) Show us the code you have to read in three commodities.
2) Show us the code that calculates the indicator.

From this code, I think I, or others, can point you in the right direction.
If steps 1 or 2 are not working, we can help you.

From your BB, CC, AA example, it looks like you are trying to reorder the
symbols. Don’t do that. Have an indicator

AA 3
BB 1
CC 2

Where BB is the first commodity to trade, CC is second and AA is third.

" rule using this ranking to size my order"

I can't tell what order sizing rule you are trying to use. If you only trade
one commodity, then you need another column for the allowed size for BB in
this case, but for all commodities in case they are traded. What seems
likely is you want to trade more than one commodity up to a risk limit of
$100,000 as an example. If your size in BB is constrained to 100 contracts
which is $75,000 of risk, then you have up to $25,000 of risk to trade in
CC. If CC maxes out at 50 contracts with $20,000 of risk, then you would add
some size for AA.


Best,

Frank

=================================

From: Michael Chen [mailto:[hidden email]]
Sent: Monday, July 31, 2017 8:56 AM
To: Frank <[hidden email]>; 'r-sig-finance'
<[hidden email]>
Subject: Re: [R-SIG-Finance] Quantstrat - Multi-symbol (cross-section)
analysis implimentation question..

Hi Frank,

Thanks.  Yes,  reproducible code would be good, but unfortunately I have
been struggling on that end, but I will try to put something together.  At
the mean time, please let me explain the problem conceptually again, it may
help.

Using Quantstrat, once you load the symbols and initialize the parameters, 
I would define the indicators.  For example:

add.indicator(strategy.st, name = "runPercentRank",
              arguments = list(x = quote(Vo(mktdata)), n=90),
              label = "VolPctRnkX")
This indicator is just the running percent rank on Volume data for the past
90 days for each symbol.  Then I add signals and strategy rules, simple
enough.    However,  as far as I can know,  strategy rules (signals and
indicators) in quanstrat are applied to each symbol in the dataset
sequentially and in chronological order.   Let's say, I have three futures
in my dataframe:  AA, BB, and CC (as an example), the "VolPctRnkX" will be
calculated for each future: AA, BB, CC.  If I examine the mktdata for each
symbol (AA, BB, or CC), each will have a column data named "VolPctRankX"

My problem is that I want to compare this indictor "VolPctRnkX" for each
symbol at each timestamp to that of other symbols.  In otherwords, ranks the
sybmols according to its own "VolPctRnkX" value.  So, in this example,  at a
particular date: "2010-10-01, the ranking could be  BB, CC, AA, based on
each's "VolPctRankX" value on that day.     
Symbol   VolPctRankX
BB            0.95
CC            0.85
AA            0.50

Then, I can make another indicator or rule using this ranking to size my
order.  For this to work, there has to be cross-section (across symbols)
comparasion at each time stamp.  How do I impliment this in the Quanstrat
framework?

Is this a little clearer??

thanks again,

Michael

________________________________________
From: Frank <mailto:[hidden email]>
Sent: Sunday, July 30, 2017 12:53 PM
To: 'Michael Chen'; 'r-sig-finance'
Subject: RE: [R-SIG-Finance] Quantstrat - Multi-symbol (cross-section)
analysis implimentation question..
 
Hi,

Well, reproducible code would get us all closer to making helpful
suggestions. Would you show us the code to read in two futures price streams
and make your calculation? Then we can see what the ranking problem is.

Thanks,

Frank
Chicago

-----Original Message-----
From: R-SIG-Finance [mailto:[hidden email]] On Behalf
Of Michael Chen
Sent: Sunday, July 30, 2017 12:11 PM
To: r-sig-finance <mailto:[hidden email]>
Subject: [R-SIG-Finance] Quantstrat - Multi-symbol (cross-section) analysis
implimentation question..


Hi,


I am trying to impliment a seemingly simple analysis in Quanstrat, but I am
stuck.  Let's say that I have 50 futures in my data frame.  Each future is a
time series of OHLC data.  I want to at each time stamp, compare the value
of an indicator for each future and rank the futures accordingly.   For
example, I want to rank the futures based on each's 5-day return.  How do I
impliment this in Quanstrat?


So far, the only way I came up with go get around this is to pre-process
each future's data then store the ranking data for each future in its own
data set.  But this is not flexible and cumbersome.


Thank your for any suggestions, obviously I don't have a reproducible code
yet.


Michael


        [[alternative HTML version deleted]]

_______________________________________________
mailto:[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.

_______________________________________________
[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.