
Nima Safaian | Rpanda trading simulation - from an idea to a multi-user shiny app | RStudio (2020)
The idea of rpanda commodities trading simulation was many years in the making. As energy trading professionals working in the industry, we had developed insights around how to make risk/reward market calls, and what skills make someone an exceptional commodities trader. Traders are one of the most expensive seats in terms of monetizing value from the assets. We developed rpanda as a simulated environment which replicates closely how real-life physical commodities trading works in order to assist talent development and selection, both in academics and enterprise. My co-founder and I did not know how to design production-ready software, but we always had used R/Shiny for market analysis in our corporate jobs. Rather than hiring expensive app developers, we decided to do it ourselves. We used Rstudio development stack such as Rstudio Connect and open source tools, like plumber to turn our idea into a production-ready app that is used by University of Alberta classes. In this presentation, we share our journey, technical challenges, and how we overcame them
image: thumbnail.jpg
Transcript#
This transcript was generated automatically and may contain errors.
I'm wondering, is this on? Oh, perfect. Yeah. Great. No, thank you very much. Thanks for the intro. And I have to admit, I feel quite inadequate with my slide decks after watching everybody else's. However, if you want tips around how to have two columns in a slide IO, I have a solution for you. So anyways, I just want to basically start by saying, I don't know if you guys attended the keynote speech by JJ. Wasn't that the most awesome keynote speech? I was getting teary-eyed. I don't know if it was me or not. It was one of those things that you're trying to hide, like cleaning your eyes. So anyways, my name is Nima Safaian. So Rpanda Trading Solution is a simulator for commodity asset optimization.
So a little bit about us. When I was preparing for this talk, I don't know to what extent you can see our pictures there. But when I was getting ready for this talk, I had a bit of a dilemma. Do I get technical talk about our solutions or talk about the business that we're in, in general, and kind of tell you about our idea and the concept? So the tip I got from a good friend was that, no, people here, they know about R. They know about Shiny. Spend your time talking about the business you're in and talk about the solution that you're providing. So I'm going to get into it. This is a side project for us. However, we have registered it as a company in Kelowna, BC. So if you're familiar where Kelowna is, it's actually not that far from here. That's almost like British Columbia's answer to Napa Valley. I mean, it's not as nice, but it's good for us. And we registered the company there and it was kind of over a couple of bottles of wine that me and my business partner basically decided that we need to get this going.
Background in energy trading
So a little bit about us. So my partner, he has built and expanded a number of commodity trading groups, working for BP Energy Trading and then working for a bank in Canada. And he has done it globally and also in Canada. I'm currently running a market analytics team for a company called Synovus Energy. So that's kind of my day job. So within that day job, we deal like in energy business. I don't know how much in this room you're familiar with that. We deal with quite a bit of complexity. So this is a leaflet map, if I can get it to work, of North American oil infrastructure. So the dots you see, the black dots you see there are refineries that are dotted in various sort of what's called pads in US. And these are districts that have a historical reason for energy security and defense. But however, that's a way of kind of looking at the market. And then what you see in terms of green are all the oil pipelines that bring oil from production basins in Canada and in Texas to where the refineries are.
So this is only oil infrastructure. I mean, if I overlaid, for example, natural gas here, it would have been a quite a complicated sort of map and it would have not been that useful. But what I want to highlight here is energy business is a complex business. It's both complex from a sort of systematic perspective because it has all sorts of moving parts, various assets, different locations, but also it's dynamically complex because it's exposed to a lot of exogenous shocks. For example, you know, right now what's topical is coronavirus. So coronavirus in Asia has had a significant impact on perception of change in jet fuel use because the idea is that, you know, you're basically, you know, you're not going to, people are not going to fly as much. So that has a cascading effect on oil demand, on refinery utilization. And given that all of these energies are interconnected, that has an impact on an overall sort of stack of energies.
So over the years to kind of working in a trade shop that actively trades these assets and energies, you kind of realize that, you know, tools like Excel wouldn't cut it anymore. So you have to kind of go for something a little bit more advanced. So I mean, since I think 2012 when I got introduced to R, I've been using that and deploying that in my workplace to effectively break down complexity, take a complex problem, albeit in a trading analytics context, use visualizations, use clustering, use machine learning to create simple models that we can basically use in order to force an action or encourage an action in terms of a trading position.
However, though, I mean, with all of that, all of those tools, and in fact, like kind of, it's a funny, funny thing in our work, just to give you a bit of a perspective is when, so people kind of joke, and these are not necessarily R users, they joke that, you know, when I'm faced with a very difficult business problem, they basically say, oh, we can hire somebody and give them Excel and get them working on it for a year, or it's going to be four lines of R code. So that's kind of, it's a bit of a mystique around it in our workplace, and it's very, it's kind of interesting, and I think it's a sentiment that's shared by a lot of people in this conference.
or it's going to be four lines of R code. So that's kind of, it's a bit of a mystique around it in our workplace, and it's very, it's kind of interesting, and I think it's a sentiment that's shared by a lot of people in this conference.
However, though, with simplifying that complexity through data science, you still need traders. I mean, we are not at a point that we can kind of automatically decide on risk reward decisions. So you need that human factor, and what makes a good trader is somebody that can scan the energy environment, find signs of market dislocations, and use optionality of their assets. So we are talking not necessarily guys that do futures trading, but these are asset traders. So they basically go and rent a tank, or, you know, they basically, these are trade shops that own refineries or own transport assets, such as pipelines, which basically, when you think about it, it's a, by itself, it can be modeled as a option. It's an option to move one energy across time, meaning that you can store in one time period, withdraw in a different time period. You can move energies through location. Let's say, you know, you basically move a barrel of crude oil from Canada to Texas, or you can change it in form. So let's say you can use a refinery to produce refined products. So at any, every, any of those steps that you take, there's an opportunity to capture an arbitrage.
So in some ways, like, you know, when you actually, we model that in, from a valuation perspective, we are looking at these as a call option on a spread or a spread option for those of you who are, come from that quant background. So the traders always are in the lookout for assets that gives them the highest optionality. So, and their ability to monetize these assets is what makes them a good trader. And some of these are, you know, when you actually pick up, pick, take a pipeline commitment, for example, as an oil company or a trade shop, you're actually looking at a long-term commitment that locks you in for, for many years. So, so there is a level of consequence involved with, with these decisions. So, so your ability to actually see complexity, see, see the effectively signal through noise and cut through complexity is, is key as a trader.
The idea behind Rpanda
So with that problem in mind and, and, you know, the, the other thing about trading is like, there is a misperception. I think that some people are naturally good traders. I think it's a skill that can be learned. And in fact, working for some of the main, big trading company as part of a multinational oil company, my past experience, you kind of realize that there is a bit of a method to this madness in terms of hiring, teaching and, and training your, your traders. And, and some companies do that consistently and do that across the board, which, which kind of got us thinking around, you know, can we actually find, develop a set of tools with assessment modules, with training courses that could actually codify some of that, some of those rules that would allow you to select good traders, train them and, and, and incorporate them into your, into your company.
So, so this is our first sort of, I kind of put that as a bit of a thing on that slide deck, which is, this is our first lean canvas. For those of you I've kind of dealt with starting, starting doing a startup, sort of starting a startup where we're actually participating in one of these startup weekends. That's one of the things that they, they usually do. So you basically have a problem. So, so the problem that we're trying to solve are trader competency assessment and development. And we are trying to solve that for universities and, and later on for, for corporations. And the solution that we provided and we have been testing it over the past, past year are effectively three tools. And these tools are developed in Shiny because that's what me and my business partner, Philip Cote, are familiar with. So it kind of goes to tell you, we are not necessarily software developers, but we had an idea. This is a very agile tool. We could basically bring it to life.
And, and also a whole bunch of trading modules and assessment frameworks that comes with that. So one is a multi-day trader game where students currently in University of Alberta, they, they basically participate in a basically one semester sort of trading game. They can trade commodity futures and as part of their curriculum, they learn how to do a proper market call, learn about risk reward analysis, learn about energy assets and infrastructures. And then every week they basically put on a trade which they, they go and try to explain to their classmates what the rationale of their trade is. And, and they see the result. By the end of the semester, they actually, they get marked on the P&L that they accumulate and, and also the insights that has, has gone to their, to their trading strategy.
The other one that I'm going to demo quickly here is our real time trader game. This is a one year of market data condensed in 20 minutes. So every, every second is almost like a day. So it's a high stress scenario that people actually trade real assets. And then the, finally the one that we're kind of right now developing in a Shiny framework, it's kind of, I guess offline right now is our negotiation game that you negotiate to purchase an asset. And, and it's, it's kind of like we're trying to capture a lot of the discussions that happens as a, in the process.
Technology stack
So in terms of our technology stack, we have, we rely fully on open source. We're using quite a bit of plumber API. So there's a lot of talks around plumber API. It's a, if you haven't used it, it's one of the best products that that's out there. We have actually developed an open source, a lot of our code. These are helper tools that, you know, helps you with energy analysis, sort of pre-trade analysis. It's called RTL. It is on GitHub currently, but as of yesterday, we submitted it to CRAN. So, so just a thing of personal experience, you know, those warnings you see when you create a package, CRAN does not accept any of those warnings. So you have to be very, very, it's, it's probably one of the most frustrating things.
Demo and scaling challenges
So, oh wow. So, so quickly I want to now go through my simulation software. So this is how it looks like. So you can trade futures. You know, it's, it's all done in a shiny dashboard. So you see like the, I'm using invalidate later quite a bit to, to kind of get this more reactive or get this reactive. So you can actually look, do on the fly analysis. You can, you can now go and look at the news, effectively the news feed. Some of it is fake news. Some of it is real. You can also see some of the, you know, the overall sort of how you're doing in, in your trades. So I'm using GT table for that, for if you're interested. So you can go and in this case you can go and buy an asset. So in this case you're bidding on a pipeline. It gives you a code based off of where the market is trading at. You go and approve it. And now you have the asset that you can trade. So, so you can go on basically put a position against it.
So given that I'm running short in time, I'm not going to go through this in detail, but but I'm, I'm going to do it, give you a real demo if you want to talk to me afterwards. So, so scaling challenges was the biggest thing, you know, up to now I've kind of had the experience using Shiny and R and I thought I had it figured out until I had to scale and use invalidate later. So, so basically our experience was after 10 users, we almost hit a limit. So what we did was we kind of went and followed the best practice, which I'm kind of lying on that one. We did not do that. So we effectively, we through brute force, we tried to add more CPUs and didn't work. So then I have, I, again, I relied on my good friend in RStudio who connected me with RStudio engineers and they basically put some sense into me in terms of my code design.
So, so the things, the takeaways, use Profvis, shiny cannon, use it early and often. You can debug your code standalone for one user. It doesn't mean that you can scale it. Do not read and write to disk. Do not use gg, ggplot. Use something like Plotly, which is JavaScript based. And then finally, you know, you can have, there's lots of talks here on, you know, how to scale things. I wish I actually attended this conference last year.
Do not read and write to disk. Do not use gg, ggplot. Use something like Plotly, which is JavaScript based.
What's next
So what's next for us? We are expanding our platform to more universities. We want to have a global competition. So scaling is important for us. We are incorporating NLP and learning algorithms, reinforcement learning in particular to actually start not only to collect a lot of, a lot more data, which is in terms of chat and texts that people, traders are actually conducting among themselves, but also to play this game in conjunction with a human trader to understand what good looks like. And also incorporate maybe features that nudge traders in terms of making better decisions. So almost like, you know, your drive assist in your car. So, so yeah, that's, that's kind of my, that sums up my presentation. Thank you very much for your attention.
Thank you, Nima. I've got just one question at the moment, but if you have more questions please add them to, to Slido. We've got time for just a couple. Have you tried training an AI model to trade commodities?
That's, that's an interesting one. That's, that's what we are trying to do using reinforcement learning. We haven't, no. So that's our next to do, effectively, going forward.
