Help with Trailing SL
#1
Hi all,

I want to add a trailing stoploss into a strat i have the code for both but no idea how to being them together, any tips?

Thanks
  Reply
#2
Anyone able to help?
  Reply
#3
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
  Reply
#4
(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;
  Reply
#5
@Kris191,

Are you getting an error message or is the trailing stop loss not working at all?
If it isn't crypto, it isn't worth mining, it isn't worth speculating.
https://www.youtube.com/c/crypto49er
  Reply
#6
@crypto49er Keep on publishing your Gekko videos on youtube, I like, great work :-D What is left on your crypto gameplan?
  Reply
#7
(07-18-2018, 07:09 PM)crypto49er Wrote: @Kris191,

Are you getting an error message or is the trailing stop loss not working at all?

Just not working, I've found a TEMA strat i'm going to try and re config, so watch this space.
  Reply
#8
So can someone advise on this please.

I want the follwoing to happen:

Buy when medium crosses above the long
Sell when medium crosses below the long
Sell when the short crosses below the medium but medium is still above the long
Buy when the short crosses above the medium but the medium is still above the long

code currently is:
Code:
if((short > medium) && (medium > long)) {
   this.advice('long')
 } else if((short < medium) && (medium > long)) {
   this.advice('short')
 } else if(((short > medium) && (medium < long))) {
   this.advice('short')
 } else {
   this.advice();

I think its almost there but can some one help me??

Also how do i add console notifications in this saying buy and sell.
thanks
  Reply
#9
@mark.sch,

Thanks! Appreciate it. I got the basics of Zappra's communicate with Gekko via Telegram working. Will try to have a video out today. Other than that, still looking into ways to have Gekko trade with a specific starting amount and have that amount go up/down based on the sell so I can run multiple gekko off the same API key for an exchange. I know AskMike has implemented the base for this in 0.6 but it isn't working yet.

@Kris191,

I will drop the "else" and just use a bunch of if statements. It just makes things more confusing. You can also use "switch" instead of if statements since you have two conditions that will trigger a sell for cleaner code. Just add log.info('message') within the if brackets to get the console notifications. Then just find time frames in backtest data that will trigger each of these if statements to confirm if they are working as you need it.
If it isn't crypto, it isn't worth mining, it isn't worth speculating.
https://www.youtube.com/c/crypto49er
  Reply
#10
(07-20-2018, 05:32 PM)crypto49er Wrote: @mark.sch,

Thanks! Appreciate it. I got the basics of Zappra's communicate with Gekko via Telegram working. Will try to have a video out today. Other than that, still looking into ways to have Gekko trade with a specific starting amount and have that amount go up/down based on the sell so I can run multiple gekko off the same API key for an exchange. I know AskMike has implemented the base for this in 0.6 but it isn't working yet.

@Kris191,

I will  drop the "else" and just use a bunch of if statements. It just makes things more confusing. You can also use "switch" instead of if statements since you have two conditions that will trigger a sell for cleaner code. Just add log.info('message') within the if brackets to get the console notifications. Then just find time frames in backtest data that will trigger each of these if statements to confirm if they are working as you need it.

wow thats just confused me more lol i need to learn some code as i back tested the strat and it didnt trigger a sell or buy
  Reply


Forum Jump:


Users browsing this thread: