@react-financial-charts/indicators
Version: 
Indicators for react-financial-charts
40 lines • 1.44 kB
JavaScript
import { zip } from "d3-array";
import { SmoothedForceIndex as defaultOptions } from "./defaultOptionsForComputation";
import ema from "./ema";
import forceIndex from "./forceIndex";
import sma from "./sma";
export default function () {
    const underlyingAlgorithm = forceIndex();
    let options = defaultOptions;
    const calculator = (data) => {
        const { smoothingType, smoothingWindow } = options;
        const { sourcePath, volumePath } = options;
        const algo = underlyingAlgorithm.options({ sourcePath, volumePath });
        // @ts-ignore
        const force = algo(data);
        const ma = smoothingType === "ema" ? ema() : sma();
        const forceMA = ma.options({
            windowSize: smoothingWindow,
            sourcePath: undefined,
        });
        // @ts-ignore
        const smoothed = forceMA(force);
        return zip(force, smoothed).map((d) => ({
            force: d[0],
            smoothed: d[1],
        }));
    };
    calculator.undefinedLength = () => {
        const { smoothingWindow } = options;
        return underlyingAlgorithm.undefinedLength() + smoothingWindow - 1;
    };
    calculator.options = (newOptions) => {
        if (newOptions === undefined) {
            return options;
        }
        options = Object.assign(Object.assign({}, defaultOptions), newOptions);
        return calculator;
    };
    return calculator;
}
//# sourceMappingURL=smoothedForceIndex.js.map