def cancel_all(): """ Generates a 'cancellation' message for Deribit API : ALL instruments in ALL currencies. :return: (dict) Message to be sent into the websocket. """ msg = message(method=METHOD_CANCEL_ALL) return add_params_to_message(None, msg)
def request_instruments(currency: str, kind: str, expired=False): # Sanitize input arguments data = sanitize(currency=currency, kind=kind) # Build basic message msg = message(method=METHOD_GET_INSTRUMENTS) params = {"currency": data["currency"], "kind": data["kind"], "expired": expired} return add_params_to_message(params, msg)
def subscription_message(channels): if not isinstance(channels, List): channels = [channels] msg = message(method=METHOD_SUBSCRIBE) params = {"channels": channels} return add_params_to_message(params, msg) # The End
def request_orderbook(instrument: str, depth: int = None): # Sanitize input arguments data = sanitize(instrument=instrument, depth=depth) # Get basic method call msg = message(method=METHOD_GET_ORDER_BOOK) # Add parameters params = {"instrument_name": data["instrument"], "depth": data["depth"]} return add_params_to_message(params, msg)
def order_status(order_id: str): """ Generates a request for qn estimated margins for a trade. :param order_id: (str) Deribit order id :return: (dict) Message to be sent into the websocket. """ data = sanitize(order_id=order_id) # Build basic message msg = message(method=METHOD_MARGINS) params = {key: value for (key, value) in data.items()} return add_params_to_message(params, msg)
def margins(instrument: str, amount: float = None, price: float = None): """ Generates a request for qn estimated margins for a trade. :param instrument: (str) Deribit instrument's name :param amount: (float) Amount in USD for future or BTC (or ETH) for options :param price: (float) Estimated price for the trade. :return: (dict) Message to be sent into the websocket. """ data = sanitize(instrument=instrument, amount=amount, price=price) # Build basic message msg = message(method=METHOD_MARGINS) params = {key: value for (key, value) in data.items()} return add_params_to_message(params, msg)
def open_orders_by_instrument(instrument: str, order_type: float = None): """ Request all open orders for a given instrument. :param instrument: (str) Deribit instrument's name :param order_type: (str) Order type. Either market, limit, stop_limit, stop_market. :return: (dict) Message to be sent into the websocket. """ # Implement Deribit default behaviour if not order_type: order_type = ORDER_TYPE.ALL.value.lower() data = sanitize(instrument=instrument, type=order_type) # Build basic message msg = message(method=METHOD_OPEN_ORDERS_BY_INSTRUMENT) params = {key: value for (key, value) in data.items()} return add_params_to_message(params, msg)
def user_trades_by_instrument(instrument: str, count: int = None, include_old: bool = None): """ Request all open user_trades for a given instrument. :param instrument: (str) Deribit instrument's name :param count: (int) The number of old trades requested. :param include_old: (bool) Retrieve trades that more than 7 days old. :return: (dict) Message to be sent into the websocket. """ data = sanitize(instrument=instrument, count=count, include_old=include_old) # Build basic message msg = message(method=METHOD_USER_TRADES_BY_INSTRUMENT) params = {key: value for (key, value) in data.items()} return add_params_to_message(params, msg)
def cancel_all_by_instrument(instrument: str, order_type: str = None): """ Generates a 'cancellation' message for Deribit API for ALL instruments in a given instrument. :param instrument: (str) Deribit instrument's name :param order_type: (str) Order type. Either limit or stop. :return: (dict) Message to be sent into the websocket. """ # Implement Deribit default behaviour if not order_type: order_type = ORDER_TYPE.ALL.value.lower() data = sanitize(instrument=instrument, type=order_type) assert_cancellation_order_type(order_type=data["type"]) # Build basic message msg = message(method=METHOD_CANCEL_ALL_BY_INSTRUMENT) params = {key: value for (key, value) in data.items()} return add_params_to_message(params, msg)
def open_orders_by_currency(currency: str, kind: str = None, order_type: float = None): """ Request all open orders for a given currency. :param currency: (str) Deribit instrument's name :param kind: (str) Kind of instrument. Either 'future' or 'option' :param order_type: (str) Order type. Either market, limit, stop_limit, stop_market. :return: (dict) Message to be sent into the websocket. """ # Implement Deribit default behaviour if not order_type: order_type = ORDER_TYPE.ALL.value.lower() data = sanitize(currency=currency, kind=kind, type=order_type) # Build basic message msg = message(method=METHOD_OPEN_ORDERS_BY_CURRENCY) params = {key: value for (key, value) in data.items()} return add_params_to_message(params, msg)
def user_trades_by_currency(currency: str, kind: str = None, count: int = None, include_old: bool = False): """ Request all open user_trades for a given currency. :param currency: (str) Deribit instrument's name :param kind: (str) Kind of instrument. Either 'future' or 'option' :param count: (int) The number of old trades requested. :param include_old: (bool) Retrieve trades that more than 7 days old. :return: (dict) Message to be sent into the websocket. """ data = sanitize(currency=currency, kind=kind, count=count, include_old=include_old) # Build basic message msg = message(method=METHOD_USER_TRADES_BY_CURRENCY) params = {key: value for (key, value) in data.items()} return add_params_to_message(params, msg)
def cancel_all_by_currency(currency: str, kind: str = None, order_type: str = None): """ Generates a 'cancellation' message for Deribit API for ALL instruments in a given currency. :param currency: (str) Deribit instrument's name :param kind: (str) Kind of instrument. Either 'future' or 'option' :param order_type: (str) Order type. Either market, limit, stop_limit, stop_market. :return: (dict) Message to be sent into the websocket. """ # Implement Deribit default behaviour if not order_type: order_type = ORDER_TYPE.ALL.value.lower() data = sanitize(currency=currency, kind=kind, type=order_type) assert_cancellation_order_type(order_type=data["type"]) # Build basic message msg = message(method=METHOD_CANCEL_ALL_BY_CURRENCY) params = {key: value for (key, value) in data.items()} return add_params_to_message(params, msg)
def close(instrument: str, order_type: str = None, limit_price: float = None): """ Generates a 'sell' message for Deribit API. :param instrument: (str) Deribit instrument's name :param order_type: (str) Order type. Either market, limit, stop_limit, stop_market. :param limit_price: Limit price, required for LIMIT and STOP_LIMIT orders. :return: (dict) Message to be sent into the websocket. """ data = sanitize(instrument=instrument, type=order_type, limit_price=limit_price) # Asset the coherence of a limit order limit_price_ = None if "limit_price" not in data else data["limit_price"] assert_limit_order_coherence(order_type=data["type"], limit_price=limit_price_) # Build basic message msg = message(method=METHOD_CLOSE) params = {key: value for (key, value) in data.items()} return add_params_to_message(params, msg)
def buy(instrument: str, amount: float, order_type: str = None, label: str = None, limit_price: float = None, time_in_force: str = None, max_show: float = None, post_only: bool = False, reduce_only: bool = False, stop_price: float = None, trigger: str = None, vol_quote: bool = False): """ Generates a 'buy' message for Deribit API. :param instrument: (str) Deribit instrument's name :param amount: (float) Amount in USD for future or BTC (or ETH) for options :param order_type: (str) Order type. Either market, limit, stop_limit, stop_market. :param label: My own label / id for this message. :param limit_price: Limit price, required for LIMIT and STOP_LIMIT orders. :param time_in_force: (str) Time in force. Either GTC, FOK, IOC. :param max_show: Maximum amount to be shown to other customers. 0 is invisible order. :param post_only: (bool) If the new price would cause the order to be filled immediately (as taker), the price will be changed to be just below the bid. :param reduce_only: (bool The order is intended to only reduce a current position. :param stop_price: (float) Stop price, required for STOP_LIMIT orders. :param trigger: Type of trigger to determine STOP LIMIT. :param vol_quote: (bool) True to enter price in vol (e.g. 1.0 for 100%). False to quote in USD. :return: (dict) message to be dumped to the websocket. """ # Implement Deribit default behaviour if not order_type: order_type = ORDER_TYPE.LIMIT.value.lower() data = sanitize(instrument=instrument, amount=amount, type=order_type, label=label, limit_price=limit_price, time_in_force=time_in_force, max_show=max_show, post_only=post_only, reduce_only=reduce_only, stop_price=stop_price, trigger=trigger, advanced=vol_quote) # Asset the coherence of a limit order limit_price_ = None if "limit_price" not in data else data["limit_price"] assert_limit_order_coherence(order_type=data["type"], limit_price=limit_price_) # Asset the coherence of a stop order stop_price_ = None if "stop_price" not in data else data["stop_price"] assert_stop_order_coherence(order_type=data["type"], buy_order=True, data=data, stop_price=stop_price_, limit_price=limit_price_) # Build basic message msg = message(method=METHOD_BUY) params = {key: value for (key, value) in data.items()} return add_params_to_message(params, msg)
def get_position(instrument: str): data = sanitize(instrument=instrument) msg = message(method=METHOD_GET_POSITION) return add_params_to_message({"instrument_name": data["instrument"]}, msg)
def request_currencies(): msg = message(method=METHOD_CURRENCIES) return add_params_to_message({}, msg)
def test_with_exception(): msg = message(method=METHOD_TEST) return add_params_to_message(kvp_dict={"expected_result": "exception"}, message=msg)
def test(): msg = message(method=METHOD_TEST) return add_params_to_message(kvp_dict={}, message=msg)
def get_time(): msg = message(method=METHOD_GET_TIME) return add_params_to_message(kvp_dict={}, message=msg)
def disable_heartbeat_message(): msg = message(method=METHOD_SET_HEARTBEAT) return add_params_to_message(kvp_dict={}, message=msg)
def get_account_summary(currency: str, extended=True): data = sanitize(currency=currency) msg = message(method=METHOD_GET_SUMMARY) params = {"currency": data["currency"], "extended": extended} return add_params_to_message(params, msg)
def request_index(currency: str): # Sanitize input arguments data = sanitize(currency=currency) params = {"currency": data["currency"]} msg = message(method=METHOD_INDEX) return add_params_to_message(params, msg)
def login_message(self): msg = message(method=METHOD_LOGIN) auth_msg = self.auth_with_credentials(msg) return auth_msg
def get_all_positions(currency: str = None, kind: str = None): data = sanitize(currency=currency, kind=kind) msg = message(method=METHOD_GET_POSITIONS) params = {"currency": data["currency"], "kind": data["kind"]} return add_params_to_message(params, msg)
def logout_message(self): msg = message(method=METHOD_LOGOUT) return self.auth_with_credentials(msg)
def get_announcements(): msg = message(method=METHOD_GET_ANNOUNCEMENTS) return add_params_to_message({}, msg)
def set_heartbeat_message(): msg = message(method=METHOD_SET_HEARTBEAT) return add_params_to_message(kvp_dict={"interval": 30}, message=msg)