# Constructing a Currency Portfolio in MetaTrader

24 January 2018

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.
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

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

.. 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:

## $$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.

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:

Instructions:

1. Launch MetaTrader 4, and open your data folder (File -> 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.