Skip to content

Instantly share code, notes, and snippets.

@milktrader
milktrader / happy.jl
Last active December 16, 2015 12:48
What percentage of n-period investors are above water in SPX, NDX, RUT? An n-period investor is the one who got into the market n days ago. We survey 200 for n = 1:200
julia> nperiodinvestors("YAHOO/INDEX_GSPC", 200)
0.925
julia> nperiodinvestors("YAHOO/INDEX_NDX", 200)
0.72
julia> nperiodinvestors("YAHOO/INDEX_RUT", 200)
0.81
@milktrader
milktrader / cot.jl
Last active December 16, 2015 01:38
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");
@milktrader
milktrader / timeseries.jl
Created April 5, 2013 14:23
Experimental TimeSeries look and feel
julia> spx
15914-element TimeSeries Array:
1950-01-03 | 16.66 16.66 16.66 16.66
1950-01-04 | 16.85 16.85 16.85 16.85
1950-01-05 | 16.93 16.93 16.93 16.93
1950-01-06 | 16.98 16.98 16.98 16.98
1950-01-09 | 17.08 17.08 17.08 17.08
1950-01-10 | 17.03 17.03 17.03 17.03
1950-01-11 | 17.09 17.09 17.09 17.09
1950-01-12 | 16.76 16.76 16.76 16.76
@milktrader
milktrader / chain_fred.jl
Created March 26, 2013 19:13
what was the average rate for the 10-year in December of 1977
julia> fred("DGS10") | x -> byyear(x, 1977) | x -> bymonth(x, 12) | x -> nanmean(val(x))
7.685238095238096
@milktrader
milktrader / ts_v_xts.jl
Created March 22, 2013 15:23
Execution times for experimental Julia type vs R's xts (R matrix indexed by time)
julia> timetrial(max, vclose(ts), 100) / 0.001 # system.time(max(Cl(GSPC)))
0.17385107000000002
# experimental type about 5x faster on 15,905 rows of data
@milktrader
milktrader / dgs.jl
Created March 21, 2013 15:16
When has the 10-year closed below 1.5% since 1962? Just a couple of compact lines of Julia will tell you.
julia> using TimeSeries
julia> d = imfred("DGS10");
julia> d[v(d) .< 1.5]
6-element TimeStamp Array:
2012-06-01 | 1.47
2012-07-20 | 1.49
2012-07-23 | 1.47
2012-07-24 | 1.44
@milktrader
milktrader / add_up_with_NaN.jl
Created March 21, 2013 14:19
sum array with NaN with two approaches. Create a new array or pass through a do loop in Julia
foo = [ones(1000), NaN]
function removeNaN_sum(x::Array)
newa = typeof(x[1])[]
for i in 1:length(x)
if x[i] <= max(x)
push!(newa, x[i])
end
end
sum(newa)
@milktrader
milktrader / fred_dgs.jl
Created March 21, 2013 01:51
Three lines to find the highest rate on the Ten year note
julia> using TimeSeries
julia> dgs = imfred("DGS10");
julia> maxrows(dgs)
1-element TimeStamp Array:
1981-09-30 | 15.84
@milktrader
milktrader / array_v_dataframe.jl
Last active December 15, 2015 01:59
Speed trials of an Array{TimeStamp} vs DataFrame
# var is an Array{TimeStamp}
# df is a DataFrame
# each is 15850 rows long
julia> @elapsed mean(df["Open"])
0.176795466
julia> @elapsed mean(var)
0.017835577
@milktrader
milktrader / show_timestamp.jl
Created March 17, 2013 16:36
Trying to get an Array{TimeStamp} to show nice
show(io::IO, ts::TimeStamp) = println(ts.timestamp, " ", ts.value)
julia> var[1]
1971-12-31 102.09
julia> var[1:2]
2-element TimeStamp Array:
1971-12-31 102.09
1971-12-30 101.78