Last active
July 30, 2020 19:06
-
-
Save strikeout/90031426f53ec47e5cb7237d6b50add7 to your computer and use it in GitHub Desktop.
Aural indication of XBTUSD price movements via BitMEX websocket realtime-trade data. Uses the blues scale to add some funk to your trading. Copy & paste into your browser's console..
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* BitMEX JAZZ BEEP | |
* | |
* Aural indication of XBTUSD price movements via BitMEX websocket realtime-trade data. | |
* Uses the blues scale to add some funk to your trading. | |
* | |
*/ | |
var options = { | |
symbol: "XBTUSD", // subscribe to trades of this instrument | |
largeOrderThreshold: 10000, // only beep on trades larger than | |
durationDelta: 750000, // trades of this size beep 1s, smaller trades beep shorter | |
durationMin: .05 // minimum time of beep, to reduce jitter | |
}; | |
// blues scale (pentatonic minor C) | |
// notes: C, Eb, F, F#, G, Bb | |
const bluesScale = [ | |
256.87, 305.47, 342.88, 363.27, 384.87, 457.69, | |
513.74, 610.94, 685.76, 726.53, 769.74, 915.38, | |
1027.47, 1221.88, 1371.51, 1453.07, 1539.47, 1830.75, | |
2054.95 | |
]; | |
var scale = bluesScale; | |
var key = Math.floor(scale.length / 2); // start with middle key in scale | |
// WebAudio init. | |
var beep = (function () { | |
var audioCtx = new (window.AudioContext || window.webkitAudioContext || window.audioContext); | |
var gainNode = audioCtx.createGain(); | |
gainNode.connect(audioCtx.destination); | |
return function (duration, frequency, volume, type, callback) { | |
var oscillator = audioCtx.createOscillator(); | |
oscillator.connect(gainNode); | |
if (volume){gainNode.gain.value = volume;}; | |
if (frequency){oscillator.frequency.value = frequency;} | |
if (type){oscillator.type = type;} | |
if (callback){oscillator.onended = callback;} | |
oscillator.start(); | |
setTimeout(function(){oscillator.stop()}, (duration ? duration : options.durationMin*1000)); | |
}; | |
})(); | |
// WebSocket init. | |
var s = new WebSocket("wss://www.bitmex.com/realtime?subscribe=trade:" + options.symbol); | |
s.onmessage = function (frame) { | |
var msg = JSON.parse(frame.data); | |
if (msg.action === 'insert') { | |
for (var i = 0; i < msg.data.length; i++) { | |
var t = msg.data[i]; | |
if (t.size >= options.largeOrderThreshold) { | |
// shift key in scale according to tick | |
if (['PlusTick', 'ZeroPlusTick'].includes(t.tickDirection)) key += 1; | |
if (['MinusTick', 'ZeroMinusTick'].includes(t.tickDirection)) key -= 1; | |
// loop scale top to bottom / bottom to top | |
if (key >= scale.length - 1) key = 0; | |
else if (key <= 0) key = scale.length - 1; | |
// calc duration, bigger trades, longer beep | |
var duration = t.size / options.durationDelta; | |
if (duration <= options.durationMin) duration = options.durationMin; | |
duration *=1000; | |
// logging | |
var dir = (t.side == 'Buy') ? '+' : '-'; | |
console.debug(`${dir} ${t.price} ${t.size}`) | |
beep(duration, scale[key]) | |
} | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment