diff --git a/.DS_Store b/.DS_Store index d39b4ad..5008ddf 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/.gitignore b/.gitignore index 768f930..af6d7a0 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,10 @@ pids *.seed *.pid.lock +# OS/IDE specific +*.idea +*.DS_Store + database # Directory for instrumented libs generated by jscoverage/JSCover diff --git a/package-lock.json b/package-lock.json index 18ebcaf..f4488e9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1913,6 +1913,7 @@ "minimist": "0.0.8" } }, + "mocha": { "version": "5.1.1", "resolved": "https://registry.npmjs.org/mocha/-/mocha-5.1.1.tgz", diff --git a/src/functions/calculateRSI.js b/src/functions/calculateRSI.js index e33beba..eb0c6f6 100644 --- a/src/functions/calculateRSI.js +++ b/src/functions/calculateRSI.js @@ -7,7 +7,7 @@ const RSI = require('@solazu/technicalindicators').RSI; * @param {object} priceArray The proce data * @return {object} the RSI Array and close Arrays */ -module.exports = function calculateRSI(priceArray) { +export function calculateRSI(priceArray) { return new Promise(function(resolve, reject) { let values = []; priceArray.forEach((entry) => { diff --git a/src/functions/createColumns.js b/src/functions/createColumns.js index fe4e75c..a9a45f1 100644 --- a/src/functions/createColumns.js +++ b/src/functions/createColumns.js @@ -11,7 +11,7 @@ const spike = require('./spike'); * @param {object} pair The pair * @return {boolean} has a divergence been found true/false */ -module.exports = function createColumns(price, rsi, timeFrame, pair) { +export function createColumns(price, rsi, timeFrame, pair) { return new Promise(function(resolve, reject) { let columns = []; price.forEach((entry, i) => { diff --git a/src/functions/divergenceStrategy.js b/src/functions/divergenceStrategy.js index d899f57..29f1c5a 100644 --- a/src/functions/divergenceStrategy.js +++ b/src/functions/divergenceStrategy.js @@ -1,5 +1,85 @@ 'use strict'; const slope = require('./slope'); + +const UP = 'up'; +const DOWN = 'down'; +const BEAR = 'bearish'; +const BULL = 'bullish'; + +/** + * + * @param {number} i + * @param {{}} column + * @param {string} upDown + * @param {16|18} ? magicNumber + * @returns {boolean} + */ +function checkIfUpOrDown(i, column, upDown, magicNumber) { + const secondColumn = column[2]; + const rsiSpike = 'rsiSpike'; + const rsiValue = 'rsiValue'; + const priceSpike = 'priceSpike'; + const priceValue = 'priceValue'; + + if (i <= magicNumber && + secondColumn[priceSpike] === upDown && + column[i][priceSpike] === upDown && + secondColumn[rsiSpike] === upDown && + column[i][rsiSpike] === upDown && + column[i][priceValue] < secondColumn[priceValue] && + column[i][rsiValue] > secondColumn[rsiValue]) { + return true; + } + return false; +} + +/** + * + * @param {{}} column + * @param {number} i + * @returns {{}} + */ +function getValues(column, i) { + const firstPriceSpikeValue = column[2].priceValue; + const secondPriceSpikeValue = column[i].priceValue; + const firstRsiSpikeValue = column[2].rsiValue; + const secondRsiSpikeValue = column[i].rsiValue; + + return { + firstPriceSpikeValue, + secondPriceSpikeValue, + firstRsiSpikeValue, + secondRsiSpikeValue + }; +} + +/** + * + * @param {{}} column + * @param {number} i + * @param {string} bullOrBear + * @param {Promise} resolve + * @param {number} pair + * @param {number} timeFrame + * @param {number} period + * + * would be best to de-construct all these params + */ +function resolveSlope(column, i, bullOrBear, resolve, pair, timeFrame, period) { + const {firstPriceSpikeValue, secondPriceSpikeValue, firstRsiSpikeValue, secondRsiSpikeValue} = getValues(column, i); + for (let x = 2; x <= i; x++) { + const priceSlope = slope(i, firstPriceSpikeValue, secondPriceSpikeValue, bullOrBear); + const rsiSlope = slope(i, firstRsiSpikeValue, secondRsiSpikeValue, bullOrBear); + if (priceSlope && rsiSlope && x === i) { + const divergence = true; + const period = i; + resolve({divergence, period, bullOrBear, pair, timeFrame, column}); + } else { + break; + } + } +} + /** * Divergence Strategy * For each of the items in the column array we look for divergence @@ -9,68 +89,16 @@ const slope = require('./slope'); * @param {object} period The period between spikes * @return {object} divergence report */ -module.exports = function divergenceStrategy(column, pair, timeFrame) { - return new Promise(function(resolve, reject) { + +export function divergenceStrategy(column, pair, timeFrame, period) { + return new Promise(function(resolve) { column.forEach((i) => { - if ( - i <= 18 && - column[2].priceSpike === 'up' && - column[i].priceSpike === 'up' && - column[2].rsiSpike === 'up' && - column[i].rsiSpike === 'up' && - column[i].priceValue < column[2].priceValue && - column[i].rsiValue > column[2].rsiValue - ) { - console.log('Bearish Divergence Found'); - console.log(column); - const firstPriceSpikeValue = column[2].priceValue; - const secondPriceSpikeValue = column[i].priceValue; - const firstRsiSpikeValue = column[2].rsiValue; - const secondRsiSpikeValue = column[i].rsiValue; - let x; - for (x = 2; x <= i; x++) { - const priceSlope = slope(i, firstPriceSpikeValue, secondPriceSpikeValue, 'bearish'); - const rsiSlope = slope(i, firstRsiSpikeValue, secondRsiSpikeValue, 'bearish'); - if (priceSlope && rsiSlope && x === i) { - const divergence = true; - const period = i; - const direction = 'bearish'; - const data = column; - resolve({divergence, period, direction, pair, timeFrame, data}); - } else { - break; - } - } + if (checkIfUpOrDown(column, i, UP, 16, BULL, pair, timeFrame, period)) { + resolveSlope(i, column, resolve); } - if ( - i <= 18 && - column[2].priceSpike === 'down' && - column[i].priceSpike === 'down' && - column[2].rsiSpike === 'down' && - column[i].rsiSpike === 'down' && - column[i].priceValue > column[2].priceValue && - column[i].rsiValue < column[2].rsiValue - ) { - console.log('Bullish Divergence Found'); - console.log(column); - const firstPriceSpikeValue = column[2].priceValue; - const secondPriceSpikeValue = column[i].priceValue; - const firstRsiSpikeValue = column[2].rsiValue; - const secondRsiSpikeValue = column[i].rsiValue; - let x; - for (x = 2; x <= i; x++) { - const priceSlope = slope(i, firstPriceSpikeValue, secondPriceSpikeValue, 'bullish'); - const rsiSlope = slope(i, firstRsiSpikeValue, secondRsiSpikeValue, 'bullish'); - if (priceSlope && rsiSlope && x === i) { - const divergence = true; - const period = i; - const direction = 'bullish'; - const data = column; - resolve({divergence, period, direction, pair, timeFrame, data}); - } else { - break; - } - } + if (checkIfUpOrDown(column, i, DOWN, 15, BEAR, pair, timeFrame, period)) { + resolveSlope(i, column, resolve); + } }); }); diff --git a/src/functions/isJson.js b/src/functions/isJson.js index 1e95469..05ef586 100644 --- a/src/functions/isJson.js +++ b/src/functions/isJson.js @@ -1,10 +1,10 @@ 'use strict'; -module.exports = function isJson(json) { - const text = JSON.stringify(json); - try { - JSON.parse(text); - return true; - } catch (error) { - return false; - }; +export function isJson(json) { + const text = JSON.stringify(json); + try { + JSON.parse(text); + return true; + } catch (error) { + return false; + }; }; diff --git a/src/functions/slope.js b/src/functions/slope.js index f984c7a..0de1879 100644 --- a/src/functions/slope.js +++ b/src/functions/slope.js @@ -9,7 +9,7 @@ * @param {string} direction The proce data * @return {boolean} true or false */ -module.exports = function slope(period, firstValue, secondValue, direction) { +exports function slope(period, firstValue, secondValue, direction) { const slopeValue = ((firstValue - secondValue) / Math.abs(period - 2)) * period; if (direction === 'bullish') { return (secondValue <= slopeValue); diff --git a/src/functions/spike.js b/src/functions/spike.js index 92925b7..7815c1b 100644 --- a/src/functions/spike.js +++ b/src/functions/spike.js @@ -11,7 +11,7 @@ * @return {string} the string indicating direction */ - module.exports = function spike(left, target, right) { +export function spike(left, target, right) { if (target > left && target > right) { return 'up'; } else if (target < left && target < right) { diff --git a/src/services/bitfinex.js b/src/services/bitfinex.js index b6998c9..de1d35c 100644 --- a/src/services/bitfinex.js +++ b/src/services/bitfinex.js @@ -4,7 +4,7 @@ const dbSet = require('../tasks/dbSet'); /** * A service that deal with bitfinex service */ -module.exports = class BitFinexService { +export class BitFinexService { /** * Create BitFinexService * @param {Object[]} timeFrames the time frame needed diff --git a/src/services/scanner.js b/src/services/scanner.js index ea9ed9c..bf9d922 100644 --- a/src/services/scanner.js +++ b/src/services/scanner.js @@ -5,7 +5,7 @@ const dbSet = require('../tasks/dbSet'); /** * A service that scans for divergences */ -module.exports = class ScannerService { +export class ScannerService { /** * @param {bitfinexData[]} bitfinexData * @return {Void} empty promise diff --git a/src/tasks/dbSet.js b/src/tasks/dbSet.js index dd0b7b9..fc44f82 100644 --- a/src/tasks/dbSet.js +++ b/src/tasks/dbSet.js @@ -9,7 +9,7 @@ const isJson = require('../functions/isJson'); * @param {string} key The proce value * @param {object} value The proce value */ -module.exports = function dbSet(key, value) { +export function dbSet(key, value) { if (isJson(value)) { const data = JSON.stringify(value); db.put(key, data, function(err) {