|
5 | 5 | To load the IBM historical data, we will use the `MarketData.yahoo` function from [MarketData.jl](https://github.com/JuliaQuant/MarketData.jl), which returns the data in the form of a `TimeArray`. We just simply pass this on to the `TSFrame` constructor.
|
6 | 6 |
|
7 | 7 | ```@repl e1
|
8 |
| -using TSFrames, MarketData, Plots, Statistics |
| 8 | +using TSFrames, MarketData, Plots, Statistics, Impute |
9 | 9 | ibm_ts = TSFrame(MarketData.yahoo(:IBM))
|
10 | 10 | ```
|
11 | 11 |
|
@@ -55,6 +55,12 @@ merged two same-named columns (`AdjClose`) so we use
|
55 | 55 | `TSFrames.rename!()` method to rename the columns to easily
|
56 | 56 | remembered stock names.
|
57 | 57 |
|
| 58 | +## Fill missing values |
| 59 | + |
| 60 | +```@repl e1 |
| 61 | +ibm_aapl = ibm_aapl |> Impute.locf() |
| 62 | +``` |
| 63 | + |
58 | 64 | ## Convert data into weekly frequency using last values
|
59 | 65 |
|
60 | 66 | Here, we convert daily stock data into weekly frequency by taking the
|
@@ -106,3 +112,50 @@ savefig("ts-plot.svg"); nothing # hide
|
106 | 112 | ```
|
107 | 113 |
|
108 | 114 | 
|
| 115 | + |
| 116 | +## Aggregation and rolling window operations |
| 117 | + |
| 118 | +Here, we compute realised volatility of returns of both IBM and Apple |
| 119 | +stock weekly and bi-monthly. Then, we compute daily returns volatility |
| 120 | +on a rolling basis with a window size of 10. |
| 121 | + |
| 122 | +```@repl e1 |
| 123 | +daily_returns = diff(log.(ibm_aapl)) |
| 124 | +rvol = apply(daily_returns, Week(1), std) # Compute the realised volatility |
| 125 | +rvol = apply(daily_returns, Month(2), std) # Every two months |
| 126 | +rollapply(daily_returns, std, 10) # Compute rolling vols |
| 127 | +``` |
| 128 | + |
| 129 | +## Rolling regression with a window of 10 |
| 130 | + |
| 131 | +One of the common finance problems is to run a rolling window |
| 132 | +regression of firm returns over market returns. For doing this, we |
| 133 | +will use the `lm()` function from the `GLM` package. We will create a |
| 134 | +separate function `regress()` which would take in the data as an |
| 135 | +argument and use pre-defined strings to identify the returns columns, |
| 136 | +pass them to `lm()`, and return the results. |
| 137 | + |
| 138 | +We start by downloading the S&P500 daily data from Yahoo Finance, then |
| 139 | +performing the same steps as above to come to a joined `TSFrame` object |
| 140 | +containing daily returns of S&P500 and IBM stock prices. Then, use |
| 141 | +`rollapply()` with `bycolumn=false` to tell `rollapply()` to pass in |
| 142 | +the entire `TSFrame` to the function in one go for each iteration |
| 143 | +within the window. |
| 144 | + |
| 145 | +```@repl e1 |
| 146 | +sp500 = TSFrame(MarketData.yahoo("^GSPC")); |
| 147 | +sp500_adjclose = TSFrames.subset(sp500, date_from, date_to)[:, ["AdjClose"]] |
| 148 | +
|
| 149 | +sp500_ibm = join(sp500_adjclose, ibm_adjclose, jointype=:JoinBoth) |
| 150 | +sp500_ibm_returns = diff(log.(sp500_ibm)) |
| 151 | +TSFrames.rename!(sp500_ibm_returns, ["SP500", "IBM"]); |
| 152 | +
|
| 153 | +function regress(data) |
| 154 | + ll = lm(@formula(IBM ~ SP500), data) |
| 155 | + co::Real = coef(ll)[coefnames(ll) .== "IBM"][1] |
| 156 | + sd::Real = Statistics.std(residuals(ll)) |
| 157 | + return (co, sd) |
| 158 | +end |
| 159 | +
|
| 160 | +rollapply(sp500_ibm_returns, regress, 10, bycolumn=false) |
| 161 | +``` |
0 commit comments