Skip to content
This repository has been archived by the owner on Feb 15, 2022. It is now read-only.

Commit

Permalink
add talib "TRIX - 1-day Rate-Of-Change (ROC) of a Triple Smooth EMA" …
Browse files Browse the repository at this point in the history
…strategy (#1280)
  • Loading branch information
Haehnchen authored and DeviaVir committed Feb 5, 2018
1 parent a6cdb37 commit 99b2353
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 1 deletion.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,15 @@ ta_macd
--overbought_rsi_periods=<value> number of periods for overbought RSI (default: 25)
--overbought_rsi=<value> sold when RSI exceeds this value (default: 70)
ta_trix
description:
TRIX - 1-day Rate-Of-Change (ROC) of a Triple Smooth EMA with rsi oversold
options:
--period=<value> period length eg 10m (default: 5m)
--timeperiod=<value> timeperiod for TRIX (default: 30)
--overbought_rsi_periods=<value> number of periods for overbought RSI (default: 25)
--overbought_rsi=<value> sold when RSI exceeds this value (default: 70)
trend_ema (default)
description:
Buy when (EMA - last(EMA) > 0) and sell when (EMA - last(EMA) < 0). Optional buy on low RSI.
Expand Down
6 changes: 6 additions & 0 deletions extensions/strategies/ta_trix/_codemap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
_ns: 'zenbot',

'strategies.ta_trix': require('./strategy'),
'strategies.list[]': '#strategies.ta_trix'
}
82 changes: 82 additions & 0 deletions extensions/strategies/ta_trix/strategy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
var z = require('zero-fill')
, n = require('numbro')

module.exports = function container (get, set, clear) {
return {
name: 'ta_trix',
description: 'TRIX - 1-day Rate-Of-Change (ROC) of a Triple Smooth EMA with rsi oversold',

getOptions: function () {
this.option('period', 'period length eg 10m', String, '5m')
this.option('timeperiod', 'timeperiod for TRIX', Number, 30)
this.option('overbought_rsi_periods', 'number of periods for overbought RSI', Number, 25)
this.option('overbought_rsi', 'sold when RSI exceeds this value', Number, 70)
},

calculate: function (s) {
if (s.options.overbought_rsi) {
// sync RSI display with overbought RSI periods
s.options.rsi_periods = s.options.overbought_rsi_periods
get('lib.rsi')(s, 'overbought_rsi', s.options.overbought_rsi_periods)
if (!s.in_preroll && s.period.overbought_rsi >= s.options.overbought_rsi && !s.overbought) {
s.overbought = true

if (s.options.mode === 'sim' && s.options.verbose) {
console.log(('\noverbought at ' + s.period.overbought_rsi + ' RSI, preparing to sold\n').cyan)
}
}
}
},

onPeriod: function (s, cb) {
if (!s.in_preroll && typeof s.period.overbought_rsi === 'number') {
if (s.overbought) {
s.overbought = false
s.signal = 'sell'
return cb()
}
}

get('lib.ta_trix')(s, s.options.timeperiod).then(function(signal) {
s.period['trix'] = signal;

if (s.period.trix && s.lookback[0] && s.lookback[0].trix) {
s.period.trend_trix = s.period.trix >= 0 ? 'up' : 'down';
}

if (s.period.trend_trix == 'up') {
if (s.trend !== 'up') {
s.acted_on_trend = false
}

s.trend = 'up'
s.signal = !s.acted_on_trend ? 'buy' : null
} else if (s.period.trend_trix == 'down') {
if (s.trend !== 'down') {
s.acted_on_trend = false
}

s.trend = 'down'
s.signal = !s.acted_on_trend ? 'sell' : null
}

cb()
}).catch(function(error) {
console.log(error)
cb()
})
},

onReport: function (s) {
let cols = []

if (typeof s.period.trix === 'number') {
let color = s.period.trix > 0 ? 'green' : 'red';

cols.push(z(8, n(s.period.trix).format('0.0000'), ' ')[color])
}

return cols
}
}
}
2 changes: 1 addition & 1 deletion lib/_codemap.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module.exports = {
_ns: 'zenbot',
_folder: 'lib',

'ta_ema': require('./ta_ema'),
'ta_macd': require('./ta_macd'),
'ta_trix': require('./ta_trix'),
'cci': require('./cci'),
'ema': require('./ema'),
'engine': require('./engine'),
Expand Down
43 changes: 43 additions & 0 deletions lib/ta_trix.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
var talib = require('talib')

module.exports = function container (get, set, clear) {
return function trix(s, timeperiod) {
return new Promise(function(resolve, reject) {
// create object for talib. only close is used for now but rest might come in handy
if (!s.marketData) {
s.marketData = { open: [], close: [], high: [], low: [], volume: [] };
}

if (s.lookback.length > s.marketData.close.length) {
for (var i = (s.lookback.length - s.marketData.close.length) - 1; i >= 0; i--) {
s.marketData.close.push(s.lookback[i].close);
}
}

if (s.marketData.close.length < timeperiod) {
resolve();
return;
}

let tmpMarket = JSON.parse(JSON.stringify(s.marketData.close));

// add current period
tmpMarket.push(s.period.close)

talib.execute({
name: "TRIX",
startIdx: 0,
endIdx: tmpMarket.length -1,
inReal: tmpMarket,
optInTimePeriod: timeperiod
}, function (err, result) {
if (err) {
reject(err, result);
return
}

resolve(result.result.outReal[(result.nbElement - 1)]);
});
});
};
}

0 comments on commit 99b2353

Please sign in to comment.