{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Load Data \n",
"\n",
"Returns from FX investments (for a US investor) in percent are in `Data_Returns.csv` (extract the returns into a matrix `R`) and log forward premia in percent are in `Data_Forwardpremia.csv` (extract the forward premia into a matrix `fp`). Convert the first columns to Julia dates.\n",
"\n",
"Test if the dates in the two files are the same.\n",
"\n",
"The currency names/abbreviations are\n",
"`[\"AUD\",\"CAD\",\"EUR\",\"JPY\",\"NZD\",\"NOK\",\"SEK\",\"CHF\",\"GBP\"]` "
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"using DelimitedFiles, Printf, Dates, Plots, Statistics\n",
"\n",
"include(\"jlFiles/printmat.jl\")\n",
"gr(size=(480,320))\n",
"default(fmt = :svg)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"All dates are the same"
]
}
],
"source": [
"# Load data\n",
"R = readdlm(\"Data/Data_Returns.csv\",',',skipstart=1) \n",
"fp = readdlm(\"Data/Data_Forwardpremia.csv\",',',skipstart=2) \n",
"\n",
"# Convert to julia dates\n",
"R[:,1] .= Date.(R[:,1])\n",
"fp[:,1] .= Date.(fp[:,1])\n",
"\n",
"# Check if all dates are the same\n",
"if all(fp[:,1] .== R[:,1])\n",
" print(\"All dates are the same\")\n",
"else \n",
" print(\"Some dates are different\")\n",
"end\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Plot the forward premia\n",
"\n",
"against time. Create a figure with 3x3 subplots. In the plot show the forward premia multiplied by 12 so they can be interpreted as the (annualised) interest rate differential (foreign minus US). \n",
"\n",
"Make sure to \n",
"1. have tick marks on the x-axis at `[Date(2000),Date(2010),Date(2020)]`\n",
"\n",
"2. limit the y-axis to `(-6,6)`\n",
"\n",
"3. Put the currency name/abbreviation in the title of each subfigure."
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": "\n\n"
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Dates\n",
"dates = fp[:,1]\n",
"\n",
"# Function to create the Plots\n",
"function getForwardPremiaPlot(y, title)\n",
" return plot(dates, y .* 12, \n",
" size = (1280, 720),\n",
" ylim = (-6, 6),\n",
" xticks = ([Date(2000),Date(2010),Date(2020)], [\"2000\",\"2010\",\"2020\"]),\n",
" legend = false,\n",
" title = title,\n",
" color = \"blue\",\n",
" )\n",
"end\n",
"\n",
"# Create plot with 3x3 subplots \n",
"plot(getForwardPremiaPlot(fp[:,2], \"AUD\"), getForwardPremiaPlot(fp[:,3], \"CAD\"), getForwardPremiaPlot(fp[:,4], \"EUR\"),\n",
"getForwardPremiaPlot(fp[:,5], \"JPY\"),getForwardPremiaPlot(fp[:,6], \"NZD\"),getForwardPremiaPlot(fp[:,7], \"NOK\"),\n",
"getForwardPremiaPlot(fp[:,8], \"SEK\"),getForwardPremiaPlot(fp[:,9], \"CHF\"),getForwardPremiaPlot(fp[:,10], \"GBP\"))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Plot cumulated log returns\n",
"\n",
"in a 3x3 figure (similar to before). The cumulated log returns for a given currency are $r_1,r_1+r_2,r_1+r_2+r_3,...$ etc where $r_t$ is the log return in period $t$.\n",
"\n",
"Make sure to\n",
"1. use the same tick marks on the x-axis as before\n",
"2. limit the y-axis to (-1,1)\n",
"\n",
"Hints:\n",
"1. log returns are $r= \\ln(1+R/100)$ since the returns in `R` are in percent. In Julia, use `log()` for the natural logarithm.\n",
"2. cumulate using `cumsum()` or by a loop so you get a $T \\times 9$ matrix."
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": "\n\n"
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Function to create the Plots\n",
"function getCumulatedLogReturnsPlot(y, title)\n",
" return plot(dates, cumsum(log.(1 .+ y./100)), \n",
" size = (1280, 720),\n",
" ylim = (-1, 1),\n",
" xticks = ([Date(2000),Date(2010),Date(2020)], [\"2000\",\"2010\",\"2020\"]),\n",
" legend = false,\n",
" title = title,\n",
" color = \"blue\"\n",
" )\n",
"end\n",
"\n",
"# Create plot with 3x3 subplots \n",
"plot(getCumulatedLogReturnsPlot(R[:,2], \"AUD\"), getCumulatedLogReturnsPlot(R[:,3], \"CAD\"), getCumulatedLogReturnsPlot(R[:,4], \"EUR\"),\n",
"getCumulatedLogReturnsPlot(R[:,5], \"JPY\"),getCumulatedLogReturnsPlot(R[:,6], \"NZD\"),getCumulatedLogReturnsPlot(R[:,7], \"NOK\"),\n",
"getCumulatedLogReturnsPlot(R[:,8], \"SEK\"),getCumulatedLogReturnsPlot(R[:,9], \"CHF\"),getCumulatedLogReturnsPlot(R[:,10], \"GBP\"))\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Implement a carry trade strategy\n",
"\n",
"\n",
"1. Find the 4 currencies with the highest forward premia (interest rate differential) in $t-1$ and give each a portfolio weight `w[t,i]=1/4`. These are the investment currencies.\n",
"\n",
"2. Find the 4 currencies with the lowest forward premia in $t-1$ and give each a portfolio weight `w[t,i]=-1/4`. These are the funding currencies.\n",
"\n",
"3. The portfolio return in t is `w[t,:]'*R[t,:]`. For all periods that lacks data, set the portfolio return to 0.\n",
"\n",
"4. Create log returns, cumulate and plot (as above).\n",
"\n",
"5. In the plot, add a comparison with an equally weighted portfolio (1/9 in each currency). "
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": "\n\n"
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Remove date from matrix\n",
"fpNoDates = fp[:,2:10]\n",
"RNoDates = R[:,2:10]\n",
"\n",
"(T, n) = size(fpNoDates)\n",
"\n",
"#number of high/low positions \n",
"m = 4 \n",
"\n",
"# Create empty array\n",
"R_1 = fill(NaN,T)\n",
"EW = fill(NaN,T)\n",
"Frequency = fill(0, (1, n))\n",
"\n",
"#loop over periods\n",
"for t = 2:T \n",
" s = sortperm(fpNoDates[t-1,:])\n",
" w = zeros(n)\n",
" # Set portfolio weight to -1/4 for the lowest 4 forward premia\n",
" w[s[1:m]] .= -1/m\n",
" # Set portfolio weight to 1/4 for the highest 4 forward premia\n",
" w[s[end-m+1:end]] .= 1/m\n",
" # Calculate weighted portfolio returns\n",
" R_1[t] = w'RNoDates[t,:]\n",
" # Calculate EW portfolio returns\n",
" EW[t] = fill(1/n, n)'RNoDates[t,:]\n",
" # Calculate frequency of currencies for the highest 4 forward premia\n",
" Frequency[s[end-m+1:end]] .+= 1\n",
"end\n",
"\n",
"# Replace NaN with 0\n",
"R_1[isnan.(R_1)] .= 0\n",
"EW[isnan.(EW)] .= 0\n",
"\n",
"plot(dates, [cumsum(log.(1 .+ R_1./100)) cumsum(log.(1 .+ EW./100))], \n",
" size = (1280, 720),\n",
" ylim = (-1, 1),\n",
" xticks = ([Date(2000),Date(2010),Date(2020)], [\"2000\",\"2010\",\"2020\"]),\n",
" label = [\"CT\" \"EW\"],\n",
" title = \"Comparison of performance\",\n",
" color = [\"red\" \"blue\"]\n",
" )\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Report some return statistics\n",
"\n",
"1. For each of the two portfolio returns, report the average returns, standard deviation and their ratio (the \"Sharpe ratio\").\n",
"\n",
"2. For each currency, report the frequency of periods that the currency is in the \"high\" carry trade portfolio."
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1.\n",
" CT EW\n",
"avg return 0.284 0.018\n",
"std 2.042 2.259\n",
"SR 0.139 0.008\n",
"\n",
"2. frequency of periods in high carry trade portfolio\n",
" amount\n",
"AUD 295 \n",
"CAD 140 \n",
"EUR 7 \n",
"JPY 0 \n",
"NZD 287 \n",
"NOK 213 \n",
"SEK 95 \n",
"CHF 0 \n",
"GBP 159 \n",
"\n"
]
}
],
"source": [
"# Calculate avg, std, sr\n",
"R_all = [R_1[2:end] EW[2:end]]\n",
"\n",
"μ = mean(R_all,dims=1)\n",
"σ = std(R_all,dims=1)\n",
"SR = μ./σ\n",
"\n",
"println(\"1.\")\n",
"printmat([μ;σ;SR],colNames=[\"CT\",\"EW\"],rowNames=[\"avg return\",\"std\",\"SR\"])\n",
"\n",
"println(\"2. frequency of periods in high carry trade portfolio\")\n",
"printmat(Frequency[:] ,colNames=[\"amount\"],rowNames=[\"AUD\",\"CAD\",\"EUR\",\"JPY\",\"NZD\",\"NOK\",\"SEK\",\"CHF\",\"GBP\"])"
]
}
],
"metadata": {
"@webio": {
"lastCommId": null,
"lastKernelId": null
},
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Julia 1.7.0-rc1",
"language": "julia",
"name": "julia-1.7"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "1.7.0"
}
},
"nbformat": 4,
"nbformat_minor": 4
}