Last active
December 28, 2017 06:13
-
-
Save howie50417/53cd438ce425b45a6e218cc22e27f726 to your computer and use it in GitHub Desktop.
BBands.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var _ = require('lodash'); | |
var log = require('../core/log.js'); | |
// let's create our own method | |
var method = {}; | |
// prepare everything our method needs | |
method.init = function() { | |
this.name = 'BBands'; | |
this.candle_queue = []; | |
this.is_buyin = false; | |
this.price_buyin = 0; | |
this.BBand_candles = 21; | |
this.BBand_StdDevs = 2; | |
this.stoch_FastK = 5; | |
this.stoch_SlowK = 2; | |
this.stoch_SlowD = 3; | |
this.cci_candles = 20; | |
this.stop_loss_percent = 4; | |
if(typeof this.settings != "undefined"){ | |
if(typeof this.settings.BBand_candles != "undefined"){ | |
this.BBand_candles = this.settings.BBand_candles; | |
} | |
if(typeof this.settings.BBand_StdDevs != "undefined"){ | |
this.BBand_StdDevs = this.settings.BBand_StdDevs; | |
} | |
if(typeof this.settings.stoch_FastK != "undefined"){ | |
this.stoch_FastK = this.settings.stoch_FastK; | |
} | |
if(typeof this.settings.stoch_SlowK != "undefined"){ | |
this.stoch_SlowK = this.settings.stoch_SlowK; | |
} | |
if(typeof this.settings.stoch_SlowD != "undefined"){ | |
this.stoch_SlowD = this.settings.stoch_SlowD; | |
} | |
if(typeof this.settings.cci_candles != "undefined"){ | |
this.cci_candles = this.settings.cci_candles; | |
} | |
if(typeof this.settings.stop_loss_percent != "undefined"){ | |
this.stop_loss_percent = this.settings.stop_loss_percent; | |
} | |
} | |
log.debug('BBands init:'); | |
log.debug('\t', 'BBand_candles:', this.BBand_candles); | |
log.debug('\t', 'BBand_StdDevs:', this.BBand_StdDevs); | |
log.debug('\t', 'stoch_FastK:', this.stoch_FastK); | |
log.debug('\t', 'stoch_SlowK:', this.stoch_SlowK); | |
log.debug('\t', 'stoch_SlowD:', this.stoch_SlowD); | |
log.debug('\t', 'cci_candles:', this.cci_candles); | |
log.debug('\t', 'stop_loss_percent:', this.stop_loss_percent); | |
// add the indicator to the strategy | |
this.addTulipIndicator('my_bbands', 'bbands', { | |
optInTimePeriod: this.BBand_candles, | |
optInNbStdDevs: this.BBand_StdDevs | |
}); | |
//results: ['bbandsLower', 'bbandsMiddle', 'bbandsUpper'], | |
this.addTulipIndicator('my_stoch', 'stoch', { | |
optInFastKPeriod: this.stoch_FastK, | |
optInSlowKPeriod: this.stoch_SlowK, | |
optInSlowDPeriod: this.stoch_SlowD | |
}); | |
//results: ['sotchK', 'stochD'], | |
this.addTulipIndicator('my_cci', 'cci', { | |
optInTimePeriod: this.cci_candles | |
}); | |
//results: ['result'], | |
} | |
method.update = function(candle) { | |
if(this.candle_queue.length>0){ | |
candle.delta = candle.close - this.candle_queue[0].close; | |
} | |
this.candle_queue.unshift(candle); | |
} | |
method.check = function(candle) { | |
if(this.candle_queue.length < this.BBand_candles){ | |
return; | |
} | |
var bbands = this.tulipIndicators.my_bbands.result; | |
var stoch = this.tulipIndicators.my_stoch.result; | |
var cci = this.tulipIndicators.my_cci.result.result; | |
if(candle.close < bbands.bbandsLower && | |
stoch.sotchK < 20 && | |
stoch.stochD < 20 && | |
cci < -90 | |
){ | |
this.advice("long"); | |
log.debug('check:'); | |
log.debug('\t', 'buy price:', candle.close); | |
log.debug('\t', 'bbands Lower:', bbands.bbandsLower); | |
this.is_buyin = true; | |
this.price_buyin = candle.close; | |
}else if(candle.close > bbands.bbandsUpper && | |
stoch.sotchK > 70 && | |
stoch.stochD > 70 && | |
cci > 100 | |
){ | |
this.advice("short"); | |
log.debug('check:'); | |
log.debug('\t', 'sell price:', candle.close); | |
log.debug('\t', 'bbands Upper:', bbands.bbandsUpper); | |
this.is_buyin = false; | |
} | |
if(this.is_buyin == true){ | |
if(candle.close / this.price_buyin * 100 < 100 - this.stop_loss_percent){ | |
this.advice("short"); | |
log.debug('check:'); | |
log.debug('\t', 'stop_loss at percent:', candle.close / this.price_buyin * 100); | |
log.debug('\t', 'sell price:', candle.close); | |
this.is_buyin = false; | |
} | |
} | |
} | |
module.exports = method; | |
// in BBands.toml | |
// | |
// BBand_candles = 21 | |
// BBand_StdDevs = 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment