Skip to content

Commit

Permalink
Added new x2 (double) EMA-based MACD method
Browse files Browse the repository at this point in the history
  • Loading branch information
Sarah White committed Jun 26, 2014
1 parent 82004f7 commit de84075
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 0 deletions.
20 changes: 20 additions & 0 deletions methods/indicators/x2EMA.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// filename "DEMA.js" already in use by some old legacy code
// name x2EMA means: "double EMA" (usually called DEMA)
// (2 * x1EMA(x)) - x1EMA(x1EMA(x))

var x1EMA = require('./EMA.js');

var Indicator = function(weight) {
this.EMA = new x1EMA(weight);
this.EMAprime = new x1EMA(weight);
}

Indicator.prototype.update = function(weight) {
var EMA = 0.0;
this.EMA.update(weight);
EMA = this.EMA.result;
this.EMAprime.update(EMA);
this.result = (2 * EMA) - this.EMAprime.result;
}

module.exports = Indicator;
26 changes: 26 additions & 0 deletions methods/indicators/x2MACD.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// DEMA-based MACD by kuzetsa, 2014 June 26
var x2EMA = require('./x2EMA.js');

var Indicator = function(config) {
this.diff = false;
this.short = new x2EMA(config.short);
this.long = new x2EMA(config.long);
this.signal = new x2EMA(config.signal);
}

Indicator.prototype.update = function(price) {
this.short.update(price);
this.long.update(price);
this.calculateEMAdiff();
this.signal.update(this.diff);
this.result = this.diff - this.signal.result;
}

Indicator.prototype.calculateEMAdiff = function() {
var shortEMA = this.short.result;
var longEMA = this.long.result;

this.diff = shortEMA - longEMA;
}

module.exports = Indicator;
105 changes: 105 additions & 0 deletions methods/x2MACD.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
MACD, double-EMA variant
by kuzetsa 2014 June 26
(CEXIO lizards variant)
only change is x2EMA instead of (x1)EMA
*/

// helpers
var _ = require('lodash');
var log = require('../core/log.js');

// configuration
var config = require('../core/util.js').getConfig();
var settings = config.x2MACD;

// let's create our own method
var method = {};

// prepare everything our method needs
method.init = function() {

this.name = 'x2MACD';

// keep state about the current trend
// here, on every new candle we use this
// state object to check if we need to
// report it.
this.trend = {
direction: 'none',
duration: 0
};

// how many candles do we need as a base
// before we can start giving advice?
this.requiredHistory = config.tradingAdvisor.historySize;

// define the indicators we need
this.addIndicator('x2macd', 'x2MACD', settings);

}

// 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 digits = 8;
var x2macd = this.indicators.x2macd;

var diff = x2macd.diff;
var signal = x2macd.signal.result;

log.debug('calculated MACD properties for candle:');
log.info('\t', 'short:', x2macd.short.result.toFixed(digits));
log.info('\t', 'long:', x2macd.long.result.toFixed(digits));
log.info('\t', 'macd:', diff.toFixed(digits));
log.info('\t', 'signal:', signal.toFixed(digits));
log.info('\t', 'macdiff:', x2macd.result.toFixed(digits));
}

method.check = function() {
var price = this.lastPrice;
var x2macd = this.indicators.x2macd;

var long = x2macd.long.result;
var short = x2macd.short.result;
var signal = x2macd.signal.result;
var macddiff = x2macd.result;

if(macddiff > settings.thresholds.up) {

// new trend detected
if(this.trend.direction !== 'up')
// reset the state for the new trend
this.trend = {
duration: 0,
direction: 'up',
};

this.trend.duration++;

log.info('In uptrend since', this.trend.duration, 'candle(s)');

this.advice('long');

} else {

this.trend = {
duration: 0,
direction: 'lizards',
};


this.advice('lizards');

}
}

module.exports = method;

0 comments on commit de84075

Please sign in to comment.