Visualize performance of a domestic vs. foreign asset adjusted for exchange rate
This article demonstrates how a user can use Eikon Python API to quickly prototype and plot custom analytics which are not readily available within Eikon. The example shown here is one of the many use cases, that Quants/Analysts can employ to analyze financial data.
Eikon Python API
Refinitiv provides an ease of use python library for accessing market data, timeseries, fundamental and reference data and news. This Eikon library depends on Eikon Scripting Proxy, which runs with user's Eikon credentials, and provides data retrieval Web interface on top of which ease of use Python library is built.
For more information on Eikon Python API, please visit Refinitiv Developers Portal.
The goal of this article is to demonstrate how easy it is to perform a simple analysis and plot it on a chart. This what-if analysis compares the return of a foreign asset against a domestic asset, if the same dollar amount was invested in both; taking into account the currency exchange rate during the comparison period. The term domestic is used loosely here and this approach can be used to compare any two assets, trading in any currencies. The sample accomplishes this by getting the trade currency of both the assets, and then requesting the calculated cross rate for this currency pair. Timeseries of price history for both assets are first expressed in units of domestic currency and then rebased to the start of the comparison period. The rebased data is then plotted on a line chart.
The accompanying sample compares the performance of a Canadian bank - TD (TD.TO), trading on Toronto Stock Exchange in Canadian Dollars with British bank - Barclays (BARC.L), trading on London Stock Exchange in Pound Sterling.
To try the code provided in this article, a setup and familiarity with following technologies is required:
- Python language
- Pandas Data Analysis library
- Jupyter (iPython) Notebooks
- Plotly (or Cufflinks) Charting library
The code provided with this article was tested using python (3.6.1), eikon (0.1.7), numpy (1.12.1), pandas (0.20.1) and plotly (1.12.9)
Following steps are executed to create the comparison chart:
- Define domestic and foreign instruments and a time range for comparison. Since the performance data is normalized, any one of the two instruments can be considered as domestic and the other as foreign, and it will produce the same output.
- Get the trading currency for both instruments. In this example we use "TR.CompanyMarketCap.Currency" field, which generally provides the currency of the main equity issue of the company on the main listing exchange.
data,err = ek.get_data([instr1, instr2], "TR.CompanyMarketCap.Currency")
3. Create a cross-currency exchange rate RIC and get historical data for the given time range. The cross rate is calculated by Refinitiv for most currency pairs. This RIC is formed by appending =R to both currencies. For e.g. Calculated cross rate RIC for CAD vs GBP would be "GBPCAD=R".
crossRIC = data['Currency'] + data['Currency'] + '=R'
curr = ek.get_timeseries([crossRIC], fields='CLOSE', start_date=start_date, end_date=end_date)
4. Get historical data (CLOSE Price) for both instruments for requested time range.
5. Convert the price history of the foreign asset into domestic currency using the cross-currency rate we retrieved earlier.
# create a single DataFrame with both data series (useful if using cufflinks to plot instead of plotly)
perf = pd.concat([perf1, perf2 * curr], axis=1)
perf.columns = [instr1, instr2]
# drop invalid entries from dataframe
perf = perf.dropna()
6. Rebase the data to the start of the time period, so that initial investment in both instruments shows up as 100%.
# rebase both timeseries to 100% at start date
perf_rb = perf * 100 / (perf[instr1], perf[instr2])
7. Plot a double line chart. The two chart lines will show the % gain/loss on an investment in both instruments in domestic currency.
# using offline plotly for charting
} for col in perf_rb.columns])
The code sample above breaks the implementation into multiple steps for clarification and to show as an example, how various data and timeseries can be retrieved using Eikon API. A simple implementation of this code would be:
# get and rebase the data
response, error = ek.get_data(instruments=[instr1, instr2], fields=['TR.ClosePrice.Date', 'TR.ClosePrice.Value'],
df = response.pivot_table(values='Close Price', index=['Date', 'Instrument']).unstack('Instrument').dropna()
rebased = df.apply(lambda series: series/series*100)
# plot it in a chart
} for column in rebased.columns])
The resulting chart shows that an equal Canadian dollar amount invested in TD on Jan 2nd 2015 and liquidated on Jan 3rd 2017 would yield 21% gain vs. 15% loss on investment in Barclays, when factoring in the currency movement.
See the attached iPython notebook for complete working sample.
Refinitiv github samples repository has many more samples and snippets of code showing other uses of Eikon scripting API. Just search for python or EikonAPI within repositories.