Skip to content

Commit

Permalink
kama: accept integer TimeArray
Browse files Browse the repository at this point in the history
close #83
  • Loading branch information
iblislin committed May 14, 2017
1 parent 84078b8 commit ee3662b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/movingaverages.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function kama{T,N}(ta::TimeArray{T,N}, n::Int=10, fn::Int=2, sn::Int=30)
sc = (er .* (2 / (fn + 1) - 2 / (sn + 1)) .+ 2 / (sn + 1)).^2

cl = ta[n+1:end]
vals = similar(cl.values)
vals = similar(Array{Float64}, indices(cl.values))
# using simple moving average as initial kama
pri_kama = mean(ta[1:n].values, 1)

Expand All @@ -68,7 +68,14 @@ function kama{T,N}(ta::TimeArray{T,N}, n::Int=10, fn::Int=2, sn::Int=30)
pri_kama .+ sc[idx].values .* (cl[idx].values .- pri_kama)
end

TimeArray(cl.timestamp, vals, ["$c\_kama" for c in ta.colnames])
cols =
if length(ta.colnames) == 1
["kama"]
else
["$c\_kama" for c in ta.colnames]
end

TimeArray(cl.timestamp, vals, cols)
end

# Array dispatch for use by other algorithms
Expand Down
4 changes: 4 additions & 0 deletions test/movingaverages.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,14 @@ facts("Moving averages on TimeArrays") do
@fact ta.values[2] --> roughly(99.0098, atol=.01)
@fact ta.values[3] --> roughly(99.4499, atol=.01)
@fact ta.timestamp[1] --> Date(2000, 1, 18)
@fact ta.colnames --> ["kama"]

ta = kama(ohlc)
@fact length(ta.colnames) --> 4
@fact ta.timestamp[1] --> Date(2000, 1, 18)

ta = kama(TimeArray(collect(Date(2011, 1, 1):Date(2011, 1, 20)), 1:20))
@fact ta.timestamp[end] --> Date(2011, 1, 20)
end
end

Expand Down

0 comments on commit ee3662b

Please sign in to comment.