Skip to content

Instantly share code, notes, and snippets.

@John1231983
Last active October 8, 2018 23:35
Show Gist options
  • Save John1231983/0822062d6e252dda82e1759bb47ef4fb to your computer and use it in GitHub Desktop.
Save John1231983/0822062d6e252dda82e1759bb47ef4fb to your computer and use it in GitHub Desktop.
/*
StochRSI - SamThomp 11/06/2014
(updated by askmike) @ 30/07/2016
*/
// helpers
var _ = require('lodash');
var log = require('../core/log.js');
var RSI = require('./indicators/RSI.js');
// let's create our own method
var method = {};
// prepare everything our method needs
method.init = function() {
this.interval = this.settings.interval;
this.trend = {
direction: 'none',
duration: 0,
persisted: false,
adviced: false
};
this.requiredHistory = this.tradingAdvisor.historySize;
// define the indicators we need
this.addIndicator('rsi', 'RSI', { interval: this.interval });
this.addIndicator('macd', 'MACD', this.settings);
this.addIndicator('bb', 'BB', this.settings.bbands);
this.RSIhistory = [];
}
// what happens on every new candle?
method.update = function(candle) {
this.rsi = this.indicators.rsi.result;
this.RSIhistory.push(this.rsi);
if(_.size(this.RSIhistory) > this.interval)
// remove oldest RSI value
this.RSIhistory.shift();
this.lowestRSI = _.min(this.RSIhistory);
this.highestRSI = _.max(this.RSIhistory);
this.stochRSI = ((this.rsi - this.lowestRSI) / (this.highestRSI - this.lowestRSI)) * 100;
}
// for debugging purposes log the last
// calculated parameters.
method.log = function(candle) {
var digits = 8;
log.debug('calculated StochRSI properties for candle:');
log.debug('\t', 'rsi:', this.rsi.toFixed(digits));
log.debug("StochRSI min:\t\t" + this.lowestRSI.toFixed(digits));
log.debug("StochRSI max:\t\t" + this.highestRSI.toFixed(digits));
log.debug("StochRSI Value:\t\t" + this.stochRSI.toFixed(2));
//MACD
var macd = this.indicators.macd;
var diff = macd.diff;
var signal = macd.signal.result;
var BB = this.indicators.bb;
var price = candle.close;
var macddiff = this.indicators.macd.result;
//BB
var BB = this.indicators.bb;
//BB.lower; BB.upper; BB.middle are your line values
var MACDsaysBUY = macddiff > this.settings.thresholds.up;
var MACDsaysSELL = macddiff <= this.settings.thresholds.down;
var StochRSIsaysBUY = this.stochRSI < this.settings.thresholds.low;
var StochRSIsaysSELL = this.stochRSI >= this.settings.thresholds.high;
var BBsayBUY=price >= (BB.middle-(BB.middle-BB.lower)/4);
var BBsaySELL=price <= BB.lower; //>= BB.upper || price <= BB.lower
log.debug('calculated MACD properties for candle:');
log.debug('\t', 'short:', macd.short.result.toFixed(digits));
log.debug('\t', 'long:', macd.long.result.toFixed(digits));
log.debug('\t', 'macd:', diff.toFixed(digits));
log.debug('\t', 'signal:', signal.toFixed(digits));
log.debug('\t', 'macdiff:', macd.result.toFixed(digits));
log.debug('\t', 'BB.lower:', BB.lower.toFixed(digits));
log.debug('\t', 'BB.middle:', BB.middle.toFixed(digits));
log.debug('\t', 'BB.upper:', BB.upper.toFixed(digits));
log.debug('\t', 'price', price.toFixed(digits));
if(BBsayBUY) {
log.debug('\t', 'BBsaysBUY');
}
if(MACDsaysBUY) {
log.debug('\t', 'MACDsaysBUY');
}
if(StochRSIsaysBUY) {
log.debug('\t', 'StochRSIsaysBUY');
}
if(BBsaySELL) {
log.debug('\t', 'BBsaysSELL');
}
if(MACDsaysSELL) {
log.debug('\t', 'MACDsaysSELL');
}
if(StochRSIsaysSELL) {
log.debug('\t', 'StochRSIsaysSELL');
}
if(MACDsaysSELL && StochRSIsaysSELL) {
log.debug('\t', 'StochRSIsaysSELL and MACDsaysSELL');
}
if(MACDsaysBUY && StochRSIsaysBUY && BBsayBUY) {
log.debug('\t', 'MACDsaysBUY and StochRSIsaysBUY and BBsaysBUY');
}
}
method.check = function(candle) {
//MACD
var macddiff = this.indicators.macd.result;
//BB
var BB = this.indicators.bb;
//BB.lower; BB.upper; BB.middle are your line values
var price = candle.close;
//buy when stochRSI in low and MACD in up
//short->sell, long->buy
var MACDsaysBUY = macddiff > this.settings.thresholds.up;
var MACDsaysSELL = macddiff <= this.settings.thresholds.down;
var StochRSIsaysBUY = this.stochRSI < this.settings.thresholds.low;
var StochRSIsaysSELL = this.stochRSI >= this.settings.thresholds.high;
var BBsayBUY=price >= (BB.middle-(BB.middle-BB.lower)/4);
var BBsaySELL=price <= BB.lower; //>= BB.upper || price <= BB.lower
if(MACDsaysSELL && StochRSIsaysSELL) {
// new trend detected
if(this.trend.direction !== 'high')
this.trend = {
duration: 0,
persisted: false,
direction: 'high',
adviced: false
};
this.trend.duration++;
log.debug('In high since', this.trend.duration, 'candle(s)');
if(this.trend.duration >= this.settings.thresholds.persistence )
this.trend.persisted = true;
if(this.trend.persisted && !this.trend.adviced) {
this.trend.adviced = true;
this.advice('short');
} else
this.advice();
}
//buy when stochRSI in high and MACD in low
else if(MACDsaysBUY && StochRSIsaysBUY && BBsayBUY) {
// new trend detected
if(this.trend.direction !== 'low')
this.trend = {
duration: 0,
persisted: false,
direction: 'low',
adviced: false
};
this.trend.duration++;
log.debug('In low since', this.trend.duration, 'candle(s)');
if(this.trend.duration >= this.settings.thresholds.persistence)
this.trend.persisted = true;
if(this.trend.persisted && !this.trend.adviced) {
this.trend.adviced = true;
this.advice('long');
} else
this.advice();
} else {
// trends must be on consecutive candles
this.trend.duration = 0;
log.debug('In no trend');
this.advice();
}
}
module.exports = method;
@patjk
Copy link

patjk commented Jan 4, 2018

What are the needed parameters in the .toml file in config for this?

And I'm using these parameters, taken from StochRSI and MACD:
interval = 14
short = 10
long = 21
signal = 9

[thresholds]
low = 20
high = 80
persistence = 3
down = -0.025
up = 0.025

However, when I click Backtest, nothing happens. Are the parameters above the correct parameters I should be using in the gekko-stable/config/StochRSI_MACD.toml file? Thanks.

@ttutuncu
Copy link

ttutuncu commented Jan 9, 2018

Has this strategy worked for you?

@laravellously
Copy link

@patjk your persistence value is a bit high. You could use 1 or 2.

@dumbanddumbererest
Copy link

@patjk - almost mate, just needed to add the bbands section.
eg: (nb the settings here are pretty whacked and will pretty much never enter a trade)

StochRSI_MACD.toml

short = 10
long = 21
signal = 9
interval = 3

[thresholds]
down = -0.025
up = 0.025
persistence = 1
low = 20
high = 80

[bbands]
TimePeriod = 1
NbDevDn = 1.96
NbDevUp = 1.96

@socialj80
Copy link

Hi guys,
please help me. I've created StochRSI_MACD.js and put it in strategies folder. I've created StochRSI_MACD.toml and put it config/strategies. I select the dataset but unfortunately when I click on "Backctest" button nothing happens. Please could you help me.?
thanks
joe

@avi123r
Copy link

avi123r commented Feb 7, 2018

2018-02-07 17:39:13 (INFO): Setting up Gekko in backtest mode
2018-02-07 17:39:13 (INFO):
2018-02-07 17:39:13 (INFO): Setting up:
2018-02-07 17:39:13 (INFO): Trading Advisor
2018-02-07 17:39:13 (INFO): Calculate trading advice
2018-02-07 17:39:13 (INFO): Using the strategy: StochRSI_MACD
xxx POST /api/backtest 500 185ms -

Error: non-error thrown: Child process has died.
at Object.onerror (/Users/avi/Documents/gekko/node_modules/koa/lib/context.js:105:40)
at
at process._tickCallback (internal/process/next_tick.js:160:7)

getting this error when running the script on Gekko

@jeongmincha
Copy link

jeongmincha commented Feb 10, 2018

I'm just curious about the part var BBsaySELL=price <= BB.lower;
I know that I should BUY shares at the point because if the price is lower than the lower bound of Bollinger bands, that point might be the lowest price.

@brobyns
Copy link

brobyns commented Feb 15, 2018

Why is BBsaySELL not used in the if condition?
if(MACDsaysSELL && StochRSIsaysSELL)

@toyotajon93
Copy link

Did anyone figure out the error?

Error: non-error thrown: Child process has died.
at Object.onerror (/Users/avi/Documents/gekko/node_modules/koa/lib/context.js:105:40)
at
at process._tickCallback (internal/process/next_tick.js:160:7)

@toyotajon93
Copy link

@John1231983 Did you use Windows for this one and were able to get the TALIB working? I cant get mine to install and I think thats causing the error some are seeing.

@kode8
Copy link

kode8 commented Feb 26, 2018

Thanks for all your effort with this, could you post instructions on how to get this running on Gekko, the instructions seem to be fragmented across a few gists and I can't seem to get it working. thanks

@p0ntsNL
Copy link

p0ntsNL commented Jul 12, 2018

I am having the following error after adding this into the strategies directory.

  This Gekko instance encountered an error and can't continue
      at Object.onerror (/opt/gekko/gekko/node_modules/koa/lib/context.js:105:40)
      at <anonymous>
      at process._tickCallback (internal/process/next_tick.js:188:7)```

@CryptoCoeus
Copy link

@p0nt, i am the same error as well.. Is anybody able to run this strat ?

@seamail
Copy link

seamail commented Sep 4, 2018

Can you create SMA and RSI ?
Thank

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment