Ejemplo n.º 1
0
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)
Ejemplo n.º 2
0
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)
Ejemplo n.º 3
0
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)
Ejemplo n.º 4
0
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)
Ejemplo n.º 5
0
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)
Ejemplo n.º 6
0
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)
Ejemplo n.º 7
0
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)
Ejemplo n.º 8
0
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)
Ejemplo n.º 9
0
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)
Ejemplo n.º 10
0
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)
Ejemplo n.º 11
0
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)
Ejemplo n.º 12
0
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)
Ejemplo n.º 13
0
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)