BBRSI Stop loss?
#9
bojim,

I was wrong. And I'm happy to be! I'm glad you asked more questions about it. 

I just correctly implemented stop loss this time. The simple idea is to send a sell signal in a separate if statement that checks the buy price vs. the current price. 

The code is below:

Code:
/*

 BB strategy - okibcn 2018-01-03
 With Stop Loss - Crypto49er 2018-05-03

*/
// helpers
var _ = require('lodash');
var log = require('../core/log.js');

var BB = require('./indicators/BB.js');
var rsi = require('./indicators/RSI.js');

var advised = false;
var buyPrice = 0.0;

// 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);
}


// 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;

 // 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
   }
 }

 if (!advised && price <= BB.lower && rsiVal <= this.settings.thresholds.low && this.trend.duration >= this.settings.thresholds.persistence) {
   this.advice('long');

   advised = true;
   buyPrice = candle.close;

 }

 if (advised && buyPrice > candle.close * (1 + this.settings.stoploss.percentage * .01)){
   log.debug("Stop loss triggered, sell at", candle.close);
   this.advice('short');
   advised = false;
 }

 if (advised && price >= BB.middle && rsiVal >= this.settings.thresholds.high) {
   this.advice('short');
   advised = false;


 }

 // this.trend = {
 //   zone: zone,  // none, top, high, low, bottom
 //   duration: 0,
 //   persisted: false


}

module.exports = method;
Here is the TOML file: 
Code:
interval = 14

[thresholds]
low = 40
high = 40
persistence = 9

[bbands]
TimePeriod = 20
NbDevUp = 2
NbDevDn = 2

#Should be a number between 1 - 99
[stoploss]
percentage = 3

I eventually will put this on a Github repo somewhere. But honestly it wasn't that much of a contribution.
So I tested it and confirmed that it works. It does, although there will be times when you will lose more than 3% as it depends on the duration of your candles (the longer, the higher the chance of losing more than 3%). It still took off the more damaging trades than the original so it performs another 5% better in the date range that you were testing.

Original:
[Image: kYxBpAM.png]
With Stop Loss:
[Image: Va3Qd4f.png]
Hope this helps!
If it isn't crypto, it isn't worth mining, it isn't worth speculating.
https://www.youtube.com/c/crypto49er
  Reply


Messages In This Thread
BBRSI Stop loss? - by bojim - 04-24-2018, 08:53 PM
Stop loss? - by susitronix - 04-25-2018, 12:59 AM
RE: BBRSI Stop loss? - by crypto49er - 04-26-2018, 12:47 PM
RE: BBRSI Stop loss? - by susitronix - 05-02-2018, 10:41 AM
RE: BBRSI Stop loss? - by bojim - 05-03-2018, 04:06 PM
RE: BBRSI Stop loss? - by xFFFFF - 05-02-2018, 12:05 PM
RE: BBRSI Stop loss? - by bojim - 05-03-2018, 04:51 PM
RE: BBRSI Stop loss? - by crypto49er - 05-03-2018, 08:16 PM
RE: BBRSI Stop loss? - by crypto49er - 05-03-2018, 09:27 PM
RE: BBRSI Stop loss? - by bojim - 05-06-2018, 11:30 PM
RE: BBRSI Stop loss? - by crypto49er - 05-07-2018, 03:08 PM
RE: BBRSI Stop loss? - by crypto49er - 05-08-2018, 04:55 PM
RE: BBRSI Stop loss? - by bojim - 05-09-2018, 06:26 PM

Forum Jump:


Users browsing this thread: