請(qǐng)記住,期權(quán)費(fèi)用不能超過(guò)期權(quán)價(jià)格的 12.5%。此外,期貨和期權(quán)合約有交割費(fèi),如下圖所示:

至于清算費(fèi)用,則如下:

Deribit 不收取任何存款費(fèi)用,而 BTC 的提款費(fèi)用可能取決于比特幣網(wǎng)絡(luò)的當(dāng)前狀態(tài)。

在為您的賬戶(hù)注資時(shí),您只有一個(gè)選擇,那就是比特幣。這意味著 Deribit 不支持任何法定貨幣存款方式。

Deribit 需要 KYC 嗎?

自2020年11月9日起,Deribit 要求所有新客戶(hù)獲得驗(yàn)證身份才能進(jìn)行交易。這意味著需要提供居住證明和身份證件。

我為什么要使用 Deribit?

為什么我不應(yīng)該使用 Deribit?

我的國(guó)家可以使用 Deribit 嗎?

自 2021 年起,Deribit 已無(wú)法訪(fǎng)問(wèn)

如果您位于、注冊(cè)或以其他方式設(shè)立于下列國(guó)家/地區(qū),或者是下列國(guó)家/地區(qū)的公民或居民:

使用 Deribit 有哪些替代方案?

Deribit 可以用其他更適合您需求的應(yīng)用程序替代。以下是列表:

哪些客戶(hù)端可用于 Deribit API?

Deribit 擁有以下客戶(hù):

請(qǐng)務(wù)必訪(fǎng)問(wèn)以下 GitHub 來(lái)訪(fǎng)問(wèn)這些客戶(hù)端:

https://github.com/deribit/deribit-api-clients

如何開(kāi)始使用 Deribit?

為了充分了解 Deribit API 的功能,我們首先需要開(kāi)設(shè)一個(gè)帳戶(hù)以獲取 API 密鑰。我將向您展示幾個(gè)交易場(chǎng)景,我們需要的帳戶(hù)是此鏈接上的測(cè)試帳戶(hù):

https://test.deribit.com/

測(cè)試版和正式版 Deribit 賬戶(hù)的注冊(cè)流程基本相同。以下是正式版網(wǎng)站的鏈接:

https://www.deribit.com/

到達(dá)那里后,你會(huì)看到一個(gè)帳戶(hù)創(chuàng)建框。請(qǐng)務(wù)必點(diǎn)擊綠色的“創(chuàng)建帳戶(hù)”按鈕,然后填寫(xiě)你的電子郵件地址、昵稱(chēng)、創(chuàng)建密碼并選擇你的國(guó)家/地區(qū)。

之后,您將收到來(lái)自 Deribit 的驗(yàn)證電子郵件。請(qǐng)務(wù)必仔細(xì)檢查并確認(rèn)。完成后,您將進(jìn)入以下屏幕:

現(xiàn)在,我們需要的是 API 密鑰。為此,請(qǐng)轉(zhuǎn)到右上角的個(gè)人資料圖標(biāo),然后單擊“設(shè)置”。之后,導(dǎo)航到 API 部分并單擊“添加新密鑰”按鈕。

會(huì)出現(xiàn)一個(gè)彈出屏幕,提示您分配新 API 密鑰的主要功能。選項(xiàng)范圍從帳戶(hù)到托管權(quán)限。在本文中,我將為它們?nèi)抠x予讀取 + 寫(xiě)入權(quán)限。

批準(zhǔn)后,將創(chuàng)建 API 密鑰,您將清楚地看到您的客戶(hù)端 ID 和客戶(hù)端密鑰。您還可以執(zhí)行多種操作,例如二維碼、重置密鑰、刪除密鑰等。

由于我們需要的一切都已設(shè)置好,以下標(biāo)題將從主要公共端點(diǎn)開(kāi)始探索 API,并以?xún)蓚€(gè)交易場(chǎng)景中的私有 API 順序結(jié)束。

如何使用 Deribit 獲取貨幣信息?

為了獲取 Deribit 提供的所有貨幣,您可以使用 get_currencies 端點(diǎn)。我們將導(dǎo)入所需的庫(kù)并以以下方式調(diào)用其 API:

import asyncio
import websockets
import json
import pandas as pd
import pprint
import nest_asyncio
nest_asyncio.apply()

msg = \
{
"jsonrpc" : "2.0",
"id" : 7538,
"method" : "public/get_currencies",
"params" : {

}
}

async def call_api(msg):
async with websockets.connect('wss://test.deribit.com/ws/api/v2') as websocket:
await websocket.send(msg)
while websocket.open:
response = await websocket.recv()
json_par = json.loads(response)
print(json_par)
return(json_par)

response = asyncio.get_event_loop().run_until_complete(call_api(json.dumps(msg)))

由于響應(yīng)看起來(lái)不太好,我們將使用 pprint 庫(kù)來(lái)漂亮地打印它。

pprint.pprint(response)

更好的是,讓我們?cè)诤瘮?shù)中添加 pprint 作為我們的主要打印方式,并調(diào)用下一個(gè)將獲取價(jià)格數(shù)據(jù)的端點(diǎn)。

如何獲取 Deribit 上的價(jià)格數(shù)據(jù)?

有多個(gè) Deribit 端點(diǎn)包含價(jià)格數(shù)據(jù),但最“精確”的是公共行情端點(diǎn)。讓我們更新該函數(shù)以獲得更漂亮的打印效果。

msg = {
"jsonrpc" : "2.0",
"id" : 8106,
"method" : "public/ticker",
"params" : {
"instrument_name" : "BTC-PERPETUAL"
}
}

async def call_api(msg):
async with websockets.connect('wss://test.deribit.com/ws/api/v2') as websocket:
await websocket.send(msg)
while websocket.open:
response = await websocket.recv()
json_par = json.loads(response)
pprint.pprint(json_par)
return(json_par)

response = asyncio.get_event_loop().run_until_complete(call_api(json.dumps(msg)))

如果您想獲取指數(shù)價(jià)格,您可以使用“get_index_price”端點(diǎn)。

msg = {"jsonrpc": "2.0",
"method": "public/get_index_price",
"id": 42,
"params": {
"index_name": "btc_usd"}
}

response = asyncio.get_event_loop().run_until_complete(call_api(json.dumps(msg)))
{'id': 42,
'jsonrpc': '2.0',
'result': {'estimated_delivery_price': 57848.15, 'index price': 57848.15},
'testnet': True,
'usDiff': 177,
'usIn': 1616071078267979,
'usOut': 16160710782681561}

如何使用 Deribit 獲取歷史數(shù)據(jù)?

Deribit 提供了一些歷史數(shù)據(jù)端點(diǎn),例如資金利率歷史和歷史波動(dòng)率。讓我們從后者開(kāi)始,獲取這兩個(gè)數(shù)據(jù):

msg = {
"jsonrpc" : "2.0",
"id" : 8387,
"method" : "public/get_historical_volatility",
"params" : {
"currency" : "BTC"
}
}

response = asyncio.get_event_loop().run_until_complete(call_api(json.dumps(msg)))

我們要做的下一步是將響應(yīng)放入 pandas 數(shù)據(jù)框中,以便以后使用。

volatility = pd.DataFrame(response['result'])
volatility.head()

為了獲取有關(guān)數(shù)據(jù)框的一些基本統(tǒng)計(jì)數(shù)據(jù),您可以使用 pd.describe 函數(shù):

pd.set_option('precision', 3)
volatility.describe()

現(xiàn)在來(lái)看看融資利率的歷史:

msg = {
"jsonrpc" : "2.0",
"id" : 7617,
"method" : "public/get_funding_rate_history",
"params" : {
"instrument_name" : "BTC-PERPETUAL",
"start_timestamp" : 1569888000000,
"end_timestamp" : 1569902400000
}
}

response = asyncio.get_event_loop().run_until_complete(call_api(json.dumps(msg)))

如何使用 Deribit 獲取訂單簿數(shù)據(jù)?

訂單簿數(shù)據(jù)可以通過(guò)使用公共 get_order_book 端點(diǎn)獲取。交易所未指定訂單簿的最大層級(jí)深度。我們將使用層級(jí) 25 作為請(qǐng)求:

msg = {
"jsonrpc" : "2.0",
"id" : 8772,
"method" : "public/get_order_book",
"params" : {
"instrument_name" : "BTC-PERPETUAL",
"depth" : 25
}
}

response = asyncio.get_event_loop().run_until_complete(call_api(json.dumps(msg)))

我們現(xiàn)在應(yīng)該通過(guò)提取其中的重要部分來(lái)清理響應(yīng)。為此,我將為買(mǎi)入價(jià)和賣(mài)出價(jià)創(chuàng)建單獨(dú)的數(shù)據(jù)框,然后將兩者合并在一起。

bids = pd.DataFrame(response['result']['bids'])
asks = pd.DataFrame(response['result']['asks'])

book = pd.merge(bids, asks, left_index=True, right_index=True)
book.head()

如果您想重命名列,您可以傳遞以下命令:

book = df.rename({"0_x":"Bid Price","1_x":"Bid Amount",
"0_y":"Ask Price","1_y":"Ask Amount"}, axis='columns')

如何與 Deribit 進(jìn)行交易?

交易(又稱(chēng)交易)可通過(guò)使用按貨幣或按工具獲取交易端點(diǎn)從 Deribit 獲取。對(duì)于結(jié)算,您只需在請(qǐng)求消息中將“交易”更改為“結(jié)算”。

讓我們從前者開(kāi)始,按貨幣獲取交易和結(jié)算:

msg = {
"jsonrpc" : "2.0",
"id" : 9290,
"method" : "public/get_last_trades_by_currency",
"params" : {
"currency" : "BTC",
"count" : 2
}
}

response = asyncio.get_event_loop().run_until_complete(call_api(json.dumps(msg)))

現(xiàn)在談?wù)劧ň狱c(diǎn):

msg = {
"jsonrpc" : "2.0",
"id" : 4497,
"method" : "public/get_last_settlements_by_currency",
"params" : {
"currency" : "BTC",
"type" : "delivery",
"count" : 2
}
}

response = asyncio.get_event_loop().run_until_complete(call_api(json.dumps(msg)))

如何使用 Deribit 訂購(gòu)?

Deribit 提供多種可通過(guò)其端點(diǎn)啟動(dòng)的訂單類(lèi)型。但在我們逐一介紹每種訂單類(lèi)型并編寫(xiě)兩個(gè)交易方案之前,我們應(yīng)該先了解一下訂單由哪些參數(shù)組成。

基本訂單參數(shù)如下:

現(xiàn)在我們已經(jīng)了解了主要的訂單構(gòu)建模塊,接下來(lái)讓我們探索一些特殊的訂單構(gòu)建模塊以及如何啟動(dòng)它們。

如何使用 Deribit 來(lái)下單永久合約?

Deribit 中的所有訂單都具有類(lèi)似的結(jié)構(gòu),如最后一個(gè)標(biāo)題所示。這意味著,為了觸發(fā)永續(xù)合約的訂單,我們只需指定正確的工具名稱(chēng)(例如 BTC-PERPETUAL)。

訂單信息的示例如下:

msg = {
"jsonrpc" : "2.0",
"id" : 4122,
"method" : "private/cancel_all_by_instrument",
"params" : {
"instrument_name" : "BTC-PERPETUAL",
"type" : "all"
}
}

如果您跳過(guò)此部分,這是我們用來(lái)發(fā)送請(qǐng)求的函數(shù):

async def call_api(msg):
async with websockets.connect('wss://test.deribit.com/ws/api/v2') as websocket:
###############
# Before sending a message, make sure that your connection
# is authenticated (use public/auth call before)
###############
await websocket.send(msg)
while websocket.open:
response = await websocket.recv()
json_par = json.loads(response)
print(json_par)
return(json_par)

Deribit 永續(xù)合約的特點(diǎn)是持續(xù)測(cè)量合約標(biāo)記價(jià)格與 Deribit BTC 指數(shù)之間的差額。

這兩個(gè)價(jià)格水平之間的百分比差異是適用于所有未平倉(cāng)永續(xù)合約的 8 小時(shí)融資利率的基礎(chǔ)。有關(guān)更多信息,請(qǐng)?jiān)L問(wèn)以下鏈接:

https://www.deribit.com/pages/docs/perpetual

如何使用 Deribit 來(lái)下達(dá)期貨合約訂單?

Deribit 中的所有訂單都具有類(lèi)似的結(jié)構(gòu)。這意味著,為了觸發(fā)期貨合約的訂單,我們只需指定正確的工具名稱(chēng)(例如 BTC-24SEP21)。

訂單信息的示例如下:

msg = {
"jsonrpc" : "2.0",
"id" : 4122,
"method" : "private/cancel_all_by_instrument",
"params" : {
"instrument_name" : "BTC-24SEP21",
"type" : "all"
}
}

Deribit 上的比特幣期貨以現(xiàn)金結(jié)算,而非通過(guò)“實(shí)物”交割 BTC 進(jìn)行結(jié)算。這意味著在結(jié)算時(shí),BTC 期貨的買(mǎi)家不會(huì)購(gòu)買(mǎi)實(shí)際的 BTC,賣(mài)家也不會(huì)出售 BTC。

只有在合約結(jié)算時(shí)才會(huì)根據(jù)到期價(jià)格(以 BTC 價(jià)格指數(shù)的最近 30 分鐘平均值計(jì)算)轉(zhuǎn)移損失/收益。有關(guān)更多信息,請(qǐng)?jiān)L問(wèn)以下鏈接:

https://www.deribit.com/pages/docs/futures

如何使用 Deribit 來(lái)下達(dá)期權(quán)合約的訂單?

Deribit 中的所有訂單都具有類(lèi)似的結(jié)構(gòu)。這意味著,為了觸發(fā)期貨合約的訂單,我們只需指定正確的工具名稱(chēng)(例如 ETH-19MAR21)。

訂單信息的示例如下:

msg = {
"jsonrpc" : "2.0",
"id" : 4122,
"method" : "private/cancel_all_by_instrument",
"params" : {
"instrument_name" : "ETH-19MAR21",
"type" : "all"
}
}

Deribit 遵循歐式現(xiàn)金結(jié)算期權(quán),這意味著期權(quán)僅在到期時(shí)行使,不能提前行使。在 Deribit 上,這將自動(dòng)發(fā)生。

更多信息請(qǐng)?jiān)L問(wèn)以下鏈接:

https://www.deribit.com/pages/docs/options

當(dāng) BTC 達(dá)到某個(gè)價(jià)格時(shí),如何使用 Deribit API 對(duì) ETH 執(zhí)行交易?

在此示例中,我將向您展示如何正確安全地啟動(dòng)具有指定要求的訂單。我們想要做的是當(dāng) BTC 達(dá)到特定價(jià)格時(shí)啟動(dòng) ETH 交易。

為了實(shí)現(xiàn)這一點(diǎn),我們需要設(shè)置訂單基礎(chǔ),然后創(chuàng)建一個(gè)循環(huán)來(lái)檢查價(jià)格水平是否被觸及。如果價(jià)格被觸及,我們將執(zhí)行市價(jià)訂單。否則,我們將繼續(xù)循環(huán)。

當(dāng)價(jià)格執(zhí)行后,我們會(huì)等待幾秒鐘,然后檢查訂單是否已完成。這一額外步驟對(duì)于您的交易策略非常重要,因?yàn)榻灰姿?wù)器可能會(huì)遇到一些問(wèn)題。

讓我們繼續(xù)導(dǎo)入相關(guān)庫(kù)并設(shè)置主交易循環(huán)。

#Order Foundation
import asyncio
import websockets
import json
import pandas as pd
from time import sleep
import pprint
import hmac
import hashlib
from datetime import datetime
import nest_asyncio
nest_asyncio.apply()

async def call_api(msg):
async with websockets.connect('wss://test.deribit.com/ws/api/v2') as websocket:
await websocket.send(msg)
while websocket.open:
response = await websocket.recv()
json_par = json.loads(response)
#print(json_par)
return(json_par)

clientId = "TzxHdA_N"
clientSecret = "3LsbOcO7Fqzv_oT9-RDy1JwvYG7uR3NnF5HXDnvn6AA"
timestamp = round(datetime.now().timestamp() * 1000)
nonce = "123"
data = ""

signature = hmac.new(
bytes(clientSecret, "latin-1"),
msg=bytes('{}\n{}\n{}'.format(timestamp, nonce, data), "latin-1"),
digestmod=hashlib.sha256).hexdigest().lower()

auth_msg = {
"jsonrpc" : "2.0",
"id" : 42,
"method" : "public/auth",
"params" : {
"grant_type" : "client_signature",
"client_id" : clientId,
"timestamp" : timestamp,
"signature" : signature,
"nonce" : nonce,
"data" : data
}
}

price_msg = {
"jsonrpc" : "2.0",
"id" : 42,
"method" : "public/ticker",
"params" : {
"instrument_name" : "BTC-PERPETUAL"
}
}

order_msg = {
"jsonrpc" : "2.0",
"id" : 42,
"method" : "private/buy",
"params" : {
"instrument_name" : "ETH-PERPETUAL",
"amount" : 5,
"type" : "market",
"label" : "algoorder"
}
}

check_order = {
"jsonrpc" : "2.0",
"id" : 42,
"method" : "private/get_order_state",
"params" : {
"order_id" : "ETH-331562"
}
}

#Main order loop
while True:
try:
btc = asyncio.get_event_loop().run_until_complete(call_api(json.dumps(price_msg)))
except exception as e:
print("Unable to obtain BTC price")

if btc['result']['best_ask_price'] < 58800.0:
print("The price mark was not reached.")
sleep(60)
continue

elif btc['result']['best_ask_price'] >= 58800.0:
try:
auth = asyncio.get_event_loop().run_until_complete(call_api(json.dumps(auth_msg)))
pprint.pprint(auth)
except exception as e:
print('There was an authentication error')

try:
order = asyncio.get_event_loop().run_until_complete(call_api(json.dumps(order_msg)))
#print(order)
except exception as e:
print("Error occurred while placing order")

sleep(2)

try:
order_check = asyncio.get_event_loop().run_until_complete(call_api(json.dumps(check_order)))
pprint.pprint(order_check)
except excpetion as e:
print("Error checking order")

if order_check['result']['order_state'] == 'filled' or order_check['result']['order_state'] == "open":
print ('Order placed at {}'.format(pd.Timestamp.now()))
break

else:
print('Order was canceled {}'.format(pd.Timestamp.now()))
break

當(dāng) BTC 在過(guò)去 5 分鐘內(nèi)變動(dòng) 5% 時(shí),如何使用 Deribit API 執(zhí)行 ETH 交易?

如果我們想計(jì)算兩種貨幣之間的百分比變動(dòng),然后下訂單,該怎么辦?好吧,這個(gè)例子將解決這個(gè)問(wèn)題!

主要任務(wù)是當(dāng) BTC 在過(guò)去 5 分鐘內(nèi)波動(dòng) 5% 時(shí)執(zhí)行 ETH 交易。這意味著我們需要?jiǎng)?chuàng)建一個(gè)循環(huán)來(lái)獲取兩種加密貨幣的價(jià)格并計(jì)算兩者之間的百分比變化。

如果百分比變化小于 5%,算法將休眠 5 分鐘并再次計(jì)算百分比變化。如果百分比變化等于或大于 5%,則交易將執(zhí)行。

交易執(zhí)行后,我們將休眠幾秒鐘,然后檢查交易是否已完成?,F(xiàn)在邏輯已經(jīng)設(shè)置好,是時(shí)候編寫(xiě)代碼了:

clientId = "ID_HERE"
clientSecret = "SECRET_HERE"
timestamp = round(datetime.now().timestamp() * 1000)
nonce = "123"
data = ""

signature = hmac.new(
bytes(clientSecret, "latin-1"),
msg=bytes('{}\n{}\n{}'.format(timestamp, nonce, data), "latin-1"),
digestmod=hashlib.sha256).hexdigest().lower()

auth_msg = {
"jsonrpc" : "2.0",
"id" : 42,
"method" : "public/auth",
"params" : {
"grant_type" : "client_signature",
"client_id" : clientId,
"timestamp" : timestamp,
"signature" : signature,
"nonce" : nonce,
"data" : data
}
}

btc_price_msg = {
"jsonrpc" : "2.0",
"id" : 42,
"method" : "public/ticker",
"params" : {
"instrument_name" : "BTC-PERPETUAL"
}
}

order_msg = {
"jsonrpc" : "2.0",
"id" : 42,
"method" : "private/buy",
"params" : {
"instrument_name" : "ETH-PERPETUAL",
"amount" : 5,
"type" : "market",
"label" : "algoorder"
}
}

check_order = {
"jsonrpc" : "2.0",
"id" : 42,
"method" : "private/get_order_state",
"params" : {
"order_id" : "ETH-331562"
}
}

while True:
try:
btc_old = asyncio.get_event_loop().run_until_complete(call_api(json.dumps(btc_price_msg)))
except exception as e:
print("Unable to obtain BTC price")

sleep(300)

try:
btc_new = asyncio.get_event_loop().run_until_complete(call_api(json.dumps(btc_price_msg)))
except exception as e:
print("Unable to obtain BTC price")

percent = (btc_new['result']['best_ask_price'] - btc_old['result']['best_ask_price'] * 100) / btc_old['result']['best_ask_price']

if percent < 5:
print("The requirement was not fulfilled.")
sleep(0.1)
continue

elif percent >= 5:
try:
auth = asyncio.get_event_loop().run_until_complete(call_api(json.dumps(auth_msg)))
pprint.pprint(auth)
except exception as e:
print('There was an authentication error')

try:
order = asyncio.get_event_loop().run_until_complete(call_api(json.dumps(order_msg)))
#print(order)
except exception as e:
print("Error occurred while placing order")

sleep(2)

try:
order_check = asyncio.get_event_loop().run_until_complete(call_api(json.dumps(check_order)))
pprint.pprint(order_check)
except excpetion as e:
print("Error checking order")

if order_check['result']['order_state'] == 'filled' or order_check['result']['order_state'] == "open":
print ('Order placed at {}'.format(pd.Timestamp.now()))
break

else:
print('Order was canceled {}'.format(pd.Timestamp.now()))
break

如何取消 Deribit 的訂單?

如果您想取消并使用 Deribit 下訂單,您可以傳遞類(lèi)似以下請(qǐng)求:

msg = \
{
"jsonrpc" : "2.0",
"id" : 4214,
"method" : "private/cancel",
"params" : {
"order_id" : "ETH-SLIS-12"
}
}

async def call_api(msg):
async with websockets.connect('wss://test.deribit.com/ws/api/v2') as websocket:
###############
# Before sending the message, make sure that your connection
# is authenticated (use public/auth call before)
###############
await websocket.send(msg)
while websocket.open:
response = await websocket.recv()
# do something with the response...
print(response)

asyncio.get_event_loop().run_until_complete(call_api(json.dumps(msg)))

上一篇:

Python字典(dict)完全指南

下一篇:

DeepSpeed-Chat 代碼分析
#你可能也喜歡這些API文章!

我們有何不同?

API服務(wù)商零注冊(cè)

多API并行試用

數(shù)據(jù)驅(qū)動(dòng)選型,提升決策效率

查看全部API→
??

熱門(mén)場(chǎng)景實(shí)測(cè),選對(duì)API

#AI文本生成大模型API

對(duì)比大模型API的內(nèi)容創(chuàng)意新穎性、情感共鳴力、商業(yè)轉(zhuǎn)化潛力

25個(gè)渠道
一鍵對(duì)比試用API 限時(shí)免費(fèi)

#AI深度推理大模型API

對(duì)比大模型API的邏輯推理準(zhǔn)確性、分析深度、可視化建議合理性

10個(gè)渠道
一鍵對(duì)比試用API 限時(shí)免費(fèi)