All
Lọc theo:
Tôi có thể nạp tiền mặt vào tài khoản của mình bằng cách nào?
Tôi cần trợ giúp xác minh tài khoản
Tại sao tôi không thể truy cập vào tài khoản của mình?
Có phí rút tiền điện tử không?
Tôi cần trợ giúp để đăng nhập vào tài khoản của tôi
Các điểm cuối được xác thực của API REST của chúng tôi có thể truy cập thông qua một yêu cầu HTTP đơn giản (giống như cách một trang web được yêu cầu qua trình duyệt web), vì vậy một ứng dụng khách API để nhập dữ liệu thị trường vào Google Sheet có thể được triển khai chỉ với vài dòng mã Google Script.
Tạo một Google Sheet mới hoặc mở một trang tính hiện có.
Mở trình chỉnh sửa tập lệnh thông qua menu Tiện ích mở rộng -> Apps Script.
Xóa mã mặc định được hiển thị (ví dụ: Chọn tất cả rồi Xóa/Backspace).
Sao chép/Dán mã API Google Script (hiển thị bên dưới) vào trình chỉnh sửa tập lệnh.
Cập nhật khóa API ví dụ để sử dụng khóa API từ tài khoản Kraken của bạn.
Tùy chọn - Thêm bất kỳ hàm tùy chỉnh bổ sung nào bạn yêu cầu (ví dụ: để gọi các điểm cuối khác nhau hoặc trả về các trường JSON khác nhau).
Lưu mã Google Script thông qua
biểu tượng (Lưu dự án).


Hàm KAPI_Private() chịu trách nhiệm tạo URL thích hợp, tính toán chữ ký xác thực và thực hiện yêu cầu HTTP tới API. Hàm KAPI_Private() có thể được gọi trực tiếp bằng cách nhập nội dung sau (hoặc tương tự tùy thuộc vào ID tài khoản, điểm cuối và các tham số) vào một ô trong Google Sheet:
Bash
=KAPI_Private("TEST", "Balance", "")
=KAPI_Private("TEST", "TradeBalance", "asset=xdg")
=KAPI_Private("TEST", "OpenOrders", "userref=886794735")Hàm KAPI_Private() trả về phản hồi JSON gốc từ API, chẳng hạn như sau cho ví dụ điểm cuối Số dư ở trên:
Bash
{"error":[],"result":{"ZUSD":"16.4272","ZEUR":"0.3880","ZJPY":"0.45","KFEE":"10368.39","XXBT":"0.0000000072","XXRP":"0.00000000","XLTC":"0.0000000100","XXDG":"13997.00000000","XXLM":"100.00000000"}}Để đặt các trường/giá trị riêng lẻ vào các ô riêng biệt trong Google Sheet, có thể gọi các hàm tùy chỉnh bổ sung để phân tích cú pháp phản hồi JSON, chẳng hạn như các hàm ví dụ KAPI_Balance() và KAPI_OpenOrders():
=KAPI_Balance("TEST")
=KAPI_OpenOrders("TEST")
Bash
=KAPI_Balance("TEST")
=KAPI_OpenOrders("TEST")Điều này sẽ hiển thị kết quả tương tự như sau, trong đó dữ liệu sẽ có thể truy cập được bằng bất kỳ hàm Google Trang tính tiêu chuẩn nào (SUM, AVERAGE, COUNT, v.v.):

Thuật toán xác thực:
Bash
API_Public_Key = {
'TEST': 'INSERT YOUR API PUBLIC KEY'
}
API_Private_Key = {
'TEST': 'INSERT YOUR API PRIVATE KEY'
}
function KAPI_Private(acc_id, endpoint, parameters) {
Utilities.sleep(Math.random() * 100)
api_key = API_Public_Key[acc_id]
api_secret = Utilities.base64Decode(API_Private_Key[acc_id])
api_path = Utilities.newBlob('/0/private/' + endpoint).getBytes()
api_nonce = Date.now().toString()
api_post = 'nonce=' + api_nonce + '&' + parameters
api_sha256 = Utilities.computeDigest(
Utilities.DigestAlgorithm.SHA_256,
api_nonce + api_post
)
api_hmac = Utilities.computeHmacSignature(
Utilities.MacAlgorithm.HMAC_SHA_512,
api_path.concat(api_sha256),
api_secret
)
api_signature = Utilities.base64Encode(api_hmac)
http_options = {
'method': 'post',
'payload': api_post,
'headers': {
'API-Key': api_key,
'API-Sign': api_signature
}
}
http_response = UrlFetchApp.fetch(
'https://api.kraken.com/0/private/' + endpoint,
http_options
)
api_data = http_response.getContentText()
return api_data
}Để sử dụng các tập lệnh sau với Thuật toán xác thực ở trên, bạn chỉ cần thêm hàm vào tập lệnh ở trên.
*Xin lưu ý rằng tất cả các Điểm cuối riêng tư đều yêu cầu triển khai Thuật toán xác thực.*
Số dư:
Bash
// Formula to use in Excel cell: '=KAPI_Balance("TEST")
function KAPI_Balance(acc_id) {
acc_balances_json = JSON.parse(
KAPI_Private(acc_id, 'Balance', '')
)
acc_balances = new Array()
for (name in acc_balances_json['result']) {
acc_balances.push([
name,
parseFloat(acc_balances_json['result'][name])
])
}
return acc_balances
}Số dư mở rộng:
Bash
// Formula to use in Excel cell: '=KAPI_BalanceEx("TEST")
function KAPI_BalanceEx(acc_id) {
acc_balances_json = JSON.parse(KAPI_Private(acc_id, 'BalanceEx', ''))
acc_balances = new Array
for (name in acc_balances_json['result']) {
acc_balances.push([
name,
parseFloat(acc_balances_json['result'][name]['balance']),
parseFloat(acc_balances_json['result'][name]['hold_trade']),
parseFloat(acc_balances_json['result'][name]['credit'] ? acc_balances_json['result'][name]['credit'] : '') || 0,
parseFloat(acc_balances_json['result'][name]['credit_used'] ? acc_balances_json['result'][name]['credit_used'] : '') || 0
]);
}
return acc_balances
}Số dư giao dịch:
Bash
// Formula to use in Excel cell: '=KAPI_TradeBalance("TEST", "ASSET")'
function KAPI_TradeBalance(acc_id, currency) {
acc_balances_json = JSON.parse(
KAPI_Private(acc_id, 'TradeBalance', 'asset=' + currency)
)
acc_balances = new Array()
for (name in acc_balances_json['result']) {
acc_balances.push([
name,
parseFloat(acc_balances_json['result'][name])
])
}
return acc_balances
}Lệnh mở:
Bash
// Formula to use in Excel cell: '=KAPI_Private("TEST","OpenOrders","")'
function KAPI_OpenOrders(acc_id) {
acc_orders_json = JSON.parse(
KAPI_Private(acc_id, 'OpenOrders', '')
)
acc_orders = new Array()
for (name in acc_orders_json['result']['open']) {
acc_orders.push([
name,
acc_orders_json['result']['open'][name]['descr']['pair'],
acc_orders_json['result']['open'][name]['descr']['type'],
parseFloat(acc_orders_json['result']['open'][name]['descr']['price']),
parseFloat(acc_orders_json['result']['open'][name]['vol']),
parseFloat(acc_orders_json['result']['open'][name]['vol_exec']),
parseFloat(acc_orders_json['result']['open'][name]['opentm'])
])
}
return acc_orders
}Lệnh đã đóng:
Bash
// Formula to use in Excel cell: '=KAPI_ClosedOrders("TEST","0")'
// Or =KAPI_PRIVATE("TEST","ClosedOrders","0")
function KAPI_ClosedOrders(acc_id, offset) {
acc_orders_json = JSON.parse(
KAPI_Private(acc_id, 'ClosedOrders', 'ofs=' + offset)
)
acc_orders = new Array()
for (name in acc_orders_json['result']['closed']) {
acc_orders.push([
name,
acc_orders_json['result']['closed'][name]['descr']['pair'],
acc_orders_json['result']['closed'][name]['descr']['type'],
acc_orders_json['result']['closed'][name]['descr']['ordertype'],
acc_orders_json['result']['closed'][name]['descr']['price'],
acc_orders_json['result']['closed'][name]['descr']['order'],
acc_orders_json['result']['closed'][name]['price'],
acc_orders_json['result']['closed'][name]['vol'],
acc_orders_json['result']['closed'][name]['vol_exec'],
acc_orders_json['result']['closed'][name]['cost'],
acc_orders_json['result']['closed'][name]['fee'],
acc_orders_json['result']['closed'][name]['status']
])
}
return acc_orders
}Lịch sử giao dịch:
Bash
// Formula to be used in Excel cell '=KAPI_TradesHistory("TEST","0")'
function KAPI_TradesHistory(acc_id, offset) {
acc_trades_json = JSON.parse(KAPI_Private(acc_id, 'TradesHistory', 'ofs=' + offset))
acc_trades = new Array
for (name in acc_trades_json['result']['trades']) {
acc_trades.push([
name,
acc_trades_json['result']['trades'][name]['ordertxid'],
acc_trades_json['result']['trades'][name]['postxid'],
acc_trades_json['result']['trades'][name]['pair'],
acc_trades_json['result']['trades'][name]['time'],
acc_trades_json['result']['trades'][name]['type'],
acc_trades_json['result']['trades'][name]['ordertype'],
acc_trades_json['result']['trades'][name]['price'],
acc_trades_json['result']['trades'][name]['cost'],
acc_trades_json['result']['trades'][name]['fee'],
acc_trades_json['result']['trades'][name]['vol'],
acc_trades_json['result']['trades'][name]['margin'],
acc_trades_json['result']['trades'][name]['leverage'],
acc_trades_json['result']['trades'][name]['misc'],
acc_trades_json['result']['trades'][name]['trade_id'],
acc_trades_json['result']['trades'][name]['maker']
])
}
return acc_trades
}Vị thế mở:
Bash
//Formula to use in Excel Cell '=KAPI_Private("TEST", "OpenPositions", "docalcs=true")'
function KAPI_OpenPositions(acc_id) {
acc_positions_json = JSON.parse(
KAPI_Private(acc_id, 'OpenPositions', 'docalcs=true')
)
acc_positions = new Array()
for (name in acc_positions_json['result']) {
acc_positions.push([
name,
acc_positions_json['result'][name]['ordertxid'],
acc_positions_json['result'][name]['posstatus'],
acc_positions_json['result'][name]['pair'],
acc_positions_json['result'][name]['type'],
acc_positions_json['result'][name]['ordertype'],
acc_positions_json['result'][name]['cost'],
acc_positions_json['result'][name]['fee'],
acc_positions_json['result'][name]['vol'],
acc_positions_json['result'][name]['vol_closed'],
acc_positions_json['result'][name]['margin'],
acc_positions_json['result'][name]['value'],
acc_positions_json['result'][name]['net'],
acc_positions_json['result'][name]['time']
])
}
return acc_positions
}Sổ cái:
Bash
//Formula to use in Excel Cell '=KAPI_Ledgers("TEST", "0")'
function KAPI_Ledgers(acc_id, offset) {
acc_ledgers_json = JSON.parse(KAPI_Private(acc_id, 'Ledgers', 'ofs=' + offset))
acc_ledgers = new Array
for ( name in acc_ledgers_json['result']['ledger'] ) {
acc_ledgers.push([name, acc_ledgers_json['result']['ledger'][name]['refid'], acc_ledgers_json['result']['ledger'][name]['time'], acc_ledgers_json['result']['ledger'][name]['type'], acc_ledgers_json['result']['ledger'][name]['subtype'], acc_ledgers_json['result']['ledger'][name]['asset'], acc_ledgers_json['result']['ledger'][name]['amount'], acc_ledgers_json['result']['ledger'][name]['fee'], acc_ledgers_json['result']['ledger'][name]['balance']])
}
return acc_ledgers
}Truy vấn thông tin lệnh:
Bash
// Formula to use in Excel cell '=KAPI_QueryOrders("TEST","Order ID")'
function KAPI_QueryOrders(acc_id, order_id) {
acc_orders_json = JSON.parse(
KAPI_Private(acc_id, 'QueryOrders', 'txid=' + order_id)
)
acc_orders = new Array()
for (name in acc_orders_json['result']) {
acc_orders.push([
name,
acc_orders_json['result'][name]['descr']['pair'],
acc_orders_json['result'][name]['descr']['type'],
acc_orders_json['result'][name]['descr']['ordertype'],
acc_orders_json['result'][name]['descr']['price'],
acc_orders_json['result'][name]['descr']['order'],
acc_orders_json['result'][name]['price'],
acc_orders_json['result'][name]['vol'],
acc_orders_json['result'][name]['vol_exec'],
acc_orders_json['result'][name]['cost'],
acc_orders_json['result'][name]['fee'],
acc_orders_json['result'][name]['status']
])
}
return acc_orders
}Ví dụ về một hàm Google Script đầy đủ gọi điểm cuối Lịch sử giao dịch:
Bash
const API_Public_Key = { 'TEST':'API PUBLIC KEY'}
const API_Private_Key = { 'TEST':'API PRIVATe KEY' }
function KAPI_Private(acc_id, endpoint, parameters) {
Utilities.sleep(Math.random() * 100)
api_key = API_Public_Key[acc_id]
api_secret = Utilities.base64Decode(API_Private_Key[acc_id])
api_path = Utilities.newBlob('/0/private/' + endpoint).getBytes()
api_nonce = Date.now().toString()
api_post = 'nonce=' + api_nonce + '&' + parameters
api_sha256 = Utilities.computeDigest(Utilities.DigestAlgorithm.SHA_256, api_nonce + api_post)
api_hmac = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_512, api_path.concat(api_sha256), api_secret)
api_signature = Utilities.base64Encode(api_hmac)
http_options = {'method':'post', 'payload':api_post, 'headers':{'API-Key':api_key, 'API-Sign':api_signature}}
http_response = UrlFetchApp.fetch('https://api.kraken.com/0/private/' + endpoint, http_options)
api_data = http_response.getContentText()
return api_data
}
function KAPI_TradesHistory(acc_id, offset) {
acc_trades_json = JSON.parse(KAPI_Private(acc_id, 'TradesHistory', 'ofs=' + offset))
acc_trades = new Array
for (name in acc_trades_json['result']['trades']) {
acc_trades.push([
name,
acc_trades_json['result']['trades'][name]['ordertxid'],
acc_trades_json['result']['trades'][name]['postxid'],
acc_trades_json['result']['trades'][name]['pair'],
acc_trades_json['result']['trades'][name]['time'],
acc_trades_json['result']['trades'][name]['type'],
acc_trades_json['result']['trades'][name]['ordertype'],
acc_trades_json['result']['trades'][name]['price'],
acc_trades_json['result']['trades'][name]['cost'],
acc_trades_json['result']['trades'][name]['fee'],
acc_trades_json['result']['trades'][name]['vol'],
acc_trades_json['result']['trades'][name]['margin'],
acc_trades_json['result']['trades'][name]['leverage'],
acc_trades_json['result']['trades'][name]['misc'],
acc_trades_json['result']['trades'][name]['trade_id'],
acc_trades_json['result']['trades'][name]['maker']
])
}
return acc_trades
}Dấu phân cách thập phân và hàng nghìn hiển thị trong bài viết này có thể khác với các định dạng hiển thị trên nền tảng giao dịch của chúng tôi. Xem lại bài viết của chúng tôi về cách chúng tôi sử dụng dấu chấm và dấu phẩy để biết thêm thông tin.
