Microservices: Swap equivalents in Julia

  • Julia is a modern high-level, high-performance language for numerical computing.
  • Clarus API functions are easily accessed in Julia.

What is Julia?

Julia is a relatively new computing language, combining the ease of development (similar to python and matlab) with a Just-In-Time compiler and other language features to deliver runtime performance close to that of C. It has gained some interest in finance, see for example the New York Fed’s implementation of the DGSE model and the recent article on automatic differentiation with Julia in Wilmott magazine.

Swap Equivalents

The swap equivalents calculation is described in the previous blog, Swap Equivalents via Waves. The key feature of the methodology is that it can deliver rock solid swap equivalents even when smooth global interpolation methods are used in the yield curves. The calculation is available in the Clarus API and documented here.

Calling the API from Julia

To call the Clarus Swap Equivalent function in Julia, we first need to check if our installation of Julia has the libraries Requests and Dataframes . If not, we can install them with the following commands;

Pkg.add("Requests")
Pkg.add("DataFrames")

Now we set the api key and secret obtained by registering here.

apiKey = "..."
apiSecret = "..."

Next we prepare some parameters, a portfolio of a single 11Y par swap, and the common risk pillars

myTrade = "USD 11Y 100m payer"
myPillars = "2Y,3Y,4Y,5Y,6Y,7Y,8Y,9Y,10Y,12Y,15Y,20Y,25Y,30Y"

And finally we can call the swap equivalents function,

r = request("hedge", "Equivalents", portfolios=myTrade, tenors=myPillars, basis="false")
df = dataframe(r)
println(df)

Yielding the result below.

│ Row │ SwapEquivalents_in_USD_MM_ │ USD │
├─────┼────────────────────────────┼─────┤
│ 1   │ "2Y"                       │ 0   │
│ 2   │ "3Y"                       │ 0   │
│ 3   │ "4Y"                       │ 0   │
│ 4   │ "5Y"                       │ 0   │
│ 5   │ "6Y"                       │ 0   │
│ 6   │ "7Y"                       │ 0   │
│ 7   │ "8Y"                       │ 0   │
│ 8   │ "9Y"                       │ 0   │
│ 9   │ "10Y"                      │ 49  │
│ 10  │ "12Y"                      │ 51  │
│ 11  │ "15Y"                      │ 0   │
│ 12  │ "20Y"                      │ 0   │
│ 13  │ "25Y"                      │ 0   │
│ 14  │ "30Y"                      │ 0   │
│ 15  │ "Total"                    │ 100 │

By comparing with previous examples, A SIMM Sensitivities Calculator and ISDA SIMM in R, one can see it is just as convenient to call Clarus API functions from Julia as it is from Python or R.

Complete code example

I did not explain the functions request and dataframe in the code samples above. They are convenience functions and included in the full script below. I have also included an example of loading a portfolio of trades from a CSV file, computing the hedge equivalents, and saving the results.

###################################
#One time only package installs
###################################
#Pkg.add("Requests")
#Pkg.add("DataFrames")
###################################

import Requests
import DataFrames

# Example of REST API call to Clarus Microservices #

# Manually edit and set key/secret here #
apiKey = ""
apiSecret = ""

function request(category, functionName; params...)
  urlBase = "https://" * apiKey * ":" * apiSecret * "@eval.clarusft.com/api/rest/v1/"
  restUrl  =  urlBase * category * "/" * functionName * ".csv"
  r = Requests.post(restUrl, json=Dict(params))
  if Requests.statuscode(r)!=200
    error("Request to " * category * "/" * functionName * " failed with status code: " * string(Requests.statuscode(r)))
  end
  return r
end

function dataframe(response)
  return DataFrames.readtable(IOBuffer(Requests.readall(response)))
end

myTrade = "USD 11Y 100m payer"
myPillars = "2Y,3Y,4Y,5Y,6Y,7Y,8Y,9Y,10Y,12Y,15Y,20Y,25Y,30Y"

r = request("hedge", "Equivalents", portfolios=myTrade, tenors=myPillars, basis="false")
df = dataframe(r)
println(df)

f = open("/home/gary/clarus/myTrades.csv")
myTrades = readstring(f)
close(f)

r = request("hedge", "Equivalents01", portfolios=myTrades, tenors=myPillars, basis="false")

f = open("/home/gary/clarus/myTrades_hedge_equivalents.csv", "w")
write(f,Requests.readall(r))
close(f)

Stay informed with our FREE newsletter, subscribe here.