Skip to content

Commit

Permalink
Merge pull request #82 from JuliaQuant/ib-roc
Browse files Browse the repository at this point in the history
momentum: implement indicator -> ROC
  • Loading branch information
iblislin committed May 15, 2017
2 parents ce5bc7d + b2d0ad7 commit 5143fb0
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
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)
```
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 @@ -86,3 +86,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
12 changes: 10 additions & 2 deletions test/momentum.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,17 @@ facts("Momentum") do
@fact ta.timestamp[end] --> Date(2001, 12, 31)
end

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

0 comments on commit 5143fb0

Please sign in to comment.