def subscribe_to_orderbook(self, symbol, precision, length, callback): """Subscribe to the orderbook of a given symbol. Parameters ---------- symbol : str Symbol to request data for. precision : str Accepted values as strings {R0, P0, P1, P2, P3} length : int Initial snapshot length. Accepted values {1,25,100} callback : func A function to use to handle incomming messages Example ------- :: def my_handler(message): # Here you can do stuff with the messages print(message) # You should only need to create and authenticate a client once. # Then simply reuse it later my_client = WssClient(key, secret) my_client.subscribe_to_orderbook( symbol="BTCUSD", precision="P1", length=25, callback=my_handler ) my_client.start() """ symbol = utils.order_symbol(symbol) id_ = "_".join(["order", symbol]) data = { 'event': 'subscribe', "channel": "book", "prec": precision, "len": length, 'symbol': symbol, } payload = json.dumps(data, ensure_ascii=False).encode('utf8') return self._start_socket(id_, payload, callback)
def subscribe_to_trades(self, symbol, callback): """Subscribe to the passed symbol trades data channel. Parameters ---------- symbol : str Symbol to request data for. callback : func A function to use to handle incomming messages Example ------- :: def my_handler(message): # Here you can do stuff with the messages print(message) # You should only need to create and authenticate a client once. # Then simply reuse it later my_client = WssClient(key, secret) my_client.authenticate(print) my_client.subscribe_to_trades( symbol="BTCUSD", callback=my_handler ) my_client.start() """ symbol = utils.order_symbol(symbol) id_ = "_".join(["trades", symbol]) data = { 'event': 'subscribe', 'channel': 'trades', 'symbol': symbol, } payload = json.dumps(data, ensure_ascii=False).encode('utf8') return self._start_socket(id_, payload, callback)
def new_order_op(self, order_type, symbol, amount, price, price_trailing=None, price_aux_limit=None, price_oco_stop=None, hidden=0, flags=None, tif=None, set_cid=True): """Create new order operation Parameters ---------- order_type : str Order type. Must be one of: "LIMIT", "STOP", "MARKET", "TRAILING STOP", "FOK", "STOP LIMIT" or equivelent with "EXCHANGE" prepended to it. All orders starting with EXCHANGE are made on the exchange wallet. Orders without it is made on the margin wallet and will start or change a position. symbol : str The currency symbol to be traded. e.g. BTCUSD amount : decimal str The amount to be traided. price : decimal str The price to buy at. Will be ignored for market orders. price_trailing : decimal string The trailing price price_aux_limit : decimal string Auxiliary Limit price (for STOP LIMIT) price_oco_stop : decimal string OCO stop price hidden : bool Whether or not to use the hidden order type. flags : list A list of integers for the different flags. Will be added together into a unique integer. tif : datetime string set_cid : bool wheter or not to set a cid. Returns ------- dict A dict containing the order detials. Used in new_order and for creating multiorders. Example ------- Note if you only want to create a new order, use the ´´new_order´´ method bellow. However if you want to submitt multiple order and cancel orders at the same time use this method to create order operations and send them with the ``multi_order`` method:: # You should only need to create and authenticate a client once. # Then simply reuse it later my_client = WssClient(key, secret) my_client.authenticate() my_client.start() order_operation = my_client.new_order_op( order_type="LIMIT", symbol="BTCUSD", amount=0.004, price=1000.0 ) # Usefull to keep track of an order by its client id, for later # operations (like cancel order). clinet_id = order_operation["cid"] my_client.multi_order( operations=[order_operation] ) """ flags = flags or [] order_op = { 'type': order_type, 'symbol': utils.order_symbol(symbol), 'amount': amount, 'price': price, 'hidden': hidden, "flags": sum(flags), } if price_trailing: order_op['price_trailing'] = price_trailing if price_aux_limit: order_op['price_aux_limit'] = price_aux_limit if price_oco_stop: order_op['price_oco_stop'] = price_oco_stop if tif: order_op['tif'] = tif if set_cid: client_order_id = utils.create_cid() order_op['cid'] = client_order_id return order_op
def subscribe_to_candles(self, symbol, timeframe, callback): """Subscribe to the passed symbol's OHLC data channel. Parameters ---------- symbol : str Symbol to request data for timeframe : str Accepted values as strings {1m, 5m, 15m, 30m, 1h, 3h, 6h, 12h, 1D, 7D, 14D, 1M} callback : func A function to use to handle incomming messages Returns ------- str The socket identifier. Example ------- :: def my_candle_handler(message): # Here you can do stuff with the candle bar messages print(message) # You should only need to create and authenticate a client once. # Then simply reuse it later my_client = WssClient(key, secret) my_client.subscribe_to_candles( symbol="BTCUSD", timeframe="1m", callback=my_candle_handler ) my_client.subscribe_to_candles( symbol="ETHUSD", timeframe="5m", callback=my_candle_handler ) my_client.start() """ valid_tfs = [ '1m', '5m', '15m', '30m', '1h', '3h', '6h', '12h', '1D', '7D', '14D', '1M' ] if timeframe: if timeframe not in valid_tfs: raise ValueError("timeframe must be any of %s" % valid_tfs) else: timeframe = '1m' identifier = ('candles', symbol, timeframe) id_ = "_".join(identifier) symbol = utils.order_symbol(symbol) key = 'trade:' + timeframe + ':' + symbol data = { 'event': 'subscribe', 'channel': 'candles', 'key': key, } payload = json.dumps(data, ensure_ascii=False).encode('utf8') return self._start_socket(id_, payload, callback)
def test_order_symbol_keeps_t_to_symbol(): assert utils.order_symbol("tBTCUSD") == "tBTCUSD"
def test_order_symbol_adds_t_to_symbol(): assert utils.order_symbol("BTCUSD") == "tBTCUSD"
def test_order_symbol_passes_on_unknown_symbols_unchanged(): assert utils.order_symbol("custom_sym") == "custom_sym"
def test_order_symbol_capital_letters(): assert utils.order_symbol("btcusd") == "tBTCUSD"
def test_order_symbol_adds_f_to_symbol_lowercase(): assert utils.order_symbol("btc", capital=False) == "fbtc"
def test_order_symbol_keeps_f_to_symbol(): assert utils.order_symbol("fBTC") == "fBTC"
def test_order_symbol_adds_f_to_symbol(): assert utils.order_symbol("BTC") == "fBTC"
def test_order_symbol_adds_t_to_symbol_lowercase(): assert utils.order_symbol("btcusd", capital=False) == "tbtcusd"