| Title: | Manage Data from the Finance Markets |
|---|---|
| Description: | A set of tools designed to perform descriptive data analysis on assets, manage asset portfolios and capital allocation, and download, organize, and maintain data from the "Tehran Stock Exchange" and "NOBITEX" platforms. |
| Authors: | Ali Saeb [aut, cre] |
| Maintainer: | Ali Saeb <[email protected]> |
| License: | BSD_2_clause + file LICENSE |
| Version: | 1.0.0 |
| Built: | 2026-06-07 07:52:04 UTC |
| Source: | https://github.com/cran/TSEtools |
Cancel a group of orders based on currency name or record time.
cancel(src, dst, hrs = 0, token)cancel(src, dst, hrs = 0, token)
src |
Source currency name ("btc", "eth", "bnb", etc). |
dst |
Destination currency name ("usdt" or "rls"). |
hrs |
The number of hours after which orders will be canceled. If the value is less than one hour, it is specified as a decimal (e.g., 15 minutes is given by |
token |
The token is string value obtain from user ID settings in the NOBITEX. If you are not registered on the website, please use the provided link. |
## Not run: tkn <- "CONTACT WITH AUTHOR" # All orders recorded more than one hour ago are canceled. cancel(" ", " ", hrs = 1, token = tkn) ## End(Not run)## Not run: tkn <- "CONTACT WITH AUTHOR" # All orders recorded more than one hour ago are canceled. cancel(" ", " ", hrs = 1, token = tkn) ## End(Not run)
Compute the capital assets pricing model including a risk-free asset.
capm(x, Rf = 0.2/270, sh = FALSE, eRtn = NULL)capm(x, Rf = 0.2/270, sh = FALSE, eRtn = NULL)
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. |
Let be random asset returns and the portfolio weights. The expected returns are In addition
to these risky investments, there is a risk-free asset (a bond or bank account)
available, which has return . Denoting the weights of for the risk-free
asset. The return of portfolio given by
where, .
Risk is measure by a deviation functional . It is a variance-covariance of asset returns. The risk-free component ignore in the objective. So, the standard deviation of portfolio is given by
To obtain the optimum value of we solve the following model:
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 ,
where, is return of market portfolio and is the beta coefficient of the portfolio . It is given by
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 |
Pflug and Romisch (2007, ISBN: 9789812707406)
## 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)## 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)
Get a cumulative volumes corresponding to the bid and ask's price
depth(src)depth(src)
src |
List of the some crypto curencies which available in the function. |
The values return as a data frame format. The names of the dimensions are
prc.ask: last 75 prices of the asks
vol.ask: a list of last 75 cumulative volumes corresponding to the ask's price.
prc.bid: last 75 prices of the bids
vol.bid: a list of last 75 cumulative volumes corresponding to the bid's price.
# Download daily data from last one month ## Not run: depth("BTCUSDT") ## End(Not run)# Download daily data from last one month ## Not run: depth("BTCUSDT") ## End(Not run)
Download the history data of the crypto currencies from the Nobitex (website of "nobitex.ir").
getNBTX(src,frame,from,to)getNBTX(src,frame,from,to)
src |
List of the some crypto curencies which available in the function. |
frame |
A character value shows the data frame. The frame is defined as |
from |
"2025-12-23 13:13:57 +0330"
The character string in the date format ("%Y-%m-%d %H:%M:%S") shows the start date of the time frame. Since, the data is time series format, the rules of |
to |
The character string in the date format ("%Y-%m-%d %H:%M:%S") shows the end date of the time frame. Since, the data is time series format, the rules of |
A symbol of the crypto currency can be named and the values will be assigned the variable. Component data is an array with xts time series object. The names for dimensions are: Open, High, Low, Close, Volume. The value of symbol are appear in Global Environment.
The output is an object of the “cryto currency” assigned the variable's name.
## Not run: # Download daily data from last one month x <- getNBTX("BTCUSDT", frame = "D", from = paste0(Sys.Date()-30, substr(Sys.time(), 11, 19)) , to=Sys.time()) ## End(Not run)## Not run: # Download daily data from last one month x <- getNBTX("BTCUSDT", frame = "D", from = paste0(Sys.Date()-30, substr(Sys.time(), 11, 19)) , to=Sys.time()) ## End(Not run)
getTSE function is an easy way to download and organize the historical dataset from websites of TSE (website of "tsetmc"). 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.
getTSE(file, symbols=NA)getTSE(file, symbols=NA)
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 |
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.
The output is an object of the “assets” name which get all the name of assets list to download.
## Not run: fpath<-system.file("extdata", "sample.dat", package="TSEtools") getTSE(fpath) ## End(Not run)## Not run: fpath<-system.file("extdata", "sample.dat", package="TSEtools") getTSE(fpath) ## End(Not run)
Shows a list of daily profit/loss with percentage and balance for the latest week or month.
lastPrft(period = "weekly", token)lastPrft(period = "weekly", token)
period |
Shows a list of daily profit/loss for a week or month using |
token |
The token is string value obtain from user ID settings in the NOBITEX. If you are not registered on the website, please use the provided link. |
report_date |
Date index of the data frame. |
total_profit |
Total daily profit/loss value. |
total_profit_percentage |
Total daily profit/loss percentage. |
total_balance |
Total balance at the end of the day. |
## Not run: tkn <- "CONTACT WITH AUTHOR" lastPrft("monthly", token = tkn) ## End(Not run)## Not run: tkn <- "CONTACT WITH AUTHOR" lastPrft("monthly", token = tkn) ## End(Not run)
This function lists all completed trades.
lst(n=500, token)lst(n=500, token)
n |
An integer value specifying the number of trades to display, with a maximum of 500. |
token |
The token is string value obtain from user ID settings in the NOBITEX. If you are not registered on the website, please use the provided link. |
srcCurrency |
Source name of the cryptocurrency |
dstCurrency |
Destination cryptocurrency; values are Tether or Rials. |
timestamp |
Trade date and time. |
market |
Market names |
price |
Trade price. |
amount |
Trade amount |
total |
Total trade value. |
type |
Trade type (buy or sell). |
fee |
Trade fee. |
id |
Trade id. |
orderId |
Trade order ID. |
## Not run: tkn <- "CONTACT WITH AUTHOR" x <- lst(n = 500, token = tkn) ## End(Not run)## Not run: tkn <- "CONTACT WITH AUTHOR" x <- lst(n = 500, token = tkn) ## End(Not run)
Obtain information about the most recent trades conducted in the cryptocurrency market. This data can provide insights into the market trends and help investors make informed decisions.
marketInfo(src ,dst)marketInfo(src ,dst)
src |
List of the some crypto curencies which available in the function. |
dst |
The destination currency can be chosen in two values: 'rls' (Rial) or 'usdt' (US. Dollar). |
The values return as format of a data frame. The colnames of the dimensions are
isClosed: Logit value. TRUE is market close and FALSE is market Open
bestSell: The first line price of bid list
bestBuy: The first line price of ask list
volumeSrc: The volume of the source (src) currency traded.
volumeDst: The volume of the distination (dst) currency traded.
latest: The last price traded.
mark: The mark price is a calculated by the bestSell, bestBuy and latest price.
dayLow: Lowest price in the day
dayHigh: Highes price in the day
dayOpen: Open price in the day
dayClose: Close price in the day
dayChange: Changes price in a day
## Not run: # Download daily data from last one month marketInfo("btc","rls") ## End(Not run)## Not run: # Download daily data from last one month marketInfo("btc","rls") ## End(Not run)
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.
mcPrt(asset, sub = "::", pstvRtn = FALSE, pr = "daily", Rf = 0.0)mcPrt(asset, sub = "::", pstvRtn = FALSE, pr = "daily", Rf = 0.0)
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 |
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. |
The values of function contain Sharpe ratio and rate of return. Sharpe ratio is defined as:
where is the mean asset return, is the risk free rate of return and is the standard deviation (volatility) of the asset.
Let is the closing price of the asset at time and its value after a period of length , say . The return over that period is defined as:
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.
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.
Carmona (2003, ISBN:0387202862)
## 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)## 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)
This function is used to submit buy and sell orders on the Nobitex website
orderADD(typ, src, dst, amnt, prc, stp = NULL, stpL = NULL, exe = "limit", token)orderADD(typ, src, dst, amnt, prc, stp = NULL, stpL = NULL, exe = "limit", token)
typ |
|
src |
Cryptocurrency sources are defined by lowercase names in the function (e.g., "btc", "eth", "bnb", "paxg"). |
dst |
For the Rials or USD system, the destination currency must be set to either |
amnt |
Amount of cryptocurrency to trade |
prc |
Price of cryptocurrency to trade. The trade price ("prc") depends on the exe's option. |
stp |
If using stop-loss or OCO, define the stop price here. Default is NULL.
When the market price reaches "stp", "prc" is activated and recorded in the order list.
For a "buy" position, "stp" must be greater than "prc"; for a "sell"" position, "stp" must be less than |
stpL |
If using "oco" then stop limit price must be defined. Defualt is NULL. While "stp" is active, "stpl" is recorded as the trade price; otherwise, "prc" is used. |
exe |
Three common ways to configure a trade: regular trading, stop-loss trading, or One-Cancels-the-Other (OCO) trading. |
token |
The token is string value obtain from user ID settings in the NOBITEX. If you are not registered on the website, please use the provided link. |
Returns a list: "ok" if the order is recorded successfully on the website; otherwise, "failed".
## Not run: tkn <- "CONTACT WITH AUTHOR" x <- orderADD(typ = "sell", src = "usdt", dst = "rls", amnt = 5, prc = 1600000, + exe = "limit", token = tkn) x <- orderADD(typ = "sell", src = "usdt", dst = "rls", amnt = 5, prc = 1400000, + stp = 1300000, exe = "stop_limit", token = tkn) x <- orderADD(typ = "sell", src = "usdt", dst = "rls", amnt = 5, prc = 1400000, + stp = 1300000, stpL = 1350000, exe = "oco", token = tkn) ## End(Not run)## Not run: tkn <- "CONTACT WITH AUTHOR" x <- orderADD(typ = "sell", src = "usdt", dst = "rls", amnt = 5, prc = 1600000, + exe = "limit", token = tkn) x <- orderADD(typ = "sell", src = "usdt", dst = "rls", amnt = 5, prc = 1400000, + stp = 1300000, exe = "stop_limit", token = tkn) x <- orderADD(typ = "sell", src = "usdt", dst = "rls", amnt = 5, prc = 1400000, + stp = 1300000, stpL = 1350000, exe = "oco", token = tkn) ## End(Not run)
Get the bid and ask prices with the volumes in orders
orderBook(src)orderBook(src)
src |
List of the some crypto curencies which available in the function. |
The values return as a data frame format. The names of the dimensions areprc.ask: last 24 prices of the asksvol.ask: a list of last 24 volumes corresponding to the ask's price.prc.bid: last 24 prices of the bidsvol.bid: a list of last 24 volumes corresponding to the bid's price.lst.prc: last trade's price
## Not run: # Download bid and ask market price orderBook("BTCUSDT") ## End(Not run)## Not run: # Download bid and ask market price orderBook("BTCUSDT") ## End(Not run)
The order list shows all registered orders waiting to be traded.
orderList(token)orderList(token)
token |
The token is string value obtain from user ID settings in the NOBITEX. If you are not registered on the website, please use the provided link. |
The value returned:
type |
Order type: "sell" or "buy". |
execution |
Order execution type can be "Limit", "Market", "StopMarket", or "StopLimit" |
tradeType |
Order trade type is "Spot" or "Margin" |
srcCurrency |
Source currency name ("Bitcoin", "Tether", etc). |
dstCurrency |
Destination currency name (USDT or RLS). |
price |
Order price |
amount |
Order amount |
totalOrderPrice |
Total order price. |
matchedAmount |
Matched amount. |
unmatchedAmount |
Remaining amount for trade. |
id |
Id number of order. |
status |
Order status: New, "Active", "Inactive", "Done", or "Canceled". |
fee |
Order fee. |
created_at |
Created time of order. |
market |
Market name of order. |
## Not run: tkn <- "CONTACT WITH AUTHOR" x <- orderList(token = tkn) ## End(Not run)## Not run: tkn <- "CONTACT WITH AUTHOR" x <- orderList(token = tkn) ## End(Not run)
Compute the efficient frontier function for some selected risk functionals in a portfolio optimization setting.
prtf (x, Rf = 0.0, sh = FALSE ,eRtn = NULL)prtf (x, Rf = 0.0, sh = FALSE ,eRtn = NULL)
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. |
Let be random asset returns and the portfolio
weights. The expected returns are In addition
to these risky investments, there is a risk-free asset (a bond or bank account)
available, which has return . Denoting the weights of for the risk-free
asset. The return of portfolio given by
where, .
Risk is measure by a deviation functional . It is a variance-covariance of asset returns. The risk-free component ignore in the objective. So, the standard deviation of portfolio is given by
To obtain the optimum value of we solve the following model:
where, is a constant value.
Note that, the portfolio weights may be negative (selling short is allowed).
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 |
Pflug and Romisch (2007, ISBN: 9789812707406)
portfolio.optimization, portfolio.optim
## 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)## 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)
Obtain information about the most recent trades conducted in the cryptocurrency market. This data can provide insights into the market trends and help investors make informed decisions.
tradeInfo(src)tradeInfo(src)
src |
List of the some crypto curencies which available in the function. |
The values return as a data frame format. The names of the dimensions are
price: List the prices at which the trade was done.
volume: List the volume corresponding the price at which the trade was done.
type: Type of trade: 'sell' means selecting the price from the bid, and 'buy' refers to the ask price from which you choose.
# Download daily data from last one month ## Not run: tradeInfo("BTCUSDT") ## End(Not run)# Download daily data from last one month ## Not run: tradeInfo("BTCUSDT") ## End(Not run)
Cancel the specified order using the order ID.
updateStatus(order, st = "canceled", token)updateStatus(order, st = "canceled", token)
order |
The order ID can be obtained by using the order list function. |
st |
By default, the specified order is canceled. |
token |
The token is string value obtain from user ID settings in the NOBITEX. If you are not registered on the website, please use the provided link. |
## Not run: tkn <- "CONTACT WITH AUTHOR" x <- orderList(token = tkn) # Ex. consider the first order recorded, ID <- x[1]$id # SELECT SPECIFY ID order from the list updateStatus( ID, st = "canceled", token = tkn) ## End(Not run)## Not run: tkn <- "CONTACT WITH AUTHOR" x <- orderList(token = tkn) # Ex. consider the first order recorded, ID <- x[1]$id # SELECT SPECIFY ID order from the list updateStatus( ID, st = "canceled", token = tkn) ## End(Not run)
The wallet list includes the balance and blocked value of each asset.
walletLST(token)walletLST(token)
token |
The token is string value obtain from user ID settings in the NOBITEX. If you are not registered on the website, please use the provided link. |
id |
Cryptocurrency name and ID. |
balance |
The balance value is available for orders. |
blocked |
The blocked value represents funds locked in orders. |
## Not run: tkn <- "CONTACT WITH AUTHOR" x <- walletLST(token = tkn) ## End(Not run)## Not run: tkn <- "CONTACT WITH AUTHOR" x <- walletLST(token = tkn) ## End(Not run)