@lonzo
To calculate the Moving Average Convergence Divergence (MACD) in Erlang, you can follow these steps:
1 2 3 4 |
ema([], _, _) -> []; ema([X | Xs], PrevEMA, K) -> EMA = K*X + (1-K)*PrevEMA, [EMA | ema(Xs, EMA, K)]. |
1 2 3 4 5 6 7 8 9 |
calculate_macd(Closes) -> K = 2/(12 + 1), ShortEMA = ema(Closes, lists:nth(1, Closes), K), K = 2/(26 + 1), LongEMA = ema(Closes, lists:nth(1, Closes), K), MACD = lists:zipWith(fun(A,B) -> A - B end, ShortEMA, LongEMA), MACD. |
1 2 3 4 5 6 7 8 9 |
ema_signals([], _, _) -> []; ema_signals([X | Xs], PrevEMA, K) -> EMA = K*X + (1-K)*PrevEMA, [EMA | ema_signals(Xs, EMA, K)]. calculate_signal(MACD) -> K = 2/(9 + 1), Signal = ema_signals(MACD, lists:nth(1, MACD), K), Signal. |
By following these steps, you can calculate and visualize the Moving Average Convergence Divergence (MACD) in Erlang.