Last active
May 24, 2017 11:39
-
-
Save rdmarsh/423683b58a11911af8ea to your computer and use it in GitHub Desktop.
BTC python code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import base64, hashlib, hmac, urllib2, time, urllib, json | |
from collections import OrderedDict | |
base_url = 'https://api.btcmarkets.net' | |
def request(action, key, signature, timestamp, path, data): | |
header = { | |
'accept': 'application/json', | |
'Content-Type': 'application/json', | |
'User-Agent': 'btc markets python client', | |
'accept-charset': 'utf-8', | |
'apikey': key, | |
'signature': signature, | |
'timestamp': timestamp, | |
} | |
request = urllib2.Request(base_url + path, data, header) | |
if action == 'post': | |
response = urllib2.urlopen(request, data) | |
else: | |
response = urllib2.urlopen(request) | |
return json.load(response) | |
def get_request(key, secret, path): | |
nowInMilisecond = str(int(time.time() * 1000)) | |
stringToSign = path + "\n" + nowInMilisecond + "\n" | |
signature = base64.b64encode(hmac.new(secret, stringToSign, digestmod=hashlib.sha512).digest()) | |
return request('get', key, signature, nowInMilisecond, path, None) | |
def post_request(key, secret, path, postData): | |
nowInMilisecond = str(int(time.time() * 1000)) | |
stringToSign = path + "\n" + nowInMilisecond + "\n" + postData | |
signature = base64.b64encode(hmac.new(secret, stringToSign, digestmod=hashlib.sha512).digest()) | |
return request('post', key, signature, nowInMilisecond, path, postData) | |
class BTCMarkets: | |
def __init__(self, key, secret): | |
self.key = key | |
self.secret = base64.b64decode(secret) | |
def trade_history(self, currency, instrument, limit, since): | |
data = OrderedDict([('currency', currency),('instrument', instrument),('limit', limit),('since', since)]) | |
postData = json.dumps(data, separators=(',', ':')) | |
urlString = '/order/trade/history' | |
return post_request(self.key, self.secret, urlString, postData) | |
def order_create(self, currency, instrument, price, volume, side, order_type, client_request_id): | |
data = OrderedDict([('currency', currency),('instrument', instrument), | |
('price', price),('volume', volume),('orderSide', side),('ordertype', order_type), | |
('clientRequestId', client_request_id)]) | |
postData = json.dumps(data, separators=(',', ':')) | |
urlString = '/order/create' | |
return post_request(self.key, self.secret, urlString, postData) | |
def order_cancel(self, orderIds): | |
data = OrderedDict([('orderIds', orderIds)]) | |
postData = json.dumps(data, separators=(',', ':')) | |
urlString = '/order/cancel' | |
return post_request(self.key, self.secret, urlString, postData) | |
def order_detail(self, order_ids): | |
data_obj = {'orderIds':order_ids} | |
postData = json.dumps(data_obj, separators=(',', ':')) | |
urlString = '/order/detail' | |
return post_request(self.key, self.secret, urlString, postData) | |
def order_history(self, currency, instrument, limit, since): | |
data = OrderedDict([('currency', currency),('instrument', instrument),('limit', limit),('since', since)]) | |
postData = json.dumps(data, separators=(',', ':')) | |
urlString = '/order/history' | |
return post_request(self.key, self.secret, urlString, postData) | |
def order_open(self, currency, instrument, limit, since): | |
data = OrderedDict([('currency', currency),('instrument', instrument),('limit', limit),('since', since)]) | |
postData = json.dumps(data, separators=(',', ':')) | |
urlString = '/order/open' | |
return post_request(self.key, self.secret, urlString, postData) | |
def account_balance(self): | |
urlString = '/account/balance' | |
return get_request(self.key, self.secret, urlString) | |
def tick(self, currency, instrument): | |
data = OrderedDict([('currency', currency),('instrument', instrument)]) | |
urlString = '/market/' + instrument + '/' + currency + '/tick' | |
return get_request(self.key, self.secret, urlString) | |
def order_book(self, currency, instrument): | |
data = OrderedDict([('currency', currency),('instrument', instrument)]) | |
urlString = '/market/' + instrument + '/' + currency + '/orderbook' | |
return get_request(self.key, self.secret, urlString) | |
def trades(self, currency, instrument): | |
data = OrderedDict([('currency', currency),('instrument', instrument),]) | |
urlString = '/market/' + instrument + '/' + currency + '/trades' | |
return get_request(self.key, self.secret, urlString) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
base_url = 'https://api.coinjar.io/v1' | |
def request(user, password, path): | |
r = requests.get(base_url + path, auth=(user, password)) | |
return r.json() | |
class CoinJar: | |
def __init__(self, user, password): | |
self.user = user | |
self.password = password | |
def fairrate(self, currency): | |
urlString = '/fair_rate/' + currency + '.json' | |
return request(self.user, self.password, urlString) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment