Constructing a Currency Portfolio in MetaTrader

24 January 2018
The Market Bull

This post describes how to construct a currency portfolio composed of any number of currency pairs (from those available on the Darwinex platform) and allocations, in MetaTrader.

A few common use-cases for constructing currency portfolios include:

  1. Studying the correlation of a trading strategy’s returns to market volatility.
  2. Trading currency strength instead of single pairs themselves.
  3. Creating custom portfolios most responsive to selected news events.
  4. Diversification of risk, trading with more controlled volatility.

While the resulting portfolio is displayed as an index similar to the one constructed for individual currencies in our previous post (Currency Index Indicator for MetaTrader 4), the key differences in this implementation are:

  1. Dynamic Portfolio Allocation (customize weights as you see fit)
  2. Dynamic Portfolio Size (select any number of assets for inclusion)

As before, MQL source code has been provided to enable traders to deploy this implementation as a custom indicator on the MetaTrader platform.


What is a Currency Portfolio?

DARWIN Underlying Currency Portfolio

DARWIN Underlying Currency Portfolio

A currency portfolio is a combination or group of currency pairs, constructed in much the same way as the currency index in our previous post.

Unlike trading one or two assets at any one time, carefully constructed portfolios of reasonably uncorrelated currency pairs enable traders to diversify risk better.

As losses on some currency pairs in the portfolio are likely to get partially (if not fully) compensated for by gains on others, overall portfolio volatility is reduced, softening the effects of sudden swings in individual portfolio components.


Portfolio Composition & Allocations

The MetaTrader indicator provided takes the following inputs:

  1. Comma-separated list of currency pairs to include in the portfolio.
  2. Comma-separated list of portfolio weights.
  3. Analysis Start Date to begin plotting the portfolio’s index from, based to 100.

In the indicator’s inputs, these three parameters are called “Portfolio_Assets”, “Portfolio_Weights” and “StartDate” respectively.

For example,

Inputs for an equally-weighted portfolio beginning on the 1st of January, 2017, and containing NZDCHF, GBPJPY, AUDUSD and EURCAD, where NZDCHF and EURCAD are being shorted, would look like this:

The indicator automatically calculates the allocation for each asset, based on the “Portfolio_Weights” inputs specified.

In this case, inputs of [-1, 1, 1, -1] translate to a 25% allocation to each of the 4 assets in the mix, shorting the first and last assets respectively.


Allocations can be modified conveniently.

Say for example, a user wanted to enter inputs for “Portfolio_Weights” as [-1, 2, 4, -1] -> the resultant weighted currency portfolio would then contain:

  • 12.5% for -NZDCHF
  • 25.0% for GBPJPY
  • 50.0% for AUDUSD
  • 12.5% for -EURCAD

.. and look like this:

Note: The indicator averages allocations internally to complete 100%. So if it’s more intuitive, [-1,2,4,-1] can also be specified as [-12.5,25.0,50.0,-12.5] -> the results are the same.


Portfolio Calculation Logic

Each currency portfolio P, at time t, where rates_total time periods are available for processing, can be mathematically represented as follows:

\(\lim_{t\rightarrow rates\_total} P(t) = P(t-1)(1 + \sum_{j=1}^{n}(\frac{C_{j,t} – C_{j,t-1}}{C_{j, t-1}})(\frac{W_j}{\sum_{k=1}^{n}(W_k)}))\)

where “C” = each currency pair’s contribution, “n” = number of currency pairs/weights in the portfolio, and:

\(j\space\ \epsilon \space\ \{1 \space\ .. \space\ n\}\)

\(t\space\ \epsilon \space\ \{1 \space\ .. \space\ rates\_total\}\)

\(W\space\ \epsilon \space\ \{-\infty \rightarrow +\infty \} \space\ – \space\ user \space\ specified \space\ portfolio \space\ weights\)

As with the Currency Index indicator before, the contribution of each currency pair to the portfolio value is calculated as above, indexed to base 100.00 from the chosen start date. The default start date is January 01, 2017.


MetaTrader Indicator (Source Code)

We’ve uploaded a functional MetaTrader 4 Indicator that constructs portfolios as described in this post, to our GitHub page under tools -> MQL4.

First, simply Right-Click and Save-As on the following link to save the file to your computer, and follow the instructions below them:

Click here to download DLabs_CurrencyPortfolio.mq4

Instructions:

  1. Launch MetaTrader 4, and open your data folder (File -> Open Data Folder)

    MetaTrader 4 Open Data Folder

    MetaTrader 4 Open Data Folder

  2. Copy the “DLabs_CurrencyPortfolio.mq4” into your MQL4\Indicators directory,
  3. Restart MetaTrader 4 and open a new chart,
  4. Double-click on DLabs_CurrencyPortfolio under Indicators to load the indicator on any chart.

To recap, the indicator takes the following inputs:

  1. Portfolio_Assets -> the currency pairs you want in the portfolio.
  2. Portfolio_Weights -> their respective allocations.
  3. StartDate -> the portfolio index’s start date.

Click OK, and the indicator will draw the selected currency portfolio in its own dedicated window, as shown below:

Important Note: In order for the indicator to draw correctly and accurately, users must ensure that their History Center contains data for all currency pairs you’ve specified in Portfolio_Assets.


[Additional Resources] Constructing Currency Portfolio Indexes in MetaTrader 4

Do you have what it takes? – Join the Darwinex Trader Movement!

Darwinex - The Open Trader Exchange