Skip to content

Instantly share code, notes, and snippets.

@howie50417
Created December 29, 2017 04:48
Show Gist options
  • Save howie50417/e4a6fd050ca17b3e8766d33f0233bfc8 to your computer and use it in GitHub Desktop.
Save howie50417/e4a6fd050ca17b3e8766d33f0233bfc8 to your computer and use it in GitHub Desktop.
BBStochstoploss.js
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.is_buyin = false;
this.candle_queue = [];
this.price_buyin = 0;
this.BBand_candles = 20;
this.BBand_StdDevs = 2;
this.stoch_FastK = 10;
this.stoch_SlowK = 2;
this.stoch_SlowD = 2;
this.cci_candles = 20;
this.stop_loss_percent = 3;
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 < -100 &&
this.is_buyin == false
){
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;
return;
} else if( candle.close > bbands.bbandsUpper &&
stoch.sotchK > 70 &&
stoch.stochD > 70 &&
cci > 100 &&
this.is_buyin == true
){
this.advice("short");
log.debug('check:');
log.debug('\t', 'sell price:', candle.close);
log.debug('\t', 'bbands Upper:', bbands.bbandsUpper);
this.is_buyin = false;
return;
}
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;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment