- The Clarus API has a function to compute ISDA SIMM™ from a CRIF file contain portfolio sensitivities.
- What-if analysis can be performed in addition to the portfolio margin calculation.
- The function is very easy to call from many popular languages, including R, Python, C++, Java and Julia.
What is R?
R is a language and environment for statistical computing and graphics, popular among some quants. Find out more here.
Calling the API from R
To call the Clarus ISDA SIMM™ calculation function in R, we first need to check if our installation of R has the libraries httr
and readr
. If not, we can install them with the following commands;
install.packages('httr')
install.packages('readr')
Now we set the api key and secret obtained by registering here.
apiKey <- ''
apiSecret <- ''
Next we open the Common Risk Interchange Format (CRIF) file. The CRIF file can be supplied by the portfolio’s host system or computed using a Clarus function, details in a previous blog here.
I have used file.path in the code, it is unnecessary, but I find it convenient as I don’t have to remember the direction of the slashes in a directory name or whether to use double slashes.
filepath <- file.path('C:', 'Users', 'gary', 'clarus', 'mycrif.csv')
mycrif <- read_file(filepath)
And finally call the Clarus API function
r <- request('simm', 'Margin', portfolios=mycrif)
The results are easily converted to an R dataframe and printed.
df <- dataframe(r)
print (df)
In my example, the following is produced.
SIMM Account Change Margin
1 Adhoc 94204616 0 94204616
2 Margin 94204616 0 94204616
Now suppose we are contemplating adding a new 10Y swap to our portfolio, we can see the impact on the margin easily using the whatif
parameter.
r <- request('simm', 'Margin', portfolios=mycrif, whatif='USD100m 10Y pay 2.2%')
df <- dataframe(r)
print (df)
The impact to the margin can be seen in the results.
SIMM Account WhatIf Change Margin
1 Adhoc 94204616 4101441 2655502 96860118
2 Margin 94204616 4101441 2655502 96860118
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.
##
##Need to install packages once, if not already installed
##install.packages('httr')
##install.packages('readr')
##
library('httr')
library('readr')
## Manually edit and set key/secret here ##
apiKey <- ''
apiSecret <- ''
request <- function(category, functionName, ...){
restUrl = paste0('https://eval.clarusft.com','/api/rest/v1/', category, '/',functionName, '.csv')
response <- POST(url=restUrl, body=list(...), encode='json', authenticate(apiKey, apiSecret, type='basic'))
if (response$status_code!=200){
stop(paste0('Request to ', category, '/', functionName, ' failed with status code: ', response$status_code))
}
return (response)
}
dataframe <- function(response){
return (read.csv(text=content(response, 'text'), sep=',', head=TRUE))
}
filename <- file.path('C:', 'Users', 'gary', 'clarus', 'mycrif.csv')
mycrif <- read_file(filename)
r <- request('simm', 'Margin', portfolios=mycrif)
df <- dataframe(r)
print (df)
r <- request('simm', 'Margin', portfolios=mycrif, whatif='USD100m 10Y pay 2.2%')
df <- dataframe(r)
print (df)