Bot giao dịch dựa trên chỉ báo API REST (Python)

Cập nhật lần cuối: 2 thg 4, 2025

Một trong những mục đích sử dụng của API REST của chúng tôi là tạo bot giao dịch tự động tương tác với các thị trường và tài khoản của chúng tôi.

Mặc dù bot giao dịch có vô số loại, nhưng tất cả đều có chung các đặc điểm tích hợp API tương tự và tất cả đều thực hiện các tác vụ phổ biến sau:

  • truy xuất dữ liệu thị trường (mã giao dịch, sổ lệnh, v.v.)

  • đưa ra quyết định giao dịch (dựa trên dữ liệu thị trường hoặc các chỉ báo được suy ra từ dữ liệu thị trường)

  • đặt/hủy lệnh

API REST của chúng tôi cung cấp tất cả các chức năng cần thiết để triển khai một bot giao dịch đầy đủ tính năng, nhưng việc biết cách kết hợp các khía cạnh khác nhau của API của chúng tôi có thể khó khăn, do đó, mã bot giao dịch sau đây được cung cấp làm ví dụ.

Mã ví dụ triển khai một bot giao dịch dựa trên chỉ báo (cụ thể là SMA 20, với các quyết định giao dịch dựa trên sự thay đổi độ dốc 1 phút) và cho thấy cách tích hợp thành công dữ liệu thị trường, việc ra quyết định và các khía cạnh giao dịch của một bot giao dịch.

Mã ví dụ (Python)

Mã bot giao dịch là một tệp Python duy nhất và tích hợp trực tiếp với API của chúng tôi (không có thư viện API của bên thứ ba). Tất cả cấu hình (khóa API, cặp tiền tệ, chỉ báo, loại lệnh, đòn bẩy, v.v.) đều được chứa trong mã để dễ tham khảo.

Mã ví dụ có thể được xem bên dưới và cũng có thể được tải xuống dưới dạng tệp .py (REST_Indicator_Bot_Example.py).

python

Python

#!/usr/bin/env python3

# Import required libraries (Python not API)
import time
import sys
import json
import base64
import hashlib
import hmac
import urllib.request

# Configure API key (copy/paste from account management)
api_key_public = 'COPY/PASTE API PUBLIC KEY HERE'
api_key_private = 'COPY/PASTE API PRIVATE KEY HERE'

# Configure market/orders/trades
trade_symbol = 'XXBTZUSD'
trade_interval = 1  # OHLC interval in minutes
trade_size = 0.0001  # Trade volume in base currency
trade_leverage = 2

# Initial indicator/trade variables
trade_direction = 0
sma_values = [0.0, 0.0, 0.0]

# Infinite loop (can be exited via keyboard interrupt)
try:
    while True:
        # Retrieve OHLC data at specified interval
        print('Retrieving OHLC data ... ', end='')
        try:
            api_request = urllib.request.Request(
                'https://api.kraken.com/0/public/OHLC?pair=%(symbol)s&interval=%(interval)d' % {
                    'symbol': trade_symbol, 'interval': trade_interval}
            )
            api_request.add_header('User-Agent', 'Kraken trading bot example')
            api_response = urllib.request.urlopen(api_request).read().decode()
            api_data = json.loads(api_response)
        except Exception as error:
            print('Failed (%s)' % error)
        else:
            print('Done' if len(api_data['error']) == 0 else 'Error (%s)' % api_data['error'])

        # Calculate SMA (20 candles / closing price)
        print('Calculating SMA 20 ... ', end='')
        api_ohlc = api_data['result'][trade_symbol]
        api_ohlc_length = len(api_ohlc) - 1
        sma_temp = 0.0
        for count in range(1, 21):
            sma_temp += float(api_ohlc[api_ohlc_length - count][4])
        sma_temp = sma_temp / 20
        print('Done')

        # Update SMA values
        sma_values[2] = sma_values[1]
        sma_values[1] = sma_values[0]
        sma_values[0] = sma_temp
        if sma_values[2] == 0.0:
            print('Waiting %d seconds ... ' % (trade_interval * 60))
            time.sleep(trade_interval * 60)
            continue
        else:
            print('SMA 20 values ... %(sma2)f / %(sma1)f / %(sma0)f' % {
                'sma2': sma_values[2], 'sma1': sma_values[1], 'sma0': sma_values[0]})

        # Trading decision (change in slope of SMA)
        print('Trading decision ... ', end='')
        if (sma_values[0] > sma_values[1]) and (sma_values[1] < sma_values[2]):
            make_trade = 1
            print('Long')
        elif (sma_values[0] < sma_values[1]) and (sma_values[1] > sma_values[2]):
            make_trade = -1
            print('Short')
        else:
            make_trade = 0
            print('No trade')

        # Place order/trade (if applicable)
        if make_trade != 0:
            print('Placing order/trade ... ', end='')
            try:
                api_path = '/0/private/AddOrder'
                api_nonce = str(int(time.time() * 1000))
                api_post = 'nonce=%(api_nonce)s&pair=%(symbol)s&type=%(direction)s&ordertype=market&volume=%(volume)f&leverage=%(leverage)s' % {
                    'api_nonce': api_nonce,
                    'symbol': trade_symbol,
                    'direction': 'buy' if make_trade == 1 else 'sell',
                    'volume': trade_size if trade_direction == 0 else trade_size * 2,
                    'leverage': str(trade_leverage) if trade_leverage > 0 else 'none'
                }

                api_sha256 = hashlib.sha256(api_nonce.encode('utf8') + api_post.encode('utf8'))
                api_hmac = hmac.new(
                    base64.b64decode(api_key_private),
                    api_path.encode('utf8') + api_sha256.digest(),
                    hashlib.sha512
                )
                api_signature = base64.b64encode(api_hmac.digest())

                api_request = urllib.request.Request(
                    'https://api.kraken.com/0/private/AddOrder', api_post.encode('utf8')
                )
                api_request.add_header('API-Key', api_key_public)
                api_request.add_header('API-Sign', api_signature)
                api_request.add_header('User-Agent', 'Kraken trading bot example')
                api_response = urllib.request.urlopen(api_request).read().decode()
                api_data = json.loads(api_response)
            except Exception as error:
                print('Failed (%s)' % error)
            else:
                trade_direction = make_trade
                print('Done (%s)' % api_response if len(api_data['error']) == 0 else 'Error (%s)' % api_data['error'])

        # Wait until next OHLC interval
        print('Waiting %d seconds ... ' % (trade_interval * 60))
        time.sleep(trade_interval * 60)

except KeyboardInterrupt:
    sys.exit(0)
except Exception as error:
    print('Error (%s)' % error)
    sys.exit(1)

Lưu ý rằng bot giao dịch chỉ nhằm mục đích làm ví dụ và do đó thiếu một số tính năng mong muốn và kiểm tra lỗi cần thiết, vì vậy không nên sử dụng cho giao dịch sản xuất (không có gì đảm bảo rằng bot sẽ tạo ra lợi nhuận).

Bạn cần thêm trợ giúp?