Microservices: An FpML Cashflow Generator

  • The Clarus API has a function to generate cashflows from an FpML description of a trade.
  • The function is very easy to call from many popular languages, including Python, R, Julia, C++, and Java.
  • Many related functions are available, see our API documentation.

Calling directly in the browser

To appreciate the ease with which the cashflows function may be called, our first example does not even involve writing code, the cashflows function can be called directly in the browser!

This URL is enough to call the function and see its results.
https://api.clarusft.com/rest/v1/trade/Cashflows.html?trade=USSW10&valueDate=2017-03-03

To ease the exposition, we set the trade parameter to USSW10, the Bloomberg ticker for the USD 10Y swap. The cashflows are show below, although you will need to enter a username/password the first time (obtained by registering here).

An example of calling the cashflows function inside a browser
An example of calling the cashflows function inside a browser

FpML

Financial Product Markup Language (FpML) is an XML standard widely used amongst capital market participants in the processing of OTC derivatives. An example of an FpML representation of an interest swap is available here. The standard is developed under the auspices of ISDA.

Calling from Python

To generate the cashflows for an FpML trade, we cannot type the entire FpML message into the URL, instead we will call the function in Python, and set the parameter trade to the value of the FpML message. We first load the FpML message from a file, and then call the cashflows API. The API will return response object containing the results in a format which can easily be read into a Pandas dataframe in Python. The code snippet below shows the main steps.


my_fpml_trade = open('/home/gary/clarus/fpmlExample.xml').read()

r = trade.cashflows(trade=my_fpml_trade, valuedate='2017-03-03', output=JSON)

df = pandas.DataFrame(r.results)

print(df)
  

The results produced by this code is the following, showing the first few columns and rows of the pandas dataframe.


        TradeID  Group      Type  Ccy     PmtDate        DF      Amount  \
0  DB~798035894  Payer  INTEREST  USD  2017-09-07  0.995195 -1236827.40   
1  DB~798035894  Payer  INTEREST  USD  2018-03-07  0.989135 -1236827.40   
2  DB~798035894  Payer  INTEREST  USD  2018-09-07  0.981766 -1236827.40   
3  DB~798035894  Payer  INTEREST  USD  2019-03-07  0.973423 -1236827.40   
4  DB~798035894  Payer  INTEREST  USD  2019-09-09  0.964039 -1250569.92   

A very similar approach works in other languages, such as R and Julia. Working examples are found at Cashflows API Documentation.

The complete code used in the example above is shown below. It relies on two packages, the Clarus python module (which relies on the popular Requests library), and the Pandas library to provide the convenient DataFrame object to hold the cashflows returned. The key/secret can be obtained by registering here.


import pandas
from clarus import ApiConfig, trade, JSON

# Example of REST API call to Clarus Microservices #

# Manually edit and set key/secret here #
ApiConfig.api_key = ''
ApiConfig.api_secret = ''

my_fpml_trade = open('/home/gary/clarus/fpmlExample.xml').read()
  
print(my_fpml_trade)

r = trade.cashflows(trade=my_fpml_trade, valuedate='2017-03-03', output=JSON)

df = pandas.DataFrame(r.results)

print(df)

Stay informed with our FREE newsletter, subscribe here.