0

怎么样了?我在 python 中获得了示例订单簿代码(https://support.kraken.com/hc/en-us/articles/360027677512-Example-order-book-code-Python-)并将其转换为 javascript 以在节点中运行. 但是这本书是错误的,它并没有删除所有旧的价格水平。我在下面发送我的代码。我想帮助解决这个问题。

const websocket = require('ws');
const ws = new websocket('wss://ws.kraken.com');
const api_book = {'bid':[], 'ask':[]};
const api_depth = 10;

const api_output_book = () => {
    bid = api_book['bid'].sort((x, y) => parseFloat(y[0])-parseFloat(x[0]));
    ask = api_book['ask'].sort((x, y) => parseFloat(x[0])-parseFloat(y[0]));
    console.log ('Bid\t\t\t\t\tAsk');

    for (let x=0;x<api_depth;x++) {
      console.log(`${bid[x][0]} (${bid[x][1]})\t\t\t${ask[x][0]} (${ask[x][1]})`);
    }
}

const api_update_book = (side, data) => {
    data.forEach((e) => {
        let index = api_book[side].findIndex(o => o[0] == e[0]);
        if (parseFloat(e[1]) > 0){
            if(index < 0){
                api_book[side].push([e[0],e[1]]);
            } else {
                api_book[side][index] = [e[0],e[1]];
            }
        } else {
            api_book[side].splice(index,1);
        }
    });

    if(side=='bid'){
        api_book['bid'].sort((x, y) => parseFloat(y[0])-parseFloat(x[0]));
    } else if(side=='ask'){
        api_book['ask'].sort((x, y) => parseFloat(x[0])-parseFloat(y[0]));
    }

}

ws.on('open', open = () => {
    ws.send('{"event":"subscribe", "subscription":{"name":"book", "depth":'+api_depth+'}, "pair":["XBT/USD"]}');
    console.log('Kraken websocket connected!');
});

ws.on('message', incoming = (data) => {
    try {
        data = JSON.parse(data.toString('utf8'));

        if (data[1]) {
            if (data[1]['as']) {
                api_update_book('ask', data[1]['as'])
                api_update_book('bid', data[1]['bs'])
            } else if (data[1]['a'] || data[1]['b']) {
                if (data[1]['a']) {
                    api_update_book('ask', data[1]['a']);
                }
                if (data[1]['b']) {
                    api_update_book('bid', data[1]['b']);
                }
            }

            api_output_book();
        } 


    } catch (error) {
        console.log(error);
    }  
});
4

1 回答 1

0

所以我也一直在玩 Kraken 的订单簿,并使用 Angular 提出了这个解决方案。我还在混合中添加了一些控制台日志,以便您可以获取它并在浏览器中运行它。希望这可以帮助!

// variables
private ws = new WebSocket('wss://ws.kraken.com')
public asks = [];
public bids = [];

// Web Socket open connection
this.ws.onopen = () => {
    this.ws.send(JSON.stringify(this.message));
    console.log('Trade WS with Kraken connected')
}

// Fires when new data is received from web socket
this.ws.onmessage = (event) => {
    var data = JSON.parse(event.data);
    if (!data.event) {
        if (data[1]['as']) {
            this.asks = data[1]['as'];
            this.bids = data[1]['bs'];

            console.log('Initialised Book');
            console.log(this.asks, this.bids);


        } else if (data[1]['a'] || data[1]['b']) {
            if (data[1]['a']) {
                this.update_book(this.asks, 'ask', data[1]['a']);
            }
            if (data[1]['b']) {
                this.update_book(this.bids, 'bid', data[1]['b']);
            }
        }
    }
}

// Updating Orderbook
update_book (arr, side, data) {
        if (data.length > 1) {                                      // If 2 sets of data are received then the first will be deleted and the second will be added
            let index = arr.findIndex(o => o[0] == data[0][0]);     // Get position of first data
            
            arr.splice(index, 1);                                   // Delete data              
            arr.push([ data[1][0], data[1][1] ]);                   // Insert new data  
            console.log('Delete and Insert');
        } else {
            let index = arr.findIndex(o => o[0] == data[0][0]);
            console.error(index);
            if (index > -1) {                           // If the index matches a price in the list then it is an update message
                arr[index] = [data[0][0], data[0][1]];  // Update matching position in the book
                console.log('Updated ' + index);
            } else {                                    // If the index is -1 then it is a new price that came in
                arr.push([data[0][0], data[0][1]]);     // Insert new price
                this.sort_book(arr, side);              // Sort the book with the new price 
                arr.splice(10, 1);                      // Delete the 11th entry
                console.log('Insert Only');
            }                   
        }
        this.sort_book(arr, side);                      // Sort the order book
}

// Sort Orderbook
sort_book (arr, side) {
    if (side == 'bid') {
        arr.sort((x, y) => parseFloat(y[0]) - parseFloat(x[0]));
    } else if (side == 'ask') {
        arr.sort((x, y) => parseFloat(x[0]) - parseFloat(y[0]));
    }
}

我还建议您看看这个资源: 如何维护有效的订单簿

于 2020-07-26T00:51:31.663 回答