Skip to content

Minor Cleanup #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified .DS_Store
Binary file not shown.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ pids
*.seed
*.pid.lock

# OS/IDE specific
*.idea
*.DS_Store

database

# Directory for instrumented libs generated by jscoverage/JSCover
Expand Down
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/functions/calculateRSI.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
2 changes: 1 addition & 1 deletion src/functions/createColumns.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
148 changes: 88 additions & 60 deletions src/functions/divergenceStrategy.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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);

}
});
});
Expand Down
16 changes: 8 additions & 8 deletions src/functions/isJson.js
Original file line number Diff line number Diff line change
@@ -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;
};
};
2 changes: 1 addition & 1 deletion src/functions/slope.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/functions/spike.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/services/bitfinex.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/services/scanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/tasks/dbSet.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down