Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Julia 0.7 #19

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ language: julia
os:
- linux
julia:
- 0.6
- 0.7
- 1.0
- nightly
matrix:
allow_failures:
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ The key indicator used in this strategy is John Ehlers's MESA Adaptive Moving Av
This strategy simply goes long when the *MAMA* crosses over the *FAMA*, and goes short when the *FAMA* crosses over the *MAMA*. Below is an implementation that shows how to set default arguments to the `Indicators.mama` function and run a simple backtest using those parameters, and also define specified ranges over which we might like to see how the strategy behaves under different parameter sets.

```julia
using Strategems, Temporal, Indicators, Base.Dates
using Strategems, Temporal, Indicators
using Dates

# define universe and gather data
assets = ["CHRIS/CME_CL1", "CHRIS/CME_RB1"]
Expand Down
2 changes: 1 addition & 1 deletion REQUIRE
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
julia 0.6
julia 0.7
Temporal
Indicators
5 changes: 3 additions & 2 deletions examples/faber.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Strategems, Temporal, Indicators, Base.Dates
using Base.Test
using Strategems, Temporal, Indicators
using Dates
using Test

# define universe and gather data
assets = ["EOD/AAPL", "EOD/MCD", "EOD/JPM", "EOD/MMM", "EOD/XOM"]
Expand Down
5 changes: 3 additions & 2 deletions examples/mama.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Strategems, Temporal, Indicators, Base.Dates
using Base.Test
using Strategems, Temporal, Indicators
using Dates
using Test

# define universe and gather data
assets = ["CHRIS/CME_CL1", "CHRIS/CME_RB1"]
Expand Down
2 changes: 1 addition & 1 deletion src/Strategems.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
VERSION >= v"0.6-" && __precompile__(true)

module Strategems
using Base.Dates
using Dates
using Temporal
using Indicators

Expand Down
2 changes: 1 addition & 1 deletion src/indicator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function calculate(indicator::Indicator, input::TS)::TS
return indicator.fun(input; generate_dict(indicator.paramset)...)
end

# function calculate!(indicator::Indicator, input::TS)::Void
# function calculate!(indicator::Indicator, input::TS)::Nothing
# indicator.data = calculate(indicator, input)
# return nothing
# end
Expand Down
10 changes: 5 additions & 5 deletions src/order.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ end
#TODO: add logic whereby the order logic is altered by the type `T` of qty
# (if T<:Int, order that many *shares*, else if T<:Float64, interpret qty as a fraction of the portfolio at time t)

liquidate{T<:Real}(qty::T) = qty
(liquidate(qty::T)::T) where {T<:Real} = qty

long{T<:Real}(qty::T)::T = qty
buy{T<:Real}(qty::T)::T = qty
(long(qty::T)::T) where {T<:Real} = qty
(buy(qty::T)::T) where {T<:Real} = qty

short{T<:Real}(qty::T)::T = qty
sell{T<:Real}(qty::T)::T = qty
(short(qty::T)::T) where {T<:Real} = qty
(sell(qty::T)::T) where {T<:Real} = qty

4 changes: 2 additions & 2 deletions src/paramset.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function get_n_runs(ps::ParameterSet)::Int
end

function get_param_combos(ps::ParameterSet; n_runs::Int=get_n_runs(ps))::Matrix
combos = Matrix{Any}(n_runs, ps.n_args)
combos = Matrix{Any}(undef, n_runs, ps.n_args)
P = 1
for j in 1:ps.n_args
n_vals = length(ps.arg_ranges[j])
Expand All @@ -38,7 +38,7 @@ function get_param_combos(ps::ParameterSet; n_runs::Int=get_n_runs(ps))::Matrix
last_row = first_row + n_vals*step_by - 1
rows = first_row:step_by:last_row
arg_val = ps.arg_ranges[j][i]
combos[rows,j] = arg_val
combos[rows,j] .= arg_val
end
P *= n_vals
end
Expand Down
2 changes: 1 addition & 1 deletion src/results.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mutable struct Results
optimization::Matrix{Float64}
function Results(trades::Dict{String,TS}=Dict{String,TS}(),
backtest::Dict{String,TS{Float64}}=Dict{String,TS{Float64}}(),
optimization::Matrix{Float64}=Matrix{Float64}(0,0))
optimization::Matrix{Float64}=Matrix{Float64}(undef,0,0))
return new(trades, backtest, optimization)
end
end
Expand Down
2 changes: 1 addition & 1 deletion src/rule.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ macro rule(logic::Expr, args...)
trigger = :($(logic.args[2]))
#action = :($(logic.args[3])$((args...)))
action = :($(logic.args[3]))
args = :($(args...))
args = :($(args))
return esc(:(Rule($trigger, $action, $args)))
end

Expand Down
14 changes: 8 additions & 6 deletions src/strategy.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Random

#=
Type definition and methods containing the overarching backtesting object fueling the engine
=#
Expand Down Expand Up @@ -32,7 +34,7 @@ function generate_trades(strat::Strategy; verbose::Bool=true)::Dict{String,TS}
return all_trades
end

function generate_trades!(strat::Strategy; args...)::Void
function generate_trades!(strat::Strategy; args...)::Nothing
strat.results.trades = generate_trades(strat; args...)
return nothing
end
Expand Down Expand Up @@ -81,7 +83,7 @@ function backtest(strat::Strategy; px_trade::Symbol=:Open, px_close::Symbol=:Set
return result
end

function backtest!(strat::Strategy; args...)::Void
function backtest!(strat::Strategy; args...)::Nothing
strat.results.backtest = backtest(strat; args...)
return nothing
end
Expand All @@ -91,7 +93,7 @@ Base.copy(strat::Strategy) = Strategy(strat.universe, strat.indicator, strat.rul
# define matrix row iterator protocol
# this allows us to `enumerate(EachRow(M))`
# thereby getting the count of the iteration as well as the row
immutable EachRow{T<:AbstractMatrix}
struct EachRow{T<:AbstractMatrix}
A::T
end
Base.start(::EachRow) = 1
Expand All @@ -106,7 +108,7 @@ function optimize(strat::Strategy; samples::Int=0, seed::Int=0, verbose::Bool=tr
n_runs = get_n_runs(strat.indicator.paramset)
idx_samples::Vector{Int} = collect(1:n_runs)
if samples > 0
srand(seed)
Random.seed!(seed)
idx_samples = rand(idx_samples, samples)
else
samples = n_runs
Expand All @@ -126,12 +128,12 @@ function optimize(strat::Strategy; samples::Int=0, seed::Int=0, verbose::Bool=tr
end

# TODO: implement function to edit results member of strat in place
function optimize!(strat::Strategy; samples::Int=0, seed::Int=0, verbose::Bool=true, summary_fun::Function=cum_pnl, args...)::Void
function optimize!(strat::Strategy; samples::Int=0, seed::Int=0, verbose::Bool=true, summary_fun::Function=cum_pnl, args...)::Nothing
n_runs = get_n_runs(strat.indicator.paramset)
idx_samples::Vector{Int} = collect(1:n_runs)
if samples > 0
if seed >= 0
srand(seed)
Random.seed!(seed)
end
idx_samples = rand(idx_samples, samples)
else
Expand Down
8 changes: 4 additions & 4 deletions src/universe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ mutable struct Universe
data::Dict{String,TS}
from::TimeType
thru::TimeType
function Universe(assets::Vector{String}, from::TimeType=Base.Dates.Date(0), thru::TimeType=Base.Dates.today())
function Universe(assets::Vector{String}, from::TimeType=Dates.Date(0), thru::TimeType=Dates.today())
@assert assets == unique(assets)
# tickers = guess_tickers(assets)
data = Dict{String,TS}()
Expand All @@ -28,9 +28,9 @@ mutable struct Universe
end

#TODO: ensure type compatibility across variables (specifically with regard to TimeTypes)
function gather!(universe::Universe; source::Function=Temporal.quandl, verbose::Bool=true)::Void
t0 = Vector{Base.Dates.Date}()
tN = Vector{Base.Dates.Date}()
function gather!(universe::Universe; source::Function=Temporal.quandl, verbose::Bool=true)::Nothing
t0 = Vector{Dates.Date}()
tN = Vector{Dates.Date}()
@inbounds for asset in universe.assets
verbose ? print("Sourcing data for asset $asset...") : nothing
indata = source(asset)
Expand Down
8 changes: 5 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Strategems, Temporal, Indicators, Base.Dates
using Base.Test
using Strategems, Temporal, Indicators
using Dates
using Test
using Pkg

# define universe and gather data
assets = ["Corn"]
Expand All @@ -22,7 +24,7 @@ assets = ["Corn"]
# end
# end
# gather!(universe, source=datasource)
gather!(universe, source=(asset)->Temporal.tsread("$(Pkg.dir("Temporal"))/data/$asset.csv"))
gather!(universe, source=(asset)->Temporal.tsread(joinpath(dirname(pathof(Temporal)), "..", "data/$asset.csv")))
@test length(setdiff(assets, collect(keys(universe.data)))) == 0
end
end
Expand Down