Type/to search

Шаблон высокочастотной торговли через WebSocket для OKX и Binance (мульти-инструмент)

Template
Created: 2022-12-27 15:37:42
Last modified: 3 years ago
12
Follow
656
Followers

event.positions) {
Log("account update", event)
}
if (event.orders) {
Log("private orders update", event)
}

// include orders, positions, balance for latest
Log("account", ctx.wsPrivate.account)

// for test only
if (false) {
return {
amendOrders: [{
instId: event.instId,
clOrdId: "xxxx*****",
cxlOnFail: true,
newSz: "2",
}],
newOrders: [{
instId: event.instId,
clOrdId: UUID(),
side: "sell",
tdMode: "cross",
ordType: "post_only",
px: event.depth.asks[0].price.toFixed(4),
sz: "1",
}, {
instId: event.instId,
clOrdId: UUID(),
side: "sell",
tdMode: "cross",
ordType: "post_only",
px: event.depth.asks[0].price.toFixed(4),
sz: "1",
}],
cancelOrders: [{
instId: order.instId,
clOrdId: order.Id
}]
}
}
}

function main() {
let instId = exchange.SetContractType("swap").InstrumentID

let ctx = $.NewWSS(exchange, function(ws) {
let msg = null
if (exchange.GetName() === 'Futures_OKCoin') {
msg = {
op: "subscribe",
args: [{
channel: "books5",
instId: instId,
}, {
channel: "trades",
instId: instId,
}]
}
} else {
let symbol = exchange.GetCurrency().replace('_', '').toLowerCase()
msg = {
method: "SUBSCRIBE",
params: [symbol + "@aggTrade", symbol + "@depth20@100ms"],
id: "1",
}
}
ws.write(JSON.stringify(msg))
Log("subscribe", msg, "channel")
LogStatus("Ready")
}, onTick, Debug)

while (true) {
ctx.poll()
EventLoop(1000)
}
}

Результат работы показан ниже. Также в функции подписки можно подписаться на несколько инструментов. [/upload/asset/107646c11b93b32c493.png](/upload/asset/107646c11b93b32c493.png) Ниже показан результат работы стратегии маркет-мейкера, реализованной на основе этого шаблона и совместимой с OKX и Binance. [/upload/asset/73393d2122c1f41a2a.png](/upload/asset/73393d2122c1f41a2a.png) [/upload/asset/17efd8896a74dc9e271.gif](/upload/asset/17efd8896a74dc9e271.gif)
Source
JavaScript

/* jshint esversion: 6 */
// AccessKey and Phassphrase only need if OKX
$.NewWSS = function(e, onWSSLogin, onWSSTick, Debug, UseMargin, AccessKey, Passphrase) {
    function NewOKXWebSocketPrivate(ctx) {
        let self = {
            ws: null,
            isReadyDic: {},
            lastPing: 0,
            ctx: ctx,
            account: {
                orders: {},
Comment
All comments (10)

    好顶赞

    2 years ago

    Invalid request: request ID must be an unsigned integer报错不能跑了,

    3 years ago

    再试一下,可以了,币安订阅的ID是数字才可以导致的

    3 years ago

    Invalid request: request ID must be an unsigned integer ID格式好像有问题了

    3 years ago

    有没有显示出错行OK还是Binance ?

    3 years ago

    binance合约报错信息Invalid request: request ID must be an unsigned integer 没有出错行 img

    3 years ago

    Debug这个设置false和true分别代表什么意思

    3 years ago

    能不能简单说下EventLoop函数的执行逻辑,我看api文档立面没有相关介绍

    4 years ago

    晚些会加到文档里去

    4 years ago

    Zero总牛🍺

    4 years ago
  • 1
Forums
PINE Language
Get the app
iPhone Download
© 2015 - ∞ INVENTOR PTE LTD (SG)