Skip to content

Commit

Permalink
Merge branch 'master' into ib-fix-cci
Browse files Browse the repository at this point in the history
  • Loading branch information
iblislin authored May 17, 2017
2 parents b02f9f6 + 20ec575 commit fa56711
Show file tree
Hide file tree
Showing 12 changed files with 95 additions and 12 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* New indicator:
* Kaufman's Adaptive Moving Average (issue #76)

* ROC (issue #82)

* doc: migrate to `Documenter.jl`, and the online doc is available at github
pages. (issue #63, #58, #69)

Expand Down
1 change: 1 addition & 0 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ makedocs(
"volatility.md",
"volume.md",
"candlesticks.md",
"utils.md",
]
)

Expand Down
1 change: 1 addition & 0 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ Pages = [
"volatility.md",
"volume.md",
"candlesticks.md",
"utils.md",
]
```
14 changes: 14 additions & 0 deletions docs/src/ma.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,17 @@ using MarketTechnicals
ema(cl, 10, wilder=true)
```

## Kaufman's Adaptive Moving Average

```@docs
kama
```


```@repl
using MarketData
using MarketTechnicals
kama(cl)
```
13 changes: 13 additions & 0 deletions docs/src/momentum.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,16 @@ using MarketTechnicals
cci(ohlc)
```

## ROC

```@docs
roc
```

```@repl
using MarketData
using MarketTechnicals
roc(cl, 5)
```
14 changes: 14 additions & 0 deletions docs/src/utils.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Utilities

## Typical Price

```@docs
typical
```

```@repl
using MarketData
using MarketTechnicals
merge(ohlc, typical(ohlc))
```
2 changes: 1 addition & 1 deletion src/MarketTechnicals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export sma, ema, kama,
bollingerbands, truerange, atr, #keltnerbands,
obv, vwap,
doji,
rsi, macd, cci,
rsi, macd, cci, roc,
floorpivots, woodiespivots,
typical

Expand Down
21 changes: 21 additions & 0 deletions src/momentum.jl
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,24 @@ function macd{T,N}(ta::TimeArray{T,N},

merge(merge(osc, dif), sig, colnames=new_cols)
end

doc"""
roc
Rate of Change
**Formula**:
```math
roc = \frac{close_{t} - close_{t-n}}{close_{t-n}}
```
**Reference**:
- [Wikipedia](https://en.wikipedia.org/wiki/Momentum_(technical_analysis))
"""
function roc(ta::TimeArray, n::Integer)
prev = lag(ta, n)
rename((ta .- prev) ./ prev, ["$c\_roc_$n" for c ta.colnames])
end
11 changes: 10 additions & 1 deletion src/utilities.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
doc"""
typical(ohlc; h="High", l="Low", c="Close")
Typical Price
```math
\text{Typical Price} = \frac{H + L + C}{3}
```
"""
function typical{T,N}(ohlc::TimeArray{T,N}; h="High", l="Low", c="Close")
val = (ohlc[h] .+ ohlc[l] .+ ohlc[c]) ./3
val = (ohlc[h] .+ ohlc[l] .+ ohlc[c]) ./ 3
TimeArray(val.timestamp, val.values, ["typical"], ohlc.meta)
end

Expand Down
10 changes: 9 additions & 1 deletion test/momentum.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,17 @@ facts("Momentum") do

context("cci") do
# TTR::CCI value is -38.931614
@fact cci(ohlc).values[1] --> roughly(-38.931614, atol=01)
@fact cci(ohlc).values[1] --> roughly(-38.931614, atol=.01)
# TTR::CCI value is 46.3511339
@fact cci(ohlc).values[end] --> roughly(46.3511339, atol=.01)
@fact cci(ohlc).timestamp[end] --> Date(2001, 12, 31)
end

context("roc") do
ta = roc(cl, 3)
@fact ta.colnames --> ["Close_roc_3"]
@fact ta.values[1] --> roughly(-0.15133107021618722, atol=.01)
@fact ta.values[2] --> roughly(-0.02926829268292683, atol=.01)
@fact ta.values[3] --> roughly(-0.06009615384615385, atol=.01)
end
end
3 changes: 2 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ using MarketTechnicals

include("candlesticks.jl")
include("levels.jl")
include("movingaverages.jl")
include("momentum.jl")
include("movingaverages.jl")
include("utilities.jl")
include("volatility.jl")
include("volume.jl")

Expand Down
15 changes: 7 additions & 8 deletions test/utilities.jl
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
facts("Utilities") do

context("Base.abs returns all positive numbers") do
@fact sum((abs(cl .- op) .>= 0).values) --> length(cl)
end
context("typical price") do

context("pad correctly pads NaN values for non-existent values") do
@fact sum((abs(cl .- op) .>= 0).values) --> length(cl)
end
ts = collect(Date(2017, 5, 1):Date(2017, 5, 5))
ta = typical(
TimeArray(ts, reshape(1:15, (5, 3)), ["High", "Low", "Close"]))

context("pad correctly pads NaN values for missing values") do
@fact sum((abs(cl .- op) .>= 0).values) --> length(cl)
@fact ta.timestamp[1] --> Date(2017, 5, 1)
@fact ta.timestamp[end] --> Date(2017, 5, 5)
@fact ta.values --> [6, 7, 8, 9, 10]
end
end

0 comments on commit fa56711

Please sign in to comment.