{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Load Packages" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "printyellow (generic function with 1 method)" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "using Printf, Dates, Statistics, CSV, DataFrames\n", "include(\"jlFiles/printmat.jl\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Loading Some Data with CSV.jl" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The first 4 lines of Data/Options_prices_US_Canada.csv:\n", "\n", "symbol,exchange,date,adjusted close,option symbol,expiration,strike,call/put,style,ask,bid,volume,open interest,unadjusted\n", "SPX,CBOE,03/30/17,2368.06,SPXW 170331C00300000,03/31/17,300,C,E,2073.9,2062.9,0,0,2368.927\n", "SPX,CBOE,03/30/17,2368.06,SPXW 170331P00300000,03/31/17,300,P,E,0.1,0,0,0,2368.927\n", "SPX,CBOE,03/30/17,2368.06,SPXW 170331C00400000,03/31/17,400,C,E,1974.1,1962.7,0,0,2368.927\n", "\n" ] } ], "source": [ "DataFile = \"Data/Options_prices_US_Canada.csv\"\n", "\n", "println(\"The first 4 lines of $(DataFile):\\n\")\n", "txt = readlines(DataFile)\n", "printmat(txt[1:4])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Use `normalizenames` to get names that can be used in Julia as variables names and specify the `dateformat` used in the csv file (to convert to proper Julia dates). The dates in the file are given as `03/30/17` which CSV/DataFrames interpret as 30 March year 17 (AD). We add `Dates.Year(2000)` to get year 2017." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[1m13952×10 DataFrame\u001b[0m\n", "\u001b[1m Row \u001b[0m│\u001b[1m symbol \u001b[0m\u001b[1m date \u001b[0m\u001b[1m close \u001b[0m\u001b[1m expiration \u001b[0m\u001b[1m strike \u001b[0m\u001b[1m call_put \u001b[0m\u001b[1m ask \u001b[0m\u001b[1m\u001b[0m ⋯\n", "\u001b[1m \u001b[0m│\u001b[90m String3 \u001b[0m\u001b[90m Date \u001b[0m\u001b[90m Float64 \u001b[0m\u001b[90m Date \u001b[0m\u001b[90m Float64 \u001b[0m\u001b[90m String1 \u001b[0m\u001b[90m Float64 \u001b[0m\u001b[90m\u001b[0m ⋯\n", "───────┼────────────────────────────────────────────────────────────────────────\n", " 1 │ SPX 2017-03-30 2368.06 2017-03-31 300.0 C 2073.9 ⋯\n", " 2 │ SPX 2017-03-30 2368.06 2017-03-31 300.0 P 0.1\n", " 3 │ SPX 2017-03-30 2368.06 2017-03-31 400.0 C 1974.1\n", " 4 │ SPX 2017-03-30 2368.06 2017-03-31 400.0 P 0.05\n", " 5 │ SPX 2017-03-30 2368.06 2017-03-31 500.0 C 1874.1 ⋯\n", " 6 │ SPX 2017-03-30 2368.06 2017-03-31 500.0 P 0.05\n", " 7 │ SPX 2017-03-30 2368.06 2017-03-31 600.0 C 1774.1\n", " 8 │ SPX 2017-03-30 2368.06 2017-03-31 600.0 P 0.05\n", " 9 │ SPX 2017-03-30 2368.06 2017-03-31 700.0 C 1673.9 ⋯\n", " 10 │ SPX 2017-03-30 2368.06 2017-03-31 700.0 P 0.05\n", " 11 │ SPX 2017-03-30 2368.06 2017-03-31 750.0 C 1624.1\n", " ⋮ │ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋱\n", " 13943 │ XIU 2017-03-30 23.06 2019-03-15 21.0 C 3.21\n", " 13944 │ XIU 2017-03-30 23.06 2019-03-15 21.0 P 1.71 ⋯\n", " 13945 │ XIU 2017-03-30 23.06 2019-03-15 22.0 C 2.53\n", " 13946 │ XIU 2017-03-30 23.06 2019-03-15 22.0 P 2.03\n", " 13947 │ XIU 2017-03-30 23.06 2019-03-15 23.0 C 1.97\n", " 13948 │ XIU 2017-03-30 23.06 2019-03-15 23.0 P 2.51 ⋯\n", " 13949 │ XIU 2017-03-30 23.06 2019-03-15 24.0 C 1.5\n", " 13950 │ XIU 2017-03-30 23.06 2019-03-15 24.0 P 3.12\n", " 13951 │ XIU 2017-03-30 23.06 2019-03-15 25.0 C 1.13\n", " 13952 │ XIU 2017-03-30 23.06 2019-03-15 25.0 P 3.64 ⋯\n", "\u001b[36m 3 columns and 13931 rows omitted\u001b[0m" ] } ], "source": [ "df1 = CSV.read(DataFile,DataFrame,normalizenames=true,dateformat=\"mm/dd/yy\")\n", "\n", "df1.date .+= Dates.Year(2000) #03/30/17 to 03/30/2017\n", "df1.expiration .+= Dates.Year(2000)\n", "\n", "select!(df1,Not([:exchange,:option_symbol,:style,:unadjusted])) #deleting some columns\n", "rename!(df1,:adjusted_close => :close) #renaming a column\n", "\n", "show(df1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Task 1\n", "\n", "Create a new DataFrame that contains only the data for SPX and those option contracts that were traded (volume > 0). Hint: `df1[vv, :]` picks out the rows of the data frame for which `vv` is `true`. " ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[1m2359×10 DataFrame\u001b[0m\n", "\u001b[1m Row \u001b[0m│\u001b[1m symbol \u001b[0m\u001b[1m date \u001b[0m\u001b[1m close \u001b[0m\u001b[1m expiration \u001b[0m\u001b[1m strike \u001b[0m\u001b[1m call_put \u001b[0m\u001b[1m ask \u001b[0m\u001b[1m \u001b[0m ⋯\n", "\u001b[1m \u001b[0m│\u001b[90m String3 \u001b[0m\u001b[90m Date \u001b[0m\u001b[90m Float64 \u001b[0m\u001b[90m Date \u001b[0m\u001b[90m Float64 \u001b[0m\u001b[90m String1 \u001b[0m\u001b[90m Float64 \u001b[0m\u001b[90m \u001b[0m ⋯\n", "──────┼─────────────────────────────────────────────────────────────────────────\n", " 1 │ SPX 2017-03-30 2368.06 2017-03-31 1600.0 C 774.0 ⋯\n", " 2 │ SPX 2017-03-30 2368.06 2017-03-31 1600.0 P 0.05\n", " 3 │ SPX 2017-03-30 2368.06 2017-03-31 2040.0 P 0.05\n", " 4 │ SPX 2017-03-30 2368.06 2017-03-31 2050.0 P 0.05\n", " 5 │ SPX 2017-03-30 2368.06 2017-03-31 2100.0 P 0.05 ⋯\n", " 6 │ SPX 2017-03-30 2368.06 2017-03-31 2110.0 C 264.5\n", " 7 │ SPX 2017-03-30 2368.06 2017-03-31 2120.0 P 0.05\n", " 8 │ SPX 2017-03-30 2368.06 2017-03-31 2150.0 P 0.05\n", " 9 │ SPX 2017-03-30 2368.06 2017-03-31 2175.0 C 199.5 ⋯\n", " 10 │ SPX 2017-03-30 2368.06 2017-03-31 2175.0 P 0.05\n", " 11 │ SPX 2017-03-30 2368.06 2017-03-31 2180.0 C 194.5\n", " ⋮ │ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋱\n", " 2350 │ SPX 2017-03-30 2368.06 2019-12-20 1900.0 P 111.4\n", " 2351 │ SPX 2017-03-30 2368.06 2019-12-20 2000.0 P 136.0 ⋯\n", " 2352 │ SPX 2017-03-30 2368.06 2019-12-20 2200.0 C 345.7\n", " 2353 │ SPX 2017-03-30 2368.06 2019-12-20 2300.0 C 287.0\n", " 2354 │ SPX 2017-03-30 2368.06 2019-12-20 2350.0 C 259.7\n", " 2355 │ SPX 2017-03-30 2368.06 2019-12-20 2375.0 P 264.3 ⋯\n", " 2356 │ SPX 2017-03-30 2368.06 2019-12-20 2400.0 C 230.7\n", " 2357 │ SPX 2017-03-30 2368.06 2019-12-20 2400.0 P 275.3\n", " 2358 │ SPX 2017-03-30 2368.06 2019-12-20 2800.0 C 74.9\n", " 2359 │ SPX 2017-03-30 2368.06 2019-12-20 3000.0 C 37.5 ⋯\n", "\u001b[36m 3 columns and 2338 rows omitted\u001b[0m" ] } ], "source": [ "vv = (df1.symbol .== \"SPX\") .& (df1.volume .> 0) #rows with SPX, and trade\n", "\n", "df2 = df1[vv, :] #create new df, only some rows\n", "\n", "show(df2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Task 2\n", "\n", "Create a *group* for each expiration date. These groups can be referred to as `dataG2[key]`.\n", "\n", "Hints: `groupby()`" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "dataG2 = groupby(df2,:expiration); #grouped by expiration date" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Task 3\n", "\n", "Print the number of contracts (`nrow`) and the sum of the open interest `:open_interest=>sum` for each of the expiration dates.\n", "\n", "Hint: `combine()`" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
34 rows × 3 columns
expiration | nrow | open_interest_sum | |
---|---|---|---|
Date | Int64 | Int64 | |
1 | 2017-03-31 | 100 | 1010183 |
2 | 2017-04-03 | 94 | 109345 |
3 | 2017-04-05 | 84 | 102022 |
4 | 2017-04-07 | 140 | 467105 |
5 | 2017-04-10 | 73 | 51755 |
6 | 2017-04-12 | 67 | 56228 |
7 | 2017-04-13 | 109 | 303944 |
8 | 2017-04-17 | 70 | 40855 |
9 | 2017-04-19 | 57 | 25690 |
10 | 2017-04-21 | 166 | 1775684 |
11 | 2017-04-24 | 69 | 15085 |
12 | 2017-04-26 | 56 | 6560 |
13 | 2017-04-28 | 124 | 467161 |
14 | 2017-05-05 | 129 | 169530 |
15 | 2017-05-12 | 121 | 225600 |
16 | 2017-05-19 | 158 | 937452 |
17 | 2017-05-26 | 95 | 12914 |
18 | 2017-05-31 | 105 | 120944 |
19 | 2017-06-02 | 34 | 0 |
20 | 2017-06-16 | 126 | 1956193 |
21 | 2017-06-30 | 72 | 136661 |
22 | 2017-07-21 | 39 | 51058 |
23 | 2017-07-31 | 27 | 56511 |
24 | 2017-08-31 | 14 | 15578 |
25 | 2017-09-15 | 45 | 524556 |
26 | 2017-09-29 | 17 | 45842 |
27 | 2017-12-15 | 46 | 759858 |
28 | 2017-12-29 | 22 | 35398 |
29 | 2018-01-19 | 18 | 78967 |
30 | 2018-03-16 | 16 | 15051 |
⋮ | ⋮ | ⋮ | ⋮ |