2017-05-12 17:11:56 +00:00
|
|
|
import enum
|
2017-05-14 12:14:16 +00:00
|
|
|
import logging
|
2017-09-01 19:11:46 +00:00
|
|
|
from typing import List
|
|
|
|
|
2017-05-12 17:11:56 +00:00
|
|
|
from bittrex.bittrex import Bittrex
|
|
|
|
|
2017-05-14 12:14:16 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
2017-05-12 17:11:56 +00:00
|
|
|
|
2017-09-08 21:10:22 +00:00
|
|
|
# Current selected exchange
|
|
|
|
EXCHANGE = None
|
|
|
|
_API = None
|
|
|
|
_CONF = {}
|
2017-05-12 17:11:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Exchange(enum.Enum):
|
|
|
|
BITTREX = 1
|
|
|
|
|
|
|
|
|
2017-09-08 13:51:00 +00:00
|
|
|
def init(config: dict) -> None:
|
|
|
|
"""
|
|
|
|
Initializes this module with the given config,
|
|
|
|
it does basic validation whether the specified
|
|
|
|
exchange and pairs are valid.
|
|
|
|
:param config: config to use
|
|
|
|
:return: None
|
|
|
|
"""
|
2017-09-08 21:10:22 +00:00
|
|
|
global _API, EXCHANGE
|
2017-09-08 13:51:00 +00:00
|
|
|
|
2017-09-08 21:10:22 +00:00
|
|
|
_CONF.update(config)
|
2017-09-08 13:51:00 +00:00
|
|
|
|
|
|
|
if config['dry_run']:
|
|
|
|
logger.info('Instance is running with dry_run enabled')
|
|
|
|
|
|
|
|
use_bittrex = config.get('bittrex', {}).get('enabled', False)
|
2017-09-11 11:59:38 +00:00
|
|
|
if use_bittrex:
|
2017-09-08 21:10:22 +00:00
|
|
|
EXCHANGE = Exchange.BITTREX
|
|
|
|
_API = Bittrex(api_key=config['bittrex']['key'], api_secret=config['bittrex']['secret'])
|
2017-09-08 13:51:00 +00:00
|
|
|
else:
|
|
|
|
raise RuntimeError('No exchange specified. Aborting!')
|
|
|
|
|
|
|
|
# Check if all pairs are available
|
|
|
|
markets = get_markets()
|
2017-09-11 11:59:38 +00:00
|
|
|
exchange_name = EXCHANGE.name.lower()
|
|
|
|
for pair in config[exchange_name]['pair_whitelist']:
|
2017-09-08 13:51:00 +00:00
|
|
|
if pair not in markets:
|
2017-09-11 11:59:38 +00:00
|
|
|
raise RuntimeError('Pair {} is not available at {}'.format(pair, exchange_name))
|
2017-09-08 13:51:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
def buy(pair: str, rate: float, amount: float) -> str:
|
|
|
|
"""
|
|
|
|
Places a limit buy order.
|
|
|
|
:param pair: Pair as str, format: BTC_ETH
|
|
|
|
:param rate: Rate limit for order
|
|
|
|
:param amount: The amount to purchase
|
|
|
|
:return: order_id of the placed buy order
|
|
|
|
"""
|
2017-09-08 21:10:22 +00:00
|
|
|
if _CONF['dry_run']:
|
2017-09-08 13:51:00 +00:00
|
|
|
return 'dry_run'
|
2017-09-08 21:10:22 +00:00
|
|
|
elif EXCHANGE == Exchange.BITTREX:
|
|
|
|
data = _API.buy_limit(pair.replace('_', '-'), amount, rate)
|
2017-09-08 13:51:00 +00:00
|
|
|
if not data['success']:
|
|
|
|
raise RuntimeError('BITTREX: {}'.format(data['message']))
|
|
|
|
return data['result']['uuid']
|
|
|
|
|
|
|
|
|
|
|
|
def sell(pair: str, rate: float, amount: float) -> str:
|
|
|
|
"""
|
|
|
|
Places a limit sell order.
|
|
|
|
:param pair: Pair as str, format: BTC_ETH
|
|
|
|
:param rate: Rate limit for order
|
|
|
|
:param amount: The amount to sell
|
|
|
|
:return: None
|
|
|
|
"""
|
2017-09-08 21:10:22 +00:00
|
|
|
if _CONF['dry_run']:
|
2017-09-08 13:51:00 +00:00
|
|
|
return 'dry_run'
|
2017-09-08 21:10:22 +00:00
|
|
|
elif EXCHANGE == Exchange.BITTREX:
|
|
|
|
data = _API.sell_limit(pair.replace('_', '-'), amount, rate)
|
2017-09-08 13:51:00 +00:00
|
|
|
if not data['success']:
|
|
|
|
raise RuntimeError('BITTREX: {}'.format(data['message']))
|
|
|
|
return data['result']['uuid']
|
|
|
|
|
|
|
|
|
|
|
|
def get_balance(currency: str) -> float:
|
|
|
|
"""
|
|
|
|
Get account balance.
|
|
|
|
:param currency: currency as str, format: BTC
|
|
|
|
:return: float
|
|
|
|
"""
|
2017-09-08 21:10:22 +00:00
|
|
|
if _CONF['dry_run']:
|
2017-09-08 13:51:00 +00:00
|
|
|
return 999.9
|
2017-09-08 21:10:22 +00:00
|
|
|
elif EXCHANGE == Exchange.BITTREX:
|
|
|
|
data = _API.get_balance(currency)
|
2017-09-08 13:51:00 +00:00
|
|
|
if not data['success']:
|
|
|
|
raise RuntimeError('BITTREX: {}'.format(data['message']))
|
|
|
|
return float(data['result']['Balance'] or 0.0)
|
|
|
|
|
|
|
|
|
|
|
|
def get_ticker(pair: str) -> dict:
|
|
|
|
"""
|
|
|
|
Get Ticker for given pair.
|
|
|
|
:param pair: Pair as str, format: BTC_ETC
|
|
|
|
:return: dict
|
|
|
|
"""
|
2017-09-11 11:59:38 +00:00
|
|
|
if EXCHANGE == Exchange.BITTREX:
|
2017-09-08 21:10:22 +00:00
|
|
|
data = _API.get_ticker(pair.replace('_', '-'))
|
2017-09-08 13:51:00 +00:00
|
|
|
if not data['success']:
|
|
|
|
raise RuntimeError('BITTREX: {}'.format(data['message']))
|
|
|
|
return {
|
|
|
|
'bid': float(data['result']['Bid']),
|
|
|
|
'ask': float(data['result']['Ask']),
|
|
|
|
'last': float(data['result']['Last']),
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def cancel_order(order_id: str) -> None:
|
|
|
|
"""
|
|
|
|
Cancel order for given order_id
|
|
|
|
:param order_id: id as str
|
|
|
|
:return: None
|
|
|
|
"""
|
2017-09-08 21:10:22 +00:00
|
|
|
if _CONF['dry_run']:
|
2017-09-08 13:51:00 +00:00
|
|
|
pass
|
2017-09-08 21:10:22 +00:00
|
|
|
elif EXCHANGE == Exchange.BITTREX:
|
|
|
|
data = _API.cancel(order_id)
|
2017-09-08 13:51:00 +00:00
|
|
|
if not data['success']:
|
|
|
|
raise RuntimeError('BITTREX: {}'.format(data['message']))
|
|
|
|
|
|
|
|
|
|
|
|
def get_open_orders(pair: str) -> List[dict]:
|
|
|
|
"""
|
|
|
|
Get all open orders for given pair.
|
|
|
|
:param pair: Pair as str, format: BTC_ETC
|
|
|
|
:return: list of dicts
|
|
|
|
"""
|
2017-09-08 21:10:22 +00:00
|
|
|
if _CONF['dry_run']:
|
2017-09-08 13:51:00 +00:00
|
|
|
return []
|
2017-09-08 21:10:22 +00:00
|
|
|
elif EXCHANGE == Exchange.BITTREX:
|
|
|
|
data = _API.get_open_orders(pair.replace('_', '-'))
|
2017-09-08 13:51:00 +00:00
|
|
|
if not data['success']:
|
|
|
|
raise RuntimeError('BITTREX: {}'.format(data['message']))
|
|
|
|
return [{
|
|
|
|
'id': entry['OrderUuid'],
|
|
|
|
'type': entry['OrderType'],
|
|
|
|
'opened': entry['Opened'],
|
|
|
|
'rate': entry['PricePerUnit'],
|
|
|
|
'amount': entry['Quantity'],
|
|
|
|
'remaining': entry['QuantityRemaining'],
|
|
|
|
} for entry in data['result']]
|
|
|
|
|
|
|
|
|
|
|
|
def get_pair_detail_url(pair: str) -> str:
|
|
|
|
"""
|
|
|
|
Returns the market detail url for the given pair
|
|
|
|
:param pair: pair as str, format: BTC_ANT
|
|
|
|
:return: url as str
|
|
|
|
"""
|
2017-09-11 11:59:38 +00:00
|
|
|
if EXCHANGE == Exchange.BITTREX:
|
2017-09-08 13:51:00 +00:00
|
|
|
return 'https://bittrex.com/Market/Index?MarketName={}'.format(pair.replace('_', '-'))
|
|
|
|
|
|
|
|
|
|
|
|
def get_markets() -> List[str]:
|
|
|
|
"""
|
|
|
|
Returns all available markets
|
|
|
|
:return: list of all available pairs
|
|
|
|
"""
|
2017-09-11 11:59:38 +00:00
|
|
|
if EXCHANGE == Exchange. BITTREX:
|
2017-09-08 21:10:22 +00:00
|
|
|
data = _API.get_markets()
|
2017-09-08 13:51:00 +00:00
|
|
|
if not data['success']:
|
|
|
|
raise RuntimeError('BITTREX: {}'.format(data['message']))
|
|
|
|
return [m['MarketName'].replace('-', '_') for m in data['result']]
|