Skip to content

Instantly share code, notes, and snippets.

@askmike
Last active November 15, 2020 20:15
Show Gist options
  • Save askmike/715df74a8af6710b6fa64be35897ed79 to your computer and use it in GitHub Desktop.
Save askmike/715df74a8af6710b6fa64be35897ed79 to your computer and use it in GitHub Desktop.
var _ = require('lodash');
// examples values:
var RSIinterval = 10;
var RSIlow = 20;
var RSIhigh = 80;
var StochRSIinterval = 15;
var StochRSIlow = 30;
var StochRSIhigh = 70;
// Let's create our own strat
var strat = {};
// Prepare everything our method needs
strat.init = function() {
// start of with no investment
this.currentTrend = 'short';
// define the indicators we need
this.addIndicator('rsi', 'RSI', { interval: RSIinterval });
this.addIndicator('stochrsi', 'RSI', { interval: StochRSIinterval });
// we need this for our calcStochRSI method
this.RSIhistory = [];
}
// helper function to calc stochRSI based on RSI and some history
strat.calcStochRSI = function() {
// Get the indicator value, calculated by gekko
var rsi = this.indicators.stochrsi.rsi;
this.RSIhistory.push(rsi);
if(_.size(this.RSIhistory) > this.interval)
this.RSIhistory.shift();
this.lowestRSI = _.min(this.RSIhistory);
this.highestRSI = _.max(this.RSIhistory);
// return the stochRSI indicator
return ((this.rsi - this.lowestRSI) / (this.highestRSI - this.lowestRSI)) * 100;
}
// What happens on every new candle?
strat.update = function(candle) {
// do everything that isn't automatically done by gekko
// set the stochRSI indicator
this.StochRSI = this.calcStochRSI();
}
// Based on the newly calculated
// information, check if we should
// update or not.
strat.check = function() {
// either true or false
var StochRSIsaysBUY = this.stochRSI > StochRSIhigh;
var StochRSIsaysSELL = this.stochRSI < StochRSIlow;
// same for normal RSI
var RSI = this.indicators.rsi.rsi;
var RSIsaysBUY = RSI > RSIhigh;
var RSIsaysSELL = RSI < RSIlow;
if(this.currentTrend === 'long') {
// maybe we need to advice short now?
// only if BOTH RSI and stochRSI say so:
if(StochRSIsaysSELL && RSIsaysSELL) {
this.currentTrend = 'short';
this.advice('short')
}
} else if(this.currentTrend === 'short') {
// maybe we need to advice long now?
// only if BOTH RSI and stochRSI say so:
if(StochRSIsaysBUY && 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