Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Coinbase API change #476

Open
segurac opened this issue Mar 20, 2023 · 3 comments
Open

Coinbase API change #476

segurac opened this issue Mar 20, 2023 · 3 comments

Comments

@segurac
Copy link

segurac commented Mar 20, 2023

Many changes in Coinbase's API have been rolled out in the last year.
https://docs.cloud.coinbase.com/exchange/docs/changelog

In particular, this change https://docs.cloud.coinbase.com/exchange/docs/changelog#2022-jun-27 makes the websocket to disconnect, whenever new_price or old_price fields appear.

I fixed this part in my fork, but I didn't submit a PR because of lack of time. It would be great if someone could work on this and check that everything works fine.

segurac@f91977d

Now that I see some activity in the repo I remembered my patches. It could be a good time to create a small community and maintain this awesome library together.

@segurac
Copy link
Author

segurac commented Mar 22, 2023

It is even worse than I expected. Now the price in "done" messages seems to not have to match the price of the "open"

Here's a real example:
{"order_id":"480df2f8-fadd-473e-b173-e4b99f5839d4","order_type":"limit","size":"0.00012721","price":"20332.31","type":"received","side":"buy","product_id":"BTC-USD","time":"2023-03-10T00:05:24.245717Z","sequence":56308181839}
{"price":"20332.31","order_id":"480df2f8-fadd-473e-b173-e4b99f5839d4","remaining_size":"0.00012721","type":"open","side":"buy","product_id":"BTC-USD","time":"2023-03-10T00:05:24.245717Z","sequence":56308181840}
{"trade_id":506343614,"maker_order_id":"c8b6761b-a488-4071-a8a7-2e91834228cc","taker_order_id":"480df2f8-fadd-473e-b173-e4b99f5839d4","size":"0.00012721","price":"20332.32","type":"match","side":"sell","product_id":"BTC-USD","time":"2023-03-10T00:05:27.766364Z","sequence":56308184372}
{"order_id":"480df2f8-fadd-473e-b173-e4b99f5839d4","reason":"filled","price":"20332.32","remaining_size":"0","type":"done","side":"buy","product_id":"BTC-USD","time":"2023-03-10T00:05:27.766364Z","sequence":56308184373}

The price in the "done" message is different from the "open", which means that the order is not removed from the orderbook correctly and the orderbook gets corrupted.

I think this is a problem with coinbase API, but we should be able to deal with it anyway. I asked coinbase for support on this issue.

@segurac
Copy link
Author

segurac commented Mar 24, 2023

Running this code during a few hours will show orders that were opened at one price and done at another price. In self.opens I store the order_id, price as key,vals; update the price whenever there's a change, and then check the price and remove the order_id when the order is done. Am I doing something wrong here?

import cbpro, time

class myWebsocketClient(cbpro.WebsocketClient):
    def __init__(self, product_id=['BTC-USD']):
        super().__init__(products=product_id, channels = ["full"])
        self.products = product_id
        self.opens = {}

    def on_open(self):
        print("open WebsocketClient")
        
    def on_message(self, msg):
        #print(msg)
        msg_type = msg['type']
        if msg_type == 'open':
            self.opens[ msg['order_id']  ] = msg['price']
        elif msg_type == 'done' and 'price' in msg:
            order_id = msg['order_id']
            price = msg['price']
            if order_id in self.opens:
                if price != self.opens[order_id]:
                    print("order_id", order_id, "had an open price of", self.opens[order_id] , "and a done price of", price)
                del self.opens[order_id]
                
        elif msg_type == 'change' and 'new_price' in msg:
            self.opens[ msg['order_id']  ] = msg['new_price']
            

wsClient = myWebsocketClient()
wsClient.start()
time.sleep(10)
while True:
    #print(len(wsClient.opens))
    time.sleep(1)

@segurac
Copy link
Author

segurac commented Mar 25, 2023

I asked Coinbase and it turns out that this is an expected behavior. So we should adapt the library to deal with this issue too. Right now in the library it is impossible to remove an order from the orderbook if the done order has has a different price from the one in our books.

This is the email I got from support:

Hello XXXX,

Thank you for reaching out.

We would like to let you know that difference in price in “open” and “done” is due to the order being modified by customers. Pro/Exchange do support modify orders.

For example, we could see in our records that for order_id e6faa188-edd7-4cb7-964a-a539ddefabf0, buy order is modified up from 20102.65 to 20111.53

After modification, the order still rest in the book, there will be a message to say that the order has a new price. However, when modified up, it immediately matches with the other side (published as match) and the order is done (i.e., via matching with the other side, or self trade prevention or if order isn’t in the book any more, then all the public gets this order as done).

Please note that when you see a match message, you should minus the size in that match in your book for those particular order ids (i.e., both maker side and taker side) and when you see the done, you should just remove that order in your book and ignore the price field.

We hope this helps. We’d be happy to answer any more questions.

Regards,
Coinbase Support

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant