Skip to content

Instantly share code, notes, and snippets.

@abcdabcd987
Last active August 23, 2017 13:46
Show Gist options
  • Save abcdabcd987/6f106620c3a7a6fbdc98d574ee0dac09 to your computer and use it in GitHub Desktop.
Save abcdabcd987/6f106620c3a7a6fbdc98d574ee0dac09 to your computer and use it in GitHub Desktop.
#!/usr/local/bin/python3
import os
# os.environ['HTTP_PROXY'] = 'http://127.0.0.1:8081/'
# os.environ['HTTPS_PROXY'] = 'http://127.0.0.1:8081/'
import sys
import hashlib
import requests
import sqlite3
from pprint import pprint
from datetime import datetime, timezone
class OKCoin:
def __init__(self, server, api_key, secret_key, dbpath):
self._server = server
self._api_key = api_key
self._secret_key = secret_key
self._db = OKCoin._get_db(dbpath)
def _build_sign(self, params):
data = ''
for key in sorted(params.keys()):
data += key + '=' + str(params[key]) +'&'
data += 'secret_key=' + self._secret_key
return hashlib.md5(data.encode('utf-8')).hexdigest().upper()
def get(self, api_url, **kwargs):
url = 'https://{}{}'.format(self._server, api_url)
r = requests.get(url, params=kwargs, timeout=5)
return r.json()
def post(self, api_url, **kwargs):
params = dict(kwargs)
params['api_key'] = self._api_key
params['sign'] = self._build_sign(params)
url = 'https://{}{}'.format(self._server, api_url)
r = requests.post(url, data=params, timeout=5)
return r.json()
@staticmethod
def _get_db(dbpath):
conn = sqlite3.connect(dbpath, detect_types=sqlite3.PARSE_DECLTYPES)
conn.row_factory = sqlite3.Row
cur = conn.cursor()
cur.execute('CREATE TABLE IF NOT EXISTS orders (date TIMESTAMP, amount DOUBLE, price DOUBLE, type TEXT, currency TEXT, order_id TEXT)')
cur.execute('CREATE TABLE IF NOT EXISTS bases (date TIMESTAMP, asset DOUBLE)')
conn.commit()
return conn
def get_orders(self, limit):
cur = self._db.cursor()
cur.execute('SELECT * FROM orders ORDER BY date DESC LIMIT {}'.format(limit))
return cur.fetchall()
def put_orders(self, orders, currency):
cur = self._db.cursor()
for order in orders['orders']:
cur.execute('SELECT * FROM orders WHERE order_id=?', (order['order_id'], ))
if not cur.fetchone() and order['status'] == 2:
date = datetime.utcfromtimestamp(order['create_date'] / 1000)
cur.execute('INSERT INTO orders (date, amount, price, type, currency, order_id) VALUES (?, ?, ?, ?, ?, ?)',
(date, float(order['amount']), float(order['avg_price']), order['type'], currency, order['order_id']))
self._db.commit()
def get_base(self):
cur = self._db.cursor()
cur.execute('SELECT * FROM bases ORDER BY date DESC LIMIT 1')
row = cur.fetchone()
return row['asset'] if row else 1.0
def set_base(self, asset):
cur = self._db.cursor()
date = datetime.utcnow()
cur.execute('INSERT INTO bases (date, asset) VALUES (?, ?)', (date, asset))
self._db.commit()
okcoin_server = 'www.okcoin.cn'
api_key = '' # https://www.okcoin.cn/user/api.do
secret_key = '' # https://www.okcoin.cn/user/api.do
data_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'okcoin_data.db')
okcoin = OKCoin(okcoin_server, api_key, secret_key, data_path)
def bitbar():
ticker_btc = okcoin.get('/api/v1/ticker.do', symbol='btc_cny')
depth_btc = okcoin.get('/api/v1/depth.do', symbol='btc_cny', size=5, merge=1)
order_btc = okcoin.post('/api/v1/order_history.do', symbol='btc_cny', status=1, current_page=1, page_length=20)
okcoin.put_orders(order_btc, 'btc')
ticker_bcc = okcoin.get('/api/v1/ticker.do', symbol='bcc_cny')
depth_bcc = okcoin.get('/api/v1/depth.do', symbol='bcc_cny', size=5, merge=1)
order_bcc = okcoin.post('/api/v1/order_history.do', symbol='bcc_cny', status=1, current_page=1, page_length=20)
okcoin.put_orders(order_bcc, 'bcc')
ticker_eth = okcoin.get('/api/v1/ticker.do', symbol='eth_cny')
depth_eth = okcoin.get('/api/v1/depth.do', symbol='eth_cny', size=5, merge=1)
order_eth = okcoin.post('/api/v1/order_history.do', symbol='eth_cny', status=1, current_page=1, page_length=20)
okcoin.put_orders(order_eth, 'eth')
userinfo = okcoin.post('/api/v1/userinfo.do')
font = 'Fantasque Sans Mono'
style = '| font="{}" size=14'.format(font)
bar_style = '| font="{}" size=16'.format(font)
alternate = 'alternate=true'
green = 'color=#3ba027'
red = 'color=#b5272c'
free_cny = float(userinfo['info']['funds']['free']['cny'])
freezed_cny = float(userinfo['info']['funds']['freezed']['cny'])
sum_cny = free_cny + freezed_cny
free_btc = float(userinfo['info']['funds']['free']['btc'])
freezed_btc = float(userinfo['info']['funds']['freezed']['btc'])
sum_btc = free_btc + freezed_btc
last_btc = float(ticker_btc['ticker']['last'])
free_bcc = float(userinfo['info']['funds']['free']['bcc'])
freezed_bcc = float(userinfo['info']['funds']['freezed']['bcc'])
sum_bcc = free_bcc + freezed_bcc
last_bcc = float(ticker_bcc['ticker']['last'])
free_eth = float(userinfo['info']['funds']['free']['eth'])
freezed_eth = float(userinfo['info']['funds']['freezed']['eth'])
sum_eth = free_eth + freezed_eth
last_eth = float(ticker_eth['ticker']['last'])
base = okcoin.get_base()
asset_net = float(userinfo['info']['funds']['asset']['net'])
print('B{:.2f} C{:.2f} E{:.2f}'.format(last_btc, last_bcc, last_eth), bar_style, green if asset_net > base else red)
print('---')
for i, (ask_btc, ask_bcc, ask_eth) in enumerate(zip(depth_btc['asks'], depth_bcc['asks'], depth_eth['asks'])):
print('ask {}: btc {:8.2f}x{:7.3f} bcc {:7.2f}x{:7.3f} eth {:7.2f}x{:7.3f}'.format(5-i, ask_btc[0], ask_btc[1], ask_bcc[0], ask_bcc[1], ask_eth[0], ask_eth[1]), style, red)
for i, (bid_btc, bid_bcc, bid_eth) in enumerate(zip(depth_btc['bids'], depth_bcc['bids'], depth_eth['bids'])):
print('bid {}: btc {:8.2f}x{:7.3f} bcc {:7.2f}x{:7.3f} eth {:7.2f}x{:7.3f}'.format(i+1, bid_btc[0], bid_btc[1], bid_bcc[0], bid_bcc[1], bid_eth[0], bid_eth[1]), style, green)
print('---')
print('available: cny {:8.2f} btc {:6.4f} bcc {:6.4f} eth {:6.4f}'.format(free_cny, free_btc, free_bcc, free_eth), style)
print('freezed : cny {:8.2f} btc {:6.4f} bcc {:6.4f} eth {:6.4f}'.format(freezed_cny, freezed_btc, freezed_bcc, freezed_eth), style)
print('total : cny {:8.2f} btc {:6.4f} bcc {:6.4f} eth {:6.4f}'.format(sum_cny, sum_btc, sum_bcc, sum_eth), style)
bd = (asset_net - base) / base * 100
print('asset : {:8.2f} base(={:8.2f}){:+5.2f}%'.format(asset_net, base, bd), style)
command = 'bash="{}" param1="set_base" param2="{}" terminal=false'.format(os.path.realpath(__file__), asset_net)
print('click to set new base as {:7.2f}'.format(asset_net), style, command, alternate)
print('---')
print('recent txs')
for order in okcoin.get_orders(20):
date = order['date'].replace(tzinfo=timezone.utc).astimezone(tz=None).strftime('%m-%d %H:%M')
cny = order['amount'] * order['price']
print('[{}]{:4} cny {:7.2f} / {} {:.8f} = {:.2f}'.format(date, order['type'], cny, order['currency'], order['amount'], order['price']), style)
if __name__ == '__main__':
if len(sys.argv) == 3 and sys.argv[1] == 'set_base':
okcoin.set_base(float(sys.argv[2]))
else:
while True:
try:
bitbar()
break
except requests.exceptions.RequestException:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment