Skip to content

Commit

Permalink
Merge pull request #88 from JuliaQuant/ib-keltner
Browse files Browse the repository at this point in the history
Implement Keltner Channels
  • Loading branch information
iblislin committed May 29, 2017
2 parents a4888e3 + 409ba41 commit 793c701
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 24 deletions.
11 changes: 10 additions & 1 deletion docs/src/volatility.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,13 @@ atr(ohlc)

## Keltner Bands

*Not Implemented*
```@docs
keltnerbands
```

```@repl
using MarketData
using MarketTechnicals
keltnerbands(ohlc)
```
2 changes: 1 addition & 1 deletion src/MarketTechnicals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module MarketTechnicals
using TimeSeries, StatsBase

export sma, ema, kama,
bollingerbands, truerange, atr, #keltnerbands,
bollingerbands, truerange, atr, keltnerbands,
obv, vwap,
doji,
rsi, macd, cci, roc,
Expand Down
55 changes: 39 additions & 16 deletions src/volatility.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,22 +59,45 @@ function atr{T,N}(ohlc::TimeArray{T,N}, n::Integer=14;
TimeArray(res.timestamp, res.values, ["atr"], ohlc.meta)
end

# function keltnerbands{T,N}(ohlc::TimeArray{T,N}, n::Int)
# typ = typical(ohlc)
# rng = ohlc["High"] .- ohlc["Low"]
# rma = sma(rng, n)
#
# kma = sma(typ, n)
# tstamps = kma.timestamp
#
# kma = TimeArray(tstamps, kma.values, ["kma"])
# kup = TimeArray(tstamps, (kma.+rma).values, ["kup"])
# kdn = TimeArray(tstamps, (kma.-rma).values, ["kdn"])
#
# merge(kma, merge(kup, kdn))
# end

keltnerbands{T,N}(ohlc::TimeArray{T,N}) = keltnerbands(ohlc, 10)
atr{T,N}(ta::TimeArray{T,N}) = atr(ta, 14)

doc"""
keltnerbands(ohlc, n=20, w=2; h="High", l="Low", c="Close")
**Keltner Channels**
Linda Bradford Raschke introduced the newer version of Keltner Channels
in the 1980s. We implement the newer version.
**Formula**
```math
\begin{align*}
\text{Up} & = \text{Mid} + w \times ATR(n) \\
\text{Mid} & = EMA(P_{typical}, n) \\
\text{Down} & = \text{Mid} - w \times ATR(n)
\end{align*}
```
**Referenc**
- [StockCharts]
(http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:keltner_channels)
- [Wikipedia]
(https://en.wikipedia.org/wiki/Keltner_channel)
"""
function keltnerbands{T,N}(ohlc::TimeArray{T,N}, n::Integer=20, w::Integer=2;
h="High", l="Low", c="Close")
kma = rename(ema(typical(ohlc, h=h, l=l, c=c), n), "kma")
rng = atr(ohlc, n, h=h, l=l, c=c)

kup = rename(kma .+ (2 .* rng), "kup")
kdn = rename(kma .- (2 .* rng), "kdn")

merge(kup, merge(kma, kdn))
end

# # function chaikinvolatility{T,N}(ta::TimeArray{T,N}, n::Int)
# # #code here
Expand Down
17 changes: 11 additions & 6 deletions test/volatility.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,17 @@ facts("Volatility") do
@fact atr(ohlc).timestamp[1] --> Date(2000,1,24)
end

# context("keltner_bands") do
# @fact keltnerbands(ohlc)["kma"].values[end] --> roughly(21.42) # needs confirmation
# @fact keltnerbandb(ohlc)["kup"].values[end] --> roughly(22.32) # needs confirmation
# @fact keltnerbands(ohlc)["kdn"].values[end] --> roughly(20.52) # needs confirmation
# @fact keltnerbands(ohlc).timestamp[1] --> Date(2000,1,14)
# end
context("keltner_bands") do
ta = keltnerbands(ohlc)
@fact ta["kup"].values[end] > ta["kma"].values[end] --> true
@fact ta["kma"].values[end] > ta["kdn"].values[end] --> true

@fact ta["kup"].values[end] --> roughly(23.3156, atol=.01) # needs confirmation
@fact ta["kma"].values[end] --> roughly(21.3705, atol=.01) # needs confirmation
@fact ta["kdn"].values[end] --> roughly(19.4254, atol=.01) # needs confirmation
@fact ta.timestamp[1] --> Date(2000, 2, 1)
@fact ta.timestamp[end] --> Date(2001, 12, 31)
end

# context("chaikin_volatility") do
# @fact chk --> 17.0466
Expand Down

0 comments on commit 793c701

Please sign in to comment.