Skip to content

Commit

Permalink
lpr
Browse files Browse the repository at this point in the history
  • Loading branch information
ShichenXie committed Mar 24, 2024
1 parent 2a9708a commit 5273eb3
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 38 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

* fixed a bug in pq_plot function to support candle chart
* the md_stock_symbol supports to query the constituent symbols of Chinese securities index
* fixed a bug in md_moneycn function to query lpr

# pedquant 0.2.4

Expand Down
61 changes: 24 additions & 37 deletions R/md_2chinamoney.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,9 @@ md_moneycn_symbol = function() {
cnocny6m,Shibor CNY 6M - daily
cnocny9m,Shibor CNY 9M - daily
cnocny1m,Shibor CNY 1Y - daily
cn1ylpr,China 1year Loan Prime Rate - monthly
cn5ylpr,China 5year Loan Prime Rate - monthly
cn1ypr_d,China 1year Policy Rate Deposit - daily
cn1ypr_l,China 1year Policy Rate Lending - daily
cn1ylpr,China 1year Loan Prime Rate
cn5ylpr,China 5year Loan Prime Rate
cn1ydpr,China 1year Deposit Prime Rate Deposit
cn1ydy_b,China 1Y Bond Yield - daily
cn10ydy_b,China 10Y Bond Yield - daily
rmbx_cfets,CFETS RMB Index - weekly
Expand All @@ -60,9 +59,9 @@ md_moneycn_symbol = function() {
md_moneycn_widelong = function(dt) {
. = name = symbol = NULL

melt(dt, id.vars = 'date', variable.name = 'symbol', value.name = 'close')[
melt(dt, id.vars = 'date', variable.name = 'symbol', value.name = 'value')[
md_moneycn_symbol()[], on = 'symbol', nomatch = 0
][, .(symbol, name, date, open=close, high=close, low=close, close)]
][, .(symbol, name, date, value)]
}

# shanghai interbank offered rate, shibor
Expand All @@ -83,40 +82,28 @@ md_cnshibor = function(date_range = '3y', from=NULL, to=Sys.Date(), ...) {
}

# loan prime rate, lpr
md_cnlpr = function(date_range = '3y', from=NULL, to=Sys.Date(), ...) {
## from/to
to = check_to(to)
from = check_from(date_range, from, to, default_from = "2013-01-01", default_date_range = '3y')

# 'https://www.chinamoney.com.cn/dqs/rest/cm-u-bk-currency/LprHisExcel?lang=CN&strStartDate=%s&strEndDate=%s'
# https://www.shibor.org/dqs/rest/cm-u-bk-currency/LprHisExcel?lang=CN&strStartDate=2013-01-01&strEndDate=2013-12-31
## download data
dat = load_data_chinamoney('https://www.shibor.org/dqs/rest/cm-u-bk-currency/LprHisExcel?lang=CN&strStartDate=%s&strEndDate=%s', date_ft1y(from, to))

setnames(dat, c('date', 'cn1ylpr', 'cn5ylpr'))
dat = md_moneycn_widelong(dat)
return(dat[])
}

# policy rate
md_cnpr = function(date_range = '3y', from=NULL, to=Sys.Date(), ...) {
date_range = 'max'
md_cnlpr = function(date_range = 'max', from=NULL, to=Sys.Date(), ...) {
## from/to
to = check_to(to)
from = check_from(date_range, from, to, default_from = "1997-01-01", default_date_range = '3y')

## download data
dat = load_data_chinamoney(paste0('https://www.chinamoney.com.cn/dqs/rest/cm-u-bk-currency/SddsIntrRatePlRatHisExcel?lang=CN&startDate=%s&endDate=%s&t=', date_num(Sys.time(), 'ms')), data.table(f=from, t=to))
# loan prime rate
dtlpr = load_read_xl(sprintf('https://www.chinamoney.com.cn/dqs/rest/cm-u-bk-currency/LprHisExcel?lang=CN&strStartDate=%s&strEndDate=%s', data.table(f=from, t=to)$f, to))
setnames(dtlpr, c('date', "cn1ylpr", "cn5ylpr"))
dtlpr = dtlpr[, `:=`(
date = as.Date(date),
cn1ylpr = as.numeric(cn1ylpr),
cn5ylpr = as.numeric(cn5ylpr)
)][!is.na(date)][order(date)]

# policy rate
dtpr = load_data_chinamoney(paste0('https://www.chinamoney.com.cn/dqs/rest/cm-u-bk-currency/SddsIntrRatePlRatHisExcel?lang=CN&startDate=%s&endDate=%s&t=', date_num(Sys.time(), 'ms')), data.table(f=from, t=to))
setnames(dtpr, c('date', 'cn1ydpr', 'cn1ylpr'))
# https://www.chinamoney.com.cn/dqs/rest/cm-u-bk-currency/LprHisExcel?lang=CN&strStartDate=2023-03-24&strEndDate=2024-03-23

setnames(dat, c('date', "cn1ybdr", "cn1yblr"))
dat2 = merge(
data.table(date=seq(dat[,min(date)], Sys.Date(), by=1)),
dat, by = 'date', all.x=TRUE
)[, lapply(.SD, fillna)
]

dat2 = md_moneycn_widelong(dat2)
return(dat2[])
dat = md_moneycn_widelong(rbind(dtlpr, dtpr, fill = TRUE))[!is.na(value)][order(symbol, date)][]
return(dat[])
}

# yields of bond
Expand Down Expand Up @@ -178,9 +165,9 @@ md_cnrmbx = function(date_range = '3y', from=NULL, to=Sys.Date(), ...) {
md_moneycn = function(symbol=NULL, date_range = "3y", from = NULL, to = Sys.Date(), print_step = 1L) {
# syb = intersect(symbol, ibor_symbol$symbol)
if (is.null(symbol)) symbol = select_rows_df(data.table(
symbol = c('rmbx', 'shibor', 'lpr', 'pr', 'bond'),
name = c('RMB Index', 'Shanghai Interbank Offered Rate', 'Loan Prime Rate', 'Policy Deposit/Lending Rate', 'Yields of Gov. Bond'),
type = c('FX Market', 'RMB Market', 'RMB Market', 'Interest Rate', 'Interest Rate')
symbol = c('rmbx', 'shibor', 'lpr', 'bond'),
name = c('RMB Index', 'Shanghai Interbank Offered Rate', 'Loan Prime Rate', 'Yields of Gov. Bond'),
type = c('FX Market', 'RMB Market', 'RMB Market', 'Interest Rate')
), column='symbol')[,symbol]


Expand Down
4 changes: 4 additions & 0 deletions R/pq_plot2.R
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,10 @@ pq_plot = function(
arrange_rowcol_all1 = all(sapply(list('rows', 'cols'), function(x) any(arrange[[x]] == 1)))

dt = check_dt(dt, symb_name = TRUE)
if (!(y %in% names(dt))) {
y = intersect('value', names(dt))
if (length(y)==0) warning("Please specify the column name for y axis.") else warning("The y axis modified to 'value'.")
}
orders = check_odr(orders)

arglst = list(dt=dt, x=x, y=y, yb=yb, date_range=date_range, yaxis_log=yaxis_log, title=title, addti=addti, nsd_lm=nsd_lm, markline=markline, orders=orders, arrange=arrange, theme=theme, ...)
Expand Down
2 changes: 1 addition & 1 deletion man/md_stock_symbol.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5273eb3

Please sign in to comment.