Weekend Coding, Shiny Edition

I’ve been using R for over 5 years now, mostly generating reports and performing analysis on financial data from stocks, bonds, currencies to modelling portfolios and using various functions to determine statistics for risk, etc. I recently as of last year started taking some of the output that I would normally slap in a PDF or other document and ship out to people and making it available on the web in realtime using a new server called Shiny. You can learn more about how to get started with Shiny at this link.

Shiny running on top of underlying R functions now powers the ATYS Bonds Auctions app which displays much of the same information that was once only available in a PDF document that I would have to manually generate charts and assemble them while automatically populating statistics data in  S/Word. While I still produce that same document with much less frequency (you can view one such document for US auctions here), I focused most of my energy on using Shiny to bring that same data and charts to people in near realtime (for US auctions, it’s usually within the first 5 minutes of the auction results being publically available). That effort has been met with resounding success, as people from across the internet now use that app almost everyday (thanks to Stocktwits for raising awareness of the app). I am planning to take my development “lessons learned” and begin revamping the app to make it more responsive to clients (currently we are wasting a lot of computing power on processing ALL auctions and generating charts/stats before even displaying them to the user).

Value At Risk

I created a simple app in order to give me a quick snapshot of some key metrics for a individual security, mostly using the daily return timeseries to generate Value At Risk and some visualizations that I hope will enable me to determine seasonal trends. Here’s a snapshot of the app generating daily return bars with a daily VaR overlay for the S&P 500 index for the past 2 years:



Here’s another chart that is generated showing the VaR sensitivity for the same series/data spread across varying confidence intervals:



Before Shiny, I would generate all of these by hand and save them to various file types (PNG, PDF, etc.) and then take a look at them when I had more time. It’s amazing that now typically within seconds that I have this and more at hand which saves a lot of time. You can see the beta version of this app here.

Portfolio Analytics

Ever wonder how to optimize a portfolio that you own? Using the ReturnAnalytics set of packages (PortfolioAnalytics, PerformanceAnalytics) and fPortfolio package, I started out working on an idea that would show me optimal portfolio allocations based on various objectives. fPortfolio is part of the R/RMetrics set of packages developed by Diethelm Wuertz and others and you can learn how to start using the functions in the package by reading this sample of their e-book Portfolio Optimization with R/RMetrics.

This new shiny app uses various functions contained in the packages mentioned earlier to visualize your portfolio and show optimal weightings that one could use when figuring out how to rebalance your portfolio. A lot of the math is covered in this paper at SSRN: Asset Allocation With Conditional Value At Risk Budgets which is written in part by the same people who are responsible for the ReturnAnalytics packages that are used in R. Here’s the output of the shiny app which is running:

Efficient Portfolio Frontier

Efficient Portfolio Frontier

Another snapshot showing portfolio weightings:


Portfolio Weightings

Portfolio Weightings

And another screenshot that contains basic summary information for the optimized portfolio:

Portfolio Summary

Portfolio Summary

This app is self contained at the moment and uses pre-supplied data files and is meant as a introduction and means to guide me as I continue to build out a custom solution that I can use for myself in optimizing portfolios, whether it be a portfolio of single name securities, ETF’s or optimizing holdings of various funds (i.e. Fund Of Funds, etc.)

The public code that you can run via Shiny is at my github gist which is here: https://gist.github.com/chibondking/5606460

To run the gist, all you need to do is:



Created by Pretty R at inside-R.org

You can also access the same app which is running on this site here

%d bloggers like this: