发布于 2025-02-05 10:27:58 · 阅读量: 172950
如果你想在Probit交易所上实现自动化交易,API(应用程序接口)绝对是你的最佳拍档。不管你是量化玩家,还是想解放双手的套利选手,Probit的API都能帮你实现交易策略的自动执行。本文将手把手教你如何配置Probit API,并且让你的交易机器人跑起来。
首先,登录 Probit官网,按照以下步骤申请API Key:
API Key
和 Secret Key
,务必保存好,因为Secret Key只会显示一次! 注意:API权限设置很关键,切勿随便给太多权限,尤其是“提现”相关的权限,以防被盗用。
Probit的API采用RESTful风格,同时支持WebSocket流式数据。我们这里以Python为例,使用 requests
进行API调用。
pip install requests
import time import hmac import hashlib import requests
API_KEY = "你的API Key" SECRET_KEY = "你的Secret Key" BASE_URL = "https://api.probit.com/api/exchange/v1"
def sign_request(endpoint, params=None): """ 生成签名 """ timestamp = str(int(time.time() * 1000)) message = timestamp + endpoint if params: message += str(params) signature = hmac.new(SECRET_KEY.encode(), message.encode(), hashlib.sha256).hexdigest() headers = { "API-Key": API_KEY, "Timestamp": timestamp, "Signature": signature, "Content-Type": "application/json" } return headers
endpoint = "/balance" headers = sign_request(endpoint) response = requests.get(BASE_URL + endpoint, headers=headers) print(response.json())
有了API授权后,你就可以让机器人帮你下单了。
def get_market_price(pair="BTC-USDT"): """ 查询交易对最新价格 """ endpoint = "/ticker" response = requests.get(BASE_URL + endpoint, params={"market_id": pair}) return response.json()["data"][0]["last"]
def place_limit_order(market, side, quantity, price): """ 下限价单 """ endpoint = "/new_order" params = { "market_id": market, "side": side, # "buy" or "sell" "quantity": str(quantity), "price": str(price), "order_type": "limit" } headers = sign_request(endpoint, params) response = requests.post(BASE_URL + endpoint, json=params, headers=headers) return response.json()
market = "BTC-USDT" grid_size = 50 # 间隔价差 quantity = 0.001 # 交易数量 while True: current_price = float(get_market_price(market)) buy_price = current_price - grid_size sell_price = current_price + grid_size
print(f"当前价: {current_price}, 挂买单: {buy_price}, 挂卖单: {sell_price}")
place_limit_order(market, "buy", quantity, buy_price)
place_limit_order(market, "sell", quantity, sell_price)
time.sleep(10) # 每10秒检查一次
如果你想更快获取市场变化,而不是定时轮询,那WebSocket就派上用场了。
import websocket import json
def on_message(ws, message): data = json.loads(message) print("最新成交价:", data["data"][0]["last"])
ws_url = "wss://api.probit.com/api/exchange/v1/ws" ws = websocket.WebSocketApp(ws_url, on_message=on_message) ws.run_forever()
这样,你的交易机器人就可以自动化执行策略了。Happy Trading! 🚀