Function Prototype
/* Moving Average Convergence/Divergence */
/* Type: indicator */
/* Input arrays: 1 Options: 3 Output arrays: 3 */
/* Inputs: real */
/* Options: short period, long period, signal period */
/* Outputs: macd, macd_signal, macd_histogram */
int ti_macd_start(TI_REAL const *options);
int ti_macd(int size,
TI_REAL const *const *inputs,
TI_REAL const *options,
TI_REAL *const *outputs);
Description
This documentation is still a work in progress. It has omissions, and it probably has errors too. If you see any issues, or have any general feedback, please get in touch.
Moving Average Convergence/Divergence helps follow trends and has several uses.
It takes three parameter, a short period n
, a long period m
, and a signal period p
.
Moving Average Convergence/Divergence is calculated as follows.
$$short_{t} = ema(n, input)$$
$$long_{t} = ema(m, input)$$
$$macd_{t} = short_{t} - long_{t}$$
$$signal_{t} = ema(p, macd_{t})$$
$$histogram_{t} = macd_{t} - signal_{t}$$
For historical reasons, if n
is 12 and m
is 26, then the Exponential Moving Average scaling factors
are adjusted from 0.1538 and 0.0740 to 0.1500 and 0.0750 respectively.
See Also
References
- Murphy, J. (1999) Technical Analysis of the Financial Markets
- Achelis, S. (2000) Technical Analysis from A to Z, 2nd Edition
Example Usage
Calling From C
/* Example usage of Moving Average Convergence/Divergence */
/* Assuming that 'input' is a pre-loaded array of size 'in_size'. */
TI_REAL *inputs[] = {input};
TI_REAL options[] = {2, 5, 9}; /* short period, long period, signal period */
TI_REAL *outputs[3]; /* macd, macd_signal, macd_histogram */
/* Determine how large the output size is for our options. */
const int out_size = in_size - ti_macd_start(options);
/* Allocate memory for each output. */
outputs[0] = malloc(sizeof(TI_REAL) * out_size); assert(outputs[0] != 0); /* macd */
outputs[1] = malloc(sizeof(TI_REAL) * out_size); assert(outputs[1] != 0); /* macd_signal */
outputs[2] = malloc(sizeof(TI_REAL) * out_size); assert(outputs[2] != 0); /* macd_histogram */
/* Run the actual calculation. */
const int ret = ti_macd(in_size, inputs, options, outputs);
assert(ret == TI_OKAY);
Calling From Lua (with Tulip Chart bindings)
-- Example usage of Moving Average Convergence/Divergence
macd, macd_signal, macd_histogram = ti.macd(input, 2, 5, 9)
Example Calculation
short period = 2, long period = 5, signal period = 9
date | input | macd | macd_signal | macd_histogram |
---|---|---|---|---|
2005-11-01 | 81.59 | |||
2005-11-02 | 81.06 | |||
2005-11-03 | 82.87 | |||
2005-11-04 | 83.00 | |||
2005-11-07 | 83.61 | 0.62 | 0.62 | 0.00 |
2005-11-08 | 83.15 | 0.35 | 0.56 | -0.21 |
2005-11-09 | 82.84 | 0.11 | 0.47 | -0.36 |
2005-11-10 | 83.99 | 0.42 | 0.46 | -0.05 |
2005-11-11 | 84.55 | 0.58 | 0.49 | 0.09 |
2005-11-14 | 84.36 | 0.42 | 0.47 | -0.05 |
2005-11-15 | 85.53 | 0.68 | 0.52 | 0.17 |
2005-11-16 | 86.54 | 0.93 | 0.60 | 0.33 |
2005-11-17 | 86.89 | 0.89 | 0.66 | 0.24 |
2005-11-18 | 87.77 | 0.98 | 0.72 | 0.26 |
2005-11-21 | 87.29 | 0.62 | 0.70 | -0.08 |
Chart
Other Indicators
Previous indicator: Vector Base-10 Log
Next indicator: Market Facilitation Index
Random indicator: Aroon Oscillator
Copyright © 2016-2024 Tulip Charts LLC. All Rights Reserved.