07-17-2018, 08:01 PM
(07-17-2018, 03:06 AM)askmike Wrote: There are some guides on the forum regarding stop losses, but if you are not that comfortable it is probably better to wait until we get native stop losses added, see here: https://forum.gekko.wizb.it/thread-57314.html
Well i tried to put the trailing stop loss into the strat and if im honest it looks good to me but the computer says no!! Anyone albe to tell me whats wrong with this??
Code:
// Source: https://raw.githubusercontent.com/vrfurl/gekko/stable/strategies/DEMACrossover.js
// Downloaded from: https://github.com/xFFFFF/Gekko-Strategies
// helpers
var _ = require('lodash');
var log = require('../core/log.js');
var helper = require('../helper.js');
// let's create our own method
var method = {};
// prepare everything our method needs
method.init = function() {
this.name = 'DEMACrossover';
this.currentTrend;
this.requiredHistory = 0;
// define the indicators we need
//this.addIndicator('dema', 'DEMA', this.settings);
//Determine if we first want to buy or sell
if(this.settings.firstTrade === 'buy') {
this.currentTrend = 'down';
}
else if(this.settings.firstTrade === 'sell'){
this.currentTrend = 'up';
}
log.debug("Short DEMA size: "+this.settings.shortSize);
log.debug("Long DEMA size: "+this.settings.longSize);
this.addTalibIndicator('shortDEMA', 'dema', {optInTimePeriod : this.settings.shortSize});
this.addTalibIndicator('longDEMA', 'dema', {optInTimePeriod : this.settings.longSize});
log.debug(this.name+' Strategy initialized');
}
strat.init = function() {
this.stopLoss = helper.trailingStopLoss();
this.stopLoss.percentage = this.settings.trailingStop.percentage;
}
// what happens on every new candle?
method.update = function(candle) {
// nothing!
}
// for debugging purposes: log the last calculated
// EMAs and diff.
method.log = function() {
var shortDEMA = this.talibIndicators.shortDEMA;
var longDEMA = this.talibIndicators.longDEMA;
log.debug('Required history is: '+this.requiredHistory);
log.debug('calculated DEMA properties for candle:');
log.debug('\t shortDEMA :', shortDEMA.result);
log.debug('\t', 'longDEMA:', longDEMA.result);
}
method.check = function(candle) {
var shortResult = this.talibIndicators.shortDEMA.result.outReal;
var longResult = this.talibIndicators.longDEMA.result.outReal;
var price = candle.close;
var message = '@ ' + price.toFixed(8);
if(this.stopLoss.isTriggered(currentPrice)) {
this.advice('short');
this.stopLoss.destroy();
//DEMA Golden Cross
if(shortResult > longResult) {
log.debug('we are currently in uptrend', message);
if(this.currentTrend !== 'up') {
this.currentTrend = 'up';
this.advice('long');
log.debug("Going to buy");
} else {
log.debug("Nothing to buy");
this.advice();
}
} else if(longResult > shortResult) {
log.debug('we are currently in a downtrend', message);
if(this.currentTrend !== 'down') {
this.currentTrend = 'down';
this.advice('short');
log.debug("Going to sell");
} else
log.debug("Nothing to sell");
this.advice();
} else if(shortConditions) {
this.advice('short');
this.stopLoss.destroy();
} else {
this.stopLoss.update(currentPrice);
this.advice();
}else {
log.debug('we are currently not in an up or down trend', message);
this.advice();
}
}
module.exports = method;