Skip to content

Instantly share code, notes, and snippets.

macro select(ex::Expr, sym1::Symbol, ex1::Expr, sym2::Symbol, ex3::Expr)
ex.args, sym1, ex1.args, sym2, ex3.args
end
julia> @select open, high, close in ohlcv where close > open when day=Friday
(Any[:open,:high,:(close in ohlcv)],:where,Any[:>,:close,:open],:when,Any[:day,:Friday])
# would be nice to do this instead of a one-liner
@select open, high, close
@milktrader
milktrader / monad
Last active July 28, 2016 15:50
using functions as fields in a type
type Foo
f::Function
end
bar = Foo(x->2x)
bar.f(2)
4
@milktrader
milktrader / long.jl
Created January 17, 2014 04:13
long macro
julia> using TradeModels, MarketData
julia> @long cl rsi (.>) 50 nextday op
0.3999243140318183
@milktrader
milktrader / fastmoving.jl
Last active January 3, 2016 11:49
Turbo boosting the moving method with Iterators (h/t Slender Means)
julia> using Series, MarketData, FactCheck
julia> @time fast = fastmoving(Cl, mean, 60);
elapsed time: 0.305857977 seconds (72646744 bytes allocated)
julia> @time slow = moving(Cl, mean, 60);
elapsed time: 4.925385985 seconds (2079575504 bytes allocated)
julia> @fact fast[end].value => roughly(slow[end].value)
Success :: :(fast[end].value) => :(roughly(slow[end].value))
@milktrader
milktrader / first_six_weeks_1999.jl
Created January 15, 2014 20:01
Simple return for first six weeks of 1999 for SPX
julia> using Series, MarketData, Lazy
julia> d1 = date(1999,1,1);
julia> d2 = date(1999,2,15);
julia> @> Op percentchange x->x[[d1:d2]] value sum expm1
0.017947448312809502
@milktrader
milktrader / lazyequity.jl
Created January 12, 2014 14:38
Lazy and Series
julia> using Lazy, Series, MarketData
julia> head(cl)
1-element Array{SeriesPair{Date{ISOCalendar},Float64},1}:
1980-01-03 105.220
julia> @> cl value log diff cumsum expm1 x -> x+1 x -> x[size(x,1)]
1.1647025280364955
@milktrader
milktrader / backtest_one_liner.jl
Created January 11, 2014 15:19
One liner backtest algorithm in Julia
julia> plot(cumsum(value(when(lead(percentchange(Op, method="log"), 2), index(istrue(sma(Cl,10) .> sma(Cl,30)))))))
@milktrader
milktrader / backtest2.jl
Created January 7, 2014 03:05
Iterating across parameters in naive backtest script in Julia
julia> using Series, MarketTechnicals, Jig.Quant
julia> fast = [5:15]; slow = [20:2:40];
julia> dailyreturn = percentchange(cl, method="log");
julia> for f in fast
for s in slow
sig = sma(cl,f) .> sma(cl,s)
when(dailyreturn, index(istrue(sig))) |> x -> sum(value(x)) |> expm1 |> x -> print(f," ",s," ",round(x,2), " -- ")
@milktrader
milktrader / gist:8293900
Created January 7, 2014 02:44
Nascent backtest framework in Julia
julia> using Series, MarketTechnicals, Jig.Quant
julia> fast = sma(cl, 10);
julia> slow = sma(cl, 30);
julia> sig = fast .> slow;
julia> dailyreturn = percentchange(cl, method="log");
@milktrader
milktrader / when.jl
Created December 28, 2013 15:31
more conversation
# this works but might be difficult to humanely parse
# can you guess what it means?
# answer below
julia> doji(op,hi,lo,cl) |> istrue |> index |> x -> when(cl, x)