Skip to content

Instantly share code, notes, and snippets.

@milktrader
Created February 18, 2014 03:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save milktrader/9063995 to your computer and use it in GitHub Desktop.
Save milktrader/9063995 to your computer and use it in GitHub Desktop.
MACD code
function macd{T}(ta::TimeArray{T,1}, fast::Int, slow::Int, signal::Int)
fastma = ema(ta, fast)
slowma = ema(ta, slow)
mcdval = fastma - slowma
sigval = ema(mcdval, signal)
merge(mcdval, sigval, ["macd", "signal"])
end
@milktrader
Copy link
Author

Of course, you can always refactor ...

function macd{T}(ta::TimeArray{T,1}, fast::Int, slow::Int, signal::Int) 
  mcdval =  ema(ta, fast) -  ema(ta, slow)
  merge(mcdval, ema(mcdval, signal), ["macd", "signal"])
end

@milktrader
Copy link
Author

Let's not make it a one-liner though! Which by the way is not all that big of a jump.

@milktrader
Copy link
Author

macd{T}(ta::TimeArray{T,1}, fast::Int, slow::Int, signal::Int) =  merge((ema(ta, fast) -  ema(ta, slow)), ema((ema(ta, fast) -  ema(ta, slow)), signal), ["macd", "signal"])

@milktrader
Copy link
Author

Hmm, probably the most descriptive code to match algorithm would be this

function macd{T}(ta::TimeArray{T,1}, fast::Int, slow::Int, signal::Int) 
  mcdval = ema(ta, fast) -  ema(ta, slow)
  sigval = ema(mcdval, signal)
  merge(mcdval, sigval, ["macd", "signal"])
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment