Skip to content

Instantly share code, notes, and snippets.

@jwebcat
Last active Aug 8, 2018
Embed
What would you like to do?
// Set interval at about 2000 ms for this script
// ALERTS: Alerts you when the price crosses the alert price
// SPIKES: Alerts you when the price spikes x percentage in either direction
// 0.1 means a distance of 0.1 % from either the high or the low of the current candle and timeframe
// Settings
var ALERTS = [6500, 6700]
var SPIKES = [0.2, 0.4, 0.6]
// Milliseconds to wait for the script to reset after you have turned it off. Lower values make it reset more often
self.resetInterval = 20000
self.debug = false
self.browserNotificationsOn = true
self.beepOn = true
self.voiceOn = true
self.showAlertLines = true
// Helper function
var notify = (msg, beepType) => {
self.beepOn ? beepType ? beep(beepType) : beep() : null
if (self.debug) {console.log(msg)}
if (self.browserNotificationsOn) {browserNotification("Alert", msg)}
if (self.voiceOn) {speak(msg)}
}
var lastPrice = trades.length > 0 ? trades[0].Price : 0
var lastCandle = chartData[chartData.length - 1]
// Some weird bug causing self.lastInterval to become of type "string" sometimes
if (typeof self.lastInterval == "string") {
if (self.debug) {console.log("Error: self.lastInterval is a string...");}
self.lastInterval = undefined
}
// Making sure that the script starts clean when it is stopped for longer than self.resetInterval
var lastInterval = self.lastInterval ? self.lastInterval.getTime() : undefined;
var timeDifference = new Date().getTime() - lastInterval
if (self.lastInterval == undefined || timeDifference > self.resetInterval) {
if (self.debug) {
console.log("Alerts have been reset" + " | Time difference: " + timeDifference + " ms")
}
self.lastInterval = new Date()
self.spikes = SPIKES
self.belowPrice = []
self.abovePrice = []
ALERTS.forEach(alert => {
if (alert <= lastPrice) {
self.belowPrice.push(alert)
} else {
self.abovePrice.push(alert)
}
})
}
var spikeDown = (lastCandle.high - lastPrice) / lastCandle.high * 100
var spikeUp = (lastPrice - lastCandle.low) / lastCandle.low * 100
if (self.debug) {
console.log(self.lastInterval.toLocaleTimeString() +
" | Active up alerts: " + self.abovePrice +
" | Active down alerts: " + self.belowPrice +
" | Active spike alerts: " + self.spikes +
" | LastHigh: " + lastCandle.high +
" | LastPrice: " + lastPrice +
" | LastLow: " + lastCandle.low +
" | spikeUp: " + spikeUp.toFixed(3) +
" | spikeDown: " + spikeDown.toFixed(3)
)
}
// Code for the spikes
self.spikes = self.spikes.filter(spike => {
if (spikeDown > spikeUp && spikeDown > spike) {
notify("Yikes we down more than " + spike + " percent", 2)
return false
} else if (spikeUp > spikeDown && spikeUp > spike) {
notify("Yo we up more than " + spike + " percent", 1)
return false
}
return true
})
// Reset spike alerts if a candle stays flat
if (self.spikes.length < SPIKES.length && spikeDown < Math.min(...SPIKES) && spikeUp < Math.min(...SPIKES)) {
if (self.debug) {notify("New candle, resetting spike alerts")}
self.spikes = SPIKES
}
// Code for the normal alerts
self.belowPrice = self.belowPrice.filter(alert => {
if (lastPrice < alert) {
notify("Price is below " + alert, 2)
return false
}
if (self.showAlertLines) {
showPriceLine({
price: alert,
color: "#e541f4",
text: "Alert @"
})
}
return true
})
self.abovePrice = self.abovePrice.filter(alert => {
if (lastPrice > alert) {
notify("Price is above " + alert, 1)
return false
}
if (self.showAlertLines) {
showPriceLine({
price: alert,
color: "#e541f4",
text: "Alert @",
})
}
return true
})
self.lastInterval = new Date()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment