Skip to content

Commit

Permalink
vwma function definition and test case fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dysonance committed Sep 15, 2020
1 parent 294e694 commit 21b45c8
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/Indicators.jl
Original file line number Diff line number Diff line change
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,
sma, trima, wma, ema, mma, kama, mama, hma, swma, dema, tema, alma, zlema, vwma,
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
9 changes: 5 additions & 4 deletions src/ma.jl
Original file line number Diff line number Diff line change
Expand Up @@ -382,13 +382,14 @@ vwma(cv::Matrix{T})::Array{T}
Volume weighted moving average (VWMA)
"""
function vwma(cv::Matrix{T}, n::Int64=10)::Array{Float64} where {T<:Real}
@assert n<size(x,1) && n>0 "Argument n out of bounds."
out = zeros(size(cv))[1]
function vwma(cv::Matrix{T}; n::Int64=10)::Array{Float64} where {T<:Real}
@assert n<size(cv,1) && n>0 "Argument n out of bounds."
N = size(cv, 1)
out = zeros(N)
close_price = cv[:,1]
volume = cv[:,2]
out[1:n-1] .= NaN
@inbounds for i = n:size(close_price,1)
@inbounds for i = n:N
weight = volume[i-n+1:i] # get volumes as numerator
d = sum(weight) # denominator = sum(numerator weights)
out[i] = sum(weight .* close_price[i-n+1:i]) / d
Expand Down
22 changes: 16 additions & 6 deletions src/temporal.jl
Original file line number Diff line number Diff line change
@@ -1,31 +1,40 @@
# Methods for porting Indicators.jl functions to TS objects from Temporal.jl package
function close_fun(X::TS, f::Function, flds::Vector{Symbol}; args...)
if size(X,2) == 1
return ts(f(X.values; args...), X.index, flds)
return TS(f(X.values; args...), X.index, flds)
elseif size(X,2) > 1 && has_close(X)
return ts(f(cl(X).values; args...), X.index, flds)
return TS(f(cl(X).values; args...), X.index, flds)
else
error("Must be univariate or contain Close/Settle/Last.")
end
end
function hlc_fun(X::TS, f::Function, flds::Vector{Symbol}; args...)
if size(X,2) == 3
return ts(f(X.values; args...), X.index, flds)
return TS(f(X.values; args...), X.index, flds)
elseif size(X,2) > 3 && has_high(X) && has_low(X) && has_close(X)
return ts(f(hlc(X).values; args...), X.index, flds)
return TS(f(hlc(X).values; args...), X.index, flds)
else
error("Argument must have 3 columns or have High, Low, and Close/Settle/Last fields.")
end
end
function hl_fun(X::TS, f::Function, flds::Vector{Symbol}; args...)
if size(X,2) == 2
return ts(f(X.values; args...), X.index, flds)
return TS(f(X.values; args...), X.index, flds)
elseif size(X,2) > 2 && has_high(X) && has_low(X)
return ts(f(hl(X).values; args...), X.index, flds)
return TS(f(hl(X).values; args...), X.index, flds)
else
error("Argument must have 2 columns or have High and Low fields.")
end
end
function cv_fun(X::TS, f::Function, flds::Vector{Symbol}; args...)
if size(X,2) == 2
return TS(f(X.values; args...), X.index, flds)
elseif size(X,2) > 2 && has_close(X) && has_volume(X)
return TS(f([cl(X) vo(X)].values; args...), X.index, flds)
else
error("Argument must have 2 columns or have Close and Volume fields.")
end
end

###### run.jl ######
function runcov(x::TS{V,T}, y::TS{V,T}; args...) where {V,T}
Expand Down Expand Up @@ -65,6 +74,7 @@ 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...)

##### reg.jl ######
mlr_beta(X::TS{V,T}; args...) where {V,T} = close_fun(X, mlr_beta, [:Intercept,:Slope]; args...)
Expand Down
4 changes: 4 additions & 0 deletions test/ma.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Random.seed!(SEED)
@testset "Array" begin
x = cumsum(randn(N))
X = cumsum(randn(N, 2), dims=1)
tmp = sma(x)
@test size(tmp, 1) == N
@test size(tmp, 2) == 1
Expand Down Expand Up @@ -55,6 +56,9 @@
@test size(tmp, 1) == N
@test size(tmp, 2) == 1
@test sum(isnan.(tmp)) != N
tmp = vwma(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 21b45c8

Please sign in to comment.