Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var config = require ('../core/util.js').getConfig();
- var SMA = require('./indicators/SMA.js');
- var RSI = require('./indicators/RSI.js');
- var MACD = require('./indicators/MACD.js');
- var VWAP = require('./indicators/VWAP.js');
- var strat = {};
- // helpers
- var _ = require('lodash');
- var log = require('../core/log.js');
- var timeToBuy = false;
- var timeToSell = false;
- var MacdReady = false;
- var RSIReady = false;
- var VWAPReady = false;
- var firstrun = true;
- var timePeriods = 10;
- var buyPrice = 0;
- var sellPrice = 0;
- var cutLossesPrice = 0;
- var candlesHistory = [];
- var rsiHistory = [];
- var buyPriceTimeStamp = "";
- //MARK: - INIT()
- strat.init = function() {
- console.log('init function');
- this.name = 'Fckin asdf for new POLO';
- this.requiredHistory = this.tradingAdvisor.historySize;
- this.trend = {
- direction: 'none',
- duration: 0,
- persisted: false,
- adviced: false
- };
- // how many candles do we need as a base
- // before we can start giving advice?
- this.requiredHistory = this.tradingAdvisor.historySize;
- // define the indicators we need
- this.addIndicator('macd', 'MACD', this.settings);
- this.addIndicator('rsi', 'RSI', this.settings);
- this.addIndicator('vwap', 'VWAP', this.settings);
- console.log(this.name);
- }
- // EMAs and diff.
- strat.log = function() {
- var digits = 8;
- var macd = this.indicators.macd;
- var diff = macd.diff;
- var signal = macd.signal.result;
- 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));
- }
- //MARK: - CHECK()
- strat.check = function(candle) {
- // if (firstrun == true) {
- // candlesHistory = [];
- // rsiHistory = [];
- // firstrun = false;
- // }
- candlesHistory.unshift(candle);
- // console.log(candlesHistory);
- if (candlesHistory.lenght > timePeriods) {
- candlesHistory.pop();
- }
- // this.indicators.vwap.input = this.candle;
- var vwap = this.indicators.vwap;
- console.log("--------");
- console.log(vwap.result);
- console.log("--------");
- var rsi = this.indicators.rsi;
- var rsiVal = rsi.result;
- rsiHistory.unshift(rsiVal);
- // console.log(rsiHistory[0]);
- if (rsiHistory.lenght > timePeriods) {
- rsiHistory.pop();
- }
- var macddiff = this.indicators.macd.result;
- //MARK: - CONSTANT CHECK
- if(!timeToSell) {
- if(macddiff > this.settings.thresholds.up) {
- // new trend detected
- if(this.trend.direction !== 'up') {
- // reset the state for the new trend
- this.trend = {
- duration: 1,
- persisted: false,
- direction: 'up',
- adviced: false
- };
- this.trend.duration++;
- log.debug('In uptrend 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;
- MacdReady = true
- if (rsiHistory.length > 1) {
- console.log("asdfasdf");
- if (rsiHistory[0] < 33 || rsiHistory[1] < 33) {
- console.log("rsi gogogo");
- RSIReady = true;
- // this.advice('long');
- } else {
- RSIReady = false;
- }
- }
- }
- }
- }
- }
- // else if(macddiff < this.settings.thresholds.down) {
- // // new trend detected
- // if(this.trend.direction !== 'down')
- // // reset the state for the new trend
- // this.trend = {
- // duration: 2,
- // persisted: false,
- // direction: 'down',
- // adviced: false
- // };
- // this.trend.duration++;
- // log.debug('In downtrend 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;
- // MacdReady = true
- // if (this.rsiHistory.lenght > 2) {
- // if (this.rsiHistory[this.rsiHistory.lenght] < 33
- // || this.rsiHistory[this.rsiHistory.lenght - 1] < 33
- // || this.rsiHistory[this.rsiHistory.lenght - 2] < 33) {
- // RSIReady = true;
- // // this.advice('long');
- // } else {
- // RSIReady = false;
- // }
- // }
- // // this.advice('short');
- // } else {
- // MacdReady = false;
- // }
- // }
- if (MacdReady && RSIReady) {
- timeToBuy = true;
- timeToSell = false;
- } else {
- timeToBuy = false;
- timeToSell = true;
- MacdReady = false;
- RSIReady = false;
- }
- }
- if (timeToBuy) {
- timeToBuy = false;
- this.advice('long');
- buyPrice = this.candle.close;
- buyPriceTimeStamp = this.candle.start._d
- sellPrice = this.candle.close * 1.005;
- cutLossesPrice = this.candle.close * 0.98;
- timeToSell = true;
- }
- if (timeToSell) {
- //MARK: - TAKE PROFITS
- if (sellPrice <= this.candle.close) {
- timeToSell = false;
- this.advice('short');
- }
- if (cutLossesPrice >= this.candle.close) {
- timeToSell = false;
- this.advice('short');
- } else {
- let buyinStamp = (new Date(buyPriceTimeStamp).getTime()/1000);
- let shouldSelloutStamp = (new Date(this.candle.start._d).getTime()/1000);
- let gap = shouldSelloutStamp - buyinStamp;
- //one hour 3600
- //three hours 10800
- //one day 86400
- // if (gap >= 10800) {
- // sellPrice = buyPrice * 1.003
- // if (sellPrice <= this.candle.close) {
- // timeToSell = false;
- // this.advice('short');
- // }
- // }
- // if (gap >= 36000) {
- // this.advice('short');
- // }
- // console.log(buyinStamp);
- // console.log(buyPriceTimeStamp);
- // console.log(this.candle.start._d);
- }
- }
- }
- module.exports = strat;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement