stock-indicator
Version:
Stock Indicator Library
39 lines (36 loc) • 1.44 kB
text/typescript
import {
funMA,
funRound,
TMixed,
checkArrayOfNumbers,
checkNumber,
} from "../common";
/**
* DMA - Different of Moving Average
* @param {number} CLOSE - closing prices of the stock
* @param {number} N1 - number of periods for the first moving average, must be greater than 1 and less than or equal to the length of the CLOSE array,default is 10
* @param {number} N2 - number of periods for the second moving average, must be greater than N1 and less than or equal to the length of the CLOSE array,default is 50
* @param {number} M - number of periods for the moving average of the DIF, must be greater than 1 and less than or equal to the length of the DIF array,default is 10
* @returns {{DIF: TMixed[], DIFMA: TMixed[]}} - DIF and DIFMA arrays, DIFMA is the moving average of the DIF array
*/
export function DMA(
CLOSE: number[],
N1: number = 10,
N2: number = 50,
M: number = 10
): { DIF: TMixed[]; DIFMA: TMixed[] } {
// - check input data
checkArrayOfNumbers(CLOSE, "CLOSE");
checkNumber(N1, 2, CLOSE, "N1", "CLOSE");
checkNumber(N1, N1, CLOSE, "N2", "CLOSE");
checkNumber(M, 2, N2, "M", "N2");
const maN1 = funMA(CLOSE, N1);
const maN2 = funMA(CLOSE, N2);
const DIF = maN1.map((item, index) =>
item !== null && maN2[index] !== null
? funRound(item - maN2[index], 3)
: null
);
const DIFMA = funMA(DIF, M).map((item) => funRound(item, 3));
return { DIF, DIFMA };
}