Skip to content

Instantly share code, notes, and snippets.

@Pedro942
Created May 4, 2017 12:21
Show Gist options
  • Save Pedro942/4edb1bcac372bba9254080553ff39231 to your computer and use it in GitHub Desktop.
Save Pedro942/4edb1bcac372bba9254080553ff39231 to your computer and use it in GitHub Desktop.
Strategy MACD + RSI
// Let's create our own strategy
var strat = {};
var parameters = {
short: 12,
long: 26,
signal: 9,
down: 0,
up: 0,
persistence: 1
};
var parameters1 = {
interval: 14,
low: 30,
high: 70,
persistence: 2
};
// Prepare everything our strat needs
strat.init = function() {
// start of with no investment
this.currentTrend = 'short';
// your code!
// add a native MACD
this.addIndicator('mynativemacd', 'MACD', parameters);
//add native RSI
this.addIndicator('mynativeRSI', 'RSI', parameters1);
}
// What happens on every new candle?
strat.update = function(candle) {
// your code!
}
// For debugging purposes.
strat.log = function() {
// your code!
}
// Based on the newly calculated
// information, check if we should
// update or not.
strat.check = function() {
// your code!
//setting vars for MACD results
var MACD = this.indicators.mynativemacd.macd;
var MACDsaysBUY = parameters.down < 0;
var MACDsaysSELL = parameters.up > 0;
// setting vars for RSI results
var RSI = this.indicators.mynativeRSI.rsi;
var RSIsaysBUY = RSI > parameters1.high;
var RSIsaysSELL = RSI < parameters1.low;
/// Setting conditions
if (this.currentTrend === 'long') {
// maybe we need to advice short now?
// only if BOTH MACD and RSI and CCI say so:
if (MACDsaysSELL && RSIsaysSELL) {
this.currentTrend = 'short';
this.advice('short')
}
} else if (this.currentTrend === 'short') {
// maybe we need to advice long now?
// only if BOTH MACD and RSI and CCI say so:
if (MACDsaysBUY && RSIsaysBUY) {
this.currentTrend = 'long';
this.advice('long')
}
}
}
module.exports = strat;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment