Skip to content

Instantly share code, notes, and snippets.

@paitonic
Created December 27, 2014 12:24
Show Gist options
  • Save paitonic/1f06556dbe0f8b732078 to your computer and use it in GitHub Desktop.
Save paitonic/1f06556dbe0f8b732078 to your computer and use it in GitHub Desktop.
npyscreen - btce depth
#!/usr/bin/env python
# encoding: utf-8
import npyscreen
import qbtce
import datetime
api = qbtce.API()
def parse_depth():
depth = api.depth('btc_usd')['btc_usd']
asks = []
bids = []
for ask in depth['asks']:
price = "{:03.3f}".format(ask[0])
quantity = "{:03.8f}".format(ask[1])
asks.append(price + ' @ ' + quantity)
for bid in depth['bids']:
price = "{:03.3f}".format(bid[0])
quantity = "{:03.8f}".format(bid[1])
bids.append(price + ' @ ' + quantity)
# additional information
ltime = "Time: {time}".format(time=str(datetime.datetime.now()))
best = "Bid: {bid:03.3f}, Ask: {ask:03.3f}".format(bid=depth['bids'][0][0], ask=depth['asks'][0][0])
spread = "Spread: {:03.3f}".format(abs(depth['bids'][0][0] - depth['asks'][0][0]))
return asks, bids, ltime, spread, best
class MainForm(npyscreen.Form):
def while_waiting(self):
(asks, bids, ltime, spread, best) = parse_depth()
self.bidBox.values = bids
self.askBox.values = asks
self.info.values = [ltime, spread, best]
# redraw
self.display()
# pause redraw for 5 seconds
self.keypress_timeout = 50
def create(self):
# create boxes
# adding editable=False, disables scroll on widgets.
# but, it fixes bug with content being dissapeared when form is active.
self.bidBox = self.add(npyscreen.BoxTitle, name="Market Bid", width=30, relx=15, height=20)
self.askBox = self.add(npyscreen.BoxTitle, name="Market Ask", rely=2, relx=45, width=30, height=20)
self.info = self.add(npyscreen.BoxTitle, name="Information", height=10)
self.info.entry_widget.scroll_exit = True
class MainApp(npyscreen.NPSAppManaged):
keypress_timeout_default = 50
def onStart(self):
self.addForm("MAIN", MainForm, name="BTC-E")
if __name__ == "__main__":
App = MainApp()
App.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment