Skip to content

Instantly share code, notes, and snippets.

@milktrader
milktrader / TradingInstrumentDemo.jl
Created March 1, 2013 17:07
Gold data is merged with Durable Goods data on Date. It's magic.
julia> using TradingInstrument
julia> GLD = yahoo("GLD");
julia> DurableGoods = fred("DGORDER", "Durable Goods");
julia> head(merge(GLD, DurableGoods))
6x8 DataFrame:
Date Open High Low Close Vol Adj Durable Goods
[1,] 2010-03-01 109.48 109.9 108.97 109.43 12830900 109.43 181812.0
@milktrader
milktrader / testing_in_julia.jl
Last active December 13, 2015 21:48
package testing setup
julia> @timeseries
Running tests:
** test/returns.jl
** test/lag.jl
** test/moving.jl
** test/upto.jl
** test/indexdate.jl
macro in testtimeseries.jl
@milktrader
milktrader / min_var_port.jl
Created February 8, 2013 03:14
An example of 5 assets and their allocation for min variance (tip of the bullet)
julia> foo = ["AMZN", "SPY", "TLT", "SLV", "TWO"];
julia> bar = stock_returns(foo); minvarport(bar)
1x5 DataFrame:
1 2 3 4 5
[1,] 0.365124 0.181558 -0.0579279 0.34096 0.170285
@milktrader
milktrader / asset_allocation.jl
Created February 7, 2013 11:39
Asset allocation from analytic solution
julia> head(df,2) #past 60 months log returns
2x4 DataFrame:
spy tlt gld aapl
[1,] 0.0 0.0 0.0 0.0
[2,] -0.0262318 -0.00458016 0.050976 -0.0794715
julia> dfc = cov(df)
4x4 Float64 Array:
0.00302343 -0.00083886 0.00052545 0.00315787
@milktrader
milktrader / pivotsthisweek.jl
Last active December 12, 2015 05:49
floor trader weekly pivots for this week in SPX index
julia> using Oil
julia> s = yip("^GSPC", 1,1,2013, 2,5,2013, "w");
julia> ss = floor_pivots(s);
julia> tail(ss[:,[1,8:end]], 1)
1x8 DataFrame:
Date S3 S2 S1 pivot R1 R2 R3
[1,] 2013-02-04 1483.45 1489.89 1501.53 1507.97 1519.61 1526.05 1532.49
@milktrader
milktrader / RUT_in_Feb.jl
Last active December 12, 2015 00:29
A couple moments of RUT daily returns in February
✈ julia
_
_ _ _(_)_ | A fresh approach to technical computing
(_) | (_) (_) | Documentation: http://docs.julialang.org
_ _ _| |_ __ _ | Type "help()" to list help topics
| | | | | | |/ _` | |
| | |_| | | | (_| | | Version 0.0.0+108423170.r890f
_/ |\__'_|_|_|\__'_| | Commit 890fa928ca (2013-01-28 20:11:56)
|__/ |
@milktrader
milktrader / add_sma_to_julia_dataframe.jl
Created January 4, 2013 22:46
adding an sma to a Julia DataFrame
julia> spx = read_stock("data/spx.csv");
julia> head(spx, 3)
3x7 DataFrame:
Date Open High Low Close Volume Adj Close
[1,] 1970-01-02 92.06 93.54 91.79 93.0 8050000 93.0
[2,] 1970-01-05 93.0 94.25 92.53 93.46 11490000 93.46
[3,] 1970-01-06 93.46 93.81 92.13 92.82 11460000 92.82
julia> wat = uoo(spx, 4, "Low");
@milktrader
milktrader / load_spx.jl
Created December 28, 2012 14:37
getting market data into Julia
julia> require("Calendar")
julia> using Calendar
julia> require("DataFrames")
julia> using DataFrames
julia> require("Thyme")
julia> using Thyme
julia> spx = read_stock("spx.csv");
julia> check = falses(nrow(spx));
julia> for i in 1:nrow(spx)
check[i] = dayofweek(spx[i,1]) == 6
@milktrader
milktrader / fridays.jl
Created December 28, 2012 14:27
snapshot of SPX returns on Fridays from 1970-71
julia> head(fridays)
6x9 DataFrame:
Date Open High Low Close Volume Adj Close friday RET
[1,] 1970-01-02 92.06 93.54 91.79 93.0 8050000 93.0 true 0.0
[2,] 1970-01-09 92.68 93.25 91.82 92.4 9380000 92.4 true -0.00302115
[3,] 1970-01-16 91.68 92.49 90.36 90.92 11940000 90.92 true -0.0082897
[4,] 1970-01-23 90.04 90.45 88.74 89.37 11000000 89.37 true -0.00744114
[5,] 1970-01-30 85.69 86.33 84.42 85.02 12320000 85.02 true -0.00781888
[6,] 1970-02-06 85.9 86.88 85.23 86.33 10150000 86.33 true 0.00500582
@milktrader
milktrader / nuke_environments.R
Created December 18, 2012 02:21
cleanup the environments that get polluted after a quantstrat strategy is sourced
suppressWarnings(rm(list=ls(.strategy), pos=.strategy))
suppressWarnings(rm(list=ls(.blotter), pos=.blotter))
suppressWarnings(rm(list=ls()))