Index Tracker

Dhruva Reddy,

For context, this project was developed as part of an internship I was doing at the time but I thought it was incredibly useful just as a general tool for exploring financial market data and for custom portfolio generation. Here's a link (opens in a new tab) to the GitHub repo where I have my source code stored. I worked on this with my friend and colleague whose name I omitted for the sake of their privacy.

Inspiration

This project was heavily inspired by something I came across online from another GitHub repo by @pmaji (opens in a new tab). In an effort to recreate it I dove down the rabbit hole of R and RShiny's library of packages and functions.

Difficulties Encountered

At the time I was working on this, I think the R ecosystem was very different from what it is now. I remember trying to start up my code again a few months ago and having to update a lot of the packages because they were basically discontinued libraries. A couple of the packages that we referenced for this are still actively maintained and being developed.

  1. Cleaning Data

    This was a surprisingly difficult task. We actually ended up doing this manually because we simply couldn't figure out a way to

    My colleague and I grinded this out manually in excel where we used if-else statements to mark out gaps between indexes in the csv and remove those points across all the 11 indexes to standardize the number of data points in each index.

  2. Understanding data types in R

    I think for new users of R, the data types can be a little confusing. I remember first starting up the app and thinking I could do everything I wanted with the raw csv files. While that can be true (I think?), for the types of plotting we wanted to do, using the xts package and the zoo data type was the best way to go. This is essentially a wrapper object for time-series data that forces you to standardize your data in such a way that common library functions can be applied to it to get the plots that you need

  3. Deploying to RShiny

    RShiny is a wonderful library for building static web apps with R code. But it comes with its own set of difficulties. For instance, while it has some stylistic similarities to HTML, it is not HTML. So you can't just copy and paste HTML code to render the site the way you want.

    RShiny apps (in general) consist of 3 main components:

    1. ui.R - This is where you define the layout of your app.
    2. server.R - This is where you define the logic of your app.
    3. app.R - This is where you define any global variables that you want to

    One can almost liken it to the MVC framework where we have a 'model' handling the data, a 'view' handling the layout and a 'controller' handling the logic. The app.R file is the 'controller' in this case, the ui.R file is the 'view' and the server.R file is the 'model'.

    Having a basic understanding of this will help you understand how to format your code and where calls are being made to functions and variables.

Demo

Here are some screenshots of interesting features we implemented in this portfolio tracker.

  1. Efficient Frontier

    This is a well documented plot in finance literature but essentially it is a plot of the maximal return you can achieve given a certain level of risk. The idea is that you want to be on the higher of the 2 intercepts of the y = (some risk level) line and the frontier. The faint black line on the chart represents your indicated target volatility. For the best risk-adjusted return, you want to be somewhere in the deep purple zone which indicates a higher sharpe ratio (opens in a new tab).

    If you notice, our web app allows you to select the number of indices you want to include in your portfolio and declare your risk tolerance. The app will then compute the efficient market parabola given an equally weighted portfolio of these indices demo1

  2. Maximal Drawdown

    The following image shows the maximal drawdown of any 2 indices over a specified time window. It allows investors to visually observe if any indices have been more strongly correlated during periods of downturn and if they should be included in the same portfolio if diversification is the aim.

    demo2

  3. Custom Portfolio Generator

    This is kind of like a sandbox where you can play around with different combinations of indices and their weights in your own custom portfolio and track what their historical return would've been. While the past is not necessarily indicative of the future, it's just a fun tool for you to test certain theories or strategies.

    Imgur

Overall, this was a really fun project to do and I learned a lot about R and RShiny through this. Would I be do something like this again? Unlikely. But I think it's a good example of just trying something new and picking up some useful skills along the way.

As always I'm always open to feedback, comments or questions!