This repository has been archived by the owner. It is now read-only.

Handling of all Bittrex order types? #9

Closed
skeller88 opened this Issue Sep 24, 2017 · 10 comments

Comments

Projects
None yet
4 participants
@skeller88
Contributor

skeller88 commented Sep 24, 2017

I've been looking for a good bittrex websocket and your repo looks really promising. I'm just making sure I understand all of the code first.

What do the different Bittrex order book entry types mean? There are 3 types, "0", "1", and "2". I confirmed with the code I paste below. Based on your code, I'd guess that 1 is a FILL, and either 0 or 2 is PARTIAL_FILL. It's unclear what the last type would be because Bittrex's documentation is limited. Maybe CANCEL?

Anyways, how'd you figure out the different types? It doesn't look like all of these entry types are being handled. In https://github.com/gliwka/bittrex-market/blob/master/lib/market.js#L139, if an entry type is 1, then the entry is removed from the order book state. If it's 0 or 2, then it's either added to the order book if the entry doesn't exist, or overwrites the existing entry. 0 and 2 must be different types, so I would imagine they'd have to be dealt with differently. Especially if one is CANCEL, in which case you'd want to remove that entry from the order book also.

Below I paste the code used:

const BITTREX = require('node-bittrex-api');

const TOP_USDT_MARKETS = [
    'USDT-BTC',
]

const WEBSOCKET_CLIENT = BITTREX.websockets.subscribe(TOP_USDT_MARKETS, function( data ) {
 if (data.M == 'updateExchangeState') {
        data.A.forEach(function(market) {
            market['Buys'].forEach((buy) => {
                console.log('Buy', buy)
            });
            market['Sells'].forEach((sell) => {
                console.log('Sell', sell)
            });
            market['Fills'].forEach((fill) => {
                console.log('Fill', fill)
            });
        });
  }
});
@gliwka

This comment has been minimized.

Owner

gliwka commented Sep 24, 2017

I've got the meaning of the types by reverse engineering the behavior of the bittrex's website JavaScript code.

I'm currently on the go, so I'll respond with a more detailed answer later. I'm considering to add a detailed description of the known parts to the wiki of this project.

@gliwka

This comment has been minimized.

Owner

gliwka commented Sep 24, 2017

The relevant code from the bittrex website js source is:(https://bittrex.com/bundles/marketViewModel):

 $.each(a.Buys, function() {
                        var i = this, r, n, o, f, e, s;
                        switch (i.Type) {
                        case 0:
                            v(i);
                            break;
                        case 1:
                            r = t.buys.remove(function(n) {
                                return n.rate() == i.Rate
                            });
                            r.length > 0 && (c -= r[0].total(),
                            l -= r[0].size());
                            break;
                        case 2:
                            n = ko.utils.arrayFirst(t.buys(), function(n) {
                                return i.Rate === n.rate()
                            });
                            n ? (f = n.total(),
                            e = n.size(),
                            n.update(i),
                            c += n.total() - f,
                            l += n.size() - e) : (o = u.find(function(n) {
                                return n.Rate == i.Rate
                            }),
                            o.length > 0 ? (f = n.total(),
                            e = n.size(),
                            n.update(o[0]),
                            c += n.total() - f,
                            l += n.size() - e,
                            s = u.indexOf(o[0]),
                            s > -1 && u.splice(s, 1)) : v(i))
                        }

The type '0' is a simple add operation, the type '1' is a delete operation and the type '2' is a replace/update operation.

The operations themselve are implemented in https://bittrex.com/bundles/bittrexViewModel - insert and remove are self-explanatory, let's take a look at the update method:

    i.update = function(n) {
        i.size(n.Quantity);
        i.total(i.rate() * n.Quantity)
    }

As you can see an update simply replaces the "quantity" and "total" values. Thats why bittrex-market only checks if its a delete operations. Update and insert can be implemented using the same logic:

https://github.com/gliwka/bittrex-market/blob/master/lib/market.js#L137-L143

Does this answer your question?

@skeller88

This comment has been minimized.

Contributor

skeller88 commented Sep 24, 2017

Yep, makes more sense. The minified code for v() is a bit tough to completely understand, but I get the gist:

    function v(i) {
        var f, e, u;
        if (i.serviceData && (f = i.serviceData, t.mySells = [], t.myBuys = [], f && f.Orders))
            for (e in f.Orders) u = f.Orders[e], u.Closed || (u.Exchange == n.marketName && (u.OrderType == "LIMIT_BUY" ? t.myBuys.push(u.Limit) : u.OrderType == "LIMIT_SELL" && t.mySells.push(u.Limit)), r());
        return
    }

Thanks for walking me through it.

@gliwka

This comment has been minimized.

Owner

gliwka commented Sep 25, 2017

You're welcome!

@gliwka gliwka closed this Sep 25, 2017

@armoreal

This comment has been minimized.

armoreal commented Oct 27, 2017

There's an bug somewhere. From time to time the biggest BID is greater than smallest ASK!
So may be (at least sometime) type=2 means executed+need to be removed from orderbook?

@skeller88

This comment has been minimized.

Contributor

skeller88 commented Oct 27, 2017

How is that a bug? That could happen if a person set a bid order larger than the lowest ask, which they may do if they want to increase the chances that a bid will be filled if a security's price is increasing rapidly.

@armoreal

This comment has been minimized.

armoreal commented Oct 27, 2017

Nope. If BID is greater than ASK, the trade executed and BID will become lower than ASK.
For example if orderbook contain ASK orders with the price of 100 and volume 2, (and another ask order with the price 102 and volume 5). If I submit an buy order with price 101 and volume 3.... the trade will be executed and orderbook will contain BID at price 101 and volume 1, and ASK 102 volume 5.
So BID must be always be lower than ASK.

@skeller88

This comment has been minimized.

Contributor

skeller88 commented Oct 27, 2017

@askmike

This comment has been minimized.

askmike commented Nov 11, 2017

Orders that cross the book (most likely) never hit the book at all. If you do a BUY limit order below the lowest ask, this order is never added to the book (executed as MKT order). This is how most matching engines work, I am pretty sure on bittrex too.

@skeller88

This comment has been minimized.

Contributor

skeller88 commented Dec 12, 2017

That's how it works on Binance also.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.