Skip to content

Instantly share code, notes, and snippets.

@abcdabcd987
Created August 6, 2017 21:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abcdabcd987/b9b451187dd0f9ed4ea7abc6a7f015a9 to your computer and use it in GitHub Desktop.
Save abcdabcd987/b9b451187dd0f9ed4ea7abc6a7f015a9 to your computer and use it in GitHub Desktop.
#!/usr/local/bin/python2
# -*- coding: utf-8 -*-
from __future__ import unicode_literals, print_function, division
import os
# os.environ['HTTP_PROXY'] = 'http://127.0.0.1:8081/'
# os.environ['HTTPS_PROXY'] = 'http://127.0.0.1:8081/'
import datetime
import hashlib
import requests
from pprint import pprint
from dateutil.parser import parse
import pytz
from lbcapi import api
hmac_key = ''
hmac_secret = ''
conn = api.hmac(hmac_key, hmac_secret)
style = '| font="Source Code Pro" size=12'
green = 'color=#3ba027'
red = 'color=#b5272c'
ticker = conn.call('GET', '/bitcoinaverage/ticker-all-currencies/').json()['CNY']
print('{:.2f}'.format(float(ticker['avg_1h'])))
print('---')
asks = conn.call('GET', '/buy-bitcoins-online/CNY/.json').json()['data']['ad_list']
for i, ask in reversed(list(enumerate(asks[:5], start=1))):
price = float(ask['data']['temp_price'])
url = ask['actions']['public_view']
min_amount = ask['data']['min_amount']
max_amount = ask['data']['max_amount']
print('ask {}: price {:.2f}, amount [{}, {}]'.format(i, price, min_amount, max_amount), style, red, 'href='+url)
bids = conn.call('GET', '/sell-bitcoins-online/CNY/.json').json()['data']['ad_list']
for i, bid in enumerate(bids[:5], start=1):
price = float(bid['data']['temp_price'])
url = bid['actions']['public_view']
min_amount = bid['data']['min_amount']
max_amount = bid['data']['max_amount']
print('bid {}: price {:.2f}, amount [{}, {}]'.format(i, price, min_amount, max_amount), style, green, 'href='+url)
print('---')
wallet = conn.call('GET', '/api/wallet-balance/').json()
balance = float(wallet['data']['total']['balance'])
sendable = float(wallet['data']['total']['sendable'])
print('balance : BTC {:.8f}'.format(balance), style)
txs = conn.call('GET', '/api/dashboard/released/').json()['data']['contact_list']
total_buy = sum(float(tx['data']['amount']) for tx in txs if tx['data']['advertisement']['trade_type'] == 'ONLINE_SELL')
total_sell = sum(float(tx['data']['amount']) for tx in txs if tx['data']['advertisement']['trade_type'] == 'ONLINE_BUY')
print('total buy : CNY {:.2f}'.format(total_buy), style)
print('total sell: CNY {:.2f}'.format(total_sell), style)
print('buy - sell: CNY {:.2f}'.format(total_buy - total_sell), style)
print('avg price : CNY {:.2f}'.format((total_buy-total_sell) / balance), style)
print('---')
sell_all = float(bids[0]['data']['temp_price']) * sendable
diff = sell_all - (total_buy - total_sell)
ratio = diff / (total_buy - total_sell) * 100
print('sendable: BTC {:.8f}'.format(sendable), style)
print('sell all: CNY {:.2f}'.format(sell_all), style)
print('diff : CNY{:+.2f} ({:+.1f}%)'.format(diff, ratio), style, red if ratio < 0 else green)
print('---')
print('recent txs')
for tx in txs:
created_at = parse(tx['data']['created_at']).astimezone(pytz.timezone('Asia/Shanghai'))
created_at = created_at.strftime('%Y-%m-%d')
cny = float(tx['data']['amount'])
btc = float(tx['data']['amount_btc'])
op = 'buy ' if tx['data']['advertisement']['trade_type'] == 'ONLINE_SELL' else 'sell'
print('[{}]{} CNY {:7.2f} / BTC {:.8f} = {:.2f}'.format(created_at, op, cny, btc, cny / btc), style)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment