02-13-2018, 09:52 AM
(This post was last modified: 02-13-2018, 09:58 AM by tommiehansen.)
For a really simple stoploss you could do something like below:
Note that this is just an example. It could ofc be improved upon e.g. we could skip X amount of candles for the stoploss check etc or begin doing stuff such as having 5 min candles but
doing long/short on 2x candles (which would equal 10 minutes) in order to have a stoploss that checks each 5 min candle and a strategy that works on 10 min candles.
Do note that this isn't a 'true' stoploss since candle resolution (time e.g. 10 minutes) will affect the stoploss since the strategy will not be aware of anything that happens in-between candles. In order for it to know such things you would need to use the minimum resolution available (which would be 1 minute in Gekko) and then use some other value for the strategy itself like i write about above. Actually that's pretty interesting so might write something that does it but does it inline (so it doesn't require 1 billion dependencies).
Your ability to write code is the limit.
Code:
// req's
var log = require('../core/log.js'),
config = require('../core/util.js').getConfig();
// create method
var method = {
/* INIT */
init: function()
{
this.name = 'Double TEMA';
// state, current trend
this.resetTrend();
// yes
this.requiredHistory = config.tradingAdvisor.historySize;
/* TEMP: add programmatically */
this.settings.stoploss = -10; // %
this.settings.TEMA_long = 200; // /4 = 4 hour candle
this.settings.TEMA_short = 50;
// add params
this.addTulipIndicator('maSlow', 'tema', { optInTimePeriod: this.settings.TEMA_long });
this.addTulipIndicator('maFast', 'tema', { optInTimePeriod: this.settings.TEMA_short });
}, // init()
/* RESET TREND */
resetTrend: function()
{
var trend = {
duration: 0,
direction: 'none',
longPos: false,
};
this.trend = trend;
},
/* STOPLOSS */
stoploss: function( maxDiff, cur, old )
{
let diff = ((cur/old)-1) * 100;
diff = diff.toFixed(2),
ret = false;
if( maxDiff >= diff )
{
ret = true;
let str = 'Stoploss hit! Stoploss @ ' + maxDiff + '% Current diff: ' + diff + '%';
log.debug(str);
}
return ret;
},
/* CHECK */
check: function( candle )
{
// fetch indicators
let ti = this.tulipIndicators;
let maFast = ti.maFast.result.result,
maSlow = ti.maSlow.result.result;
// base rules
let goLong, goShort;
goLong = maFast > maSlow ? true : false;
goShort = maFast < maSlow ? true : false;
// stoploss (if a long pos has been taken)
if( this.trend.longPos )
{
// check
let stop = this.stoploss( this.settings.stoploss, candle.close, this.trend.longPos );
// if true act on it
if( stop ){
goLong = false;
goShort = true;
this.trend.direction = 'none';
}
}
// LONG
if( goLong )
{
// new trend? (only act on new trends)
if (this.trend.direction !== 'up')
{
this.resetTrend(); // reset
this.trend.direction = 'up';
this.trend.longPos = candle.close; // set long position
this.advice('long'); // go long
//log.debug('LONG');
} // !== 'up'
//this.trend.duration++;
//log.debug ('Pos trend', this.trend.duration, 'candle(s)');
}
// SHORT
else if( goShort )
{
// new trend?
if( this.trend.direction !== 'down' )
{
this.resetTrend(); // reset
this.trend.direction = 'down';
this.advice('short');
//log.debug('SHORT');
} // !== 'down'
//this.trend.duration++;
//log.debug ('Downtrend since ', this.trend.duration, 'candle(s)');
}
else
{
//log.debug('In no trend');
this.advice();
}
} // check()
}; // method {}
module.exports = method;
Note that this is just an example. It could ofc be improved upon e.g. we could skip X amount of candles for the stoploss check etc or begin doing stuff such as having 5 min candles but
doing long/short on 2x candles (which would equal 10 minutes) in order to have a stoploss that checks each 5 min candle and a strategy that works on 10 min candles.
Do note that this isn't a 'true' stoploss since candle resolution (time e.g. 10 minutes) will affect the stoploss since the strategy will not be aware of anything that happens in-between candles. In order for it to know such things you would need to use the minimum resolution available (which would be 1 minute in Gekko) and then use some other value for the strategy itself like i write about above. Actually that's pretty interesting so might write something that does it but does it inline (so it doesn't require 1 billion dependencies).
Your ability to write code is the limit.