trading-utils
Version:
A collection of helpful trading utility functions.
42 lines • 1.93 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.StochasticIndicatorImpl = void 0;
const lodash_1 = require("lodash");
const movingAverageService_1 = require("../common/movingAverageService");
class StochasticIndicatorImpl {
calculate(ohlcPrices) {
const stochasticValues = this.calculateStochasticValues(ohlcPrices);
const oversoldArea = 20;
const overboughtArea = 80;
let suggestion = 'NEUTRAL';
stochasticValues.forEach((stochasticValue) => {
if (stochasticValue.kPercent <= oversoldArea && stochasticValue.dPercent <= oversoldArea) {
suggestion = 'LONG';
}
else if (stochasticValue.kPercent >= overboughtArea && stochasticValue.dPercent >= overboughtArea) {
suggestion = 'SHORT';
}
});
return suggestion;
}
calculateStochasticValues = (ohlcPrices) => {
const kPercents = [];
const stochasticValues = [];
for (let i = 14, a = 0; i < ohlcPrices.length; i++, a++) {
const highestHigh = lodash_1.max(ohlcPrices.map((ohlcPrice) => ohlcPrice.high).slice(i - 14, i));
const lowestLow = lodash_1.min(ohlcPrices.map((ohlcPrice) => ohlcPrice.low).slice(i - 14, i));
const kPercent = (ohlcPrices[i - 1].close - lowestLow) / (highestHigh - lowestLow) * 100;
kPercents.push(kPercent);
if (a > 2) {
const dPercent = movingAverageService_1.movingAverageService.calculateSMA(kPercents.slice(a - 3, a));
stochasticValues.push({
kPercent: kPercents[a - 1],
dPercent
});
}
}
return stochasticValues;
};
}
exports.StochasticIndicatorImpl = StochasticIndicatorImpl;
//# sourceMappingURL=stochasticIndicator.js.map