pq_portfolio
calculates the weighted returns or the equity of a portfolio assets.
pq_portfolio(dt, x, orders, dtb = NULL, init_fund = NULL,
method = "arithmetic", cols_keep = NULL, ...)
a list/dataframe of price by asset.
the column name of adjusted asset price.
a data frame of transaction orders, which includes symbol, date, prices, volumes and type columns.
a list/dataframe of price base asset.
initial fund value.
the method to calculate asset returns, the available values include arithmetic and log, defaults to arithmetic.
the columns keep in the return data. The columns of symbol, name and date will always kept if they are exist in the input data.
ignored
library(pedquant)
data(dt_banks)
datadj = md_stock_adjust(dt_banks, adjust = TRUE)
# example I
orders = data.frame(
symbol = c("601288.SS","601328.SS","601398.SS","601939.SS","601988.SS"),
volumes = c(100, 200, 300, 300, 100)
)
dtRa = pq_portfolio(datadj, x='close', orders=orders)
#> Warning: The initial value is setting to 2778.19
e1 = pq_plot(dtRa, y = 'cumreturns')
e1[[1]]
#> NULL
# example II
data(dt_ssec)
orders = data.frame(
symbol = rep(c("601288.SS","601328.SS","601398.SS","601939.SS","601988.SS"), 3),
date = rep(c('2009-03-02', '2010-01-04', '2014-09-01'), each = 5),
volumes = rep(c(100, 200, 300, 300, 100), 3) * rep(c(1, -1, 2), each = 5)
)
dtRab = pq_portfolio(datadj, x='close', orders=orders, dtb = dt_ssec, init_fund = 10000)
e2 = pq_plot(dtRab, y = 'cumreturns', yb = 'cumreturns_000001.SS', addti = list(portfolio=list()))
e2[[1]]
#> NULL
# example III
orders = data.frame(symbol = "000001.SS",
date = c("2009-04-13", "2010-03-24", "2014-08-13", "2015-09-10"),
volumes = c(400, -400, 300, -300))
dtRa2 = pq_portfolio(dt_ssec, x='close', orders=orders, cols_keep = 'all')
#> Warning: The initial value is setting to 666863.1
e3 = pq_plot(dtRa2, y = 'close', addti = list(cumreturns=list(), portfolio=list()))
e3[[1]]
#> NULL