[R-SIG-Finance] addTxns addfees
Hideyoshi Maeda
hideyoshi.maeda at gmail.com
Thu Jul 5 00:46:11 CEST 2012
If an example is required
Here is an example:
rm(list=ls(pos=.blotter),pos=.blotter)
rm(list=ls(pos=.instrument),pos=.instrument)
rm(list=ls(pos=.strategy),pos=.strategy)
currency('USD')
stock("SPY", currency="USD", mulitplier=1)
getSymbols('SPY', from='2012-03-01', to='2012-07-04')
portf.name <- "dummy.Portfolio"
initPortf(portf.name, 'SPY', initDate='2012-02-29')
initAcct(portf.name, portf.name, initDate='2012-02-29', initEq=1e6)
qty <- rep(c(1,-1), nrow(SPY)/2)
price <- SPY[,4]
txnfees <- rep(-5, nrow(SPY))
txndata <- cbind(qty, price, txnfees)
colnames(txndata) <- c("Quantity","Price","txnfees")
blotter:::addTxns(Portfolio=portf.name, Symbol='SPY', TxnData=txndata )
txns <- getTxns(Portfolio=portf.name, Symbol='SPY')
head(txns)
This will show the buying and selling of 1 share on alternate days at the close but will not show any of the fees relating to each transaction.
Thanks
HLM
On 5 Jul 2012, at 01:48, Hideyoshi Maeda wrote:
> Hi I am looking at the addTxns function in blotter, and I would like to add fees data/information in the TxnData argument (as a column).
>
> When looking at the function, by running
>
> blotter:::addTxns
> it seems to use the column names "Price" and "Quantity" but automatically sets/assigns the TxnFees to zero.
>
> Is there a way of overwriting this, so that it can be included in my analysis?
>
>
> On 4 Jul 2012, at 20:00, r-sig-finance-request at r-project.org wrote:
>
>> Send R-SIG-Finance mailing list submissions to
>> r-sig-finance at r-project.org
>>
>> To subscribe or unsubscribe via the World Wide Web, visit
>> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>> or, via email, send a message with subject or body 'help' to
>> r-sig-finance-request at r-project.org
>>
>> You can reach the person managing the list at
>> r-sig-finance-owner at r-project.org
>>
>> When replying, please edit your Subject line so it is more specific
>> than "Re: Contents of R-SIG-Finance digest..."
>>
>>
>> Today's Topics:
>>
>> 1. Rbbg - curious performance of bdh? (Gordon Morrison)
>> 2. Re: Rbbg - curious performance of bdh? (julien cuisinier)
>> 3. Re: financial data on flat matrix (Ben quant)
>> 4. Re: financial data on flat matrix (Ben quant)
>> 5. Re: financial data on flat matrix (Ben quant)
>> 6. Re: Calculating Hasbrouck's information share and
>> Gonzalo-Granger weights on R (Drew Harris)
>>
>>
>> ----------------------------------------------------------------------
>>
>> Message: 1
>> Date: Tue, 3 Jul 2012 15:00:23 +0100
>> From: Gordon Morrison <Gordon.Morrison at ftse.com>
>> To: "'R-SIG-Finance at r-project.org'" <R-SIG-Finance at r-project.org>
>> Subject: [R-SIG-Finance] Rbbg - curious performance of bdh?
>> Message-ID:
>> <887B4B788BA1F0429670B96750DA450401D73E06BD at EXCH07B.ftse.com>
>> Content-Type: text/plain
>>
>> I seem to be getting some surprising results when I run the function "bdh". The code I am running is as follows
>>
>>
>>> library(RBloomberg)
>>
>> Loading required package: rJava
>>
>>> conn <- blpConnect()
>>
>> R version 2.15.0 (2012-03-30)
>>
>> rJava Version 0.9-3
>>
>> RBloomberg Version 0.4-151
>>
>> Java environment initialized successfully.
>>
>> Looking for most recent blpapi3.jar file...
>>
>> Adding C:\blp\API\APIv3\JavaAPI\v3.4.8.1\lib\blpapi3.jar to Java classpath
>>
>> Bloomberg API Version 3.4.8.1
>>
>> The connection seems to work OK
>>
>>> bdp(conn, "AMZN US Equity", "NAME")
>>
>> NAME
>>
>> AMZN US Equity AMAZON.COM INC
>>
>> And I can download a time series of sales/revenue - as shown below
>>
>>
>>> bdh(conn, "FP FP Equity", "SALES_REV_TURN", start_date = "20080101")
>>
>> date SALES_REV_TURN
>>
>> 2008-03-31 2008-03-31 39287
>>
>> 2008-06-30 2008-06-30 43300
>>
>> 2008-09-30 2008-09-30 44039
>>
>> 2008-12-31 2008-12-31 33705
>>
>> 2009-03-31 2009-03-31 25468
>>
>> 2009-06-30 2009-06-30 26574
>>
>> 2009-09-30 2009-09-30 28816
>>
>> 2009-12-31 2009-12-31 31295
>>
>> 2010-03-31 2010-03-31 33161
>>
>> 2010-06-30 2010-06-30 36327
>>
>> 2010-09-30 2010-09-30 35228
>>
>> 2010-12-31 2010-12-31 35760
>>
>> 2011-03-31 2011-03-31 41602
>>
>> 2011-06-30 2011-06-30 40465
>>
>> 2011-09-30 2011-09-30 41525
>>
>> 2011-12-31 2011-12-31 42958
>>
>> 2012-03-31 2012-03-31 46775
>>
>> ... and even get the same data for historic fiscal years
>>
>>> bdh(conn, "FP FP Equity", "SALES_REV_TURN", start_date = "-4fy")
>>
>> date SALES_REV_TURN
>>
>> 2007-12-31 2007-12-31 37697
>>
>> 2008-03-31 2008-03-31 39287
>>
>> 2008-06-30 2008-06-30 43300
>>
>> 2008-09-30 2008-09-30 44039
>>
>> 2008-12-31 2008-12-31 33705
>>
>> 2009-03-31 2009-03-31 25468
>>
>> 2009-06-30 2009-06-30 26574
>>
>> 2009-09-30 2009-09-30 28816
>>
>> 2009-12-31 2009-12-31 31295
>>
>> 2010-03-31 2010-03-31 33161
>>
>> 2010-06-30 2010-06-30 36327
>>
>> 2010-09-30 2010-09-30 35228
>>
>> 2010-12-31 2010-12-31 35760
>>
>> 2011-03-31 2011-03-31 41602
>>
>> 2011-06-30 2011-06-30 40465
>>
>> 2011-09-30 2011-09-30 41525
>>
>> 2011-12-30 2011-12-30 42958
>>
>> 2012-03-30 2012-03-30 46775
>>
>> But when I run the same function for "ENERGY_CONSUMPTION" it returns an empty dataset
>>
>>> bdh(conn, "FP FP Equity", "ENERGY_CONSUMPTION", start_date = "20080101")
>>
>> [1] date ENERGY_CONSUMPTION
>>
>> <0 rows> (or 0-length row.names)
>>
>>
>>> bdh(conn, "FP FP Equity", "ENERGY_CONSUMPTION", start_date = "-4fy")
>>
>> [1] date ENERGY_CONSUMPTION
>>
>> <0 rows> (or 0-length row.names)
>>
>> The data does exist in Bloomberg as I can use the same function to download the data into Excel and even see it on the Bloomberg terminal.
>>
>> Does anyone have any ideas?
>>
>>
>> Gordon Morrison
>>
>>
>> ______________________________________________________________________
>> This e-mail and any attachments may contain confidential...{{dropped:24}}
>>
>>
>>
>> ------------------------------
>>
>> Message: 2
>> Date: Tue, 3 Jul 2012 16:30:03 +0200
>> From: julien cuisinier <j_cuisinier at hotmail.com>
>> To: <gordon.morrison at ftse.com>, <r-sig-finance at r-project.org>
>> Subject: Re: [R-SIG-Finance] Rbbg - curious performance of bdh?
>> Message-ID: <COL102-W34E63D8141509E0017FF668FE90 at phx.gbl>
>> Content-Type: text/plain
>>
>>
>> Have the same in Rbbg, but when trying in XL:
>> =BDH("FP FP Equity","ENERGY_CONSUMPTION","-4FY")
>>
>> I am getting:
>>
>> #N/A N/A
>>
>> ???
>>
>>
>>> From: Gordon.Morrison at ftse.com
>>> To: R-SIG-Finance at r-project.org
>>> Date: Tue, 3 Jul 2012 15:00:23 +0100
>>> Subject: [R-SIG-Finance] Rbbg - curious performance of bdh?
>>>
>>> I seem to be getting some surprising results when I run the function "bdh". The code I am running is as follows
>>>
>>>
>>>> library(RBloomberg)
>>>
>>> Loading required package: rJava
>>>
>>>> conn <- blpConnect()
>>>
>>> R version 2.15.0 (2012-03-30)
>>>
>>> rJava Version 0.9-3
>>>
>>> RBloomberg Version 0.4-151
>>>
>>> Java environment initialized successfully.
>>>
>>> Looking for most recent blpapi3.jar file...
>>>
>>> Adding C:\blp\API\APIv3\JavaAPI\v3.4.8.1\lib\blpapi3.jar to Java classpath
>>>
>>> Bloomberg API Version 3.4.8.1
>>>
>>> The connection seems to work OK
>>>
>>>> bdp(conn, "AMZN US Equity", "NAME")
>>>
>>> NAME
>>>
>>> AMZN US Equity AMAZON.COM INC
>>>
>>> And I can download a time series of sales/revenue - as shown below
>>>
>>>
>>>> bdh(conn, "FP FP Equity", "SALES_REV_TURN", start_date = "20080101")
>>>
>>> date SALES_REV_TURN
>>>
>>> 2008-03-31 2008-03-31 39287
>>>
>>> 2008-06-30 2008-06-30 43300
>>>
>>> 2008-09-30 2008-09-30 44039
>>>
>>> 2008-12-31 2008-12-31 33705
>>>
>>> 2009-03-31 2009-03-31 25468
>>>
>>> 2009-06-30 2009-06-30 26574
>>>
>>> 2009-09-30 2009-09-30 28816
>>>
>>> 2009-12-31 2009-12-31 31295
>>>
>>> 2010-03-31 2010-03-31 33161
>>>
>>> 2010-06-30 2010-06-30 36327
>>>
>>> 2010-09-30 2010-09-30 35228
>>>
>>> 2010-12-31 2010-12-31 35760
>>>
>>> 2011-03-31 2011-03-31 41602
>>>
>>> 2011-06-30 2011-06-30 40465
>>>
>>> 2011-09-30 2011-09-30 41525
>>>
>>> 2011-12-31 2011-12-31 42958
>>>
>>> 2012-03-31 2012-03-31 46775
>>>
>>> ... and even get the same data for historic fiscal years
>>>
>>>> bdh(conn, "FP FP Equity", "SALES_REV_TURN", start_date = "-4fy")
>>>
>>> date SALES_REV_TURN
>>>
>>> 2007-12-31 2007-12-31 37697
>>>
>>> 2008-03-31 2008-03-31 39287
>>>
>>> 2008-06-30 2008-06-30 43300
>>>
>>> 2008-09-30 2008-09-30 44039
>>>
>>> 2008-12-31 2008-12-31 33705
>>>
>>> 2009-03-31 2009-03-31 25468
>>>
>>> 2009-06-30 2009-06-30 26574
>>>
>>> 2009-09-30 2009-09-30 28816
>>>
>>> 2009-12-31 2009-12-31 31295
>>>
>>> 2010-03-31 2010-03-31 33161
>>>
>>> 2010-06-30 2010-06-30 36327
>>>
>>> 2010-09-30 2010-09-30 35228
>>>
>>> 2010-12-31 2010-12-31 35760
>>>
>>> 2011-03-31 2011-03-31 41602
>>>
>>> 2011-06-30 2011-06-30 40465
>>>
>>> 2011-09-30 2011-09-30 41525
>>>
>>> 2011-12-30 2011-12-30 42958
>>>
>>> 2012-03-30 2012-03-30 46775
>>>
>>> But when I run the same function for "ENERGY_CONSUMPTION" it returns an empty dataset
>>>
>>>> bdh(conn, "FP FP Equity", "ENERGY_CONSUMPTION", start_date = "20080101")
>>>
>>> [1] date ENERGY_CONSUMPTION
>>>
>>> <0 rows> (or 0-length row.names)
>>>
>>>
>>>> bdh(conn, "FP FP Equity", "ENERGY_CONSUMPTION", start_date = "-4fy")
>>>
>>> [1] date ENERGY_CONSUMPTION
>>>
>>> <0 rows> (or 0-length row.names)
>>>
>>> The data does exist in Bloomberg as I can use the same function to download the data into Excel and even see it on the Bloomberg terminal.
>>>
>>> Does anyone have any ideas?
>>>
>>>
>>> Gordon Morrison
>>>
>>>
>>> ______________________________________________________________________
>>> This e-mail and any attachments may contain confidential...{{dropped:24}}
>>>
>>> _______________________________________________
>>> 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.
>>
>> [[alternative HTML version deleted]]
>>
>>
>>
>> ------------------------------
>>
>> Message: 3
>> Date: Tue, 3 Jul 2012 11:21:22 -0600
>> From: Ben quant <ccquant at gmail.com>
>> To: Jeffrey Ryan <jeffrey.ryan at lemnica.com>
>> Cc: r-sig-finance at r-project.org
>> Subject: Re: [R-SIG-Finance] financial data on flat matrix
>> Message-ID:
>> <CAG2PC+dv3SEAGj+GbRor=hUj2LgNNs1=9YQnvCfZZ8WHhnfOVg at mail.gmail.com>
>> Content-Type: text/plain
>>
>> xts endpoints uses 'existing' dates in the data. I want to use my dates and
>> put data on the correct row.
>>
>> Here are the details of some sample data...
>>
>> library('Rbbg')
>> library('stringr')
>>
>> conn <- blpConnect(throw.ticker.errors = F)
>>
>> daysback<- (261 * 7) # or whatever
>> start.date <- as.POSIXct(Sys.Date()-daysback)
>> end.date <- as.POSIXct(Sys.Date())
>> tickers<- c("A US Equity", "AA US Equity")
>>
>> fields<-c("SALES_REV_TURN")
>> o_names = c("periodicitySelection")
>> o_values = c("QUARTERLY")
>>
>> dat1<-bdh(conn, tickers, fields, start.date,end.date,option_names =
>> o_names, option_values = o_values,
>> always.display.tickers = TRUE,include.non.trading.days = T)
>>
>>> dat1
>> ticker date SALES_REV_TURN
>> 1 A US Equity 2007-07-31 1374
>> 2 A US Equity 2007-10-31 1446
>> 3 A US Equity 2008-01-31 1393
>> 4 A US Equity 2008-04-30 1456
>> 5 A US Equity 2008-07-31 1444
>> 6 A US Equity 2008-10-31 1481
>> 7 A US Equity 2009-01-31 1166
>> 8 A US Equity 2009-04-30 1091
>> 9 A US Equity 2009-07-31 1057
>> 10 A US Equity 2009-10-31 1167
>> 11 A US Equity 2010-01-31 1213
>> 12 A US Equity 2010-04-30 1271
>> 13 A US Equity 2010-07-31 1384
>> 14 A US Equity 2010-10-31 1576
>> 15 A US Equity 2011-01-31 1519
>> 16 A US Equity 2011-04-30 1677
>> 17 A US Equity 2011-07-31 1691
>> 18 A US Equity 2011-10-31 1728
>> 19 A US Equity 2012-01-31 1635
>> 20 A US Equity 2012-04-30 1733
>> 21 AA US Equity 2007-09-30 7387
>> 22 AA US Equity 2007-12-31 5919
>> 23 AA US Equity 2008-03-31 6998
>> 24 AA US Equity 2008-06-30 7245
>> 25 AA US Equity 2008-09-30 6970
>> 26 AA US Equity 2008-12-31 5688
>> 27 AA US Equity 2009-03-31 4147
>> 28 AA US Equity 2009-06-30 4244
>> 29 AA US Equity 2009-09-30 4615
>> 30 AA US Equity 2009-12-31 5433
>> 31 AA US Equity 2010-03-31 4887
>> 32 AA US Equity 2010-06-30 5187
>> 33 AA US Equity 2010-09-30 5287
>> 34 AA US Equity 2010-12-31 5652
>> 35 AA US Equity 2011-03-31 5958
>> 36 AA US Equity 2011-06-30 6585
>> 37 AA US Equity 2011-09-30 6419
>> 38 AA US Equity 2011-12-31 5989
>> 39 AA US Equity 2012-03-31 6006
>>
>> So, for example, I'd like these two values on the same row:
>> A US Equity 2012-04-30 1733
>> AA US Equity 2012-03-31 6006
>>
>> ...and the row name would be 2012-03-31. A's value should be on the
>> 2012-03-31 row (despite having a date of 2012-04-30) because I'm putting
>> everything on 'normal' quarter end dates. Therefore, all of A's values
>> would shift back one in date. It has to be a general solution that puts
>> 'abnormal' fiscal quarter end company data on the same row as 'normal'
>> quarter end dates. (I understand this introduces look-ahead bias, but I
>> correct for it later when applicable.) Is there a function out there that
>> does this already?
>>
>> Again here is how I produce the 'normal' fiscal quarter end dates that I
>> want as row names:
>> to_date = as.Date(Sys.time())
>> from_date = as.Date(to_date - (daysback + 100))
>> get_dates <- function(Start, End,end_of,every)
>> {
>> Vec <- as.Date(levels(cut(seq.Date(Start, End, by = end_of),breaks =
>> every)))
>> Vec[-1] - 1
>> }
>> qtrs = get_dates(from_date, to_date,end_of= "month",every = "quarter")
>> qtrs = qtrs[length(qtrs):1]
>>> qtrs
>> [1] "2012-03-31" "2011-12-31" "2011-09-30" "2011-06-30" "2011-03-31"
>> "2010-12-31" "2010-09-30" "2010-06-30" "2010-03-31"
>> [10] "2009-12-31" "2009-09-30" "2009-06-30" "2009-03-31" "2008-12-31"
>> "2008-09-30" "2008-06-30" "2008-03-31" "2007-12-31"
>> [19] "2007-09-30" "2007-06-30" "2007-03-31"
>>
>> On Sat, Jun 30, 2012 at 1:10 PM, Jeffrey Ryan <jeffrey.ryan at lemnica.com>wrote:
>>
>>> You can look into ?endpoints in xts, but you haven't exactly provided
>>> a _reproducible_ example as requested in the posting guidelines. Not
>>> personally sure what "flat matrix" is for instance.
>>>
>>> Also likely why no one has responded thus far. If you make it _easy_
>>> to answer (simple examples which we can try and run), people tend to
>>> answer.
>>>
>>> Jeff
>>>
>>> On Thu, Jun 28, 2012 at 11:42 AM, Ben quant <ccquant at gmail.com> wrote:
>>>> Hello,
>>>>
>>>> I'm using the Rbbg package (please suggest an alternative that is faster
>>> if
>>>> you know of one). I'd like to get my data into a flat matrix where row
>>>> names are found like this for quarterly data:
>>>>
>>>> get_dates <- function(Start, End,end_of,every)
>>>> {
>>>> Vec <- as.Date(levels(cut(seq.Date(Start, End, by = end_of),breaks =
>>>> every)))
>>>> Vec[-1] - 1
>>>> }
>>>> qtrs = get_dates(from_date, to_date,end_of= "month",every = "quarter")
>>>> qtrs = qtrs[length(qtrs):1]
>>>>> qtrs
>>>> [1] "2012-03-31" "2011-12-31" "2011-09-30" "2011-06-30" "2011-03-31"
>>>> "2010-12-31" "2010-09-30" "2010-06-30" "2010-03-31" "2009-12-31"
>>>> [11] "2009-09-30" "2009-06-30" "2009-03-31" "2008-12-31" "2008-09-30"
>>>> "2008-06-30" "2008-03-31" "2007-12-31" "2007-09-30" "2007-06-30"
>>>>
>>>> ...and all companies that report after the quarter end date (above) and
>>>> before the next one are moved back one. For example, if a company
>>> reporting
>>>> period ended in Jan. 2012, those values would be on the 2011-12-31 row.
>>>> (These are then adjusted for look-ahead using the actual report date.)
>>>>
>>>> Is there a function that will do this in zoo or another package?
>>>>
>>>> Thanks!
>>>>
>>>> Ben
>>>>
>>>> [[alternative HTML version deleted]]
>>>>
>>>> _______________________________________________
>>>> 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.
>>>
>>>
>>>
>>> --
>>> Jeffrey Ryan
>>> jeffrey.ryan at lemnica.com
>>>
>>> www.lemnica.com
>>> www.esotericR.com
>>>
>>
>> [[alternative HTML version deleted]]
>>
>>
>>
>> ------------------------------
>>
>> Message: 4
>> Date: Tue, 3 Jul 2012 12:32:33 -0600
>> From: Ben quant <ccquant at gmail.com>
>> To: Jeffrey Ryan <jeffrey.ryan at lemnica.com>
>> Cc: r-sig-finance at r-project.org
>> Subject: Re: [R-SIG-Finance] financial data on flat matrix
>> Message-ID:
>> <CAG2PC+cYKahN6MhmZEve0pVj4Am0gomFHQwLCg-jKgBnwDfwxA at mail.gmail.com>
>> Content-Type: text/plain
>>
>> dat = do.call(cbind, lapply(dat2, function(.vec){
>> if(length(.vec) >= 20){
>> .vec[1:20]
>> }else if(length(.vec) <
>> 20){
>> c(rep(NaN, 20 -
>> length(.vec)),.vec)
>> }
>> }
>> )
>> )
>>
>>
>> On Tue, Jul 3, 2012 at 11:21 AM, Ben quant <ccquant at gmail.com> wrote:
>>
>>> xts endpoints uses 'existing' dates in the data. I want to use my dates
>>> and put data on the correct row.
>>>
>>> Here are the details of some sample data...
>>>
>>> library('Rbbg')
>>> library('stringr')
>>>
>>> conn <- blpConnect(throw.ticker.errors = F)
>>>
>>> daysback<- (261 * 7) # or whatever
>>> start.date <- as.POSIXct(Sys.Date()-daysback)
>>> end.date <- as.POSIXct(Sys.Date())
>>> tickers<- c("A US Equity", "AA US Equity")
>>>
>>> fields<-c("SALES_REV_TURN")
>>> o_names = c("periodicitySelection")
>>> o_values = c("QUARTERLY")
>>>
>>> dat1<-bdh(conn, tickers, fields, start.date,end.date,option_names =
>>> o_names, option_values = o_values,
>>> always.display.tickers = TRUE,include.non.trading.days = T)
>>>
>>>> dat1
>>> ticker date SALES_REV_TURN
>>> 1 A US Equity 2007-07-31 1374
>>> 2 A US Equity 2007-10-31 1446
>>> 3 A US Equity 2008-01-31 1393
>>> 4 A US Equity 2008-04-30 1456
>>> 5 A US Equity 2008-07-31 1444
>>> 6 A US Equity 2008-10-31 1481
>>> 7 A US Equity 2009-01-31 1166
>>> 8 A US Equity 2009-04-30 1091
>>> 9 A US Equity 2009-07-31 1057
>>> 10 A US Equity 2009-10-31 1167
>>> 11 A US Equity 2010-01-31 1213
>>> 12 A US Equity 2010-04-30 1271
>>> 13 A US Equity 2010-07-31 1384
>>> 14 A US Equity 2010-10-31 1576
>>> 15 A US Equity 2011-01-31 1519
>>> 16 A US Equity 2011-04-30 1677
>>> 17 A US Equity 2011-07-31 1691
>>> 18 A US Equity 2011-10-31 1728
>>> 19 A US Equity 2012-01-31 1635
>>> 20 A US Equity 2012-04-30 1733
>>> 21 AA US Equity 2007-09-30 7387
>>> 22 AA US Equity 2007-12-31 5919
>>> 23 AA US Equity 2008-03-31 6998
>>> 24 AA US Equity 2008-06-30 7245
>>> 25 AA US Equity 2008-09-30 6970
>>> 26 AA US Equity 2008-12-31 5688
>>> 27 AA US Equity 2009-03-31 4147
>>> 28 AA US Equity 2009-06-30 4244
>>> 29 AA US Equity 2009-09-30 4615
>>> 30 AA US Equity 2009-12-31 5433
>>> 31 AA US Equity 2010-03-31 4887
>>> 32 AA US Equity 2010-06-30 5187
>>> 33 AA US Equity 2010-09-30 5287
>>> 34 AA US Equity 2010-12-31 5652
>>> 35 AA US Equity 2011-03-31 5958
>>> 36 AA US Equity 2011-06-30 6585
>>> 37 AA US Equity 2011-09-30 6419
>>> 38 AA US Equity 2011-12-31 5989
>>> 39 AA US Equity 2012-03-31 6006
>>>
>>> So, for example, I'd like these two values on the same row:
>>> A US Equity 2012-04-30 1733
>>> AA US Equity 2012-03-31 6006
>>>
>>> ...and the row name would be 2012-03-31. A's value should be on the
>>> 2012-03-31 row (despite having a date of 2012-04-30) because I'm putting
>>> everything on 'normal' quarter end dates. Therefore, all of A's values
>>> would shift back one in date. It has to be a general solution that puts
>>> 'abnormal' fiscal quarter end company data on the same row as 'normal'
>>> quarter end dates. (I understand this introduces look-ahead bias, but I
>>> correct for it later when applicable.) Is there a function out there that
>>> does this already?
>>>
>>> Again here is how I produce the 'normal' fiscal quarter end dates that I
>>> want as row names:
>>> to_date = as.Date(Sys.time())
>>> from_date = as.Date(to_date - (daysback + 100))
>>>
>>> get_dates <- function(Start, End,end_of,every)
>>> {
>>> Vec <- as.Date(levels(cut(seq.Date(Start, End, by = end_of),breaks =
>>> every)))
>>> Vec[-1] - 1
>>> }
>>> qtrs = get_dates(from_date, to_date,end_of= "month",every = "quarter")
>>> qtrs = qtrs[length(qtrs):1]
>>>> qtrs
>>> [1] "2012-03-31" "2011-12-31" "2011-09-30" "2011-06-30" "2011-03-31"
>>> "2010-12-31" "2010-09-30" "2010-06-30" "2010-03-31"
>>> [10] "2009-12-31" "2009-09-30" "2009-06-30" "2009-03-31" "2008-12-31"
>>> "2008-09-30" "2008-06-30" "2008-03-31" "2007-12-31"
>>> [19] "2007-09-30" "2007-06-30" "2007-03-31"
>>>
>>>
>>> On Sat, Jun 30, 2012 at 1:10 PM, Jeffrey Ryan <jeffrey.ryan at lemnica.com>wrote:
>>>
>>>> You can look into ?endpoints in xts, but you haven't exactly provided
>>>> a _reproducible_ example as requested in the posting guidelines. Not
>>>> personally sure what "flat matrix" is for instance.
>>>>
>>>> Also likely why no one has responded thus far. If you make it _easy_
>>>> to answer (simple examples which we can try and run), people tend to
>>>> answer.
>>>>
>>>> Jeff
>>>>
>>>> On Thu, Jun 28, 2012 at 11:42 AM, Ben quant <ccquant at gmail.com> wrote:
>>>>> Hello,
>>>>>
>>>>> I'm using the Rbbg package (please suggest an alternative that is
>>>> faster if
>>>>> you know of one). I'd like to get my data into a flat matrix where row
>>>>> names are found like this for quarterly data:
>>>>>
>>>>> get_dates <- function(Start, End,end_of,every)
>>>>> {
>>>>> Vec <- as.Date(levels(cut(seq.Date(Start, End, by = end_of),breaks =
>>>>> every)))
>>>>> Vec[-1] - 1
>>>>> }
>>>>> qtrs = get_dates(from_date, to_date,end_of= "month",every = "quarter")
>>>>> qtrs = qtrs[length(qtrs):1]
>>>>>> qtrs
>>>>> [1] "2012-03-31" "2011-12-31" "2011-09-30" "2011-06-30" "2011-03-31"
>>>>> "2010-12-31" "2010-09-30" "2010-06-30" "2010-03-31" "2009-12-31"
>>>>> [11] "2009-09-30" "2009-06-30" "2009-03-31" "2008-12-31" "2008-09-30"
>>>>> "2008-06-30" "2008-03-31" "2007-12-31" "2007-09-30" "2007-06-30"
>>>>>
>>>>> ...and all companies that report after the quarter end date (above) and
>>>>> before the next one are moved back one. For example, if a company
>>>> reporting
>>>>> period ended in Jan. 2012, those values would be on the 2011-12-31 row.
>>>>> (These are then adjusted for look-ahead using the actual report date.)
>>>>>
>>>>> Is there a function that will do this in zoo or another package?
>>>>>
>>>>> Thanks!
>>>>>
>>>>> Ben
>>>>>
>>>>> [[alternative HTML version deleted]]
>>>>>
>>>>> _______________________________________________
>>>>> 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.
>>>>
>>>>
>>>>
>>>> --
>>>> Jeffrey Ryan
>>>> jeffrey.ryan at lemnica.com
>>>>
>>>> www.lemnica.com
>>>> www.esotericR.com
>>>>
>>>
>>>
>>
>> [[alternative HTML version deleted]]
>>
>>
>>
>> ------------------------------
>>
>> Message: 5
>> Date: Tue, 3 Jul 2012 12:38:04 -0600
>> From: Ben quant <ccquant at gmail.com>
>> To: Jeffrey Ryan <jeffrey.ryan at lemnica.com>
>> Cc: r-sig-finance at r-project.org
>> Subject: Re: [R-SIG-Finance] financial data on flat matrix
>> Message-ID:
>> <CAG2PC+d163hpHz2mF0Za+cEuD1+zyDafOp2CsXN-TsR=7ExLKA at mail.gmail.com>
>> Content-Type: text/plain
>>
>> forgot this part:
>> frmla = as.formula(paste(fields,"~","ticker"))
>> dat2<- unstack(dat1, frmla)
>>
>> dat = do.call(cbind, lapply(dat2, function(.vec){
>>
>>>
>>> if(length(.vec) >= 20){
>>> .vec[1:20]
>>> }else if(length(.vec) <
>>> 20){
>>> c(rep(NaN, 20 -
>>> length(.vec)),.vec)
>>> }
>>> }
>>> )
>>> )
>>
>>
>>
>> On Tue, Jul 3, 2012 at 12:32 PM, Ben quant <ccquant at gmail.com> wrote:
>>
>>> dat = do.call(cbind, lapply(dat2, function(.vec){
>>> if(length(.vec) >= 20){
>>> .vec[1:20]
>>> }else if(length(.vec) <
>>> 20){
>>> c(rep(NaN, 20 -
>>> length(.vec)),.vec)
>>> }
>>> }
>>> )
>>> )
>>>
>>>
>>>
>>> On Tue, Jul 3, 2012 at 11:21 AM, Ben quant <ccquant at gmail.com> wrote:
>>>
>>>> xts endpoints uses 'existing' dates in the data. I want to use my dates
>>>> and put data on the correct row.
>>>>
>>>> Here are the details of some sample data...
>>>>
>>>> library('Rbbg')
>>>> library('stringr')
>>>>
>>>> conn <- blpConnect(throw.ticker.errors = F)
>>>>
>>>> daysback<- (261 * 7) # or whatever
>>>> start.date <- as.POSIXct(Sys.Date()-daysback)
>>>> end.date <- as.POSIXct(Sys.Date())
>>>> tickers<- c("A US Equity", "AA US Equity")
>>>>
>>>> fields<-c("SALES_REV_TURN")
>>>> o_names = c("periodicitySelection")
>>>> o_values = c("QUARTERLY")
>>>>
>>>> dat1<-bdh(conn, tickers, fields, start.date,end.date,option_names =
>>>> o_names, option_values = o_values,
>>>> always.display.tickers = TRUE,include.non.trading.days = T)
>>>>
>>>>> dat1
>>>> ticker date SALES_REV_TURN
>>>> 1 A US Equity 2007-07-31 1374
>>>> 2 A US Equity 2007-10-31 1446
>>>> 3 A US Equity 2008-01-31 1393
>>>> 4 A US Equity 2008-04-30 1456
>>>> 5 A US Equity 2008-07-31 1444
>>>> 6 A US Equity 2008-10-31 1481
>>>> 7 A US Equity 2009-01-31 1166
>>>> 8 A US Equity 2009-04-30 1091
>>>> 9 A US Equity 2009-07-31 1057
>>>> 10 A US Equity 2009-10-31 1167
>>>> 11 A US Equity 2010-01-31 1213
>>>> 12 A US Equity 2010-04-30 1271
>>>> 13 A US Equity 2010-07-31 1384
>>>> 14 A US Equity 2010-10-31 1576
>>>> 15 A US Equity 2011-01-31 1519
>>>> 16 A US Equity 2011-04-30 1677
>>>> 17 A US Equity 2011-07-31 1691
>>>> 18 A US Equity 2011-10-31 1728
>>>> 19 A US Equity 2012-01-31 1635
>>>> 20 A US Equity 2012-04-30 1733
>>>> 21 AA US Equity 2007-09-30 7387
>>>> 22 AA US Equity 2007-12-31 5919
>>>> 23 AA US Equity 2008-03-31 6998
>>>> 24 AA US Equity 2008-06-30 7245
>>>> 25 AA US Equity 2008-09-30 6970
>>>> 26 AA US Equity 2008-12-31 5688
>>>> 27 AA US Equity 2009-03-31 4147
>>>> 28 AA US Equity 2009-06-30 4244
>>>> 29 AA US Equity 2009-09-30 4615
>>>> 30 AA US Equity 2009-12-31 5433
>>>> 31 AA US Equity 2010-03-31 4887
>>>> 32 AA US Equity 2010-06-30 5187
>>>> 33 AA US Equity 2010-09-30 5287
>>>> 34 AA US Equity 2010-12-31 5652
>>>> 35 AA US Equity 2011-03-31 5958
>>>> 36 AA US Equity 2011-06-30 6585
>>>> 37 AA US Equity 2011-09-30 6419
>>>> 38 AA US Equity 2011-12-31 5989
>>>> 39 AA US Equity 2012-03-31 6006
>>>>
>>>> So, for example, I'd like these two values on the same row:
>>>> A US Equity 2012-04-30 1733
>>>> AA US Equity 2012-03-31 6006
>>>>
>>>> ...and the row name would be 2012-03-31. A's value should be on the
>>>> 2012-03-31 row (despite having a date of 2012-04-30) because I'm putting
>>>> everything on 'normal' quarter end dates. Therefore, all of A's values
>>>> would shift back one in date. It has to be a general solution that puts
>>>> 'abnormal' fiscal quarter end company data on the same row as 'normal'
>>>> quarter end dates. (I understand this introduces look-ahead bias, but I
>>>> correct for it later when applicable.) Is there a function out there
>>>> that does this already?
>>>>
>>>> Again here is how I produce the 'normal' fiscal quarter end dates that I
>>>> want as row names:
>>>> to_date = as.Date(Sys.time())
>>>> from_date = as.Date(to_date - (daysback + 100))
>>>>
>>>> get_dates <- function(Start, End,end_of,every)
>>>> {
>>>> Vec <- as.Date(levels(cut(seq.Date(Start, End, by = end_of),breaks =
>>>> every)))
>>>> Vec[-1] - 1
>>>> }
>>>> qtrs = get_dates(from_date, to_date,end_of= "month",every = "quarter")
>>>> qtrs = qtrs[length(qtrs):1]
>>>>> qtrs
>>>> [1] "2012-03-31" "2011-12-31" "2011-09-30" "2011-06-30" "2011-03-31"
>>>> "2010-12-31" "2010-09-30" "2010-06-30" "2010-03-31"
>>>> [10] "2009-12-31" "2009-09-30" "2009-06-30" "2009-03-31" "2008-12-31"
>>>> "2008-09-30" "2008-06-30" "2008-03-31" "2007-12-31"
>>>> [19] "2007-09-30" "2007-06-30" "2007-03-31"
>>>>
>>>>
>>>> On Sat, Jun 30, 2012 at 1:10 PM, Jeffrey Ryan <jeffrey.ryan at lemnica.com>wrote:
>>>>
>>>>> You can look into ?endpoints in xts, but you haven't exactly provided
>>>>> a _reproducible_ example as requested in the posting guidelines. Not
>>>>> personally sure what "flat matrix" is for instance.
>>>>>
>>>>> Also likely why no one has responded thus far. If you make it _easy_
>>>>> to answer (simple examples which we can try and run), people tend to
>>>>> answer.
>>>>>
>>>>> Jeff
>>>>>
>>>>> On Thu, Jun 28, 2012 at 11:42 AM, Ben quant <ccquant at gmail.com> wrote:
>>>>>> Hello,
>>>>>>
>>>>>> I'm using the Rbbg package (please suggest an alternative that is
>>>>> faster if
>>>>>> you know of one). I'd like to get my data into a flat matrix where row
>>>>>> names are found like this for quarterly data:
>>>>>>
>>>>>> get_dates <- function(Start, End,end_of,every)
>>>>>> {
>>>>>> Vec <- as.Date(levels(cut(seq.Date(Start, End, by = end_of),breaks =
>>>>>> every)))
>>>>>> Vec[-1] - 1
>>>>>> }
>>>>>> qtrs = get_dates(from_date, to_date,end_of= "month",every = "quarter")
>>>>>> qtrs = qtrs[length(qtrs):1]
>>>>>>> qtrs
>>>>>> [1] "2012-03-31" "2011-12-31" "2011-09-30" "2011-06-30" "2011-03-31"
>>>>>> "2010-12-31" "2010-09-30" "2010-06-30" "2010-03-31" "2009-12-31"
>>>>>> [11] "2009-09-30" "2009-06-30" "2009-03-31" "2008-12-31" "2008-09-30"
>>>>>> "2008-06-30" "2008-03-31" "2007-12-31" "2007-09-30" "2007-06-30"
>>>>>>
>>>>>> ...and all companies that report after the quarter end date (above) and
>>>>>> before the next one are moved back one. For example, if a company
>>>>> reporting
>>>>>> period ended in Jan. 2012, those values would be on the 2011-12-31 row.
>>>>>> (These are then adjusted for look-ahead using the actual report date.)
>>>>>>
>>>>>> Is there a function that will do this in zoo or another package?
>>>>>>
>>>>>> Thanks!
>>>>>>
>>>>>> Ben
>>>>>>
>>>>>> [[alternative HTML version deleted]]
>>>>>>
>>>>>> _______________________________________________
>>>>>> 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.
>>>>>
>>>>>
>>>>>
>>>>> --
>>>>> Jeffrey Ryan
>>>>> jeffrey.ryan at lemnica.com
>>>>>
>>>>> www.lemnica.com
>>>>> www.esotericR.com
>>>>>
>>>>
>>>>
>>>
>>
>> [[alternative HTML version deleted]]
>>
>>
>>
>> ------------------------------
>>
>> Message: 6
>> Date: Tue, 3 Jul 2012 21:34:52 -0700 (PDT)
>> From: Drew Harris <drew.harris.nz at gmail.com>
>> To: r-sig-finance at r-project.org
>> Subject: Re: [R-SIG-Finance] Calculating Hasbrouck's information share
>> and Gonzalo-Granger weights on R
>> Message-ID: <1341376492947-4635353.post at n4.nabble.com>
>> Content-Type: text/plain; charset=us-ascii
>>
>> Hi Nidhi,
>>
>> I have been attempting to expand your code above into an n dimensional
>> format. I am having a bit of trouble getting my Beta and Alpha orthogonal
>> components to generate and they are giving me (I believe) extremely
>> unbalanced IS measures. The following is what I tried to implement:
>>
>> ## the beta_orthogonal and alpha_orthogonal vectors :
>> beta_ortt <- matrix(0, nrow = n, ncol=n)
>> for (l in 1:n) {
>> for (j in 1:n) {
>> beta_ortt[j, l] <- if((n+1-j)==l) {cointest at V[(n+1-j),l]}
>> else { -cointest at V[(n+1-j),l] }
>> }}
>>
>> alpha_ortt <- matrix(0, nrow = n, ncol=n)
>> for (l in 1:n) {
>> for (j in 1:n) {
>> alpha_ortt[j, l] <- if((n+1-j)==l) {cointest at W[(n+1-j),l]}
>> else { -cointest at W[(n+1-j),l] }
>> }}
>>
>> ## Gamma matrix construction
>> gammat <- matrix(0, nrow = n, ncol=n)
>> Identity <- diag(1, n)
>> for (l in 1:n) {
>> for (j in 1:n) {
>> for (i in 1:(k-2)) {
>> gammat[j,l] <- gammat[j,l] +vecm$rlm$coefficients[((n*i)+(l-1)),j]
>> }}}
>>
>> --
>> View this message in context: http://r.789695.n4.nabble.com/Calculating-Hasbrouck-s-information-share-and-Gonzalo-Granger-weights-on-R-tp2395838p4635353.html
>> Sent from the Rmetrics mailing list archive at Nabble.com.
>>
>>
>>
>> ------------------------------
>>
>> _______________________________________________
>> R-SIG-Finance mailing list
>> R-SIG-Finance at r-project.org
>> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>>
>>
>> End of R-SIG-Finance Digest, Vol 98, Issue 4
>> ********************************************
>
More information about the R-SIG-Finance
mailing list