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...
To read the rest of the article on the Cuemacro website, please click here!
Is it also possible to backtest Crypto within finmarketpy?
It's a real hassle to calculate total returns for FX (and indeed many other asset classes) and it's something that ends up getting replicated in fund after fund.. so thought it was worth open sourcing some Python code to do the job for FX spot, forwards + options! I'm also looking to add more total returns for other assets, if I can get some help with it.