Skip to content

Commit

Permalink
Merge pull request #33 from thanhnguyennguyen/alert-breakout-bb
Browse files Browse the repository at this point in the history
resolve #28: alert when price breaks out bollingerBands
  • Loading branch information
thanhnguyennguyen authored Feb 29, 2020
2 parents 15c9ffb + 35524a7 commit d2b4b18
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 22 deletions.
27 changes: 27 additions & 0 deletions alerts/bollinger_band.js
Original file line number Diff line number Diff line change
@@ -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,
}
4 changes: 2 additions & 2 deletions alerts/ema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]],
Expand All @@ -19,4 +19,4 @@ const priceCrossEMA = async (period, symbol, interval, exchange, isFuture = fals

module.exports = {
priceCrossEMA: priceCrossEMA,
}
}
5 changes: 3 additions & 2 deletions alerts/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module.exports = {
...require('./bollinger_band.js'),
...require('./ema.js'),
...require('./sma.js'),
...require('./rsi.js')
}
...require('./rsi.js'),
}
6 changes: 3 additions & 3 deletions alerts/rsi.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
20 changes: 10 additions & 10 deletions alerts/sma.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -19,34 +19,34 @@ 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
}

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
}

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]],
Expand All @@ -63,4 +63,4 @@ module.exports = {
goldenCross: goldenCross,
deathCross: deathCross,
priceCrossSMA: priceCrossSMA,
}
}
16 changes: 11 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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)
}
Expand Down

0 comments on commit d2b4b18

Please sign in to comment.