Skip to content
Permalink
Browse files

init

  • Loading branch information...
ShichenXie committed Jan 21, 2019
1 parent 05f2f5f commit 924cbd88bf1e1e0808a2d3abd903eca46480e4f4
Showing with 2,096 additions and 1,268 deletions.
  1. BIN .DS_Store
  2. 0 .Rbuildignore
  3. 0 .gitignore
  4. +10 −10 DESCRIPTION
  5. 0 LICENSE
  6. +15 −12 NAMESPACE
  7. 0 NEWS.md
  8. BIN R/.DS_Store
  9. +92 −18 R/{condition_inner_fun.R → condition_helper_fun.R}
  10. +19 −0 R/dim_0code_cate.R
  11. 0 R/dim_admin_div_cn.R
  12. +35 −35 R/dim_country_currency.R
  13. +15 −15 R/dim_exchange_commodity.R
  14. +17 −17 R/dim_exchange_stock.R
  15. +6 −0 R/ed_cate.R
  16. +26 −21 R/{geted_fred.R → ed_fred.R}
  17. +8 −47 R/{getmd_2money.R → ed_interest.R}
  18. +35 −35 R/{geted_nbs.R → ed_nbs.R}
  19. +0 −102 R/getfr_cn.R
  20. +0 −105 R/getmd.R
  21. +0 −49 R/getmd_2bond.R
  22. +0 −82 R/getmd_4future_cn.R
  23. +0 −34 R/getmd_index_cons.R
  24. +42 −0 R/md_0cate.R
  25. +173 −0 R/md_1forex.R
  26. +154 −0 R/md_2bond.R
  27. +170 −0 R/md_2money.R
  28. +34 −0 R/md_3index.R
  29. +47 −0 R/md_4commodity.R
  30. +118 −0 R/md_future.R
  31. +68 −0 R/md_stock1_.R
  32. +19 −80 R/{getmd_3stock_cn.R → md_stock1_163.R}
  33. +98 −86 R/{getmd_yahoo.R → md_stock1_yahoo.R}
  34. +101 −0 R/md_stock_fs.R
  35. +131 −33 R/{getmd_symbol_stock.R → md_stock_symbol.R}
  36. +25 −4 R/{ped_ti.R → ped_addti.R}
  37. +36 −0 R/ped_backtest.R
  38. +19 −4 R/ped_index.R
  39. +8 −8 R/ped_perf.R
  40. +65 −57 R/ped_plot.R
  41. +16 −18 R/{ped_dailyto.R → ped_to_freq.R}
  42. +11 −0 R/stock_adj.R
  43. BIN R/sysdata.rda
  44. +13 −2 R/z_dim__.R
  45. +105 −0 R/z_md.R
  46. 0 _pkgdown.yml
  47. 0 cran-comments.md
  48. BIN data/.DS_Store
  49. BIN data/code_country.RData
  50. BIN data/code_currency.RData
  51. BIN data/exchange_commodity.RData
  52. BIN data/exchange_stock.RData
  53. 0 docs/LICENSE-text.html
  54. 0 docs/authors.html
  55. 0 docs/docsearch.css
  56. 0 docs/docsearch.js
  57. 0 docs/index.html
  58. 0 docs/link.svg
  59. 0 docs/news/index.html
  60. 0 docs/pkgdown.css
  61. 0 docs/pkgdown.js
  62. 0 docs/pkgdown.yml
  63. 0 docs/reference/code_country.html
  64. 0 docs/reference/code_currency.html
  65. 0 docs/reference/exchange_commodity.html
  66. 0 docs/reference/exchange_stock.html
  67. 0 docs/reference/geted_nbs.html
  68. 0 docs/reference/geted_nbs_region.html
  69. 0 docs/reference/geted_nbs_symbol.html
  70. 0 docs/reference/getfr.html
  71. 0 docs/reference/getfs.html
  72. 0 docs/reference/getmd.html
  73. 0 docs/reference/getmd_index_cons.html
  74. 0 docs/reference/getmd_symbol.html
  75. 0 docs/reference/index.html
  76. 0 docs/reference/ped_index.html
  77. 0 docs/reference/ped_plot.html
  78. +11 −0 man/code_cate.Rd
  79. +0 −23 man/code_country.Rd
  80. +0 −22 man/code_currency.Rd
  81. +11 −0 man/ed_cate.Rd
  82. +7 −7 man/{geted_fred.Rd → ed_fred.Rd}
  83. +24 −0 man/ed_fred_symbol.Rd
  84. +16 −17 man/{geted_nbs.Rd → ed_nbs.Rd}
  85. +30 −0 man/ed_nbs_region.Rd
  86. +8 −8 man/{geted_nbs_symbol.Rd → ed_nbs_symbol.Rd}
  87. +0 −22 man/exchange_commodity.Rd
  88. +0 −22 man/exchange_stock.Rd
  89. +0 −24 man/geted_fred_symbol.Rd
  90. +0 −30 man/geted_nbs_region.Rd
  91. +0 −37 man/getfr.Rd
  92. +0 −65 man/getmd.Rd
  93. +0 −27 man/getmd_index_cons.Rd
  94. +0 −32 man/getmd_symbol.Rd
  95. +12 −0 man/md_cate.Rd
  96. +12 −0 man/md_future.Rd
  97. +11 −0 man/md_future_symbol.Rd
  98. +62 −0 man/md_stock.Rd
  99. +38 −0 man/md_stock_financials.Rd
  100. +11 −0 man/md_stock_symbol.Rd
  101. +46 −0 man/ped_addti.Rd
  102. +0 −24 man/ped_dailyto.Rd
  103. +18 −3 man/ped_index.Rd
  104. +24 −7 man/ped_plot.Rd
  105. +0 −24 man/ped_ti.Rd
  106. +24 −0 man/ped_to_freq.Rd
  107. 0 getpedr.Rproj → pedar.Rproj
BIN +6 KB .DS_Store
Binary file not shown.
0 .Rbuildignore 100644 → 100755
No changes.
0 .gitignore 100644 → 100755
No changes.
20 DESCRIPTION 100644 → 100755
@@ -1,12 +1,12 @@
Package: getpedr
Package: pedar
Version: 0.0.1
Title: get public economic data
Title: public economic and financial data
Description:
The `getpedr` provides an interface to access public economic
data, which are provided by national statistics, central bank,
stock exchange etc. Three kinds of datasets are included in
`getpedr`, market data (`getmd*`), financial reports
(`getfr`), and economic data (`geted*`).
The `pedar` provides an interface to access public economic and financial
data for analyst and researcher. The data souces include NBS, FRED, Yahoo
Finance, 163 Finance etc. The functions are grouped into four categories,
market data (md_*), financial statements (fs_*), economic data (ed_*) and
analysis functions (ped_*).
Authors@R:
person("Shichen", "Xie", email = "xie@shichen.name", role = c("aut", "cre"))
Depends:
@@ -21,10 +21,10 @@ Suggests:
knitr,
rmarkdown
License: GPL-3 + file LICENSE
URL: https://github.com/ShichenXie/getpedr
BugReports: https://github.com/ShichenXie/getpedr/issues
URL: https://github.com/ShichenXie/pedar
BugReports: https://github.com/ShichenXie/pedar/issues
LazyData: true
VignetteBuilder: knitr
RoxygenNote: 6.1.0
RoxygenNote: 6.1.1
Roxygen: list(markdown = TRUE)
Encoding: UTF-8
0 LICENSE 100644 → 100755
No changes.
27 NAMESPACE 100644 → 100755
@@ -1,18 +1,22 @@
# Generated by roxygen2: do not edit by hand

export(geted_fred)
export(geted_fred_symbol)
export(geted_nbs)
export(geted_nbs_region)
export(geted_nbs_symbol)
export(getfr)
export(getmd)
export(getmd_index_cons)
export(getmd_symbol)
export(ped_dailyto)
export(code_cate)
export(ed_cate)
export(ed_fred)
export(ed_fred_symbol)
export(ed_nbs)
export(ed_nbs_region)
export(ed_nbs_symbol)
export(md_cate)
export(md_future)
export(md_future_symbol)
export(md_stock)
export(md_stock_financials)
export(md_stock_symbol)
export(ped_addti)
export(ped_index)
export(ped_plot)
export(ped_ti)
export(ped_to_freq)
import(data.table)
import(ggplot2)
import(gridExtra)
@@ -37,7 +41,6 @@ importFrom(readr,stop_for_problems)
importFrom(readxl,read_excel)
importFrom(stats,complete.cases)
importFrom(stats,lm)
importFrom(stats,predict)
importFrom(stats,sd)
importFrom(stringi,stri_unescape_unicode)
importFrom(utils,data)
0 NEWS.md 100644 → 100755
No changes.
BIN +6 KB R/.DS_Store
Binary file not shown.
@@ -13,10 +13,11 @@ check_arg = function(arg, choices, default=NULL) {
return(arg)
}

# check date range
check_date_range = function(date_range, default = "max") {
if (!grepl("max|ytd|[1-9][0-9]*w|[1-9][0-9]*w|[1-9,10,11]m|[1-9][0-9]*y", tolower(date_range))) {
if (!grepl("max|ytd|[1-9][0-9]*d|[1-9][0-9]*w|[1-9,10,11]m|[1-9][0-9]*y", tolower(date_range))) {
date_range = default
warning("The `date_range` is set to '", default, "'")
warning(sprintf('The \'date_range\' is set to %s.', date_range))
}
return(date_range)
}
@@ -195,10 +196,15 @@ check_mkt_src = function(market=NULL, source=NULL) {
}


########################### inner functions ###########################
########################### helper functions ###########################
# # select rows in a dataframe
# sel_row_df = function(df, col_name = NULL, stop_condi = NULL) {
#
# }

# download and read excel file from website
#' @importFrom readxl read_excel
#' @importFrom utils download.file
# @importFrom utils download.file
#' @importFrom curl curl_download new_handle
load_read_xl = function(url, handle=new_handle()) {
temp = tempfile()
@@ -207,7 +213,7 @@ load_read_xl = function(url, handle=new_handle()) {
curl_download(url, destfile = temp, handle = handle)
dat = read_excel(temp)

return(dat)
return(setDT(dat))
}

#download and read csv file from website
@@ -227,7 +233,7 @@ load_read_csv = function(url, encode="UTF-8", handle=new_handle()) {

curl_download(url, destfile = temp, handle = handle)
dat = read.csv(temp, fileEncoding = encode)
return(dat)
return(setDT(dat))
}


@@ -263,29 +269,34 @@ date_to_sec = function(date=Sys.time()) {
return(trunc(as.numeric(datetime)))
}

# end date of month
first_month_date = function(date) {
as.Date(paste0(format(date, '%Y-%m'),'-01'))
}
last_month_date = function(date) {
first_month_date(first_month_date(date) + 31) - 1
}

# loop on get_data1
load_dat_loop = function(symbol, func, args=list(), print_step) {
runif = dt_list = NULL
symbol_len = length(symbol)
for (i in 1:symbol_len) {
symbol_i = symbol[i]
syb_len = length(symbol)
for (i in 1:syb_len) {
syb_i = symbol[i]
# print
if ((print_step>0) & (i %% print_step == 0)) cat(paste0(format(c(i,symbol_len)),collapse = "/"), symbol_i,"\n")

dt_list[[symbol_i]] = do.call(eval(parse(text = func)), c(symbol_i, args))

if ((print_step>0) & (i %% print_step == 0)) cat(sprintf('%s %s\n', paste0(format(c(i, syb_len)), collapse = '/'), syb_i))
dt_list[[syb_i]] = do.call(func, c(syb_i, args))
# sleep for 1s
Sys.sleep(runif(1))
# Sys.sleep(runif(1))
}
return(dt_list)
}


# extract table from html via xml2 package
#' @import data.table
xml_table = function(wb, num=NULL, sup_rm = NULL) {
doc0 = xml_find_all(wb, "//table")
xml_table = function(wb, num=NULL, sup_rm=NULL, attr=NULL, header=FALSE) {
doc0 = xml_find_all(wb, paste0("//table",attr))
if (!is.null(num)) doc0 = doc0[num]

doc = lapply(
@@ -296,7 +307,11 @@ xml_table = function(wb, num=NULL, sup_rm = NULL) {
dt = lapply(doc, function(x) {
if (!is.null(sup_rm)) x = gsub(sup_rm, "", x)

data.table(x = x)[, tstrsplit(x, "[\n\t\r]+")]
dat = data.table(x = x)[, tstrsplit(x, "[\n\t\r]+")]
if (header) {
dat = setnames(dat[-1], as.character(dat[1]))
}
return(dat)
})

return(dt)
@@ -348,4 +363,63 @@ lwd <- function(n = 0, d = Sys.Date(), tz = Sys.timezone()) {

api_key = function(src){
if (src=="fred") return("4330f2f7aab9a42ab9b950cec4428b91")
}
}



# select rows from a dataframe
select_rows_df = function(dt, column=NULL, input_string=NULL, onerow=FALSE) {
seleted_rows = NULL

while (is.null(seleted_rows) || nrow(seleted_rows) == 0) {
if (is.null(input_string)) {
print(copy(dt)[,lapply(.SD, format)])
if (is.null(column)) {
txt = "select rows via ('r'+rowid): "
} else {
txt = sprintf("select rows via ('r'+rowid) or (%s): ", column)
}
if (onerow) txt = sub('rows', 'one row', txt)
sel_id = readline(txt)
} else {
sel_id = input_string
}

if (grepl('^r[1-9]', sel_id)) {
while (grepl("^r", sel_id)) {
sel_id_string = gsub('-',':',gsub("[^(0-9|:)]+", ",", sel_id))
sel_id_string = gsub('^[^0-9]+|[^0-9]+$','',sel_id_string)
sel_id = eval(parse( text = sprintf('c(%s)',sel_id_string) ))
sel_id = intersect(sel_id, dt[,.I])
if (length(sel_id)==0) {
sel_id = 'r'
} else {
seleted_rows=dt[sel_id]
break
}
}
} else {
seleted_rows=dt[grepl(sel_id, eval(parse(text = column)))]
}
if (onerow & nrow(seleted_rows) >1) {
# cat(sprintf('Only %s was selected\n.', seleted_rows[1]))
seleted_rows = seleted_rows[1]
}
}

return(seleted_rows)
}


# ceiling on decimal
ceiling2 = function(x) {
x_sci = format(x, scientific = TRUE, digits=2)
z = ceiling(as.numeric(substr(x_sci, 1, 3)))
e = substr(x_sci, 4, nchar(x_sci))
as.numeric(paste0(z, e))
}

# is date/time class
isdatetime = function(x) {
any(class(x) %in% c("Date","POSIXlt","POSIXct","POSIXt"))
}
@@ -0,0 +1,19 @@

#' code list by category
#'
#'
#'
#' @export
code_cate = function(cate=NULL, ...) {
code_category = c('country', 'currency', 'stock exchange', 'commodity exchange')
# market category
if (!is.null(cate)) cate = check_arg(cate, code_category)
while (is.null(cate)) {
cate = select_rows_df(dt=setDT(list(code_category=code_category)), column='code_category', onerow=TRUE
)[,code_category]
}

cod = setDT(copy(eval(parse(text = gsub(' ','_',paste("code", cate)) ))))
return(cod)
}

0 R/dim_admin_div_cn.R 100644 → 100755
No changes.
70 R/dim_country_currency.R 100644 → 100755
@@ -1,38 +1,38 @@
#' country code list
#'
#' Code list for the representation of names
#' of countries and their subdivisions.
#'
#' @docType data
#' @keywords data
#' @name code_country
#' @usage data(code_country)
#' @format A data frame with 249 rows and 4 columns.
#' @source \url{https://en.wikipedia.org/wiki/ISO_3166-1}
#' @examples
#' # load code of country
#' data(code_country)
#'
NULL




#' currency code list
#'
#' Current currency & funds code list.
#'
#' @docType data
#' @keywords data
#' @name code_currency
#' @usage data(code_currency)
#' @format A data frame with 278 rows and 3 columns.
#' @source \url{https://en.wikipedia.org/wiki/ISO_4217}, \url{https://www.currency-iso.org/en/home/tables/table-a1.html}
#' @examples
#' # load code of currency
#' data(code_currency)
#'
NULL
# country code list
#
# Code list for the representation of names
# of countries and their subdivisions.
#
# @docType data
# @keywords data
# @name code_country
# @usage data(code_country)
# @format A data frame with 249 rows and 4 columns.
# @source \url{https://en.wikipedia.org/wiki/ISO_3166-1}
# @examples
# # load code of country
# data(code_country)
#
# NULL




# currency code list
#
# Current currency & funds code list.
#
# @docType data
# @keywords data
# @name code_currency
# @usage data(code_currency)
# @format A data frame with 278 rows and 3 columns.
# @source \url{https://en.wikipedia.org/wiki/ISO_4217}, \url{https://www.currency-iso.org/en/home/tables/table-a1.html}
# @examples
# # load code of currency
# data(code_currency)
#
# NULL



30 R/dim_exchange_commodity.R 100644 → 100755
@@ -1,18 +1,18 @@
#' commodity exchanges list
#'
#' List of commodities exchanges
#'
#' @docType data
#' @keywords data
#' @name exchange_commodity
#' @usage data(exchange_commodity)
#' @format A data frame with 97 rows and 7 columns.
#' @source \url{https://en.wikipedia.org/wiki/List_of_commodities_exchanges}
#' @examples
#' # load commodity exchange
#' data(exchange_commodity)
#'
NULL
# commodity exchanges list
#
# List of commodities exchanges
#
# @docType data
# @keywords data
# @name exchange_commodity
# @usage data(exchange_commodity)
# @format A data frame with 97 rows and 7 columns.
# @source \url{https://en.wikipedia.org/wiki/List_of_commodities_exchanges}
# @examples
# # load commodity exchange
# data(exchange_commodity)
#
# NULL


#' @import data.table xml2

0 comments on commit 924cbd8

Please sign in to comment.
You can’t perform that action at this time.