@thuantan2060/technicalindicators
Version:
Techincal Indicators written in javascript
80 lines (79 loc) • 2.14 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.TrueRangeInput = exports.TrueRange = void 0;
exports.truerange = truerange;
var _indicator = require("../indicator/indicator");
/**
* Created by AAravindan on 5/8/16.
*/
/**
* Created by AAravindan on 5/8/16.
*/
;
class TrueRangeInput extends _indicator.IndicatorInput {
low;
high;
close;
}
exports.TrueRangeInput = TrueRangeInput;
;
class TrueRange extends _indicator.Indicator {
result;
generator;
constructor(input) {
super(input);
var lows = input.low;
var highs = input.high;
var closes = input.close;
var format = this.format;
if (lows.length != highs.length) {
throw 'Inputs(low,high) not of equal size';
}
this.result = [];
this.generator = function* () {
var current = yield;
var previousClose, result;
while (true) {
if (previousClose === undefined) {
previousClose = current.close;
current = yield result;
}
result = Math.max(current.high - current.low, isNaN(Math.abs(current.high - previousClose)) ? 0 : Math.abs(current.high - previousClose), isNaN(Math.abs(current.low - previousClose)) ? 0 : Math.abs(current.low - previousClose));
previousClose = current.close;
if (result != undefined) {
result = format(result);
}
current = yield result;
}
}();
this.generator.next();
lows.forEach((tick, index) => {
var result = this.generator.next({
high: highs[index],
low: lows[index],
close: closes[index]
});
if (result.value != undefined) {
this.result.push(result.value);
}
});
}
static calculate = truerange;
nextValue(price) {
return this.generator.next(price).value;
}
}
exports.TrueRange = TrueRange;
function truerange(input) {
_indicator.Indicator.reverseInputs(input);
var result = new TrueRange(input).result;
if (input.reversedInput) {
result.reverse();
}
_indicator.Indicator.reverseInputs(input);
return result;
}
;
//# sourceMappingURL=TrueRange.js.map