Calculating FX total returns in Python

If you want a train, you have to build a train track. It doesn’t matter, if it’s a steam train or bullet train, or any other train. It’s a prerequisite. No track kind of implies the train can’t run. Obviously, each train needs a different type of track, but ultimately the principle is the same in how the track works (admittedly, if it’s a maglev train then perhaps not).

 

When it comes to developing trading strategies in financial markets, there are many to choose from, whether it’s trend, stat-arb, carry, value, high frequency and so on. The key similarity is that in all of these, we are trying to optimize the trades we do in some way to generate P&L. Ok, that’s not exactly rocket science. But before we even begin to understand the P&L of a trading strategy, we need to understand the total returns. The total returns are another way of saying, if we were to buy an asset and hold it what would our P&L be? It’s our train track, and our trading strategy is the train being driven along that track! Once we have that, we can use this time series, to understand the P&L of a more active trading strategy. There are of course also factors we need to account for to, to generate a realistic P&L, such as transaction costs (especially important for higher frequency strategies and when we trade more illiquid assets).

 

Unfortunately, the P&L is usually not simply a matter of looking at changes in the spot price. If we look at the total returns of an FX spot position, we also need to take into account the carry, the rate differential between the base and terms currency. If we try to backtest an FX carry basket, but just use the spot return (and not the total returns), our simulated P&L will be totally unrealistic. If a backtest is totally unrealistic, and we try running it with real cash, we will likely face an unpleasant surprise.


As we start looking at more complicated products, such as FX forwards we need to be able to reprice the contract every day till its rolled. This requires us to be able to calculate delivery dates, and also to interpolate the forward curve. For FX options, it becomes even more complicated, as we need to have a proper pricing tool to reprice our contract both by interpolating in tenor space and strike space etc. You also want to be able to generate the P&L from delta hedging the option (and no, we can’t simply look at implied vs realized vol – it isn’t identical to your gamma trading P&L).

 

One easy way to get total returns, is to get premade time series from a data vendor or a sell side firm. For some asset classes, like FX spot, there are many providers. However, for more complicated assets, it can be more difficult to find and also the rolling rules or underlying data used might not fit with your exact requirements.

 

The alternative is to create your own total return indices. This enables you to choose eg. whatever spot data you want (you can choose the exact trading time you want or raw data which has more appropriate licencing for you), FX vol data, FX forward points etc. Unfortunately, it ends up being a time consuming, even if their actual construction is relatively well understood amongst many in the market and I think these days it would be extremely difficult to call it proprietary. Nevertheless, this work pretty much ends up getting replicated time and again in fund and sell side firm alike. In a sense, all this work gets lumped together as proprietary, even if the main “secret sauce” is how you generate the actual trading signal, not how you calculate the P&L!

 

As a result, I’ve decided to write some of my own total return indices for FX and open source them in my finmarketpy library (download them from GitHub)! I use them myself to do my own backtesting when I create trading strategies. So far, I’ve developed total returns indices for:

 

 

In all cases, the above examples code links in with Bloomberg, to download the required raw data but this only works if you have a Bloomberg terminal. If you have data from a different source, you can substitute it. Underneath, the FX vanilla options total returns calculations use Dominic O’Kane’s great FinancePy library (downloadable from GitHub), which includes pricers not only for FX options, but for exotics and for several asset classes. I’ve also made some (very small) contributions to FinancePy to help speed up various parts using Numba. Even if you’ve already developed your

 

I’m also planning to extend the total returns work to other assets, eg. interest rate swaps etc. If you’re interested in helping out or simply learning more about total return indices in finmarketpy let me know. It’s probably going to be much more cost effective, than trying to do it all yourself! Even if you already have total returns indices from other sources, or have even coded up your own, it’s great to have an open source alternative for comparison. I often like to compare several different sources when backtesting for example.