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

Commit

Permalink
Srsi macd (#369)
Browse files Browse the repository at this point in the history
* updates to srsi_macd

* rename strategy to srsi_macd

* srsi_macd and sma indicator tweaks

* fixed type in length

* fixes
  • Loading branch information
talvasconcelos authored and DeviaVir committed Jul 10, 2017
1 parent 2c9add7 commit 26e3565
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 100 deletions.
6 changes: 0 additions & 6 deletions extensions/strategies/rsi_macd/_codemap.js

This file was deleted.

84 changes: 0 additions & 84 deletions extensions/strategies/rsi_macd/strategy.js

This file was deleted.

6 changes: 6 additions & 0 deletions extensions/strategies/srsi_macd/_codemap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
_ns: 'zenbot',

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

module.exports = function container (get, set, clear) {
return {
name: 'rsi_macd',
description: 'Stochastic MACD Strategy',

getOptions: function () {
this.option('period', 'period length', String, '30m')
this.option('min_periods', 'min. number of history periods', Number, 200)
this.option('rsi_periods', 'number of RSI periods', 14)
this.option('srsi_periods', 'number of RSI periods', Number, 9)
this.option('srsi_k', '%D line', Number, 5)
this.option('srsi_d', '%D line', Number, 3)
this.option('oversold_rsi', 'buy when RSI reaches or drops below this value', Number, 20)
this.option('overbought_rsi', 'sell when RSI reaches or goes above this value', Number, 80)
this.option('ema_short_period', 'number of periods for the shorter EMA', Number, 24)
this.option('ema_long_period', 'number of periods for the longer EMA', Number, 200)
this.option('signal_period', 'number of periods for the signal EMA', Number, 9)
this.option('up_trend_threshold', 'threshold to trigger a buy signal', Number, 0)
this.option('down_trend_threshold', 'threshold to trigger a sold signal', Number, 0)
},

calculate: function (s) {
// compute Stochastic RSI
get('lib.srsi')(s, 'srsi', s.options.rsi_periods, s.options.srsi_k, s.options.srsi_d)

// compute MACD
get('lib.ema')(s, 'ema_short', s.options.ema_short_period)
get('lib.ema')(s, 'ema_long', s.options.ema_long_period)
if (s.period.ema_short && s.period.ema_long) {
s.period.macd = (s.period.ema_short - s.period.ema_long)
get('lib.ema')(s, 'signal', s.options.signal_period, 'macd')
if (s.period.signal) {
s.period.macd_histogram = s.period.macd - s.period.signal
}
}
},

onPeriod: function (s, cb) {
if (!s.in_preroll)
if (typeof s.period.macd_histogram === 'number' && typeof s.lookback[0].macd_histogram === 'number' && typeof s.period.srsi_K === 'number' && typeof s.period.srsi_D === 'number')
// Buy signal
if (s.period.macd_histogram >= s.options.up_trend_threshold)
if (s.period.srsi_K > s.period.srsi_D && s.period.srsi_K > s.lookback[0].srsi_K && s.period.srsi_K < s.options.oversold_rsi)
s.signal = 'buy'

// Sell signal
if (s.period.macd_histogram < s.options.down_trend_threshold)
if (s.period.srsi_K < s.period.srsi_D && s.period.srsi_K < s.lookback[0].srsi_K && s.period.srsi_K > s.options.overbought_rsi)
s.signal = 'sell'

// Hold
//s.signal = null;
cb()
},
onReport: function (s) {
var cols = []
if (typeof s.period.macd_histogram === 'number') {
var color = 'grey'
if (s.period.macd_histogram > 0) {
color = 'green'
}
else if (s.period.macd_histogram < 0) {
color = 'red'
}
cols.push(z(8, n(s.period.macd_histogram).format('+00.0000'), ' ')[color])
cols.push(z(8, n(s.period.srsi_K).format('000'), ' ').cyan)
}
else {
cols.push(' ')
}
return cols
}
}
}
3 changes: 2 additions & 1 deletion lib/_codemap.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module.exports = {
'engine': require('./engine'),
'normalize-selector': require('./normalize-selector'),
'rsi': require('./rsi'),
'sma': require('./sma'),
'srsi': require('./srsi'),
'stddev': require('./stddev')
}
}
16 changes: 7 additions & 9 deletions lib/sma.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ module.exports = function container (get, set, clear) {
return function sma (s, key, length, source_key) {
if (!source_key) source_key = 'close'
if (s.lookback.length >= length) {
var prev_sma = s.lookback[0][key]
if (typeof prev_sma === 'undefined' || isNaN(prev_sma)) {
var sum = 0
s.lookback.slice(0, length).forEach(function (period) {
sum += period[source_key]
})
prev_sma = sum / length
}
s.period[key] = prev_sma
let SMA = s.lookback
.slice(0, length)
.reduce((sum, cur) => {
return sum + cur[source_key]
}, 0)

s.period[key] = SMA / length
}
}
}

0 comments on commit 26e3565

Please sign in to comment.