Skip to content

Instantly share code, notes, and snippets.

@nanvel
Created November 24, 2017 15:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nanvel/fcf28a913ac97eebc40ab9aebc5fe52f to your computer and use it in GitHub Desktop.
Save nanvel/fcf28a913ac97eebc40ab9aebc5fe52f to your computer and use it in GitHub Desktop.
aio-bittrex
import asyncio
import hashlib
import hmac
import time
from urllib.parse import urljoin, urlencode
import aiohttp
import async_timeout
class BittrexApi:
"""
https://github.com/ericsomdahl/python-bittrex/blob/master/bittrex/bittrex.py
https://bittrex.com/home/api
"""
BASE_URL = 'https://bittrex.com/api/v1.1/'
def __init__(self, api_key=None, api_secret=None, call_rate=1):
self.api_key = api_key or ''
self.api_secret = api_secret or ''
self.call_interval = 1. / call_rate
self.timeout = 20
self._last_call = time.time() - self.call_interval
async def query(self, path, options=None, authenticate=False):
options = options or {}
if path.startswith('/'):
path = path[1:]
url = urljoin(self.BASE_URL, path)
if authenticate:
nonce = str(int(time.time() * 1000))
url = "{url}?apikey={api_key}&nonce={nonce}".format(
url=url, api_key=self.api_key, nonce=nonce
)
if options:
url += '&' + urlencode(options)
signature = hmac.new(
key=self.api_secret.encode(),
msg=url.encode(),
digestmod=hashlib.sha512
).hexdigest()
to_wait = time.time() - self._last_call + self.call_interval
if to_wait > 0:
await asyncio.sleep(to_wait)
try:
async with aiohttp.ClientSession(headers={'apisign': signature}) as session:
with async_timeout.timeout(self.timeout):
async with session.get(url) as response:
return await response.json()
finally:
self._last_call = time.time()
def get_markets(self):
return self.query(path='/public/getmarkets')
def get_currencies(self):
return self.query(path='/public/getcurrencies')
def get_ticker(self, market):
return self.query(path='/public/getticker', options={'market': market})
def get_market_summaries(self):
return self.query(path='/public/getmarketsummaries')
def get_market_summary(self, market):
return self.query(path='/public/getmarketsummary', options={'market': market})
def get_order_book(self, market, order_type='both'):
"""
:param order_type: 'buy', 'sell', 'both'
"""
return self.query(
path='/public/getorderbook',
options={'market': market, 'type': order_type}
)
def get_market_history(self, market):
return self.query(path='/public/getmarkethistory', options={'market': market})
def buy_limit(self, market, quantity, rate):
return self.query(
path='/market/buylimit',
options={'market': market, 'quantity': quantity, 'rate': rate},
authenticate=True
)
def sell_limit(self, market, quantity, rate):
return self.query(
path='/market/selllimit',
options={'market': market, 'quantity': quantity, 'rate': rate},
authenticate=True
)
def cancel_trade(self, order_id):
return self.query(
path='/market/cancel',
options={'uuid': order_id},
authenticate=True
)
def get_open_orders(self, market=None):
return self.query(
path='/market/getopenorders',
options={'market': market} if market else None,
authenticate=True
)
def get_balances(self):
return self.query(
path='/account/getbalances',
authenticate=True
)
def get_balance(self, currency):
return self.query(
path='/account/getbalance',
options={'currency': currency},
authenticate=True
)
def get_deposit_address(self, currency):
return self.query(
path='/account/getdepositaddress',
options={'currency': currency},
authenticate=True
)
def withdraw(self, currency, quantity, address):
return self.query(
path='/account/getorderhistory',
options={'currency': currency, 'quantity': quantity, 'address': address},
authenticate=True
)
def get_order_history(self, market=None):
return self.query(
path='/account/getorderhistory',
options={'market': market} if market else None,
authenticate=True
)
def get_order(self, order_id):
return self.query(
path='/account/getorder',
options={'uuid': order_id},
authenticate=True
)
def get_withdrawal_history(self, currency=None):
return self.query(
path='/account/getwithdrawalhistory',
options={'currency': currency} if currency else None,
authenticate=True
)
def get_deposit_history(self, currency=None):
return self.query(
path='/account/getdeposithistory',
options={'currency': currency} if currency else None,
authenticate=True
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment