Package 'TSEtools'

Title: Manage Data from Stock Exchange Markets
Description: Tools to perform some descriptive data analysis for assets. Manage the portfolio and capital of assets. It also downloads and organizes data from the Tehran Stock Exchange (TSE).
Authors: Ali Saeb
Maintainer: Ali Saeb <[email protected]>
License: BSD_2_clause + file LICENSE
Version: 0.2.2
Built: 2025-02-09 02:43:50 UTC
Source: https://github.com/cran/TSEtools

Help Index


Capital assets pricing model including a risk-free asset.

Description

Compute the capital assets pricing model including a risk-free asset.

Usage

capm(x, Rf = 0.2/270, sh = FALSE, eRtn = NULL)

Arguments

x

a numeric matrix of random returns per unit of price within some holding period.

Rf

the return of the risk free, i.e. has variance 0.

sh

a logical indicating whether shortsales on the risky securities are allowed. Default is FALSE.

eRtn

a value of expected returen of portofilo. The mean of whole data defualt.

Details

Let ξ1,,ξn\xi_1 , \ldots,\xi_n be random asset returns and w1,,wnw_1 , \ldots, w_n the portfolio weights. The expected returns are rm=Eξm,m=1,,n.r_m = E\xi_m , m = 1, \ldots, n. In addition to these risky investments, there is a risk-free asset (a bond or bank account) available, which has return r0r_0. Denoting the weights of w0w_0 for the risk-free asset. The return of portfolio given by

Rp=wtrR_p = w^t r

where, r=(r1,,rn)tr = (r_1, \ldots, r_n)^t.

Risk is measure by a deviation functional Σ\Sigma. It is a variance-covariance of asset returns. The risk-free component w0w_0 ignore in the objective. So, the standard deviation of portfolio is given by σp=wtΣw.\sigma_p = w^t \Sigma w.

To obtain the optimum value of wi,i=1,,n,w_i, i = 1,\ldots, n, we solve the following model:

minwtΣw,    s.t:    wtr+w0r0>μ    and    wi+w0=1\min w^t \Sigma w,\;\;s.t:\;\; w^t r + w_0 r_0 > \mu \;\; and \;\;\sum w_i + w_0 = 1

Note that, the portfolio weights may be negative (selling short is allowed). Market portfolio is named MP where, the risk free weight w_0 is zero (see, the function of prtf()).

For any portfolio pp,

E(Rp)=r0+β(p)(rMPr0)E(R_p) = r_0 + \beta(p) (r_{MP} - r_0)

where, rMPr_{MP} is return of market portfolio and β(p)\beta(p) is the beta coefficient of the portfolio pp. It is given by β(p)=Cov(rMP,rp)/SD(rMP).\beta(p) = Cov( r_{MP}, r_p )/ SD(r_{MP}).

Value

wCAPM

weight of CAPM assets

wrF

weight of risk free assets

sd.capm

volatility of CAPM portfolio

rtn.capm

return of CAPM portfolio

beta

beta coefficient of portfolio

References

Pflug and Romisch (2007, ISBN: 9789812707406)

Examples

## Not run: 
x <- rnorm(500,0.05,0.02)
y <- rnorm(500,0.01,0.03)
z<-cbind(x, y)
colnames(z) <- c("prt1","prt2")

capm( z, sh = FALSE, Rf= 0.2/270, eRtn=0.02 )

## End(Not run)

Download Historical Dataset from Tehran Stock Exchange (TSE)

Description

getTSE function is an easy way to download and organize the historical dataset from websites of TSE and www.tsetmc.com. Since, the raw data are not recorded in a standard format, we provide the function for organize the data in user friendly way. Daily data updated after 13:30 UTC. The market is closed on Friday and Wensday and national holidays.

Usage

getTSE(file, symbols=NA)

Arguments

file

the path and name of external file which include the name of symbol and company and a specific code.

symbols

a vector of character given the name of assets to download. The name of assets has to be included in the first column of file. The default value is NA which consider all assets at source file to download.

Details

The list of symbols or assets can be named, and the list names will be used as names for the variables. Component data is an array with xts time series object. The name for dimensions are: Open, High, Low, Close, Volume, Last. The value of symbols are appear in Global Environment.

Value

The output is an object of the “assets” name which get all the name of assets list to download.

Examples

## Not run: 
	fpath<-system.file("extdata", "sample.dat", package="TSEtools")
	getTSE(fpath)
	
## End(Not run)

Matrix Conversion in Portfolio

Description

mcPrt function compound the vector of assets in matrix form to using the multivariate data analysis. The assets must be included the items of Open, Close, High, Low and Volumn. The function also calculate the rate of return and Sharpe ratio for portfolio selection.

Usage

mcPrt(asset, sub = "::", pstvRtn = FALSE, pr = "daily", Rf = 0.0)

Arguments

asset

a character vector of symbols.

sub

The character string in the form of "start date :: end date". The date format is "%Y-%M-%D". Since, the data is time series format, the rules of xts satisfied.

pstvRtn

logical flag to determine, if the symbols with mean of return (see, details) should be positive/negative. Missing value is taken as false. By setting pstvRtn=TRUE, only a check the symbols that the mean of return is not negative and symbols with negative returns will be vanished from the list of calculation.

pr

a character specifying the desired period time of return. Default value is daily. The values get "daily", "weekly" and "monthly".

Rf

an integer value to give risk free.

Details

The values of function contain Sharpe ratio and rate of return. Sharpe ratio is defined as:

SR=RˉRfσRSR=\frac{\bar{R}-R_f}{\sigma_R}

where Rˉ\bar{R} is the mean asset return, RfR_f is the risk free rate of return and σR\sigma_R is the standard deviation (volatility) of the asset.

Let XtX_t is the closing price of the asset at time tt and its value after a period of length Δt\Delta t, say Xt+ΔtX_{t+\Delta t}. The return over that period is defined as:

Rt=Xt+ΔtXt1R_t=\frac{X_{t+\Delta t}}{X_t}-1

Note that the return equation is equivalent to log return asset when the ratio value of the asset is close to 1 (see, Carmona 2003). To check the goodness of fit test on return value, maximum and minimum of prices, suggested to use the package of gnFit.

Value

The values of function involve close, return, max and min as a matrix and xts format. The columns are a value of assets which is ordered by date. The mean return, volatility and Sharpe ratio are assigned in the out. The plot of return vs volatility and also the graph of Sharpe ratio are outputs of function.

References

Carmona (2003, ISBN:0387202862)

Examples

## Not run: 
fpath<-system.file("extdata", "sample.dat", package="TSEtools")
getTSE(fpath)
rtn<-mcPrt(asset0, sub="2016::", pstvRtn=TRUE, pr="weekly")$return

## End(Not run)

Design the Portfolio of assets

Description

Compute the efficient frontier function for some selected risk functionals in a portfolio optimization setting.

Usage

prtf (x, Rf = 0.0, sh = FALSE ,eRtn = NULL)

Arguments

x

a numeric matrix of random returns per unit of price within some holding period.

sh

a logical indicating whether shortsales on the risky securities are allowed. Default is FALSE.

Rf

the return of the risk free, i.e. has variance 0.

eRtn

a value of expected returen of portofilo. The mean of whole data defualt.

Details

Let ξ1,,ξn\xi_1 , \ldots,\xi_n be random asset returns and w1,,wnw_1 , \ldots, w_n the portfolio weights. The expected returns are rm=Eξm,m=1,,n.r_m = E\xi_m , m = 1, \ldots, n. In addition to these risky investments, there is a risk-free asset (a bond or bank account) available, which has return r0r_0. Denoting the weights of w0w_0 for the risk-free asset. The return of portfolio given by

Rp=wtrR_p = w^t r

where, r=(r1,,rn)tr = ( r_1, \ldots, r_n)^t.

Risk is measure by a deviation functional Σ\Sigma. It is a variance-covariance of asset returns. The risk-free component w0w_0 ignore in the objective. So, the standard deviation of portfolio is given by σp=wtΣw.\sigma_p = w^t \Sigma w.

To obtain the optimum value of wi,i=1,,n,w_i, i=1,\ldots, n, we solve the following model:

minwtΣw    s.t:    wtr>μ    and    wi=1.\min w^t \Sigma w\;\; s.t:\;\;w^t r > \mu \;\; and\;\; \sum w_i = 1.

where, μ\mu is a constant value. Note that, the portfolio weights may be negative (selling short is allowed).

Value

The minimum weights show with MIN which is the portfolio with the minimum volatility. Market portfolio is given by MP where, the risk free weight w_0 is zero. MP is the tangency point between the market line and efficient frountier curve. A list containing the following components:

prt

list the name of assests in the portfolio

obs.p

return and volatiliy of overall portfolio

vol

volatility of portfolio

rtn

return of portfolio

w

weigths of portfolio

References

Pflug and Romisch (2007, ISBN: 9789812707406)

See Also

portfolio.optimization, portfolio.optim

Examples

## Not run: 
x <- rnorm(500,0.05,0.02)
y <- rnorm(500,0.01,0.03)
z<-cbind(x, y)
colnames(z) <- c("prt1","prt2")

prtf (z, sh = FALSE)

## End(Not run)