Skip to content

Instantly share code, notes, and snippets.

@prof7bit
Created July 13, 2013 15:28
Show Gist options
  • Save prof7bit/5991094 to your computer and use it in GitHub Desktop.
Save prof7bit/5991094 to your computer and use it in GitHub Desktop.
Geiger counter for MtGox. This goxtool strategy module adds sound effects to goxtool.py. It makes a click sound for every depth message and a louder click for every trade.
"""
Geiger counter for MtGox.
This goxtool strategy module makes a click sound for every
depth message and a louder click for every trade.
You must have python-alsaaudio installed to use this.
usage:
save file as "geiger.py" in the goxtool folder and then:
./goxtool.py --strategy=geiger
or if you want to run it along with other bots:
./goxtool.py --strategy=mybot,otherbot,holygrailbot,geiger
"""
import goxapi
import strategy
import alsaaudio
import math
def make_click_sound_sample(size, period, damp):
"""create the waveform for a 'click' sound.
Higher values of period make a lower frequency and higher values of
damp make a shorter sound (damp is decline per frame in percent)."""
buf = ""
amp = 1.0
dampf = 1 - damp / 100.0
for i in range (size):
xxx = float(i) / period
yyy = math.sin(xxx) * amp
buf += chr(int(round(127 + 127 * yyy)))
amp *= dampf
return buf
SAMPLE_SIZE = 2 * 32
SAMPLE_RATE = 48000
WAV_SILENCE = chr(127) * SAMPLE_SIZE
WAV_SELL = make_click_sound_sample(SAMPLE_SIZE, 3, 4)
WAV_BUY = make_click_sound_sample(SAMPLE_SIZE, 2, 4)
WAV_TICK = make_click_sound_sample(SAMPLE_SIZE, 1, 15)
class Strategy(strategy.Strategy):
"""make clicking sounds on events in the order book"""
def __init__(self, gox):
self.sample = WAV_SILENCE
self.running = True
self.pcm = alsaaudio.PCM(alsaaudio.PCM_PLAYBACK)
self.pcm.setchannels(1)
self.pcm.setformat(alsaaudio.PCM_FORMAT_U8)
self.pcm.setrate(SAMPLE_RATE)
self.pcm.setperiodsize(SAMPLE_SIZE)
goxapi.start_thread(self._audio_write_thread, "Geiger ALSA write")
strategy.Strategy.__init__(self, gox)
def slot_before_unload(self, _sender, _data):
self.running = False
self.pcm = None
def _audio_write_thread(self):
"""thread for writing the samples to the audio device"""
while self.running:
out = self.sample
self.sample = WAV_SILENCE
self.pcm.write(out)
def sound(self, sample):
"""Make noise"""
# the if is here to make sure we won't overwrite a pending buy or sell
# sound with a mere tick, but still allow to overwrite a pending tick
# sound with a louder and more important buy or sell.
if self.sample in [WAV_SILENCE, WAV_TICK]:
self.sample = sample
def slot_depth(self, gox, (typ, price, volume, total_volume)):
self.sound(WAV_TICK)
def slot_trade(self, gox, (date, price, volume, typ, own)):
if typ == "ask":
self.sound(WAV_SELL)
else:
self.sound(WAV_BUY)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment