Skip to content

Commit

Permalink
Merge branch 'master' into ib-keltner
Browse files Browse the repository at this point in the history
  • Loading branch information
iblislin committed May 29, 2017
2 parents 245e536 + a4888e3 commit 409ba41
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 8 deletions.
4 changes: 3 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
### 0.6.0

* Drop 0.4 support (issue #86)

* New indicator:
* Kaufman's Adaptive Moving Average (issue #76)
* Kaufman's Adaptive Moving Average (issue #76, #83)

* ROC (issue #82)

Expand Down
2 changes: 1 addition & 1 deletion src/MarketTechnicals.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION >= v"0.4.0-dev+6521" && __precompile__(true)
__precompile__(true)

using TimeSeries, StatsBase

Expand Down
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
8 changes: 4 additions & 4 deletions src/volatility.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,15 @@ Bollinger Bands
\end{align*}
```
"""
function bollingerbands{T,N}(ta::TimeArray{T,N}, ma::Int, width::Float64)
function bollingerbands{T,N}(ta::TimeArray{T,N}, ma::Integer=20,
width::AbstractFloat=2.0)
tama = sma(ta, ma)
upband = tama .+ moving(ta, std, ma) .* width .* sqrt((ma-1)/ma) # take out Bessel correction, per algorithm
dnband = tama .- moving(ta, std, ma) .* width .* sqrt((ma-1)/ma)
bands = merge(upband, dnband)
merge(bands, tama, colnames = ["up", "down", "mean"])
end

bollingerbands{T,N}(ta::TimeArray{T,N}) = bollingerbands(ta, 20, 2.0)

doc"""
truerange(ohlc; h="High", l="Low", c="Close")
Expand Down Expand Up @@ -53,7 +52,8 @@ It's the exponential moving average of [`truerange`](@ref)
```
"""
function atr{T,N}(ohlc::TimeArray{T,N}, n::Int; h="High", l="Low", c="Close")
function atr{T,N}(ohlc::TimeArray{T,N}, n::Integer=14;
h="High", l="Low", c="Close")
# atr was invented by Wilder, so only his ema is currently supported
res = ema(truerange(ohlc), n, wilder=true)
TimeArray(res.timestamp, res.values, ["atr"], ohlc.meta)
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 409ba41

Please sign in to comment.