Skip to content

Instantly share code, notes, and snippets.

@milktrader
Last active December 16, 2015 01:38
Show Gist options
  • Save milktrader/5356452 to your computer and use it in GitHub Desktop.
Save milktrader/5356452 to your computer and use it in GitHub Desktop.
Commitment of Traders as features of a model
julia> using Quandl, TimeSeries
julia> rut = quandl("YAHOO/INDEX_RUT");
julia> cot_rut = quandl("OFDP/FUTURE_COT_38");
julia> r = merge(rut, cot_rut, "Date");
julia> simple_return!(r, "Close");
julia> colnames(r)
17-element Union(UTF8String,ASCIIString) Array:
"Date"
"Open"
"High"
"Low"
"Close"
"Volume"
"Adjusted Close\n"
"Non-Commercial Long"
"Non-Commercial Short"
"Non-Commercial Spreads"
"Commercial Long"
"Commercial Short"
"Total Long"
"Total Short"
"Nonreportable Positions Long"
"Nonreportable Positions Short\n"
"Close_RET"
julia> within!(r, :(y = $lead($r["Close_RET"])));
julia> head(r[:,["Close_RET", "y"]])
6x2 DataFrame:
Close_RET y
[1,] 0.0 0.0129526
[2,] 0.0129526 -0.0397666
[3,] -0.0397666 -0.0270627
[4,] -0.0270627 0.0218141
[5,] 0.0218141 -0.0293981
[6,] -0.0293981 0.0356801
julia> rfit = r[:,[8:16, 18]];
julia> rX = [ones(nrow(rfit))];
julia> for i in 2:ncol(rfit)-1
rX = [rX rfit[i]]
end
julia> rX = rX[1:end-1,:]; # taking out the last observation since it's prediction is missing
julia> ry = [float(rfit[1:end-1,ncol(rfit)])]; # matches what we just did with feature matrix
# theta = pinv(X'X) * X'* y
julia> rtheta = pinv(rX'rX) * rX'* ry # regression parameters
julia> rtheta' * rXnext'
1x1 Float64 Array:
-0.00167277
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment