Skip to content

Instantly share code, notes, and snippets.

@askmike
Forked from Pedro942/strat.js
Created May 4, 2017 11:48
Show Gist options
  • Save askmike/a50c1438095bc56a09723c90bfefacdf to your computer and use it in GitHub Desktop.
Save askmike/a50c1438095bc56a09723c90bfefacdf to your computer and use it in GitHub Desktop.
Start.JS
// Let's create our own strategy
var strat = {};
var parameters = {
short: 12,
long: 26,
signal: 9,
down: -0.0001,
up: 0.005,
persistence: 1
};
var parameters1 = {
interval: 14,
low: 30,
high: 70,
persistence: 2
};
var parameters2 = {
constant: 0.015,
history: 90,
up: 100,
down: -100,
persistence: 0
};
// 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);
//add native CCI
this.addIndicator('mynativeCCI', 'CCI', parameters2);
}
// 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.0001;
var MACDsaysSELL = parameters.up > 0.005;
// setting vars for RSI results
var RSI = this.indicators.mynativeRSI.rsi;
var RSIsaysBUY = RSI > parameters1.high;
var RSIsaysSELL = RSI < parameters1.low;
// setting vars for CCI results
var CCI = this.indicators.mynativeCCI.cci;
var CCIsaysBUY = parameters2.down < -100;
var CCIsaysSELL = parameters2.down > 100;
/// 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 && CCIsaysSELL) {
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 && CCIsaysBUY) {
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