A trading firm in Chicago contacted us at the end of 2017 about development of a series Shiny applications intended to:
- track forward rate movements in a series of combinations of futures contracts, highlighting large changes and graphing time series and price histograms
- track live positions of individual traders at the firm, aggregating numbers across multiple contracts into individual and firm totals, assisting with daily accounting
- allow construction of any given combination of futures contracts, calculating forward rates from settlement, live midpoint, and custom input rates
- automate daily data processing to calculate fees, current positions, and better inform decision-making for the firm.
Forward Rate Tracker
The client had already begun work on a Shiny app to calculate and display price tables, using an Excel template as a guide.
You’ll notice a lot of Excel replication here – one of the broader goals of this set of projects was to move analyses out of Excel and into the wide world of R, where it’s easier to develop custom formulas/functions, track and debug dependent calculations, and create web-based interfaces.
We first extended the table template to twelve different combinations (eight pictured below).
After the first tab was working, we added another tab that tracked movements live during the day, checking in with a database of prices every few seconds.
It doesn’t matter what the interface looks like – live updates make everything look cool.
Next we added tabs that replicated the live table for each combination, but added a time series plot with a moving average, along with a histogram of prices from the time series window.
So why basic ggplot2 graphics instead of something fancy like Plotly? First, for simplicity – if the goal is to visualize quickly but not need any of Plotly’s slick add-ons like tooltips, zooming, and png export, then why add the overhead and dependence on another library? We can always upgrade later if those features become necessary.
Trader/Firm Position Tracker
The next step was replicating an Excel-based interface that calculated daily beginning and live positions for each trader, aggregating to a total by trader, and then summing the results across all traders to produce firm-wide position totals.
Here’s a screenshot with fake names inserted. Looks a lot like Excel, right? Thanks to using custom CSS with
dataTableOutput, it was pretty easy to get this to work, and we learned a lot about
nth-child CSS selectors too.
Another goal was to build a tool that would allow the user to select from a list of existing futures contracts to create a custom combination. This tool also allows the user to choose from three different rates: the previous day’s settlement, the live midpoint, or manually-entered custom rates, allowing exploration of never-ending what-if scenarios.
One of the cool additions to this app is a triangular table showing forward rates between every combination of two contracts. Obviously this would be near impossible with three or more contracts – doesn’t mean we didn’t think about it, though :).
When on live midpoint mode, the table updates every few seconds. This is a lot of math to repeat so frequently, so we’re exploring a way to minimize the load of updating.
So why Shiny? I think the reasons were pretty clear:
- The client had already begun development on several analysis scripts in R, and expressed an interest in moving away from Excel and into R.
- Shiny’s rapid development cycle from idea/script into a functional user interface makes it hard to argue against, especially for prototyping an application layout and data flow.
- There wasn’t an immediate need to think about scaling to a huge number of users or amount of data, where something like OpenCPU might make more sense.
- The appropriate database client is well-supported in R (this is true of other languages/frameworks too, but I mention it because it wasn’t a barrier).
So far the client has been really pleased with the results, and we’re planning a next round of moving tools from Excel into Shiny.
If this sounds like a good plan for your firm, whether financial or not, please don’t hesitate to send us a message on the contact page.