# Constructing a Currency Portfolio in MetaTrader

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?

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

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.

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.

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.

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

• ###### Miguel
Posted July 18, 2018 at 7:18 am

¿Como tiene en cuenta el swap?
Es decir la curva ya ha descontado este efecto.

• Post Author
###### The Market Bull
Posted July 26, 2018 at 8:00 am

Hi Miguel,
When constructing the curve, the indicator uses the Close price for each constituent asset in the user-specified portfolio.
These prices are then used to calculate a weighted return at each interval, all such weighted returns then being accumulated to produce the curve displayed.
This implementation can be found in the for() loop inside the following file:
https://raw.githubusercontent.com/darwinex/DarwinexLabs/master/tools/MQL4/DLabs_CurrencyPortfolio.mq4

The Darwinex® brand and the http://www.darwinex.com domain are commercial names used by Tradeslide Trading Tech Limited, a company regulated by the Financial Conduct Authority (FCA) in the United Kingdom with FRN 586466, with company registration number 08061368 and registered office in Acre House, 11-15 William Road, London NW1 3ER, UK. and by Sapiens Markets EU Sociedad de Valores SA, a company regulated by the Comisión Nacional del Mercado de Valores (CNMV) in Spain under the number 311, with CIF A10537348 and registered office in Calle de Recoletos, 19, Bajo, 28001 Madrid, Spain.

CFDs are complex instruments and come with a high risk of losing money rapidly due to leverage. 64% of retail investor accounts lose money when trading CFDs with this provider. You should consider whether you understand how CFDs work and whether you can afford to take the high risk of losing your money.