Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/mom.jl
Original file line number Diff line number Diff line change
Expand Up @@ -319,3 +319,39 @@ 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


"""
MFI (Money Flow Index) gives almost the same results as tradingview.com
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Documentation comments should follow the same convention as other functions (function signature in triple-backticks followed by indicator name/interpretation two lines below).

"""
function MFI_logic(x::Array{T}; n::Int64=14, args...)::Array{T} where {T<:Real}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function naming convention is all lowercase, e.g. mfi.

@assert n<size(x,1) && n>0 "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

##### X::TS should contain traditional OHLC+Volume too
function MFI(X::TS, flds::Vector{Symbol}; args...)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment about naming convention. The MFI method that takes a TS type as input should go in src/temporal.jl and should have the same function name as the base Array type method (mfi). Julia will automatically know which method to apply based on the input types :)

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