[R-SIG-Finance] Quantstrat with TAQ data

Brian G. Peterson brian at braverock.com
Thu Dec 13 13:52:59 CET 2012

On 12/12/2012 09:45 PM, Robert A'gata wrote:
> Thank you Aleksey. My doubt is less about whether it will be xts or zoo. My
> apology as I should be more precise. Says, I have an xts data with the
> following columns: TradePrice, TradeSize, Bid, Ask, HistoricalVolatility.
> Then my questions are:
> 1) How can I make this xts available to add.indicator? In most examples, I
> see that one has to create an instrument out of it. Then assign instrument
> ID to portfolio or strategy object. Finally rely on mktdata to look up.
> 2) For each tick, I'd like to access all the columns in the xts. A simplest
> example would be, I'd like to compute VWAP with 100-trade rolling window.
> Then compute the distance betwen current mid price and the running VWAP.
> How can I let my add.indicator know that I want a whole row and not only
> price? My understanding is that quote(mktData) will try to look for price
> only.

As is outlined in the documentation, you need instrument metadata that 
defines the instruments that you're trying to use.  For TAQ data from 
NYSE, these would be stock() instruments.

quantstrat, by default, assumes that you will have data named for each 
instrument, just as though you had called getSymbols.  Other approaches 
are possible, but harder, so we'll skip that.

If you use RTAQ (or the newer 'highfrequency' from R-Forge) to create 
xts objects from your TAQ CSV files, you can easily save these parsed 
xts objects to disk in a format suitable for getSymbols (specifically, 
getSymbols.FI) to go get them for you with from and to dates.  This will 
have the added advantage of making in faster and easier to get this data 
back into R for later analysis.

Let's assume you've done all that, and want to add a 100-trade VWAP 
indicator, as you've requested.  That would look like:

add.indicator(s, name="VWAP",
         arguments = list(
                 price = quote(mktdata$TradePrice)),
                 n = 100,
                 volume = quote(mktdata$TradeSize))

As a side note, You could also easily create and add your 
HistoricalVolatility column as an indicator inside the strategy as well, 
encapsulating all the required analytical code in the strategy object.


    - Brian

Brian G. Peterson
Ph: 773-459-4973
IM: bgpbraverock

More information about the R-SIG-Finance mailing list