Skip to content

Instantly share code, notes, and snippets.

@howie50417
Last active January 10, 2018 04:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save howie50417/8aae3fb6be22d7b6f880b7a29a48b37f to your computer and use it in GitHub Desktop.
Save howie50417/8aae3fb6be22d7b6f880b7a29a48b37f to your computer and use it in GitHub Desktop.
myRSI.js
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.name = 'myRSI';
this.candle_queue = [];
this.buyin = false;
this.buyin_price = 0;
this.take_profit_percent = 1;
this.stop_loss_percent = 1;
if(this.settings.take_profit_percent){
this.take_profit_percent = Number(this.settings.take_profit_percent);
}
if(this.settings.stop_loss_percent){
this.stop_loss_percent = Number(this.settings.stop_loss_percent);
}
log.debug('init:');
log.debug('\t', 'take_profit_percent:', this.take_profit_percent);
log.debug('\t', 'stop_loss_percent:', this.stop_loss_percent);
}
// for debugging purposes log the last
// calculated parameters.
method.log = function(candle) {
}
method.cal_myRSI = function(t1,t2){
var up = 0;
var down = 0;
for(var i=t1;i<=t2;i++){
var delta = this.candle_queue[i-1].delta;
if(delta >= 0){
up += delta;
}else{
down += 0-delta;
}
}
if(up+down > 0){
return (up) / (up+down) * 100;
}else{
return 50;
}
}
method.update = function(candle) {
log.debug('update:');
log.debug('\t', 'candle price:', candle.close.toFixed(8));
if(this.candle_queue.length>0){
candle.delta = candle.close - this.candle_queue[0].close;
log.debug('\t', 'candle delta:', candle.delta);
}
this.candle_queue.unshift(candle);
if(this.candle_queue.length > 20){
this.candle_queue.pop();
}
if(this.candle_queue.length < 13){
this.myRSI = null;
}else{
this.myRSI={};
this.myRSI.rsi_6_1=this.cal_myRSI(1,6);
this.myRSI.rsi_12_1=this.cal_myRSI(1,12);
this.myRSI.rsi_6_2=this.cal_myRSI(2,6);
this.myRSI.rsi_12_2=this.cal_myRSI(2,12);
log.debug('\t', 'rsi_6_1:', this.myRSI.rsi_6_1);
log.debug('\t', 'rsi_12_1:', this.myRSI.rsi_12_1);
log.debug('\t', 'rsi_6_2:', this.myRSI.rsi_6_2);
log.debug('\t', 'rsi_12_2:', this.myRSI.rsi_12_2);
}
}
method.check = function() {
if(this.myRSI == null){
return;
}
var rsi_6_1 = this.myRSI.rsi_6_1;
var rsi_12_1 = this.myRSI.rsi_12_1;
var rsi_6_2 = this.myRSI.rsi_6_2;
var rsi_12_2 = this.myRSI.rsi_12_2;
if(this.buyin == false){
var time_to_buy = false;
if(rsi_6_2 < rsi_12_2 && rsi_12_2 < 20){
time_to_buy = true;
log.debug('check:');
log.debug('\t', 'buy: rsi_6_2 < rsi_12_2 < 20');
}else if(rsi_12_1 < rsi_6_1 && rsi_6_1 < 20){
time_to_buy = true;
log.debug('check:');
log.debug('\t', 'buy: rsi_12_1 < rsi_6_1 < 20');
}
if(time_to_buy == true){
this.advice("long");
this.buyin = true;
this.buyin_price = this.candle_queue[0].close;
log.debug('\t', 'buy price:', this.buyin_price);
return;
}else{
this.advice();
return;
}
}else if(this.buyin == true){
var time_to_sell = false;
if(this.buyin_price == 0){
log.error('check error: buyin_price == 0');
return;
}
if(this.candle_queue[0].close / this.buyin_price * 100 > this.take_profit_percent + 100){
time_to_sell = true;
log.debug('check:');
log.debug('\t', 'sell: take_profit_percent:',this.candle_queue[0].close / this.buyin_price * 100);
}else if(this.candle_queue[0].close / this.buyin_price * 100 < 100 - this.stop_loss_percent){
time_to_sell = true;
log.debug('check:');
log.debug('\t', 'sell: stop_loss_percent:',this.candle_queue[0].close / this.buyin_price * 100);
}
if(time_to_sell == true){
this.advice("short");
this.buyin = false;
log.debug('\t', 'sell price:', this.candle_queue[0].close);
return;
}else{
this.advice();
}
}
}
module.exports = method;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment