From 24bb1eaa687415ffb972d8407a37b5f6ccd6d57f Mon Sep 17 00:00:00 2001 From: "Artur.D" Date: Thu, 6 Aug 2020 23:52:18 +0600 Subject: [PATCH 1/2] added MFI --- src/mom.jl | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/mom.jl b/src/mom.jl index 2a3d17f..835be4a 100644 --- a/src/mom.jl +++ b/src/mom.jl @@ -319,3 +319,40 @@ function smi(hlc::Matrix{T}; n::Int64=13, nFast::Int64=2, nSlow::Int64=25, nSig: out[:,2] = maSig(out[:,1], n=nSig) return out end + + + + + + +function MFI_logic(x::Array{T}; n::Int64=14, args...)::Array{T} where {T<:Real} + @assert n0 "Argument n is out of bounds." + N = size(x,1) + ups = zeros(N) + dns = zeros(N) + zro = 0.0 + dx = [NaN; ndims(x[:,2]) > 1 ? diff(x[:,2], dims=1) : diff(x[:,2])] + @inbounds for i=2:N + if dx[i] > zro + ups[i] = x[i,3] + elseif dx[i] < zro + dns[i] = x[i,3] + end + end + rs = sma(ups[1:end], n=n;) ./ sma(dns[1:end], n=n;) + return 100.0 .- 100.0 ./ (1.0 .+ rs) +end + + +function MFI(X::TS, flds::Vector{Symbol}; args...) + if size(X,2) > 1 && has_close(X) + typical_price = sum(X[[:high, :low, :close]].values, dims = 2)/3 + close = X[:close].values + volume = X[:volume].values + rmf = typical_price.*volume + return ts(MFI_logic([close typical_price rmf]; args...), X.index, flds) + #return ts(MFI_logic(X.values; args...), X.index, flds) + else + error("Must be univariate or contain Close/Settle/Last.") + end +end From 06f34364de3cb81450fc00f317b29f77ce39ce69 Mon Sep 17 00:00:00 2001 From: "Artur.D" Date: Thu, 6 Aug 2020 23:54:41 +0600 Subject: [PATCH 2/2] added comments --- src/mom.jl | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/mom.jl b/src/mom.jl index 835be4a..5b73882 100644 --- a/src/mom.jl +++ b/src/mom.jl @@ -321,10 +321,9 @@ function smi(hlc::Matrix{T}; n::Int64=13, nFast::Int64=2, nSlow::Int64=25, nSig: end - - - - +""" +MFI (Money Flow Index) gives almost the same results as tradingview.com +""" function MFI_logic(x::Array{T}; n::Int64=14, args...)::Array{T} where {T<:Real} @assert n0 "Argument n is out of bounds." N = size(x,1) @@ -343,7 +342,7 @@ function MFI_logic(x::Array{T}; n::Int64=14, args...)::Array{T} where {T<:Real} return 100.0 .- 100.0 ./ (1.0 .+ rs) end - +##### X::TS should contain traditional OHLC+Volume too function MFI(X::TS, flds::Vector{Symbol}; args...) if size(X,2) > 1 && has_close(X) typical_price = sum(X[[:high, :low, :close]].values, dims = 2)/3