Skip to content
Loren1166 edited this page Sep 23, 2024 · 4 revisions

dYdX

info 信息

We are currently working on this integration guide.

我们目前正在编写此集成指南。

dYdX is one of the largest decentralized cryptocurrency exchanges in terms of daily trading volume for crypto derivative products. dYdX runs on smart contracts on the Ethereum blockchain, and allows users to trade with no intermediaries. This integration supports live market data ingestion and order execution with dYdX v4, which is the first version of the protocol to be fully decentralized with no central components.

dYdX 是加密衍生品日交易量最大的去中心化加密货币交易所之一。dYdX 运行在以太坊区块链上的智能合约上,允许用户在没有中介的情况下进行交易。此集成支持 dYdX v4 的实时市场数据获取和订单执行,dYdX v4 是第一个完全去中心化且没有中央组件的协议版本。

Installation 安装

To install the latest nautilus-trader package along with the dydx dependencies using pip, execute:

要使用 pip 安装最新的 nautilus-trader 包以及 dydx 依赖项,请执行以下操作:

pip install -U "nautilus_trader[dydx]"

For installation via poetry, use:

要通过 poetry 安装,请使用以下命令:

poetry add "nautilus_trader[dydx]"

Overview 概述

The following documentation assumes a trader is setting up for both live market data feeds, and trade execution. The full dYdX integration consists of an assortment of components, which can be used together or separately depending on the user's needs.

以下文档假定交易者正在设置实时市场数据馈送和交易执行。完整的 dYdX 集成由各种组件组成,可以根据用户的需求一起使用或单独使用。

  • DYDXHttpClient: Low-level HTTP API connectivity. DYDXHttpClient:低级 HTTP API 连接。
  • DYDXWebSocketClient: Low-level WebSocket API connectivity. DYDXWebSocketClient:低级 WebSocket API 连接。
  • DYDXAccountGRPCAPI: Low-level gRPC API connectivity for account updates. DYDXAccountGRPCAPI:用于账户更新的低级 gRPC API 连接。
  • DYDXInstrumentProvider: Instrument parsing and loading functionality. DYDXInstrumentProvider:Instrument解析和加载功能。
  • DYDXDataClient: A market data feed manager. DYDXDataClient:市场数据馈送管理器。
  • DYDXExecutionClient: An account management and trade execution gateway. DYDXExecutionClient:账户管理和交易执行网关。
  • DYDXLiveDataClientFactory: Factory for dYdX data clients (used by the trading node builder). DYDXLiveDataClientFactory:dYdX 数据客户端的工厂(由交易节点构建器使用)。
  • DYDXLiveExecClientFactory: Factory for dYdX execution clients (used by the trading node builder). DYDXLiveExecClientFactory:dYdX 执行客户端的工厂(由交易节点构建器使用)。

note 注意

Most users will simply define a configuration for a live trading node (as below), and won't need to necessarily work with these lower level components directly.

大多数用户只需为实时交易节点定义一个配置(如下所示),而无需直接使用这些较低级别的组件。

Symbology 符号

Only perpetual contracts are available on dYdX. To be consistent with other adapters and to be futureproof in case other products become available on dYdX, NautilusTrader appends -PERP for all available perpetual symbols. For example, the Bitcoin/USD-C perpetual futures contract is identified as BTC-USD-PERP. The quote currency for all markets is USD-C. Therefore, dYdX abbreviates it to USD.

dYdX 上仅提供永续合约。为了与其他适配器保持一致,并在 dYdX 上提供其他产品的情况下保持前瞻性,NautilusTrader 为所有可用的永续符号附加 -PERP。例如,比特币/USD-C 永续期货合约被标识为 BTC-USD-PERP。所有市场的报价货币都是 USD-C。因此,dYdX 将其缩写为 USD。

Order types 订单类型

dYdX offers a flexible combination of trigger types, enabling a broader range of Nautilus orders. However, the execution engine currently only supports submitting market and limit orders. Stop orders and trailing stop orders can be implemented later.

dYdX 提供灵活的触发类型组合,支持更广泛的 Nautilus 订单。但是,执行引擎目前仅支持提交市价单和限价单。止损单和追踪止损单可以在以后实现。

Short-term and long-term orders 短期订单和长期订单

dYdX makes a distinction between short-term orders and long-term orders (or stateful orders). Short-term orders are meant to be placed immediately and belongs in the same block the order was received. These orders stay in-memory up to 20 blocks, with only their fill amount and expiry block height being committed to state. Short-term orders are mainly intended for use by market makers with high throughput or for market orders.

dYdX 区分短期订单和长期订单(或有状态订单)。短期订单旨在立即下达,并且属于接收订单的同一区块。这些订单在内存中最多保留 20 个区块,只有它们的成交量和到期区块高度被提交到状态。短期订单主要供具有高吞吐量的做市商或市价单使用。

By default, all orders are sent as short-term orders. To construct long-term orders, you can attach a tag to an order like this:

默认情况下,所有订单都作为短期订单发送。要构建长期订单,您可以像这样将标签附加到订单:

from nautilus_trader.adapters.dydx.common.common import DYDXOrderTags

order: LimitOrder = self.order_factory.limit(
    instrument_id=self.instrument_id,
    order_side=OrderSide.BUY,
    quantity=self.instrument.make_qty(self.trade_size),
    price=self.instrument.make_price(price),
    time_in_force=TimeInForce.GTD,
    expire_time=self.clock.utc_now() + pd.Timedelta(minutes=10),
    post_only=True,
    emulation_trigger=self.emulation_trigger,
    tags=[DYDXOrderTags(is_short_term_order=False).value],
)

To specify the number of blocks that an order is active:

要指定订单处于活动状态的块数:

from nautilus_trader.adapters.dydx.common.common import DYDXOrderTags

order: LimitOrder = self.order_factory.limit(
    instrument_id=self.instrument_id,
    order_side=OrderSide.BUY,
    quantity=self.instrument.make_qty(self.trade_size),
    price=self.instrument.make_price(price),
    time_in_force=TimeInForce.GTD,
    expire_time=self.clock.utc_now() + pd.Timedelta(seconds=5),
    post_only=True,
    emulation_trigger=self.emulation_trigger,
    tags=[DYDXOrderTags(is_short_term_order=True, num_blocks_open=5).value],
)

Configuration 配置

The product types for each client must be specified in the configurations.

每个客户端的产品类型必须在配置中指定。

Execution clients 执行客户端

The account type must be a margin account to trade the perpetual futures contracts.

账户类型必须是保证金账户才能交易永续期货合约。

The most common use case is to configure a live TradingNode to include dYdX data and execution clients. To achieve this, add a DYDX section to your client configuration(s):

最常见的用例是配置实时 TradingNode 以包含 dYdX 数据和执行客户端。为此,请将 DYDX 部分添加到您的客户端配置中:

from nautilus_trader.live.node import TradingNode

config = TradingNodeConfig(
    ...,  # Omitted
    data_clients={
        "DYDX": {
            "wallet_address": "YOUR_DYDX_WALLET_ADDRESS",
            "is_testnet": False,
        },
    },
    exec_clients={
        "DYDX": {
            "wallet_address": "YOUR_DYDX_WALLET_ADDRESS",
            "subaccount": "YOUR_DYDX_SUBACCOUNT_NUMBER",
            "mnemonic": "YOUR_MNEMONIC",
            "is_testnet": False,
        },
    },
)

Then, create a TradingNode and add the client factories:

然后,创建一个 TradingNode 并添加客户端工厂:

from nautilus_trader.adapters.dydx.factories import DYDXLiveDataClientFactory
from nautilus_trader.adapters.dydx.factories import DYDXLiveExecClientFactory
from nautilus_trader.live.node import TradingNode

# Instantiate the live trading node with a configuration
# 使用配置实例化实时交易节点
node = TradingNode(config=config)

# Register the client factories with the node
# 向节点注册客户端工厂
node.add_data_client_factory("DYDX", DYDXLiveDataClientFactory)
node.add_exec_client_factory("DYDX", DYDXLiveExecClientFactory)

# Finally build the node
# 最后构建节点
node.build()

API credentials API 凭证

There are two options for supplying your credentials to the dYdX clients. Either pass the corresponding wallet_address and mnemonic values to the configuration objects, or set the following environment variables:

向 dYdX 客户端提供凭据有两种选择。将相应的 wallet_addressmnemonic 值传递给配置对象,或者设置以下环境变量:

For dYdX live clients, you can set:

对于 dYdX 实时客户端,您可以设置:

  • DYDX_WALLET_ADDRESS
  • DYDX_MNEMONIC

For dYdX testnet clients, you can set:

对于 dYdX 测试网客户端,您可以设置:

  • DYDX_TESTNET_WALLET_ADDRESS
  • DYDX_TESTNET_MNEMONIC

The data client is using the wallet address to determine the trading fees. The trading fees are used during back tests only.

数据客户端使用钱包地址来确定交易费用。交易费用仅在回测期间使用。

Testnets 测试网

It's also possible to configure one or both clients to connect to the dYdX testnet. Simply set the is_testnet option to True (this is False by default):

也可以将一个或两个客户端配置为连接到 dYdX 测试网。只需将 is_testnet 选项设置为 True(默认情况下为 False):

config = TradingNodeConfig(
    ...,  # Omitted
    data_clients={
        "DYDX": {
            "wallet_address": "YOUR_DYDX_WALLET_ADDRESS",
            "is_testnet": True,
        },
    },
    exec_clients={
        "DYDX": {
            "wallet_address": "YOUR_DYDX_WALLET_ADDRESS",
            "subaccount": "YOUR_DYDX_SUBACCOUNT_NUMBER",
            "mnemonic": "YOUR_MNEMONIC",
            "is_testnet": True,
        },
    },
)

Parser warnings 解析器警告

Some dYdX instruments are unable to be parsed into Nautilus objects if they contain enormous field values beyond what can be handled by the platform. In these cases, a warn and continue approach is taken (the instrument will not be available).

如果某些 dYdX Instrument包含平台无法处理的巨大字段值,则无法将其解析为 Nautilus 对象。在这些情况下,将采用警告并继续的方法(该Instrument将不可用)。

Order books 订单簿

Order books can be maintained at full depth or top-of-book quotes depending on the subscription. The venue does not provide quote ticks, but the adapter subscribes to order book deltas and sends new quote ticks to the DataEngine when there is a top-of-book price or size change.

订单簿可以根据订阅维护完整深度或最佳报价。交易平台不提供报价,但适配器订阅订单簿增量,并在最佳报价或规模发生变化时向 DataEngine 发送新的报价。

文档 (Documentation)
入门 (Getting Started)
概念 (Concepts)
教程 (Tutorials)
集成 (Integrations)
Python API
Rust API[未翻译]
开发者指南 (Developer Guide)
Clone this wiki locally