{
 "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"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(10340, 25)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x   = readdlm(\"Data/25_Portfolios_5x5_Daily.CSV\",',',skipstart=1)        #daily return data\n",
    "ym  = round.(Int,x[:,1])          #yearmonthday, like 20071231\n",
    "\n",
    "dN = Date.(string.(ym),\"yyyymmdd\")      #covert to Julia date, eg. 2001-12-31\n",
    "\n",
    "\n",
    "vv = Date(1980,1,1) .<= dN .<= Date(2020,12,31)   #pick out the correct sample\n",
    "ym = ym[vv]\n",
    "R = x[vv,2:end]                   #returns\n",
    "\n",
    "(T,n) = size(R)                   #number of data points, number of assets"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Task 1: Two Simple Strategies\n",
    "\n",
    "`R_1`: go long each of asset 1-24 (each with the weight 1/24) and short asset 25\n",
    "\n",
    "`R_2`: go long asset 1 and short asset 25\n",
    "\n",
    "The returns of these portfolios are easy to calculate without having to explicitly construct the portfolio weights, but it still a good preparation for later to do the explicit calculations as follows:\n",
    "\n",
    "1. Construct the vector of portfolio weights `w`\n",
    "2. The portfolio return in `t` is `w'*R[t,:]`.\n",
    "\n",
    "Also, do not be afraid of loops: they are quick.\n",
    "\n",
    "Show means and standard deviations of the two strategies. Annualize the mean by `*252`  and the standard deviation by `*sqrt(252)`.\n",
    "\n",
    "Plot histograms with bins that are 0.25 wide. (Don't annualize anything in the histograms.)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Task 2: Another Trading Strategy\n",
    "\n",
    "We now do simple volatility based trading strategy.\n",
    "\n",
    "1. Find the 3 least volatile assets over `t-22:t-1` and give each a portfolio weight `w[t,i]=1/3`. \n",
    "\n",
    "2. Find the 3 most volatile assets over `t-22:t-1` and give each a portfolio weight `w[t,i]=-1/3`. \n",
    "\n",
    "3. The portfolio return in `t` is `w[t,:]'*R[t,:]`.\n",
    "\n",
    "4. Compare the average and std (annualized) with the previous portfolios, over periods `23:T`\n",
    "\n",
    "Hint: `v = sortperm(x)` gives indices such that `v[1:2]` are the indices of the lowest 2 elements in x. Try `sortperm([12,11,13])` to see."
   ]
  }
 ],
 "metadata": {
  "@webio": {
   "lastCommId": null,
   "lastKernelId": null
  },
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Julia 1.6.2",
   "language": "julia",
   "name": "julia-1.6"
  },
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "1.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
