I am trying to pull portfolio data from Bloomberg using command getPortfolio(My Portfolio,"Portfolio_Data"). Is it Possible to indicate reference date in the command?
When using excel it is possible to extract a portfolio at a certain point in time using BDS(My Portfolio,"Portfolio_Data","reference date = date") function. I wonder if this is possible in R.
BDS command returns NULL when requesting portfolio data in R. It seems like getPortfolio() should be the right solution, however I cannot figure out how to incorporate date into the function.
Figured out the correct syntax. The way to incorporate reference date into getPortfolio() function should look like the following:
overrides = c("REFERENCE_DATE" = '20181010')
getPortfolio("My Portfolio","Portfolio_Data", overrides = overrides)
"My Portfolio" should be portfolio ID from BBG's PRTU function with "client" appended (should look like this: "U1234567-8 Client")
Related
I need to find some historical time series for Stocks and have the result in R.
I tried already the package "quantmod", but unfortunately most of the stocks are not covered.
I found EXCELS "STOCKPRICEHISTORY" to yield good results.
Hence, to allow more solutions, I phrase my question more open:
How do I get from a table, that contains Stocks (Ticker), Startdate and Endate to a list in R which contains each respective stock and its stockprice timeseries?
My Startingpoint looks like this:
My aim at the very End is to have something like this:
(Its also ok if I have every single stock price timeseries as csv)
My ideas so far:
Excel VBA Solution - 1
Write a macro, that executes EXCELS "STOCKHISTORY" function on each of these stocks, and writes them as csv or so? Then after that, read them in and create a list in R
Excel VBA Solution - 2
Write a macro, that executes EXCELS "STOCKHISTORY" function on each of these stocks,
each one in a new worksheet? Bad Idea, since there are more than 4000 Stocks..
R Solution
(If possible) call "STOCKHISTORY" function from R directly (?)
Any suggestions on how to takle this?
kind regards
I would recommend using an API, especially over trying to connect to Excel via VBA. There are many that are free, but require and API key from their website. For example, alphavantage.
library(tidyverse)
library(jsonlite)
library(httr)
symbol = "IBM"
av_key = "demo"
url <- str_c("https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=", symbol ,"&apikey=", av_key, "&datatype=csv")
d <- read_csv(url)
d %>% head
Cred and other options: https://quantnomad.com/2020/07/06/best-free-api-for-historical-stock-data-examples-in-r/
Hello I don't have many experience with R but I need help using abbreviated code to specify a custom renderer for the "table1" package in R. I would like to not display the default stats(FREQ,PCT) variable for categorical data and only display the frequency and omit the percent. The code in R Documentations show how to customize this for continuous variables and show some custom statistics for different variables.
This is just for the convenience of displaying a frequency table with the built html formatting of package 1. I do know how to get this information otherwise. I am interested learning how to use more this package in R.
rndr <- function(x, name, ...)
{(what <- switch(name,
c_race = "FREQ",
parse.abbrev.render.code(c("", what))(x))
}
table1::table1(~c_race|c_ethnicity*c_gender, data = childlearn_demo,
render=rndr, overall="Total",topclass="Rtable1-zebra")
The purpose of "abbreviated code" is to allow flexibility without needing to write your own function. Thus, you can get the desired result by simply using render.categorical="Freq", i.e.:
table1::table1(~ c_race|c_ethnicity*c_gender, data = childlearn_demo,
overall="Total",topclass="Rtable1-zebra", render.categorical="Freq")
(note: I am the table1 package author)
I using the Alteryx R Tool to sign an amazon http request. To do so, I need the hmac function that is included in the digest package.
I'm using a text input tool that includes the key and a datestamp.
Key= "foo"
datastamp= "20120215"
Here's the issue. When I run the following script:
the.data <- read.Alteryx("1", mode="data.frame")
write.Alteryx(base64encode(hmac(the.data$key,the.data$datestamp,algo="sha256",raw = TRUE)),1)
I get an incorrect result when compared to when I run the following:
write.Alteryx(base64encode(hmac("foo","20120215",algo="sha256",raw = TRUE)),1)
The difference being when I hardcode the values for the key and object I get the correct result. But if use the variables from the R data frame I get incorrect output.
Does the data frame alter the data in someway. Has anyone come across this when working with the R Tool in Alteryx.
Thanks for your input.
The issue appears to be that when creating the data frame, your character variables are converted to factors. The way to fix this with the data.frame constructor function is
the.data <- data.frame(Key="foo", datestamp="20120215", stringsAsFactors=FALSE)
I haven't used read.Alteryx but I assume it has a similar way of achieving this.
Alternatively, if your data frame has already been created, you can convert the factors back into character:
write.Alteryx(base64encode(hmac(
as.character(the.data$Key),
as.character(the.data$datestamp),
algo="sha256",raw = TRUE)),1)
can anybody help me through the URL optimization in R.
Actually I need to get multiple organization related CSV file from URL.
I have to just change the one parameter in url to get that particular organization CSV table.
So mainly I was writing a function to extract that.
Example:
data<-function("bank"){table<-read.csv(url("*://.....=string.....")}
can you guide me on this.
Is that what you are looking for?
Data <- function(string){
table <- read.csv(url(paste("https://.....=", string, "...", sep="")))
# ...
}
# then you can change the parameter with function call
# (also dynamically using apply or a loop )
Data(string="bank")
I am using tableNominal{reporttools} to produce frequency tables. The way I understand it, tableNominal() produces latex code which has to be copied and pasted onto a text file and then saved as .tex. But is it possible to simple export the table produced as can be done in print(xtable(table), file="path/outfile.tex"))?
You may be able to use either latex or latexTranslate from the "Hmisc" package for this purpose. If you have the necessary program infrastructure the output gets sent to your TeX engine. (You may be able to improve the level of our answers by adding specific examples.)
Looks like that function does not return a character vector, so you need to use a strategy to capture the output from cat(). Using the example in the help page:
capture.output( TN <- tableNominal(vars = vars, weights = weights, group = group,
cap = "Table of nominal variables.", lab = "tab: nominal") ,
file="outfile.tex")