diff --git a/alerts/bollinger_band.js b/alerts/bollinger_band.js new file mode 100644 index 0000000..4942697 --- /dev/null +++ b/alerts/bollinger_band.js @@ -0,0 +1,27 @@ +const bb = require('../indicators/bollinger_band.js') +const ticker = require('../indicators/ticker.js') +const calculateBollingerBandValue = async (period, stdDev, exchange, symbol, interval, isFuture) => { + try { + return await bb(parseInt(period), parseInt(stdDev), "close", exchange, symbol, interval, isFuture) + } catch (err) { + throw (err) + } +} +const bbCheck = async (period, stdDev, exchange, symbol, interval, isFuture = false) => { + let bbVals = await calculateBollingerBandValue(period, stdDev, exchange, symbol, interval, isFuture), + lastBBVal = bbVals[bbVals.length - 1] + tick = await ticker(exchange, symbol, isFuture), + price = tick.close, + down = price < lastBBVal.lower, + up = price > lastBBVal.upper + return { + breakOut: down || up, + band: down ? "lower" : (up ? "upper" : "none"), + direction: down ? "down" : (up ? "up" : "none"), + price: price, + } +} + +module.exports = { + bbCheck: bbCheck, +} diff --git a/alerts/ema.js b/alerts/ema.js index 3c6e138..8578f97 100644 --- a/alerts/ema.js +++ b/alerts/ema.js @@ -5,7 +5,7 @@ const { crossunder } = require('../utils/cross.js') -const priceCrossEMA = async (period, symbol, interval, exchange, isFuture = false) => { +const priceCrossEMA = async (period, exchange, symbol, interval, isFuture = false) => { let maVal = await ema(parseInt(period), "close", exchange, symbol, interval, isFuture), ohlcv = await getOHLCV(exchange, symbol, interval, isFuture), price = [ohlcv[1][3], ohlcv[0][3]], @@ -19,4 +19,4 @@ const priceCrossEMA = async (period, symbol, interval, exchange, isFuture = fals module.exports = { priceCrossEMA: priceCrossEMA, -} +} \ No newline at end of file diff --git a/alerts/index.js b/alerts/index.js index dcc182c..7ccb05e 100644 --- a/alerts/index.js +++ b/alerts/index.js @@ -1,5 +1,6 @@ module.exports = { + ...require('./bollinger_band.js'), ...require('./ema.js'), ...require('./sma.js'), - ...require('./rsi.js') -} + ...require('./rsi.js'), +} \ No newline at end of file diff --git a/alerts/rsi.js b/alerts/rsi.js index 6b4829f..5f77cf0 100644 --- a/alerts/rsi.js +++ b/alerts/rsi.js @@ -1,13 +1,13 @@ const rsi = require('../indicators/rsi.js') -const calculateRSIValue = async (period, symbol, interval, exchange, isFuture) => { +const calculateRSIValue = async (period, exchange, symbol, interval, isFuture) => { try { return await rsi(parseInt(period), "close", exchange, symbol, interval, isFuture) } catch (err) { throw (err) } } -const rsiCheck = async (period, overBoughtThreshold, overSoldThreshold, symbol, interval, exchange, isFuture = false) => { - let rsiVals = await calculateRSIValue(period, symbol, interval, exchange, isFuture), +const rsiCheck = async (period, overBoughtThreshold, overSoldThreshold, exchange, symbol, interval, isFuture = false) => { + let rsiVals = await calculateRSIValue(period, exchange, symbol, interval, isFuture), rsiVal = rsiVals[rsiVals.length - 1] return { overBought: rsiVal >= overBoughtThreshold, diff --git a/alerts/sma.js b/alerts/sma.js index 7c63a83..2068e84 100644 --- a/alerts/sma.js +++ b/alerts/sma.js @@ -4,7 +4,7 @@ const { crossover, crossunder } = require('../utils/cross.js') -const calculateMA = async (MA_FAST, MA_SLOW, symbol, interval, exchange, isFuture) => { +const calculateMA = async (MA_FAST, MA_SLOW, exchange, symbol, interval, isFuture) => { try { let MA_FAST_VAL = await sma(parseInt(MA_FAST), "close", exchange, symbol, interval, isFuture) let MA_SLOW_VAL = await sma(parseInt(MA_SLOW), "close", exchange, symbol, interval, isFuture) @@ -19,9 +19,9 @@ const calculateMA = async (MA_FAST, MA_SLOW, symbol, interval, exchange, isFutur } let maFastVal, maSlowVal -const goldenCross = async (MA_FAST, MA_SLOW, symbol, interval, exchange, isFuture = false) => { +const goldenCross = async (MA_FAST, MA_SLOW, exchange, symbol, interval, isFuture = false) => { if (maFastVal == undefined || maSlowVal == undefined) { - let maVal = await calculateMA(MA_FAST, MA_SLOW, symbol, interval, exchange, isFuture) + let maVal = await calculateMA(MA_FAST, MA_SLOW, exchange, symbol, interval, isFuture) maFastVal = maVal.fast maSlowVal = maVal.slow } @@ -29,9 +29,9 @@ const goldenCross = async (MA_FAST, MA_SLOW, symbol, interval, exchange, isFutur return crossover(maFastVal, maSlowVal) } -const deathCross = async (MA_FAST, MA_SLOW, symbol, interval, exchange, isFuture = false) => { +const deathCross = async (MA_FAST, MA_SLOW, exchange, symbol, interval, isFuture = false) => { if (maFastVal == undefined || maSlowVal == undefined) { - let maVal = await calculateMA(MA_FAST, MA_SLOW, symbol, interval, exchange, isFuture) + let maVal = await calculateMA(MA_FAST, MA_SLOW, exchange, symbol, interval, isFuture) maFastVal = maVal.fast maSlowVal = maVal.slow } @@ -39,14 +39,14 @@ const deathCross = async (MA_FAST, MA_SLOW, symbol, interval, exchange, isFuture return crossunder(maFastVal, maSlowVal) } -const maCross = async (MA_FAST, MA_SLOW, symbol, interval, exchange, isFuture = false) => { +const maCross = async (MA_FAST, MA_SLOW, exchange, symbol, interval, isFuture = false) => { return { - goldenCross: await goldenCross(MA_FAST, MA_SLOW, symbol, interval, exchange, isFuture), - deathCross: await deathCross(MA_FAST, MA_SLOW, symbol, interval, exchange, isFuture), + goldenCross: await goldenCross(MA_FAST, MA_SLOW, exchange, symbol, interval, isFuture), + deathCross: await deathCross(MA_FAST, MA_SLOW, exchange, symbol, interval, isFuture), } } -const priceCrossSMA = async (period, symbol, interval, exchange, isFuture = false) => { +const priceCrossSMA = async (period, exchange, symbol, interval, isFuture = false) => { let maVal = await sma(parseInt(period), "close", exchange, symbol, interval, isFuture), ohlcv = await getOHLCV(exchange, symbol, interval, isFuture), price = [ohlcv[1][3], ohlcv[0][3]], @@ -63,4 +63,4 @@ module.exports = { goldenCross: goldenCross, deathCross: deathCross, priceCrossSMA: priceCrossSMA, -} +} \ No newline at end of file diff --git a/index.js b/index.js index 7ed913c..037e81a 100644 --- a/index.js +++ b/index.js @@ -24,6 +24,8 @@ module.exports = { ticker: ticker, wma: wma, } + +// examples for testing const main = async () => { try { console.log("RSI 14 on Binance BTC/USDT 15m") @@ -49,19 +51,23 @@ const main = async () => { console.log(await ichimokuCloud(9, 26, 52, 26, 'binance', 'BTC/USDT', '1h', false)) console.log("Test golden cross") - console.log(await alerts.goldenCross(50, 200, 'BTC/USDT', '1h', 'binance', false)) + console.log(await alerts.goldenCross(50, 200, 'binance', 'BTC/USDT', '1h', false)) console.log("Test MA cross") - console.log(await alerts.maCross(50, 200, 'BTC/USDT', '1h', 'binance', false)) + console.log(await alerts.maCross(50, 200, 'binance', 'BTC/USDT', '1h', false)) console.log("Test RSIcheck") - console.log(await alerts.rsiCheck(14, 75, 25, 'BTC/USDT', '1h', 'binance', false)) + console.log(await alerts.rsiCheck(14, 75, 25, 'binance', 'BTC/USDT', '1h', false)) console.log("Test SMA cross") - console.log(await alerts.priceCrossSMA(14, 'BTC/USDT', '1h', 'binance', false)) + console.log(await alerts.priceCrossSMA(14, 'binance', 'BTC/USDT', '1h', false)) console.log("Test EMA cross") - console.log(await alerts.priceCrossEMA(14, 'BTC/USDT', '1h', 'binance', false)) + console.log(await alerts.priceCrossEMA(14, 'binance', 'BTC/USDT', '1h', false)) + + console.log("Test break out BB") + console.log(await alerts.bbCheck(50, 2, 'binance', 'BTC/USDT', '1h', false)) + } catch (err) { console.log(err) }