1 Title: Valutakrambod - A python and bitcoin love story
5 <p>It would come as no surprise to anyone that I am interested in
6 bitcoins and virtual currencies. I've been keeping an eye on virtual
7 currencies for many years, and it is part of the reason a few months
8 ago, I started writing a python library for collecting currency
9 exchange rates and trade on virtual currency exchanges. I decided to
10 name the end result valutakrambod, which perhaps can be translated to
11 small currency shop.</p>
13 <p>The library uses the tornado python library to handle HTTP and
14 websocket connections, and provide a asynchronous system for
15 connecting to and tracking several services. The code is available
17 <a href="http://github.com/petterreinholdtsen/valutakrambod">github</a>.</p>
19 </p>There are two example clients of the library. One is very simple and
20 list every updated buy/sell price received from the various services.
21 This code is started by running bin/btc-rates and call the client code
22 in valutakrambod/client.py. The simple client look like this:</p>
28 class SimpleClient(object):
33 def newdata(self, service, pair, changed):
34 print("%-15s %s-%s: %8.3f %8.3f" % (
35 service.servicename(),
38 service.rates[pair]['ask'],
39 service.rates[pair]['bid'])
41 async def refresh(self, service):
42 await service.fetchRates(service.wantedpairs)
44 self.ioloop = tornado.ioloop.IOLoop.current()
45 self.services = valutakrambod.service.knownServices()
46 for e in self.services:
48 service.subscribe(self.newdata)
49 stream = service.websocket()
51 self.streams.append(stream)
53 # Fetch information from non-streaming services immediately
54 self.ioloop.call_later(len(self.services),
55 functools.partial(self.refresh, service))
56 # as well as regularly
57 service.periodicUpdate(60)
58 for stream in self.streams:
62 except KeyboardInterrupt:
63 print("Interrupted by keyboard, closing all connections.")
65 for stream in self.streams:
67 </pre></blockquote></p>
69 <p>The library client loops over all known "public" services,
70 initialises it, subscribes to any updates from the service, checks and
71 activates websocket streaming if the service provide it, and if no
72 streaming is supported, fetches information from the service and sets
73 up a periodic update every 60 seconds. The output from this client
74 can look like this:</p>
77 Bl3p BTC-EUR: 5687.110 5653.690
78 Bl3p BTC-EUR: 5687.110 5653.690
79 Bl3p BTC-EUR: 5687.110 5653.690
80 Hitbtc BTC-USD: 6594.560 6593.690
81 Hitbtc BTC-USD: 6594.560 6593.690
82 Bl3p BTC-EUR: 5687.110 5653.690
83 Hitbtc BTC-USD: 6594.570 6593.690
84 Bitstamp EUR-USD: 1.159 1.154
85 Hitbtc BTC-USD: 6594.570 6593.690
86 Hitbtc BTC-USD: 6594.580 6593.690
87 Hitbtc BTC-USD: 6594.580 6593.690
88 Hitbtc BTC-USD: 6594.580 6593.690
89 Bl3p BTC-EUR: 5687.110 5653.690
90 Paymium BTC-EUR: 5680.000 5620.240
91 </pre></blockquote></p>
93 <p>The exchange order book is tracked in addition to the best buy/sell
94 price, for those that need to know the details.</p>
96 <p>The other example client is focusing on providing a curses view
97 with updated buy/sell prices as soon as they are received from the
98 services. This code is located in bin/btc-rates-curses and activated
99 by using the '-c' argument. Without the argument the "curses" output
100 is printed without using curses, which is useful for debugging. The
101 curses view look like this:</p>
104 Name Pair Bid Ask Spr Ftcd Age
105 BitcoinsNorway BTCEUR 5591.8400 5711.0800 2.1% 16 nan 60
106 Bitfinex BTCEUR 5671.0000 5671.2000 0.0% 16 22 59
107 Bitmynt BTCEUR 5580.8000 5807.5200 3.9% 16 41 60
108 Bitpay BTCEUR 5663.2700 nan nan% 15 nan 60
109 Bitstamp BTCEUR 5664.8400 5676.5300 0.2% 0 1 1
110 Bl3p BTCEUR 5653.6900 5684.9400 0.5% 0 nan 19
111 Coinbase BTCEUR 5600.8200 5714.9000 2.0% 15 nan nan
112 Kraken BTCEUR 5670.1000 5670.2000 0.0% 14 17 60
113 Paymium BTCEUR 5620.0600 5680.0000 1.1% 1 7515 nan
114 BitcoinsNorway BTCNOK 52898.9700 54034.6100 2.1% 16 nan 60
115 Bitmynt BTCNOK 52960.3200 54031.1900 2.0% 16 41 60
116 Bitpay BTCNOK 53477.7833 nan nan% 16 nan 60
117 Coinbase BTCNOK 52990.3500 54063.0600 2.0% 15 nan nan
118 MiraiEx BTCNOK 52856.5300 54100.6000 2.3% 16 nan nan
119 BitcoinsNorway BTCUSD 6495.5300 6631.5400 2.1% 16 nan 60
120 Bitfinex BTCUSD 6590.6000 6590.7000 0.0% 16 23 57
121 Bitpay BTCUSD 6564.1300 nan nan% 15 nan 60
122 Bitstamp BTCUSD 6561.1400 6565.6200 0.1% 0 2 1
123 Coinbase BTCUSD 6504.0600 6635.9700 2.0% 14 nan 117
124 Gemini BTCUSD 6567.1300 6573.0700 0.1% 16 89 nan
125 Hitbtc+BTCUSD 6592.6200 6594.2100 0.0% 0 0 0
126 Kraken BTCUSD 6565.2000 6570.9000 0.1% 15 17 58
127 Exchangerates EURNOK 9.4665 9.4665 0.0% 16 107789 nan
128 Norgesbank EURNOK 9.4665 9.4665 0.0% 16 107789 nan
129 Bitstamp EURUSD 1.1537 1.1593 0.5% 4 5 1
130 Exchangerates EURUSD 1.1576 1.1576 0.0% 16 107789 nan
131 BitcoinsNorway LTCEUR 1.0000 49.0000 98.0% 16 nan nan
132 BitcoinsNorway LTCNOK 492.4800 503.7500 2.2% 16 nan 60
133 BitcoinsNorway LTCUSD 1.0221 49.0000 97.9% 15 nan nan
134 Norgesbank USDNOK 8.1777 8.1777 0.0% 16 107789 nan
135 </pre></blockquote></p>
137 <p>The code for this client is too complex for a simple blog post, so
138 you will have to check out the git repository to figure out how it
139 work. What I can tell is how the three last numbers on each line
140 should be interpreted. The first is how many seconds ago information
141 was received from the service. The second is how long ago, according
142 to the service, the provided information was updated. The last is an
143 estimate on how often the buy/sell values change.</p>
145 <p>If you find this library useful, or would like to improve it, I
146 would love to hear from you. Note that for some of the services I've
147 implemented a trading API. It might be the topic of a future blog
150 <p>As usual, if you use Bitcoin and want to show your support of my
151 activities, please send Bitcoin donations to my address
152 <b><a href="bitcoin:15oWEoG9dUPovwmUL9KWAnYRtNJEkP1u1b">15oWEoG9dUPovwmUL9KWAnYRtNJEkP1u1b</a></b>.</p>