{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "printyellow (generic function with 1 method)" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "using Printf, Dates, Statistics, DelimitedFiles, StatsBase\n", "\n", "include(\"jlFiles/printmat.jl\")" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "using Plots\n", "\n", "gr(size=(480,320))\n", "default(fmt = :svg)" ] }, { "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": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(300, 9)" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "CurrNames = [\"AUD\",\"CAD\",\"EUR\",\"JPY\",\"NZD\",\"NOK\",\"SEK\",\"CHF\",\"GBP\"]\n", "\n", "x = readdlm(\"Data_Returns.csv\",',',skipstart=1) #return data\n", "dN = Date.(x[:,1]) #covert to Julia date, eg. 2001-12-31\n", "R = Float64.(x[:,2:end])\n", "\n", "x = readdlm(\"Data_Forwardpremia.csv\",',',skipstart=2) #forward premia, skip 2 rows\n", "dN2 = Date.(x[:,1]) \n", "fp = Float64.(x[:,2:end])\n", "\n", "(T,n) = size(R) #number of data points, number of currencies" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Same dates? true\n" ] } ], "source": [ "println(\"Same dates? \",dN == dN2)" ] }, { "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": 5, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "xTicksLoc = [Date(2000),Date(2010),Date(2020)]\n", "xTicksLab = Dates.format.(xTicksLoc,\"Y\")\n", "\n", "p1 = plot( dN,fp*12,\n", " layout = (3,3),\n", " legend = false,\n", " size = (800,600),\n", " linecolor = :blue,\n", " xticks = (xTicksLoc,xTicksLab),\n", " ylims = (-6,6),\n", " title = reshape(CurrNames,1,:),\n", " titlefont = font(10) )\n", "display(p1)" ] }, { "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": 6, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "r = log.(1.0.+R/100)\n", "P = cumsum(r,dims=1)\n", "\n", "p1 = plot( dN,P,\n", " layout = (3,3),\n", " legend = false,\n", " size = (800,600),\n", " linecolor = :blue,\n", " xticks = (xTicksLoc,xTicksLab),\n", " ylims = (-1,1),\n", " title = reshape(CurrNames,1,:),\n", " titlefont = font(10) )\n", "display(p1)" ] }, { "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": 7, "metadata": {}, "outputs": [], "source": [ "m = 4 #number of long/short positions \n", "\n", "(R_CT,w_CT) = (zeros(T),zeros(T,n))\n", "for t = 2:T #loop over periods, save portfolio returns\n", " #local v,wt #local/global is needed in script\n", " v = sortperm(fp[t-1,:])\n", " w = zeros(n)\n", " w[v[1:m]] .= -1/m #low interest rate currencies\n", " w[v[end-m+1:end]] .= 1/m #high interest rate currencies \n", " R_CT[t] = w'R[t,:]\n", " w_CT[t,:] = w \n", "end\n", "\n", "R_EW = vec(mean(R,dims=2)); #equally weighted portfolio" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "P_CT = cumsum(log.(1.0.+R_CT/100))\n", "P_EW = cumsum(log.(1.0.+R_EW/100));\n", "\n", "p1 = plot( dN,[P_CT P_EW],\n", " label = [\"CT\" \"EW\"],\n", " size = (800,600),\n", " linecolor = [:red :blue],\n", " xticks = (xTicksLoc,xTicksLab),\n", " ylims = (-1,1),\n", " title = \"Comparison of performance\")\n", "display(p1)" ] }, { "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": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " CT EW\n", "mean 0.283 0.018\n", "std 2.038 2.255\n", "SR 0.139 0.008\n", "\n", "and in case you want annualised numbers...\n", " CT EW\n", "mean 3.401 0.221\n", "std 7.061 7.813\n", "SR 0.482 0.028\n", "\n" ] } ], "source": [ "μ = mean([R_CT R_EW],dims=1)\n", "σ = std([R_CT R_EW],dims=1)\n", "SR = μ./σ\n", "\n", "printmat([μ;σ;SR],colNames=[\"CT\",\"EW\"],rowNames=[\"mean\",\"std\",\"SR\"])\n", "\n", "println(\"and in case you want annualised numbers...\")\n", "(μ_a,σ_a,SR_a) = (μ*12,σ*sqrt(12),SR*sqrt(12))\n", "printmat([μ_a;σ_a;SR_a],colNames=[\"CT\",\"EW\"],rowNames=[\"mean\",\"std\",\"SR\"])" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "...and for log returns, in %\n", " CT EW\n", "mean 0.262 -0.007\n", "std 2.046 2.256\n", "SR 0.128 -0.003\n", "\n", "and in case you want annualised numbers...\n", " CT EW\n", "mean 3.147 -0.083\n", "std 7.089 7.816\n", "SR 0.444 -0.011\n", "\n" ] } ], "source": [ "println(\"...and for log returns, in %\")\n", "\n", "μ = mean(log.(1.0.+[R_CT R_EW]/100),dims=1)*100\n", "σ = std(log.(1.0.+[R_CT R_EW]/100),dims=1)*100\n", "SR = μ./σ\n", "\n", "printmat([μ;σ;SR],colNames=[\"CT\",\"EW\"],rowNames=[\"mean\",\"std\",\"SR\"])\n", "\n", "println(\"and in case you want annualised numbers...\")\n", "(μ_a,σ_a,SR_a) = (μ*12,σ*sqrt(12),SR*sqrt(12))\n", "printmat([μ_a;σ_a;SR_a],colNames=[\"CT\",\"EW\"],rowNames=[\"mean\",\"std\",\"SR\"])" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "AUD 0.983\n", "CAD 0.467\n", "EUR 0.023\n", "JPY 0.000\n", "NZD 0.957\n", "NOK 0.710\n", "SEK 0.317\n", "CHF 0.000\n", "GBP 0.530\n", "\n" ] } ], "source": [ "FreqInHi = mean(0 .< w_CT,dims=1) #frequence of times the weight is positive\n", "\n", "printmat(vec(FreqInHi),rowNames=CurrNames)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "@webio": { "lastCommId": null, "lastKernelId": null }, "anaconda-cloud": {}, "kernelspec": { "display_name": "Julia 1.6.3", "language": "julia", "name": "julia-1.6" }, "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "1.6.3" } }, "nbformat": 4, "nbformat_minor": 4 }