{ "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": "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": "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": "markdown", "metadata": {}, "source": [ "# Task 4 \n", "Creating two new DataFrames: for expiration date 2017-04-21 and another for 2017-06-16.\n", "\n", "Hint: `dataG2[(expiration = Date(\"2017-04-21\"),)]`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Task 5\n", "\n", "For the expiration date 2017-04-21, calculate the mid price as the average of the `.ask` and `.bid`. \n", "\n", "Plot the mid price as a function of the strike price `.strike` for put options. Add a curve another curve for the call options." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "@webio": { "lastCommId": null, "lastKernelId": null }, "anaconda-cloud": {}, "kernelspec": { "display_name": "Julia 1.7.0", "language": "julia", "name": "julia-1.7" }, "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "1.7.0" }, "nteract": { "version": "0.23.1" } }, "nbformat": 4, "nbformat_minor": 4 }