Skip to content

Commit

Permalink
error fixes. docstring convention.
Browse files Browse the repository at this point in the history
  • Loading branch information
dysonance committed Jan 6, 2021
1 parent 2c91e47 commit 70fb0ca
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 62 deletions.
2 changes: 1 addition & 1 deletion src/Indicators.jl
Expand Up @@ -4,7 +4,7 @@ module Indicators
export
runmean, runsum, runvar, runsd, runcov, runcor, runmax, runmin, runmad, runquantile, runacf,
wilder_sum, mode, diffn,
sma, trima, wma, ema, mma, kama, mama, hma, swma, dema, tema, alma, zlema, vwma,
sma, trima, wma, ema, mma, kama, mama, hma, swma, dema, tema, alma, zlema, vwma, vwap, hama,
mlr_beta, mlr_slope, mlr_intercept, mlr, mlr_se, mlr_ub, mlr_lb, mlr_bands, mlr_rsq,
aroon, donch, momentum, roc, macd, rsi, adx, psar, kst, wpr, cci, stoch, smi,
bbands, tr, atr, keltner,
Expand Down
10 changes: 4 additions & 6 deletions src/ma.jl
Expand Up @@ -402,14 +402,13 @@ end
vwap(cv::Matrix{T})::Array{T}
```
volume weighted average price (VWAP)
Volume-weighted average price (VWAP)
"""
function vwap(cv::Matrix{T})::Array{Float64} where {T<:Real}
out = zeros(size(cv))[1]
close_price = cv[:,1]
volume = cv[:,2]
out = cumsum(close_price .* volume) ./ cumsum(volume)
end
return out
end

Expand All @@ -418,12 +417,11 @@ end
hama(x::Array{T})::Array{T}
```
hamming moving average (HAMA)
Hamming moving average (HAMA)
"""
function hama(x::Array{T}; n::Int=10)::Array{Float64} where {T<:Real}
nums = 1:n
hamming_weights = 0.54 - 0.46.*cos.(2pi*nums/n)
out = wma(x,n, hamming_weights)
hamming_weights = 0.54 .- 0.46 .* cos.(2*pi*(1:n)/n)
out = wma(x, n=n, wts=hamming_weights)
return out
end

112 changes: 57 additions & 55 deletions src/temporal.jl
Expand Up @@ -47,72 +47,74 @@ function runcor(x::TS{V,T}, y::TS{V,T}; args...) where {V,T}
z = [x y].values
ts(runcor(z[:,1], z[:,2]; args...), x.index, :RunCor)
end
mode(X::TS{V,T}) where {V,T} = mode(X.values)
runmean(X::TS{V,T}; args...) where {V,T} = close_fun(X, runmean, [:RunMean]; args...)
runsum(X::TS{V,T}; args...) where {V,T} = close_fun(X, runsum, [:RunSum]; args...)
runmad(X::TS{V,T}; args...) where {V,T} = close_fun(X, runmad, [:RunMAD]; args...)
runvar(X::TS{V,T}; args...) where {V,T} = close_fun(X, runvar, [:RunVar]; args...)
runmax(X::TS{V,T}; args...) where {V,T} = close_fun(X, runmax, [:RunMax]; args...)
runmin(X::TS{V,T}; args...) where {V,T} = close_fun(X, runmin, [:RunMin]; args...)
runsd(X::TS{V,T}; args...) where {V,T} = close_fun(X, runsd, [:RunSD]; args...)
runquantile(X::TS{V,T}; args...) where {V,T} = close_fun(X, runquantile, [:RunQuantile]; args...)
wilder_sum(X::TS{V,T}; args...) where {V,T} = close_fun(X, wilder_sum, [:WilderSum]; args...)
runacf(X::TS{V,T}; n::Int=10, maxlag::Int=n-3, lags::AbstractArray{Int,1}=0:maxlag, cumulative::Bool=true) where {V,T} = close_fun(X, runacf, [Symbol(i) for i in lags]; n=n, maxlag=maxlag, lags=lags, cumulative=cumulative)
mode(X::TS) = mode(X.values)
runmean(X::TS; args...) = close_fun(X, runmean, [:RunMean]; args...)
runsum(X::TS; args...) = close_fun(X, runsum, [:RunSum]; args...)
runmad(X::TS; args...) = close_fun(X, runmad, [:RunMAD]; args...)
runvar(X::TS; args...) = close_fun(X, runvar, [:RunVar]; args...)
runmax(X::TS; args...) = close_fun(X, runmax, [:RunMax]; args...)
runmin(X::TS; args...) = close_fun(X, runmin, [:RunMin]; args...)
runsd(X::TS; args...) = close_fun(X, runsd, [:RunSD]; args...)
runquantile(X::TS; args...) = close_fun(X, runquantile, [:RunQuantile]; args...)
wilder_sum(X::TS; args...) = close_fun(X, wilder_sum, [:WilderSum]; args...)
runacf(X::TS; n::Int=10, maxlag::Int=n-3, lags::AbstractArray{Int,1}=0:maxlag, cumulative::Bool=true) = close_fun(X, runacf, [Symbol(i) for i in lags]; n=n, maxlag=maxlag, lags=lags, cumulative=cumulative)
runfun(X::TS, f::Function; n::Int=10, cumulative::Bool=true, args...) = TS(runfun(X, f, n=n, cumulative=cumulative, args...), X.index, [:Function])

##### ma.jl ######
sma(X::TS{V,T}; args...) where {V,T} = close_fun(X, sma, [:SMA]; args...)
hma(X::TS{V,T}; args...) where {V,T} = close_fun(X, hma, [:HMA]; args...)
mma(X::TS{V,T}; args...) where {V,T} = close_fun(X, mma, [:MMA]; args...)
swma(X::TS{V,T}; args...) where {V,T} = close_fun(X, swma, [:SWMA]; args...)
kama(X::TS{V,T}; args...) where {V,T} = close_fun(X, kama, [:KAMA]; args...)
alma(X::TS{V,T}; args...) where {V,T} = close_fun(X, alma, [:ALMA]; args...)
trima(X::TS{V,T}; args...) where {V,T} = close_fun(X, trima, [:TRIMA]; args...)
wma(X::TS{V,T}; args...) where {V,T} = close_fun(X, wma, [:WMA]; args...)
ema(X::TS{V,T}; args...) where {V,T} = close_fun(X, ema, [:EMA]; args...)
dema(X::TS{V,T}; args...) where {V,T} = close_fun(X, dema, [:DEMA]; args...)
tema(X::TS{V,T}; args...) where {V,T} = close_fun(X, tema, [:TEMA]; args...)
zlema(X::TS{V,T}; args...) where {V,T} = close_fun(X, zlema, [:ZLEMA]; args...)
mama(X::TS{V,T}; args...) where {V,T} = close_fun(X, mama, [:MAMA,:FAMA]; args...)
vwma(X::TS{V,T}; args...) where {V,T} = cv_fun(X, vwma, [:VWMA]; args...)
sma(X::TS; args...) = close_fun(X, sma, [:SMA]; args...)
hma(X::TS; args...) = close_fun(X, hma, [:HMA]; args...)
mma(X::TS; args...) = close_fun(X, mma, [:MMA]; args...)
swma(X::TS; args...) = close_fun(X, swma, [:SWMA]; args...)
kama(X::TS; args...) = close_fun(X, kama, [:KAMA]; args...)
alma(X::TS; args...) = close_fun(X, alma, [:ALMA]; args...)
trima(X::TS; args...) = close_fun(X, trima, [:TRIMA]; args...)
wma(X::TS; args...) = close_fun(X, wma, [:WMA]; args...)
ema(X::TS; args...) = close_fun(X, ema, [:EMA]; args...)
dema(X::TS; args...) = close_fun(X, dema, [:DEMA]; args...)
tema(X::TS; args...) = close_fun(X, tema, [:TEMA]; args...)
zlema(X::TS; args...) = close_fun(X, zlema, [:ZLEMA]; args...)
mama(X::TS; args...) = close_fun(X, mama, [:MAMA,:FAMA]; args...)
vwma(X::TS; args...) = cv_fun(X, vwma, [:VWMA]; args...)
vwap(X::TS; args...) = cv_fun(X, vwma, [:VWAP]; args...)
hama(X::TS; args...) = close_fun(X, hama, [:HammingMA]; args...)

##### reg.jl ######
mlr_beta(X::TS{V,T}; args...) where {V,T} = close_fun(X, mlr_beta, [:Intercept,:Slope]; args...)
mlr_slope(X::TS{V,T}; args...) where {V,T} = close_fun(X, mlr_slope, [:Slope]; args...)
mlr_intercept(X::TS{V,T}; args...) where {V,T} = close_fun(X, mlr_intercept, [:Intercept]; args...)
mlr(X::TS{V,T}; args...) where {V,T} = close_fun(X, mlr, [:MLR]; args...)
mlr_se(X::TS{V,T}; args...) where {V,T} = close_fun(X, mlr_se, [:StdErr]; args...)
mlr_ub(X::TS{V,T}; args...) where {V,T} = close_fun(X, mlr_ub, [:MLRUB]; args...)
mlr_lb(X::TS{V,T}; args...) where {V,T} = close_fun(X, mlr_lb, [:MLRLB]; args...)
mlr_bands(X::TS{V,T}; args...) where {V,T} = close_fun(X, mlr_bands, [:MLRLB,:MLR,:MLRUB]; args...)
mlr_rsq(X::TS{V,T}; args...) where {V,T} = close_fun(X, mlr_rsq, [:RSquared]; args...)
mlr_beta(X::TS; args...) = close_fun(X, mlr_beta, [:Intercept,:Slope]; args...)
mlr_slope(X::TS; args...) = close_fun(X, mlr_slope, [:Slope]; args...)
mlr_intercept(X::TS; args...) = close_fun(X, mlr_intercept, [:Intercept]; args...)
mlr(X::TS; args...) = close_fun(X, mlr, [:MLR]; args...)
mlr_se(X::TS; args...) = close_fun(X, mlr_se, [:StdErr]; args...)
mlr_ub(X::TS; args...) = close_fun(X, mlr_ub, [:MLRUB]; args...)
mlr_lb(X::TS; args...) = close_fun(X, mlr_lb, [:MLRLB]; args...)
mlr_bands(X::TS; args...) = close_fun(X, mlr_bands, [:MLRLB,:MLR,:MLRUB]; args...)
mlr_rsq(X::TS; args...) = close_fun(X, mlr_rsq, [:RSquared]; args...)

##### mom.jl ######
momentum(X::TS{V,T}; args...) where {V,T} = close_fun(X, momentum, [:Momentum]; args...)
roc(X::TS{V,T}; args...) where {V,T} = close_fun(X, roc, [:ROC]; args...)
macd(X::TS{V,T}; args...) where {V,T} = close_fun(X, macd, [:MACD,:Signal,:Histogram]; args...)
rsi(X::TS{V,T}; args...) where {V,T} = close_fun(X, rsi, [:RSI]; args...)
psar(X::TS{V,T}; args...) where {V,T} = hl_fun(X, psar, [:PSAR]; args...)
kst(X::TS{V,T}; args...) where {V,T} = close_fun(X, kst, [:KST]; args...)
wpr(X::TS{V,T}; args...) where {V,T} = hlc_fun(X, wpr, [:WPR]; args...)
adx(X::TS{V,T}; args...) where {V,T} = hlc_fun(X, adx, [:DiPlus,:DiMinus,:ADX]; args...)
cci(X::TS{V,T}; args...) where {V,T} = hlc_fun(X, cci, [:CCI]; args...)
stoch(X::TS{V,T}; args...) where {V,T} = hlc_fun(X, stoch, [:Stochastic,:Signal]; args...)
smi(X::TS{V,T}; args...) where {V,T} = hlc_fun(X, smi, [:SMI,:Signal]; args...)
donch(X::TS{V,T}; args...) where {V,T} = hl_fun(X, donch, [:Low,:Mid,:High]; args...)
aroon(X::TS{V,T}; args...) where {V,T} = hl_fun(X, aroon, [:AroonUp,:AroonDn,:AroonOsc]; args...)
momentum(X::TS; args...) = close_fun(X, momentum, [:Momentum]; args...)
roc(X::TS; args...) = close_fun(X, roc, [:ROC]; args...)
macd(X::TS; args...) = close_fun(X, macd, [:MACD,:Signal,:Histogram]; args...)
rsi(X::TS; args...) = close_fun(X, rsi, [:RSI]; args...)
psar(X::TS; args...) = hl_fun(X, psar, [:PSAR]; args...)
kst(X::TS; args...) = close_fun(X, kst, [:KST]; args...)
wpr(X::TS; args...) = hlc_fun(X, wpr, [:WPR]; args...)
adx(X::TS; args...) = hlc_fun(X, adx, [:DiPlus,:DiMinus,:ADX]; args...)
cci(X::TS; args...) = hlc_fun(X, cci, [:CCI]; args...)
stoch(X::TS; args...) = hlc_fun(X, stoch, [:Stochastic,:Signal]; args...)
smi(X::TS; args...) = hlc_fun(X, smi, [:SMI,:Signal]; args...)
donch(X::TS; args...) = hl_fun(X, donch, [:Low,:Mid,:High]; args...)
aroon(X::TS; args...) = hl_fun(X, aroon, [:AroonUp,:AroonDn,:AroonOsc]; args...)

##### vol.jl ######
bbands(X::TS{V,T}; args...) where {V,T} = close_fun(X, bbands, [:LB,:MA,:UB]; args...)
tr(X::TS{V,T}; args...) where {V,T} = hlc_fun(X, tr, [:TR]; args...)
atr(X::TS{V,T}; args...) where {V,T} = hlc_fun(X, atr, [:ATR]; args...)
keltner(X::TS{V,T}; args...) where {V,T} = hlc_fun(X, keltner, [:KeltnerLower,:KeltnerMiddle,:KeltnerUpper]; args...)
bbands(X::TS; args...) = close_fun(X, bbands, [:LB,:MA,:UB]; args...)
tr(X::TS; args...) = hlc_fun(X, tr, [:TR]; args...)
atr(X::TS; args...) = hlc_fun(X, atr, [:ATR]; args...)
keltner(X::TS; args...) = hlc_fun(X, keltner, [:KeltnerLower,:KeltnerMiddle,:KeltnerUpper]; args...)

##### trendy.jl #####
maxima(X::TS{V,T}; args...) where {V,T} = close_fun(X, maxima, [:Maxima]; args...)
minima(X::TS{V,T}; args...) where {V,T} = close_fun(X, minima, [:Minima]; args...)
support(X::TS{V,T}; args...) where {V,T} = close_fun(X, support, [:Support]; args...)
resistance(X::TS{V,T}; args...) where {V,T} = close_fun(X, resistance, [:Resistance]; args...)
maxima(X::TS; args...) = close_fun(X, maxima, [:Maxima]; args...)
minima(X::TS; args...) = close_fun(X, minima, [:Minima]; args...)
support(X::TS; args...) = close_fun(X, support, [:Support]; args...)
resistance(X::TS; args...) = close_fun(X, resistance, [:Resistance]; args...)

#### utils.jl ####
crossover(x::TS, y::TS) = ts(crossover(x.values, y.values), x.index, [:CrossOver])
Expand Down
6 changes: 6 additions & 0 deletions test/ma.jl
Expand Up @@ -59,6 +59,12 @@
tmp = vwma(X)
@test size(tmp, 1) == N
@test size(tmp, 2) == 1
tmp = vwap(X)
@test size(tmp, 1) == N
@test size(tmp, 2) == 1
tmp = hama(x)
@test size(tmp, 1) == N
@test size(tmp, 2) == 1
end
@testset "Temporal" begin
x = TS(cumsum(randn(N)))
Expand Down

0 comments on commit 70fb0ca

Please sign in to comment.