Skip to content

Commit e3e21e9

Browse files
authored
Merge pull request #166 from xKDR/finance_demo_update
update for JuliaCon 2023 talk
2 parents 305267d + 8b3daa6 commit e3e21e9

File tree

1 file changed

+54
-1
lines changed

1 file changed

+54
-1
lines changed

docs/src/demo_finance.md

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
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.
66

77
```@repl e1
8-
using TSFrames, MarketData, Plots, Statistics
8+
using TSFrames, MarketData, Plots, Statistics, Impute
99
ibm_ts = TSFrame(MarketData.yahoo(:IBM))
1010
```
1111

@@ -55,6 +55,12 @@ merged two same-named columns (`AdjClose`) so we use
5555
`TSFrames.rename!()` method to rename the columns to easily
5656
remembered stock names.
5757

58+
## Fill missing values
59+
60+
```@repl e1
61+
ibm_aapl = ibm_aapl |> Impute.locf()
62+
```
63+
5864
## Convert data into weekly frequency using last values
5965

6066
Here, we convert daily stock data into weekly frequency by taking the
@@ -106,3 +112,50 @@ savefig("ts-plot.svg"); nothing # hide
106112
```
107113

108114
![](ts-plot.svg)
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

Comments
 (0)