Lưu ý API sàn giao dịch BitMEX

Tác giả:Cỏ nhỏ, Tạo: 2019-04-04 11:40:52, Cập nhật: 2024-02-05 20:09:52

Các vấn đề sử dụng BitMEX Exchange API (BitMEX exchange API note)

img

Nền tảng FMZ API Doc Tham gia nhóm Telegram của chúng tôi.

www.fmz.com(trước đây là BotVs) là một nền tảng giao dịch chiến lược định lượng nơi bạn có thể dễ dàng học, viết, chia sẻ và giao dịch các chiến lược định lượng.

  • Nền tảng của chúng tôi có nhiều lợi thế:

    • 1, Cross-nền tảng, hỗ trợ tất cả các sàn giao dịch lớn, chiến lược viết trên nền tảng của chúng tôi là phù hợp với tất cả các sàn giao dịch lớn.
    • 2, Dễ dàng để bắt đầu, tài liệu API cụ thể và các chiến lược mẫu cổ điển giúp người dùng bắt đầu thực sự nhanh chóng.
    • Nó có một hệ thống backtesting mô phỏng hiệu quả.
    • 4、Hỗ trợ gửi email, đẩy tin nhắn đến điện thoại của bạn.
    • 5, cơ chế điều khiển dựa trên web, có thể được truy cập qua điện thoại của bạn.
    • 6、Hỗ trợ lập trình Python\C++\JavaScript đầy đủ
    • 7、Hỗ trợ giao dịch điểm và tương lai, và sẽ hỗ trợ nhiều sàn giao dịch hơn trong tương lai.
    • 8, Chi phí cực kỳ thấp. 0,125 nhân dân tệ mỗi giờ, dựa trên tỷ giá hối đoái hiện tại: USDCNY 6,9303, có nghĩa là 0,01804 đô la mỗi giờ.
    • Không có API-Key hoặc mật khẩu được lưu trong trang web của chúng tôi. FMZ đã chạy trong hơn bốn năm mà không có bất kỳ vấn đề bảo mật.

FMZ (BOTVS) hỗ trợ tất cả các hợp đồng trên BitMEX!

  • Mã kiểm tra:

var initAccount = null;
var nowAccount = null;
function main() {
    LogReset(1);
    Log("This is BitMEX test bot");
    Log("Fee:", exchange.GetFee());
    Log("Initial account:", initAccount = _C(exchange.GetAccount));    
    var info = exchange.SetContractType("XBTUSD");   // BitMEX : XBTUSD , OK : this_week
    Log("XBTUSD info:", info);   
    Log("Use GetTicker to get ticker information:", _C(exchange.GetTicker)) 
    Sleep(1000 * 10);   
    // make an order
    exchange.SetDirection("sell");                         // set order direction
    var orderId = exchange.Sell(-1, 1);                    // sell at market price。
    Sleep(6000);
    // log positions
    var positions = null;
    Log(positions = _C(exchange.GetPosition));
    Log("Account before changing leverage:", _C(exchange.GetAccount));
    // change leverage
    Log("Change leverage", _C(exchange.SetMarginLevel, positions[0].MarginLevel * 2));
    Log("Account after changing leverage:", _C(exchange.GetAccount));  
    // test GetOrder 
    if (orderId) {
        Log(_C(exchange.GetOrder, orderId));
    } 
    Sleep(1000 * 10);
    Log(_C(exchange.GetPosition));
    // set direction to close
    exchange.SetDirection("closesell");
    var go_buy = exchange.Go("Buy", -1, 1);
    var orderId2 = go_buy.wait();
    Log(_C(exchange.GetOrder, orderId2));
    Log("Current account:", nowAccount = _C(exchange.GetAccount));
    Log(_C(exchange.GetPosition));
    LogProfit(nowAccount.Stocks - initAccount.Stocks, " initAccount:", initAccount, " nowAccount:", nowAccount);
    Sleep(1000 * 10);
    var ticker = _C(exchange.GetTicker);
    exchange.SetDirection("buy");
    exchange.Buy(ticker.Last - 50, 1);
    exchange.SetDirection("sell");
    exchange.Sell(ticker.Last + 50, 1);
    // GetOrders
    Log("Test GetOrders:", _C(exchange.GetOrders));
    var e = exchange;
    while (true) {
        var orders = _C(e.GetOrders);
        if (orders.length === 0) {
            break;
        }
        Sleep(500);
        for (var j = 0; j < orders.length; j++) {
           e.CancelOrder(orders[j].Id);
            if (j < (orders.length - 1)) {
                Sleep(500);
            }
        }
    }
    Log("Cancel order, test GetOrders again:", _C(exchange.GetOrders));
}
  • Kiểm tra thông tin tài khoản của bạn tại BitMEX.

    img

    Ghi lại thông tin bằng bot, tương tự như trên BitMEX.

    img

  • Log positions after changing leverage, the leverage has been changed (Log vị trí sau khi thay đổi đòn bẩy, so sánh thông tin nắm giữ trước sau).

    img

  • Use Go function to cover your positions at the same time (Tiếng gọi hàm Go để phủ các vị trí của bạn cùng một lúc)

      exchange.SetDirection("closesell");
      var go_buy = exchange.Go("Buy", -1, 1);
      var orderId2 = go_buy.wait();
      Log(_C(exchange.GetOrder, orderId2));
      Log("当前账户:", nowAccount = _C(exchange.GetAccount));
      Log(_C(exchange.GetPosition));
      LogProfit(nowAccount.Stocks - initAccount.Stocks, " initAccount:", initAccount, " nowAccount:", nowAccount);
    

    img

  • Let's try post orders and cancel it. (Chúng ta hãy cố gắng đăng các đơn đặt hàng).

    var ticker = _C(exchange.GetTicker);
    exchange.SetDirection("buy");
    exchange.Buy(ticker.Last - 50, 1);
    exchange.SetDirection("sell");
    exchange.Sell(ticker.Last + 50, 1);  
    // GetOrders
    Log("Test GetOrders:", _C(exchange.GetOrders));
    var e = exchange;
    while (true) {
        var orders = _C(e.GetOrders);
        if (orders.length === 0) {
            break;
        }
        Sleep(500);
        for (var j = 0; j < orders.length; j++) {
            e.CancelOrder(orders[j].Id);
            if (j < (orders.length - 1)) {
                Sleep(500);
            }
        }
    }
    Log("orders have been canceled. Now check orders again, order array is empty. GetOrders:", _C(exchange.GetOrders));
    

    img

    The pending orders information (thông tin đặt hàng đang chờ được thu thập)

 [{"Id":4,"Amount":1,"Price":1679.6,"DealAmount":0,"AvgPrice":0,"Status":0,"Type":1,"ContractType":"XBTUSD"},
 {"Id":3,"Amount":1,"Price":1579.6,"DealAmount":0,"AvgPrice":0,"Status":0,"Type":0,"ContractType":"XBTUSD"}]
  • Chú ý:

    • 1, BitMEX only supoort K-line periods of 1m, 5m, 1h, 1d.

      Sử dụng các nhà quản lý mới nhất, tầng dưới có thể tự động tổng hợp các K-line, và một số dữ liệu chu kỳ K-line không được hỗ trợ bởi BITMEX cũng có thể được tổng hợp, vì vậy khi thiết lập chu kỳ K-line, bạn không còn bị giới hạn trong việc tổng hợp các K-line.1分钟、5分钟、1小时、1天Những chu kỳ này, tất cả các chu kỳ đều có thể được thiết lập.

      img

    • 2, Test holding long and short positions at the same time. (Đánh thử giữ nhiều vị trí đầu, vị trí đầu trống cùng một lúc)

      LogReset(1);
      var info = exchange.SetContractType("XBTUSD");
      exchange.SetDirection("sell");
      var orderId = exchange.Sell(-1, 1);
      Log(_C(exchange.GetPosition));
      Sleep(1000*6);
      exchange.SetDirection("buy");
      var orderId2 = exchange.Buy(-1, 1);
      Log(_C(exchange.GetPosition));
      exchange.SetDirection("closesell");
      var orderId3 = exchange.Buy(-1, 1);
      Log(_C(exchange.GetPosition));
      

      img

    • 3, The leverage can be changed while holding position. (Động lực có thể được điều chỉnh sau khi giữ vị trí.)

    • 4, Hỗ trợexchange.IOfunction for more API (Hỗ trợ các chức năng exchange.IO)

      img

      // exchange.IO example
      exchange.SetContractType("XBTUSD");
      Log(exchange.IO("api", "POST", "position/leverage", "symbol=XBTUSD&leverage=4"));
      Log(exchange.IO("api", "GET", "user"));
      

      The raw information of position/leverage API (Dữ liệu được trả về từ API đặt vị trí/đòn bẩy trực tiếp):

      {"homeNotional":0,
      "sessionMargin":0,
      "bankruptPrice":null,
      "initMarginReq":0.25,
      "execBuyQty":2,
      "execComm":184,
      "unrealisedCost":0,
      "commission":0.00075,
      "leverage":4,
      "posLoss":0,
      "posMargin":0,
      "posMaint":0,
      "liquidationPrice":null,
      "maintMarginReq":0.005,
      "grossExecCost":0,
      "execCost":7,
      "currentTimestamp":"2017-05-08T10:51:20.576Z",
      "markValue":0,
      "unrealisedGrossPnl":0,
      "taxBase":7720,
      "unrealisedPnlPcnt":0,
      "prevUnrealisedPnl":0,
      "openOrderSellCost":0,
      "deleveragePercentile":null,
      "openingComm":31588,
      "openOrderBuyCost":0,
      "posCross":0,
      "taxableMargin":0,
      "simpleCost":0,
      "underlying":"XBT",
      "quoteCurrency":"USD",
      "execBuyCost":122613,
      "execSellCost":122620,
      "execQty":0,
      "realisedCost":-7720,
      "unrealisedPnl":0,
      "openingQty":0,
      "openOrderBuyQty":0,
      "initMargin":0,
      "unrealisedTax":0,
      "simpleQty":0,
      "avgCostPrice":null,
      "rebalancedPnl":24052,
      "openingTimestamp":"2017-05-08T10:00:00.000Z",
      "unrealisedRoePcnt":0,
      "posCost":0,
      "posInit":0,
      "posComm":0,
      "realisedTax":0,
      "indicativeTax":0,
      "breakEvenPrice":null,
      "isOpen":false,
      "riskValue":0,
      "posState":"",
      "varMargin":0,
      "realisedGrossPnl":7720,
      "timestamp":"2017-05-08T10:51:20.576Z",
      "account":25992,
      "foreignNotional":0,
      "openOrderSellPremium":0,
      "simpleValue":0,
      "lastValue":0,
      "riskLimit":20000000000,
      "openOrderSellQty":0,
      "grossOpenPremium":0,
      "marginCallPrice":null,
      "prevClosePrice":1562.74,
      "openOrderBuyPremium":0,
      "currentQty":0,
      "currentCost":-7720,
      "currentComm":31772,
      "markPrice":null,
      "posCost2":0,
      "realisedPnl":-24052,
      "prevRealisedPnl":-95,
      "execSellQty":2,
      "shortBankrupt":0,
      "simplePnl":0,
      "simplePnlPcnt":0,
      "lastPrice":null,
      "posAllowance":0,
      "targetExcessMargin":0,
      "indicativeTaxRate":0,
      "grossOpenCost":0,
      "maintMargin":0,
      "crossMargin":false,
      "openingCost":-7727,
      "longBankrupt":0,
      "avgEntryPrice":null,
      "symbol":"XBTUSD",
      "currency":"XBt"}
      

Thêm nữa