def getTradeHistory(pair, connection=None, start_tid=None, count=None, error_handler=None): """ Retrieve the trade history for the given pair. Returns a list of Trade instances. If count is not None, it should be an integer, and specifies the number of items from the trade history that will be processed and returned. """ common.validatePair(pair) if connection is None: connection = common.BTERConnection() if start_tid is None: result = connection.makeJSONRequest('/api/1/trade/%s' % pair, method='GET') else: result = connection.makeJSONRequest('/api/1/trade/%s/%d' % (pair, start_tid), method='GET') result = common.validateResponse(result, error_handler=error_handler) history = result[u'data'] if type(history) is not list: raise Exception('The response does not contain a history list.') result = [] # Limit the number of items returned if requested. if count is not None: history = history[:count] for h in history: h["pair"] = pair t = Trade(**h) result.append(t) return result
def getDepth(pair, connection=None, error_handler=None): """ Retrieve the depth for the given pair. Returns a tuple (asks, bids); each of these is a list of (price, volume) tuples. """ common.validatePair(pair) if connection is None: connection = common.BTERConnection() depth = common.validateResponse(connection.makeJSONRequest('/api/1/depth/%s' % pair, method='GET'), error_handler=error_handler) if type(depth) is not dict: raise TypeError("The response is not a dict.") asks = depth.get('asks') if type(asks) is not list: raise TypeError("The response does not contain an asks list.") bids = depth.get('bids') if type(bids) is not list: raise TypeError("The response does not contain a bids list.") return asks, bids
def placeOrder(self, pair, trade_type, rate, amount, connection=None, update_delay=None, error_handler=None): common.validatePair(pair) if trade_type.lower() not in ("buy", "sell"): if trade_type.lower() == 'bid': trade_type = 'buy' elif trade_type.lower() == 'ask': trade_type = 'sell' else: raise Exception("Unrecognized trade type: %r" % trade_type) params = {"pair": pair, "type": trade_type.upper(), "rate": common.formatCurrency(rate, pair, 'price'), "amount": common.formatCurrency(amount, pair, 'amount')} order = OrderItem(self._post('placeorder', params=params, connection=connection, error_handler=error_handler).get(u'order_id'), initial_params=params, date=now()) if update_delay is not None: time.sleep(update_delay) order = self.getOrderStatus(order.order_id, connection=None) return order
def __init__(self, pairs=common.all_pairs): for pair in pairs: common.validatePair(pair) self._pairs = pairs self._all = self._pairs == common.all_pairs