04-21-2018, 07:32 AM
(This post was last modified: 04-21-2018, 07:41 AM by niquedegraaff.)
Thanks, but I still encounter some problems with this. The SMA indicator (i want smoothed K and D over stochRSI) is returning NaN results. I don't see where the problem is. The input is good (Input Price) but the SMA Indicator seems to have problems calculating it.
Result (limited to 2 results for readability):
Code:
// Prepare everything our method needs
strat.init = function () {
this.name = 'Heiken';
this.requiredHistory = 14;
this.interval = 14;
this.rsi = new RSI({interval: this.interval});
this.RSIhistory = [];
this.addIndicator('stochK', 'SMA', 3);
this.addIndicator('stochD', 'SMA', 3);
}
// What happens on every new candle?
strat.update = function (candle) {
this.heikenCandle = this.heikenAshi(candle);
this.rsi.update(this.heikenCandle);
this.RSIhistory.push(this.rsi.result);
if(_.size(this.RSIhistory) > this.interval)
this.RSIhistory.shift();
this.lowestRSI = _.min(this.RSIhistory);
this.highestRSI = _.max(this.RSIhistory);
var price = ((this.rsi.result - this.lowestRSI) / (this.highestRSI - this.lowestRSI)) * 100;
log.debug('Input price:', price);
this.indicators.stochK.update(price);
log.debug(this.indicators.stochK);
this.previousHeikenCandle = this.heikenCandle;
}
Result (limited to 2 results for readability):
Code:
2018-04-21 09:36:45 (DEBUG): Input price: 100
2018-04-21 09:36:45 (DEBUG): Indicator {
input: 'price',
windowLength: 3,
prices: [ 494.7, 100, 100 ],
result: NaN,
age: 2,
sum: NaN }
2018-04-21 09:36:45 (DEBUG): calculated properties for candle 2018-04-13 01:45:00:
2018-04-21 09:36:45 (DEBUG): Candle: open[495] close[494.7]
2018-04-21 09:36:45 (DEBUG): Heiken: open[494.24] close[495.07]
2018-04-21 09:36:45 (DEBUG): StochRSI: k[NaN]
2018-04-21 09:36:45 (DEBUG): Input price: 84.86116310138854
2018-04-21 09:36:45 (DEBUG): Indicator {
input: 'price',
windowLength: 3,
prices: [ 84.86116310138854, 100, 494 ],
result: NaN,
age: 1,
sum: NaN }
2018-04-21 09:36:45 (DEBUG): calculated properties for candle 2018-04-13 01:50:00:
2018-04-21 09:36:45 (DEBUG): Candle: open[494.71] close[494]
2018-04-21 09:36:45 (DEBUG): Heiken: open[494.65] close[494.68]
2018-04-21 09:36:45 (DEBUG): StochRSI: k[NaN]