Code:
var _ = require('lodash');
var log = require('../core/log.js');
var BB = require('./indicators/BB.js');
var rsi = require('./indicators/RSI.js');
// let's create our own method
var method = {};
// prepare everything our method needs
method.init = function () {
this.name = 'BB';
this.nsamples = 0;
this.trend = {
zone: 'none', // none, top, high, low, bottom
duration: 0,
persisted: false
};
this.requiredHistory = this.tradingAdvisor.historySize;
// define the indicators we need
this.addIndicator('bb', 'BB', this.settings.bbands);
this.addIndicator('rsi', 'RSI', this.settings);
logic = {longpos: false};
this.logic = logic;
this.lastCandle;
this.currentCandle;
}
// for debugging purposes log the last
// calculated parameters.
method.log = function (candle) {
// var digits = 8;
// var BB = this.indicators.bb;
// //BB.lower; BB.upper; BB.middle are your line values
// log.debug('______________________________________');
// log.debug('calculated BB properties for candle ', this.nsamples);
// if (BB.upper > candle.close) log.debug('\t', 'Upper BB:', BB.upper.toFixed(digits));
// if (BB.middle > candle.close) log.debug('\t', 'Mid BB:', BB.middle.toFixed(digits));
// if (BB.lower >= candle.close) log.debug('\t', 'Lower BB:', BB.lower.toFixed(digits));
// log.debug('\t', 'price:', candle.close.toFixed(digits));
// if (BB.upper <= candle.close) log.debug('\t', 'Upper BB:', BB.upper.toFixed(digits));
// if (BB.middle <= candle.close) log.debug('\t', 'Mid BB:', BB.middle.toFixed(digits));
// if (BB.lower < candle.close) log.debug('\t', 'Lower BB:', BB.lower.toFixed(digits));
// log.debug('\t', 'Band gap: ', BB.upper.toFixed(digits) - BB.lower.toFixed(digits));
// var rsi = this.indicators.rsi;
// log.debug('calculated RSI properties for candle:');
// log.debug('\t', 'rsi:', rsi.result.toFixed(digits));
// log.debug('\t', 'price:', candle.close.toFixed(digits));
}
method.check = function (candle) {
var BB = this.indicators.bb;
var price = candle.close;
this.nsamples++;
var rsi = this.indicators.rsi;
var rsiVal = rsi.result;
var Trend = 'none';
if (candle.open <= candle.close) Trend = 'Bearish';
if (candle.open >= candle.close) Trend = 'Bullish';
// price Zone detection
var zone = 'none';
if (price >= BB.upper) zone = 'top';
if ((price < BB.upper) && (price >= BB.middle)) zone = 'high';
if ((price > BB.lower) && (price < BB.middle)) zone = 'low';
if (price <= BB.lower) zone = 'bottom';
//log.debug('current zone: ', zone);
//log.debug('current trend duration: ', this.trend.duration);
if (this.trend.zone == zone) {
this.trend = {
zone: zone, // none, top, high, low, bottom
duration: this.trend.duration+1,
persisted: true
}
}
else {
this.trend = {
zone: zone, // none, top, high, low, bottom
duration: 0,
persisted: false
}
}
// <= less than or equal to & >= greater than or equal to
if (price < BB.lower && rsiVal >= this.settings.thresholds.low && this.logic.longpos !== true){ // && rsiVal <= this.settings.thresholds.low) { // && this.trend.duration >= this.settings.thresholds.persistence) {
this.logic.longpos = true;
this.advice('long')
log.info('Price has crossed Lower band at ' + price,);
log.info('Current RSI is ' + rsiVal.toFixed(2));
log.info('New Candle is a a ' + Trend, 'Candle');
}
//else if (price > BB.lower && rsiVal >= this.settings.thresholds.high && this.logic.longpos !== false) { // rsiVal <= this.settings.thresholds.low) { // && this.trend.duration >= this.settings.thresholds.persistence) {
//this.logic.longpos = false;
//this.advice('short')
//}
else if (price > BB.upper && rsiVal >= this.settings.thresholds.low && this.logic.longpos !== false) { // && rsiVal >= this.settings.thresholds.high) { // && this.trend.duration >= this.settings.thresholds.persistence) {
this.logic.longpos = false;
this.advice('short')
log.info('Price has crossed Lower band at ' + price,);
log.info('Current RSI is ' + rsiVal.toFixed(2));
log.info('New Candle is a ' + Trend, 'Candle');
}
else {log.info ('Nothing todo!')
log.info('Current Price is ' + price,);
log.info('New Candle is a ' + Trend, 'Candle');}