Permalink
Cannot retrieve contributors at this time
Fetching contributors…
| #! /usr/bin/env python | |
| import sys | |
| import json | |
| import urllib | |
| import datetime | |
| from exchanges import * | |
| _currencies = [ 'btc' ] # coins to monitor | |
| _tolerance = 0.05 # 10% maximum price deviation | |
| try: | |
| _login = json.loads(open('secrets.json').read()) | |
| except: | |
| print "secrets.json could not be read" | |
| sys.exit(1) | |
| exchanges = [ Bter(**_login['bter']), CCEDK(**_login['ccedk']), Excoin(**_login['excoin']) ] | |
| while True: | |
| timestemp = datetime.datetime.now().strftime("%Y-%m-%d") | |
| price = {} | |
| try: | |
| for coin in _currencies: # this is ugly | |
| cmc = urllib.urlopen("http://coinmarketcap.com").read() | |
| start = cmc.rfind("data-usd", 0, cmc.find(" " + coin.upper() + " ")) + 8 | |
| end = cmc.find("data-btc", start) | |
| price[coin] = float(cmc[start:end].replace('\"','').replace('=','').strip()) | |
| except: | |
| print >> sys.stderr, timestemp, "failed to read coin exchange rates from CMC" | |
| time.sleep(30) | |
| continue | |
| for ex in exchanges: | |
| for coin in _currencies: | |
| try: | |
| if ex.hascoin(coin): # if coin is supported, check price against tolerance | |
| nbt = ex.getprice(coin) | |
| if abs(nbt * price[coin] - 1) > _tolerance: | |
| target = 1.0 / price[coin] | |
| bid, ask = ex.getmargin(coin) | |
| orders = [ 'bid', 'ask' ] # check if price falls outside margin | |
| if bid > target: orders = [ 'bid' ] | |
| if ask < target: orders = [ 'ask' ] | |
| description = "%s of %.8f for coin %s on %s to %.8f" % (" and ".join(orders), | |
| nbt, coin.upper(), ex, target) | |
| if bid / target - 1 > _tolerance or target / ask - 1 > _tolerance: # deviation has support, do nothing | |
| print timestemp, "inappropriate margin for %s (bid: %.08f ask: %.08f)" % (description, bid, ask) | |
| else: | |
| print timestemp, "setting current %s: %s" % (description, | |
| "SUCCESS" if ex.setprice(coin, target, orders) else "FAILURE") | |
| except: | |
| print >> sys.stderr, "failed to access coin %s on %s: %s" % (coin.upper(), ex, sys.exc_info()[0]) | |
| time.sleep(120) |