add typehints

This commit is contained in:
gcarq
2017-09-01 21:11:46 +02:00
parent 99cbf72dc4
commit 32cac11580
6 changed files with 44 additions and 38 deletions

View File

@@ -1,5 +1,7 @@
import enum
import logging
from typing import List
from bittrex.bittrex import Bittrex
from poloniex import Poloniex
from wrapt import synchronized
@@ -9,18 +11,6 @@ logger = logging.getLogger(__name__)
_exchange_api = None
@synchronized
def get_exchange_api(conf):
"""
Returns the current exchange api or instantiates a new one
:return: exchange.ApiWrapper
"""
global _exchange_api
if not _exchange_api:
_exchange_api = ApiWrapper(conf)
return _exchange_api
class Exchange(enum.Enum):
POLONIEX = 0
BITTREX = 1
@@ -33,7 +23,7 @@ class ApiWrapper(object):
* Bittrex
* Poloniex (partly)
"""
def __init__(self, config):
def __init__(self, config: dict):
"""
Initializes the ApiWrapper with the given config, it does not validate those values.
:param config: dict
@@ -54,13 +44,13 @@ class ApiWrapper(object):
else:
self.api = None
def buy(self, pair, rate, amount):
def buy(self, 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: None
:return: order_id of the placed buy order
"""
if self.dry_run:
pass
@@ -73,7 +63,7 @@ class ApiWrapper(object):
raise RuntimeError('BITTREX: {}'.format(data['message']))
return data['result']['uuid']
def sell(self, pair, rate, amount):
def sell(self, pair: str, rate: float, amount: float) -> str:
"""
Places a limit sell order.
:param pair: Pair as str, format: BTC_ETH
@@ -92,7 +82,7 @@ class ApiWrapper(object):
raise RuntimeError('BITTREX: {}'.format(data['message']))
return data['result']['uuid']
def get_balance(self, currency):
def get_balance(self, currency: str) -> float:
"""
Get account balance.
:param currency: currency as str, format: BTC
@@ -109,7 +99,7 @@ class ApiWrapper(object):
raise RuntimeError('BITTREX: {}'.format(data['message']))
return float(data['result']['Balance'] or 0.0)
def get_ticker(self, pair):
def get_ticker(self, pair: str) -> dict:
"""
Get Ticker for given pair.
:param pair: Pair as str, format: BTC_ETC
@@ -132,7 +122,7 @@ class ApiWrapper(object):
'last': float(data['result']['Last']),
}
def cancel_order(self, order_id):
def cancel_order(self, order_id: str) -> None:
"""
Cancel order for given order_id
:param order_id: id as str
@@ -147,7 +137,7 @@ class ApiWrapper(object):
if not data['success']:
raise RuntimeError('BITTREX: {}'.format(data['message']))
def get_open_orders(self, pair):
def get_open_orders(self, pair: str) -> List[dict]:
"""
Get all open orders for given pair.
:param pair: Pair as str, format: BTC_ETC
@@ -170,7 +160,7 @@ class ApiWrapper(object):
'remaining': entry['QuantityRemaining'],
} for entry in data['result']]
def get_pair_detail_url(self, pair):
def get_pair_detail_url(self, pair: str) -> str:
"""
Returns the market detail url for the given pair
:param pair: pair as str, format: BTC_ANT
@@ -180,3 +170,15 @@ class ApiWrapper(object):
raise NotImplemented('Not implemented')
elif self.exchange == Exchange.BITTREX:
return 'https://bittrex.com/Market/Index?MarketName={}'.format(pair.replace('_', '-'))
@synchronized
def get_exchange_api(conf: dict) -> ApiWrapper:
"""
Returns the current exchange api or instantiates a new one
:return: exchange.ApiWrapper
"""
global _exchange_api
if not _exchange_api:
_exchange_api = ApiWrapper(conf)
return _exchange_api