MiRiM – RiskMetrics – v1.1

[This article was first published on Mirai Solutions, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Mirai RiskMetrics (MiRiM) is a Shiny app based on the RiskMetrics methodology enhanced to analyze the market risk of a portfolio.

MiRiM is freely available on our Website Gallery and is designed for desktop view.

In the first release, MiRiM was mainly a PoC serving as an example for the application of the J.P.Morgan/Reuters RiskMetrics methodology to a portfolio of commodities to be provided as a CSV input, where a default portfolio of commodities from LME was initiated at the start of the app. RiskMetrics is a set of tools that enables participants in the financial markets to estimate their exposure to market risk, under what has been called the ‘Value-at-Risk framework’, in portfolios of foreign exchange, fixed income, equity and commodity products.

This MiRiM V.1.1 is a release with extensive changes and comes with the following enhancements:

  • extended documentation and step by step instructions in the ‘Introduction’ page
  • ability to derive a portfolio from a list of historical closing prices taken from Yahoo Finance
    • the choice is restricted to Swiss assets present on Nasdaq
    • the default starting portfolio is composed of the top 5 Swiss assets w.r.t. to the latest transaction volumes
    • the package quantmod is introduced to read share prices from Yahoo Finance
  • the upload option of a CSV file of historical closing prices of a given asset is still available
    • added data quality checks on the uploaded file
  • historical time series and the portfolio are shown in a candlestick plot (if all required information is provided)
  • dynamic charts created with the dygraphs package
  • embellished tables with DT package
  • model output rescaled to the present value of the portfolio.

The core methodology and the model parameters were not changed.

In order to operate with the MiRiM application, a user must first select a portfolio of assets in the ‘Input’ page, either from the provided list of Swiss assets on Nasdaq or by uploading a CSV file of prices with a ‘Date’ column in the specified format.

Assets Inputs

Once the portfolio is selected move to the ‘Parameters and Model’ page, visualize how the assets’ prices and your overall portfolio perform over time, check its value and composition. By assumption, a portfolio (PF) is composed of 5 shares per asset.

Assets time series

Select the time window to be considered and the other parameters for the RiskMetrics model, e.g. the simulation size (small for this prototype).

Model parameters

Run the simulation to estimate what your worst case loss at each time point would be, e.g. with 0.95 or 0.99 probability.

Model results

The output table contains the results at the last time point.

On the R/Shiny side the main updates are related to the introduction of new packages:

quantmod is the standard tool for supporting quantitative traders in their analysis. Simply using the getSymbols function we can read prices from sources like Yahoo Finance, Google, MySQL, FRED, and Oanda (our choice is for Yahoo Finance). The package contains also functionalities for drawing static financial plots like candlestick charts and line plots.

To make the plots interactive and dynamic on the Shiny app, we have used dygraphs for R, an interface to the dygraphs JavaScript library which is quite suitable for dynamic time series plots. dygraph plots react much faster than plotly on the Shiny app when the graphs contain many points and dygraph range selector allows fast zooming on a given time window.

The code below shows how to read a symbol from Yahoo Finance (quantmod) and how to create a candlestick chart with a range selector (dygraph).

quantmod::getSymbols("AAPL", src = "yahoo")
## [1] "AAPL"
dat <- AAPL %>% setNames(gsub("AAPL.", "", names(AAPL)))
datewindow <- c(tail(as.POSIXct(zoo::index(dat)), 365)[1], max(as.POSIXct(zoo::index(dat))))
dygraphs::dygraph(dat[, 1:4], main = "Apple share price over time") %>%
    dygraphs::dyCandlestick() %>%
    dygraphs::dyOptions() %>%
    dygraphs::dyRangeSelector(height = 40, dateWindow = datewindow)

plot of chunk dyplot

The DT package provides convenient features to make static html tables look nicer.

Check the MiRiM app on our website to see the dynamic graphs, which can’t be displayed on this article, and to play with a portfolio of assets of your choice.

Do you have any suggestion for further improvement? Or if you have questions about the implementation, or about the approach of the RiskMetrics model, do not hesitate to contact us. We will be happy to hear from you and to take into consideration the relevant remarks for follow up releases.

To leave a comment for the author, please follow the link and comment on their blog: Mirai Solutions.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)